BYU-PRISM / GEKKO

GEKKO Python for Machine Learning and Dynamic Optimization
https://machinelearning.byu.edu
Other
573 stars 102 forks source link

Convert python standard float and integer variables to GKVariable #131

Closed Hirbod-JORFlint closed 2 years ago

Hirbod-JORFlint commented 2 years ago

I'm working on a minimization problem and due to the special structure of the main function, there are "sub-methods" to convert part of the decision variables to string characters as a result I use the following method to convert part of the variable array and do some internal operation on the input: num2tempstr=x[0].VALUE[1] I would like to request a method or some translation function to convert the calculated resulting variables back to GKVariable.

here is the printed result:

----------------------------------------------------------------
 APMonitor, Version 1.0.0
 APMonitor Optimization Suite
 ----------------------------------------------------------------

 --------- APM Model Size ------------
 Each time step contains
   Objects      :  0
   Constants    :  0
   Variables    :  20
   Intermediates:  0
   Connections  :  0
   Equations    :  1
   Residuals    :  1

 ________________________________________________
 WARNING: objective equation 1 has no variables
 ss.Eqn(1)
 0 = 787726150830995
 ________________________________________________
 Number of state variables:    20
 Number of total equations: -  0
 Number of slack variables: -  0
 ---------------------------------------
 Degrees of freedom       :    20

 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------
Iter:     1 I:  0 Tm:      0.00 NLPi:    1 Dpth:    0 Lvs:    0 Obj:  7.88E+14 Gap:  0.00E+00
 Successful solution

 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :  0.048299999999999996 sec
 Objective      :  7.87726150830995E+14
 Successful solution
 ---------------------------------------------------

[[12.0] [12.0] [12.0] [12.0] [12.0] [12.0] [12.0] [12.0] [12.0] [12.0]
 [12.0] [12.0] [12.0] [12.0] [12.0] [12.0] [12.0] [12.0] [12.0] [12.0]]

I appreciate any help regarding this problem, thanks.

APMonitor commented 2 years ago

If you have the same optimization problem structure, you can override the default value with x.value=new_values. If it is a new optimization problem then use x = m.Var(new_values) to create a Gekko variable from values. Does this help?

Hirbod-JORFlint commented 2 years ago

@APMonitor thanks for this great idea! well, it's ok to a certain extent

I'm using the following syntax for minimization:

def function(x):
    $$$some internal operations(including string processing + string to number conversions + import from external function)$$$
    jtemp=[]
    for i in strtemp:
        jtemp.append(m.Var(value=numkey(i),lb=12,ub=200,integer=True))
    jres=jtemp[0]+2*(jtemp[1]**2)+2000*(jtemp[2]**3)+1000*(jtemp[3]**6)+(jtemp[4]**5)+17*(jtemp[5]**6)-(jtemp[6]**7)-13371553910000
    return np.abs(jres-res)

m = GEKKO(remote=False)
x = m.Array(m.Var,20,lb=12,ub=200,integer=True)
m.Minimize(function(x))
m.options.SOLVER=1
m.options.MAX_ITER=500
m.solve()
print(x)

Unfortunately, I'm not able to get optimal values of the decision variables after the above code(even after a successful search in solution space only initial values will be reported):

----------------------------------------------------------------
 APMonitor, Version 1.0.0
 APMonitor Optimization Suite
 ----------------------------------------------------------------

 --------- APM Model Size ------------
 Each time step contains
   Objects      :  0
   Constants    :  0
   Variables    :  52
   Intermediates:  0
   Connections  :  0
   Equations    :  1
   Residuals    :  1

 Number of state variables:    52
 Number of total equations: -  0
 Number of slack variables: -  0
 ---------------------------------------
 Degrees of freedom       :    52

 ----------------------------------------------
 Steady State Optimization with APOPT Solver
 ----------------------------------------------
Iter:     1 I:  0 Tm:      0.02 NLPi:   10 Dpth:    0 Lvs:    3 Obj:  5.73E+02 Gap:       NaN
Iter:     2 I:  0 Tm:      0.00 NLPi:    9 Dpth:    1 Lvs:    4 Obj:  4.49E-01 Gap:       NaN
Iter:     3 I:  0 Tm:      0.01 NLPi:    9 Dpth:    2 Lvs:    6 Obj:  2.24E+02 Gap:       NaN
--Integer Solution:   5.72E+10 Lowest Leaf:   2.24E+02 Gap:   2.00E+00
Iter:     4 I:  0 Tm:      0.00 NLPi:    1 Dpth:    2 Lvs:    5 Obj:  5.72E+10 Gap:  2.00E+00
Iter:     5 I:  0 Tm:      0.00 NLPi:   12 Dpth:    3 Lvs:    6 Obj:  2.42E+02 Gap:  2.00E+00
Iter:     6 I:  0 Tm:      0.00 NLPi:    7 Dpth:    3 Lvs:    7 Obj:  2.02E+03 Gap:  2.00E+00
Iter:     7 I:  0 Tm:      0.00 NLPi:   10 Dpth:    3 Lvs:    8 Obj:  1.34E+02 Gap:  2.00E+00
Iter:     8 I:  0 Tm:      0.00 NLPi:    9 Dpth:    4 Lvs:   10 Obj:  9.36E+01 Gap:  2.00E+00
Iter:     9 I:  0 Tm:      0.02 NLPi:   12 Dpth:    5 Lvs:   12 Obj:  2.25E+01 Gap:  2.00E+00
Iter:    10 I:  0 Tm:      0.00 NLPi:   11 Dpth:    6 Lvs:   13 Obj:  3.40E+01 Gap:  2.00E+00
Iter:    11 I:  0 Tm:      0.00 NLPi:   15 Dpth:    6 Lvs:   15 Obj:  2.61E+01 Gap:  2.00E+00
Iter:    12 I:  0 Tm:      0.00 NLPi:    8 Dpth:    6 Lvs:   17 Obj:  9.89E+00 Gap:  2.00E+00
Iter:    13 I:  0 Tm:      0.00 NLPi:   13 Dpth:    7 Lvs:   18 Obj:  1.43E+02 Gap:  2.00E+00
Iter:    14 I:  0 Tm:      0.00 NLPi:   11 Dpth:    7 Lvs:   17 Obj:  3.43E+01 Gap:  2.00E+00
Iter:    15 I:  0 Tm:      0.00 NLPi:    8 Dpth:    7 Lvs:   19 Obj:  3.56E+00 Gap:  2.00E+00
--Integer Solution:   5.72E+10 Lowest Leaf:   3.56E+00 Gap:   2.00E+00
Iter:    16 I:  0 Tm:      0.00 NLPi:    1 Dpth:    8 Lvs:   18 Obj:  3.66E+11 Gap:  2.00E+00
Iter:    17 I:  0 Tm:      0.00 NLPi:   10 Dpth:    8 Lvs:   18 Obj:  2.75E+01 Gap:  2.00E+00
Iter:    18 I:  0 Tm:      0.02 NLPi:   13 Dpth:    8 Lvs:   20 Obj:  2.75E+00 Gap:  2.00E+00
--Integer Solution:   5.72E+10 Lowest Leaf:   2.75E+00 Gap:   2.00E+00
Iter:    19 I:  0 Tm:      0.00 NLPi:    1 Dpth:    9 Lvs:   19 Obj:  4.82E+11 Gap:  2.00E+00
Iter:    20 I:  0 Tm:      0.00 NLPi:   11 Dpth:    9 Lvs:   21 Obj:  1.37E+02 Gap:  2.00E+00
Iter:    21 I:  0 Tm:      0.00 NLPi:   12 Dpth:    9 Lvs:   23 Obj:  1.20E+03 Gap:  2.00E+00
Iter:    22 I:  0 Tm:      0.00 NLPi:   11 Dpth:    7 Lvs:   25 Obj:  2.12E+02 Gap:  2.00E+00
--Integer Solution:   5.72E+10 Lowest Leaf:   2.61E+01 Gap:   2.00E+00
Iter:    23 I:  0 Tm:      0.00 NLPi:    2 Dpth:    7 Lvs:   24 Obj:  1.71E+11 Gap:  2.00E+00
Iter:    24 I:  0 Tm:      0.00 NLPi:   10 Dpth:    7 Lvs:   26 Obj:  4.35E+01 Gap:  2.00E+00
--Integer Solution:   5.72E+10 Lowest Leaf:   3.40E+01 Gap:   2.00E+00
Iter:    25 I:  0 Tm:      0.00 NLPi:    1 Dpth:    9 Lvs:   25 Obj:  2.35E+11 Gap:  2.00E+00
Iter:    26 I:  0 Tm:      0.00 NLPi:    7 Dpth:    7 Lvs:   27 Obj:  1.02E+01 Gap:  2.00E+00
Iter:    27 I:  0 Tm:      0.00 NLPi:   11 Dpth:    8 Lvs:   28 Obj:  9.54E+01 Gap:  2.00E+00
Iter:    28 I:  0 Tm:      0.00 NLPi:   11 Dpth:    8 Lvs:   29 Obj:  5.04E+02 Gap:  2.00E+00
Iter:    29 I:  0 Tm:      0.02 NLPi:   11 Dpth:    8 Lvs:   31 Obj:  1.30E+02 Gap:  2.00E+00
--Integer Solution:   7.22E+09 Lowest Leaf:   4.35E+01 Gap:   2.00E+00
Iter:    30 I:  0 Tm:     -0.00 NLPi:    1 Dpth:    7 Lvs:   30 Obj:  7.22E+09 Gap:  2.00E+00
--Integer Solution:   7.22E+09 Lowest Leaf:   4.35E+01 Gap:   2.00E+00
Iter:    31 I:  0 Tm:     -0.00 NLPi:    1 Dpth:    8 Lvs:   29 Obj:  7.84E+10 Gap:  2.00E+00
Iter:    32 I:  0 Tm:      0.00 NLPi:   11 Dpth:    8 Lvs:   30 Obj:  7.30E+02 Gap:  2.00E+00
Iter:    33 I:  0 Tm:      0.00 NLPi:    6 Dpth:    8 Lvs:   32 Obj:  2.30E+00 Gap:  2.00E+00
Iter:    34 I:  0 Tm:      0.00 NLPi:   10 Dpth:    9 Lvs:   34 Obj:  4.53E+00 Gap:  2.00E+00
Iter:    35 I:  0 Tm:      0.00 NLPi:   11 Dpth:    9 Lvs:   36 Obj:  1.30E+01 Gap:  2.00E+00
Iter:    36 I:  0 Tm:      0.00 NLPi:   18 Dpth:    9 Lvs:   38 Obj:  1.23E+02 Gap:  2.00E+00
Iter:    37 I: -9 Tm:      0.08 NLPi:  501 Dpth:   10 Lvs:   37 Obj:  4.53E+00 Gap:  2.00E+00
Iter:    38 I:  0 Tm:      0.00 NLPi:    5 Dpth:   10 Lvs:   37 Obj:  3.91E-03 Gap:  2.00E+00
--Integer Solution:   7.22E+09 Lowest Leaf:   4.53E+00 Gap:   2.00E+00
Iter:    39 I:  0 Tm:      0.00 NLPi:    1 Dpth:   11 Lvs:   36 Obj:  1.07E+11 Gap:  2.00E+00
Iter:    40 I:  0 Tm:      0.00 NLPi:    3 Dpth:   10 Lvs:   37 Obj:  9.51E+07 Gap:  2.00E+00
--Integer Solution:   7.22E+09 Lowest Leaf:   1.30E+01 Gap:   2.00E+00
Iter:    41 I:  0 Tm:      0.00 NLPi:    1 Dpth:   10 Lvs:   36 Obj:  8.70E+10 Gap:  2.00E+00
--Integer Solution:   7.22E+09 Lowest Leaf:   1.30E+01 Gap:   2.00E+00
Iter:    42 I:  0 Tm:      0.00 NLPi:    1 Dpth:   10 Lvs:   35 Obj:  7.84E+10 Gap:  2.00E+00
Iter:    43 I:  0 Tm:      0.00 NLPi:    3 Dpth:   10 Lvs:   36 Obj:  7.75E+05 Gap:  2.00E+00
Iter:    44 I:  0 Tm:      0.00 NLPi:    5 Dpth:    5 Lvs:   37 Obj:  5.86E-03 Gap:  2.00E+00
Iter:    45 I:  0 Tm:      0.00 NLPi:   10 Dpth:    6 Lvs:   39 Obj:  7.81E-03 Gap:  2.00E+00
--Integer Solution:   7.22E+09 Lowest Leaf:   7.81E-03 Gap:   2.00E+00
Iter:    46 I:  0 Tm:      0.00 NLPi:    2 Dpth:    6 Lvs:   38 Obj:  4.03E+10 Gap:  2.00E+00
Iter:    47 I:  0 Tm:      0.00 NLPi:    9 Dpth:    7 Lvs:   40 Obj:  1.37E-02 Gap:  2.00E+00
Iter:    48 I:  0 Tm:      0.00 NLPi:    8 Dpth:    7 Lvs:   42 Obj:  9.77E-03 Gap:  2.00E+00
--Integer Solution:   7.22E+09 Lowest Leaf:   9.77E-03 Gap:   2.00E+00
Iter:    49 I:  0 Tm:      0.00 NLPi:    5 Dpth:    7 Lvs:   41 Obj:  6.43E+10 Gap:  2.00E+00
--Integer Solution:   7.22E+09 Lowest Leaf:   9.77E-03 Gap:   2.00E+00
Iter:    50 I:  0 Tm:      0.00 NLPi:    1 Dpth:    8 Lvs:   40 Obj:  9.56E+10 Gap:  2.00E+00
Iter:    51 I:  0 Tm:      0.00 NLPi:    5 Dpth:    8 Lvs:   41 Obj:  7.81E-03 Gap:  2.00E+00
Iter:    52 I:  0 Tm:      0.00 NLPi:   10 Dpth:    9 Lvs:   43 Obj:  9.77E-03 Gap:  2.00E+00
--Integer Solution:   7.22E+09 Lowest Leaf:   9.77E-03 Gap:   2.00E+00
Iter:    53 I:  0 Tm:      0.00 NLPi:    2 Dpth:    9 Lvs:   42 Obj:  6.04E+10 Gap:  2.00E+00
Iter:    54 I:  0 Tm:      0.00 NLPi:    5 Dpth:   10 Lvs:   43 Obj:  7.81E-03 Gap:  2.00E+00
--Integer Solution:   7.22E+09 Lowest Leaf:   7.81E-03 Gap:   2.00E+00
Iter:    55 I:  0 Tm:      0.00 NLPi:    1 Dpth:   11 Lvs:   42 Obj:  2.24E+11 Gap:  2.00E+00
Iter:    56 I:  0 Tm:      0.02 NLPi:    4 Dpth:   11 Lvs:   43 Obj:  7.81E-03 Gap:  2.00E+00
Iter:    57 I:  0 Tm:      0.00 NLPi:    8 Dpth:   12 Lvs:   45 Obj:  3.91E-03 Gap:  2.00E+00
--Integer Solution:   1.17E-02 Lowest Leaf:   3.91E-03 Gap:   7.81E-03
Iter:    58 I:  0 Tm:      0.00 NLPi:    2 Dpth:   13 Lvs:   45 Obj:  1.17E-02 Gap:  7.81E-03
 Successful solution

 ---------------------------------------------------
 Solver         :  APOPT (v1.0)
 Solution time  :  0.2317 sec
 Objective      :  5227317.
 Successful solution
 ---------------------------------------------------

