简体   繁体   中英

i have problem constraints with loops in GEKKO

I want to learn python and gekko but i have an issue. I want to write this matlab code again with gekko, for learning gekko. Here is matlab code that is working very well:

%transpporatation vizeden sonra slaytı sf7.
clear
clc
adilprob = optimproblem;
xdeg = optimvar('xdeg',3,3,'LowerBound',0);
%alloys = optimvar('alloys',3,'LowerBound',0);
cost=[7 9 11;
    7 11 11;
    4 5 12;];

spp=[300 350 400];
dmx=[100 100 200];

%cosst= xdeg*cost;
expr=optimexpr;% optimexpr yerine = optimexpr yapsakta calısıyor.

for i=1:3
    for j= 1:3
        expr=expr+cost(i,j)*xdeg(i,j);
    end
end

for i=1:3
    const1(i)=sum(xdeg(i,:)) ;
end

for j=1:3
    const2(j)=sum(xdeg(:,j)) ==dmx(j);
end

adilprob.Constraints.con1= const1(i)<=spp(i);
adilprob.Constraints.con2= const2;
%diqqat
adilprob.Objective = expr;
[sol,fval] = solve(adilprob)

and here is my gekko code that doesn't work

from gekko import GEKKO    
import numpy as np

m = GEKKO()
x = m.Array(m.Var,(4,4),lb=0)
const1=np.empty([2,0])
const2=np.empty([2,0])
expr=np.empty([3,3])
cost=([[7 ,9, 11],
    [7 ,11, 11],
    [4 ,5 ,12]])
spp=[300 ,350 ,400]
dmx=[100 ,100 ,200]
expr=[]
for i in range(2):
     for j in range(2):
           m.Obj(expr+cost[i][j]*x[i][j])

for k in range(2):
    const1[k]=(sum(x[k][:])) 
for m in range(2):    
    m.Equation(const1[m]<=spp[m])
for h in range(2):
    const2[h]=(sum(x[h][:]))

for y in range(2):    
    m.Equation(const2[y]==dmx[y]) 
m.solve()
print(x)

I'm running code with spyder. There is problem with constraints which have loop.

thanks for help.

Here is a version in Python gekko that simplifies the problem statement and solution.

from gekko import GEKKO    
import numpy as np

m = GEKKO(remote=False)
x = m.Array(m.Var,(3,3),lb=0)
cost=np.array([[7 ,9, 11],
               [7 ,11, 11],
               [4 ,5 ,12]])
spp=[300 ,350 ,400]
dmx=[100 ,100 ,200]

for i in range(3):
     for j in range(3):
           m.Minimize(cost[i,j]*x[i,j])

for i in range(3):
    m.Equation(m.sum(x[i,:])<=spp[i])
for j in range(3):
    m.Equation(m.sum(x[:,j])==dmx[j]) 
m.options.solver = 1
m.solve()
print('Objective Function: ' + str(m.options.objfcnval))
print(x)

A few tips:

  • There is no need to add all of the objective terms into one list. You can define m.Obj() or m.Minimize() multiple times and gekko adds them to give a final objective value.
  • The constraints do not need to be in a list. If you do have the equations in a list, use m.Equations() .

Here is the solution in Python that is the same objective function value as MATLAB but split the 200 between the first and second rows (1 and 2), and third column. This is because the cost (11) is the same for these two so it is a degenerate solution with many possible optimal answers.

Objective Function: 3100.0
[[[0.0] [0.0] [54.833333333]]
 [[0.0] [0.0] [145.16666667]]
 [[100.0] [100.0] [0.0]]]

thanks for the help, i'm starting to learn GEKKO. Now, I am solving a knapsak problem to learn, but this time I get the error "int 'object is not subscriptable". can you look at this code? what is the source of the problem How should I define the 1.10 matrices?

from gekko import GEKKO    
import numpy as np

m = GEKKO(remote=False)
x = m.Var((10),lb=0,ub=1,integer=True)
#x = m.Array(m.Var,(1,10),lb=0,ub=1,integer=True)
v=np.array([2, 2, 7, 8, 2, 1, 7, 9, 4, 10])
w=np.array([2, 2, 2, 2, 2, 1, 6, 7, 3, 3])
capacity=16

for j in range(10):
           m.Maximize(v[j]*x[j])

for i in range(10):
        m.Equation(m.sum(x[i]*w[i])<=capacity)

m.options.solver = 1
m.solve()
#print('Objective Function: ' + str(m.options.objfcnval))
print(x)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM