1

I want to formulate this constraint with Gekko: Vmin<Vi<Vmax for i=1,2,3,...,N with: Vi changes each iteration, V,P,Q,N=data(Qc_a): is a function with V is the output and Qc_a is the input and depend on the variable of decision Xi,j. My attempt:

Qc_a=[0,0,0,0,0,0,0,0,0]
V,P,Q,N=data(Qc_a) 
Qc=[150, 300, 450, 600,750, 900,1050]   
Nc=len(Qc)
m = GEKKO(remote=False)
X = m.Array(m.Var,(N,Nc),integer=True,lb=0,ub=1,value=0)
#constraint 1
for i in range(N):
    m.Equation(m.sum([X[i][j]for j in range(Nc)])<=1)
#constraint 2
Vmin=0.95
Vmax=1.05
Qc_a=np.zeros(N)
for i in range(N):
    for j in range(Nc):
        if Qc[j]*X[i,j].value!=0:
            Qc_a[i]=Qc[j]*X[i,j].value[0]
        else:
            pass
    V,P,Q,N=dta(Qc_a)
    m.Equation(V for i in range(N)>Vmin)
m.Obj(m.sum([m.sum([((P[i]**2)+(Q[i]-Qc[j]*X[i][j])**2)/V[i]**2])])) 
m.solver_options = ['minlp_gap_tol 1e-5',\
                    'minlp_maximum_iterations 10000',\
                    'minlp_max_iter_with_int_sol 2000']
m.options.SOLVER = 1
m.solve(debug=0, disp=True)  

1 Answers1

0

Below is a modified version that demonstrates how to optimize V with lower and upper bounds.

from gekko import GEKKO
import numpy as np

N = 9
Qm = np.ones(N)
Qc=[150,300,450,600,750,900,1050]   
Nc=len(Qc)

m = GEKKO(remote=False)
X = m.Array(m.Var,(N,Nc),integer=True,lb=0,ub=1)

Vmin=0.95; Vmax=1.05
V = m.Array(m.Var,N,lb=Vmin,ub=Vmax)

m.Equations([m.sum(X[i,:])<=1
             for i in range(N)])

Qc_a = X@Qc # dot product of X and Qc

m.Minimize(m.sum((Qm-Qc_a)**2/V**2)) 
m.solver_options = ['minlp_gap_tol 1e-5',\
                    'minlp_maximum_iterations 10000',\
                    'minlp_max_iter_with_int_sol 2000']
m.options.SOLVER = 1
m.solve(debug=0, disp=True)

print(X)

print(V)

The code:

for i in range(N):
    for j in range(Nc):
        if Qc[j]*X[i,j].value!=0:
            Qc_a[i]=Qc[j]*X[i,j].value[0]
        else:
            pass

can be replaced by X@Qc as the dot product of X and Qc. Only one value from each row of X can be one so the other values are zero and don't change Qc_a.

Gekko does support function calls, but it doesn't currently support black box function calls because the automatic differentiation can't "see" the equations to provide exact first and second derivatives to the gradient-based solvers. Use scipy.optimize.minimize if you need a tool that can call black-box functions. If the function can be written as Gekko variables and equations then it should work. Gekko only calls the function once to create a symbolic expression. These symbolic expressions are then compiled into byte-code for efficient solution.

John Hedengren
  • 12,068
  • 1
  • 21
  • 25
  • 1
    Thanks a lot for your answer, but this technique didn't give me the optimal solution (Solution Not Found), and I can't put all the code in public, could you help me? For me, the data() function gives me the data used to construct the objective function and the constraint. Also, when X changes all the data changes. – Chaymae Makri Mar 20 '23 at 10:18
  • I recommend that you create a minimal new question that reproduces the error, but doesn't share any proprietary data. I receive many private requests for help each week, and I need to limit my help to public forums such as StackOverflow where others can benefit from the questions and answers. – John Hedengren Mar 20 '23 at 10:21
  • 1
    Thanks for your answer and your time, if we can say that I already find the solution, but we can get the better optimal solution, how I can improve my code? – Chaymae Makri Mar 20 '23 at 10:58
  • Gekko solvers find local solutions. If you need to search for a Global solution then see methods for multi-start or hyper-parameter optimization: https://apmonitor.com/me575/index.php/Main/GlobalOptimization – John Hedengren Mar 20 '23 at 12:54
  • 1
    Is it normal to have a local solution (without hyperopt) lower than the global solution obtained with hyperopt? because I have already used hyper-parameter with the Gekko solver ' https://stackoverflow.com/questions/75554320/optimal-solution-with-gekko?noredirect=1#comment133310892_75554320' – Chaymae Makri Mar 21 '23 at 11:50
  • Hyper-opt will typically find a better solution. If you have a solution to this problem that is better then try plugging in those variables as parameters to ensure that the constraints are satisfied. – John Hedengren Mar 21 '23 at 16:20
  • 1
    Sorry, but I misunderstood your suggestion. For this problem, I know an optimal solution, but for a problem (another network) with new data (P, Q, R), I don't know. – Chaymae Makri Mar 22 '23 at 10:33