Chaymae Makri
Chaymae Makri

Reputation: 77

How formulat my optimization problem with Gekko?

I want to minimize this function:

sum[sum[Ri*{Pi² + (Qi - Qcj*Xij)²}for j in range(Nc)] for i in range(N)] 

with P and Q as constants, Qc is a list of the proposed solution, and X is our decision variable (binary variable). I have already tried this code:

from gekko import GEKKO
import numpy as np
P=[13.10511598922975,11.2611396806742,10.103920431906348,8.199519500182628,
   6.411296067052755,4.753519719147589,3.8977762462825973,2.6593092284662734,
   1.6399999999854893]
Q=[5.06643685386732,4.4344047044589585,3.8082608015186405,3.2626022579039584,
 1.2568869621197523,0.6152693459109657,0.46237064874523776,0.35226399840832523,
 0.20000000001140983]
R=[0.1233, 0.014, 0.7463, 0.6984, 1.9831, 0.9053, 2.0552, 4.7953, 5.3434]
Qc=[150, 300, 450, 600,750,900,1050,1200,1350,1500,1650,1800,
    1950,2100,2250,2400,2550,2700,2850,3000,3150,3300,3450,3600,
    3750,3900,4050]
N=len(Q)
Nc=len(Qc)
m = GEKKO(remote=False)
X = m.Array(m.Var,(N,Nc),integer=True,lb=0,ub=1,value=0)
#""""
bv = np.array([[0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
               [0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
               [0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
               [0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0],
               [0, 0, 0, 0, 0, 0, 0, 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
               [0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
               [0, 0, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
               [0, 0, 1, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
               [0, 1, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]])
for i in range(N):
    for j in range(Nc):
        X[i,j].value = bv[i,j]
        #m.Equation(X[i,j]==bv[i,j])
        
#convirtir P et Q en KW 
for i in range(N):
    Q[i]=Q[i]*1000
    P[i]=P[i]*1000
    R[i]=R[i]*0.001

#constrainte ## one per line
for i in range(N):
    m.Equation(m.sum([X[i][j]for j in range(Nc)])<=1) 
b=m.sum([m.sum([(R[i]*((P[i]**2)+(Q[i]-Qc[j]*X[i][j])**2)) for j in range(Nc)]) for i in range(N)])
m.Minimize(b)
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)

But the solution is not optimal for me when I calculate the power Loss (Ploss=R*(P²+(Q-Qc)²)).

Ploss=np.zeros(N)
for i in range(N):
    for j in range(Nc):
        #print(i,j,X[i][j].value[0])
        Ploss[i]= R[i]*((P[i]**2)+(Q[i]-Qc[j]*X[i][j].value[0])**2)
print('the total Ploss en KW',sum(Ploss))`

The total loss for the first solution is the same when I use the solver with and without the initial value of X, the total loss is equal to 350684.76, and the objective function is equal to 9554569.42. My objective is to reduce total loss.

Upvotes: 2

Views: 90

Answers (1)

John Hedengren
John Hedengren

Reputation: 14376

The Ploss calculation needs a += instead of just an = to sum the power loss over all j elements, not just the last j element. Does this help to reconcile the difference between the solver optimum and the post-processing results?

Ploss=np.zeros(N)
for i in range(N):
    for j in range(Nc):
        Ploss[i]+= R[i]*((P[i]**2)+(Q[i]-Qc[j]*X[i][j].value[0])**2)
print('the total Ploss en KW',sum(Ploss))

Upvotes: 0

Related Questions