Search code examples
optimizationgekko

Create arrays of variables in loop GEKKO


I'm wondering if it is possible to create arrays of variables with different lenghts in a loop in GEKKO.

Below is just a simple example of what I mean. Parameters in the list "lengths" define what length each GEKKO array should have:

lengths = [10,20,30]

m = GEKKO()

for i in lengths:
    # something...

So from this I would like to get something like:

array1 = m.Array(m.Var,10)
array2 = m.Array(m.Var,20)
array3 = m.Array(m.Var,30)

In the real problem that I'm trying to solve there will be quite many arrays that I want to include in the optimization, and they might be different depending on the situation. So it is not a good option to manually create them every time.


Solution

  • There is no problem to define arrays in a loop. Here is an example:

    from gekko import GEKKO
    model = GEKKO(remote=True)
    lengths = [10,20,30]
    m = GEKKO()
    x = []
    for i in lengths:
        x.append(m.Array(m.Var,i))
        for j in range(i):
            m.Minimize((x[-1][j]-j)**2)
            
    m.solve()
    
    for xi in x:
        print(xi)
    

    This gives a unique solution where the value is equal to the index.

    This is Ipopt version 3.12.10, running with linear solver ma57.
    
    Number of nonzeros in equality constraint Jacobian...:        0
    Number of nonzeros in inequality constraint Jacobian.:        0
    Number of nonzeros in Lagrangian Hessian.............:       60
    
    Total number of variables............................:       60
                         variables with only lower bounds:        0
                    variables with lower and upper bounds:        0
                         variables with only upper bounds:        0
    Total number of equality constraints.................:        0
    Total number of inequality constraints...............:        0
            inequality constraints with only lower bounds:        0
       inequality constraints with lower and upper bounds:        0
            inequality constraints with only upper bounds:        0
    
    iter    objective    inf_pr   inf_du lg(mu)  ||d||  lg(rg) alpha_du alpha_pr  ls
       0  1.1310000e+04 0.00e+00 5.80e+01   0.0 0.00e+00    -  0.00e+00 0.00e+00   0
       1  0.0000000e+00 0.00e+00 0.00e+00 -11.0 2.90e+01    -  1.00e+00 1.00e+00f  1
    
    Number of Iterations....: 1
    
                                       (scaled)                 (unscaled)
    Objective...............:   0.0000000000000000e+00    0.0000000000000000e+00
    Dual infeasibility......:   0.0000000000000000e+00    0.0000000000000000e+00
    Constraint violation....:   0.0000000000000000e+00    0.0000000000000000e+00
    Complementarity.........:   0.0000000000000000e+00    0.0000000000000000e+00
    Overall NLP error.......:   0.0000000000000000e+00    0.0000000000000000e+00
    
    
    Number of objective function evaluations             = 2
    Number of objective gradient evaluations             = 2
    Number of equality constraint evaluations            = 0
    Number of inequality constraint evaluations          = 0
    Number of equality constraint Jacobian evaluations   = 0
    Number of inequality constraint Jacobian evaluations = 0
    Number of Lagrangian Hessian evaluations             = 1
    Total CPU secs in IPOPT (w/o function evaluations)   =      0.001
    Total CPU secs in NLP function evaluations           =      0.000
    
    EXIT: Optimal Solution Found.
     
     The solution was found.
     
     The final value of the objective function is   0.000000000000000E+000
     
     ---------------------------------------------------
     Solver         :  IPOPT (v3.12)
     Solution time  :   4.600000000209548E-003 sec
     Objective      :   0.000000000000000E+000
     Successful solution
     ---------------------------------------------------
     
    [[0.0] [1.0] [2.0] [3.0] [4.0] [5.0] [6.0] [7.0] [8.0] [9.0]]
    [[0.0] [1.0] [2.0] [3.0] [4.0] [5.0] [6.0] [7.0] [8.0] [9.0] [10.0] [11.0]
     [12.0] [13.0] [14.0] [15.0] [16.0] [17.0] [18.0] [19.0]]
    [[0.0] [1.0] [2.0] [3.0] [4.0] [5.0] [6.0] [7.0] [8.0] [9.0] [10.0] [11.0]
     [12.0] [13.0] [14.0] [15.0] [16.0] [17.0] [18.0] [19.0] [20.0] [21.0]
     [22.0] [23.0] [24.0] [25.0] [26.0] [27.0] [28.0] [29.0]]