[[12.0] [12.0] [12.0] [12.0] [12.0] [12.0] [12.0] [12.0] [12.0] [12.0]
 [12.0] [12.0] [12.0] [12.0] [12.0] [12.0] [12.0] [12.0] [12.0] [12.0]]
APMonitor commented 2 years ago

Gekko calls the function goal(x) or function(x) only once when creating the optimization problem to create a symbolic function. It compiles this optimization statement into efficient byte-code that includes automatic differentiation and function evaluations. The function np.abs() needs to be replaced by the Gekko equivalent function m.abs3() or m.abs2() to give equations that are continuously differentiable to the gradient-based solvers.

Hirbod-JORFlint commented 2 years ago

@APMonitor So is it possible to report calculated variables alongside function evaluations in each step(in the case of pure python it can easily be accomplished through printing function locals). Also thank you for suggesting m.abs3() function, regretfully I'm not very familiar with the full syntax of the Gekko optimization framework.

APMonitor commented 2 years ago

Gekko produces an APM file in m.path that can be accessed with m.open_folder(). This APM file is compiled into byte-code and run by the solver. The solution doesn't return to Gekko until it is finished solving so intermediate function evaluations aren't possible. If you need the value at a particular iteration, try setting m.options.MAX_ITER=23 to stop of iteration 23. The solution can be printed but you would need to restart again with m.options.MAX_ITER=24 to see the current solution at iteration 24.