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.