简体   繁体   English

我对 GEKKO 中的循环有问题限制

[英]i have problem constraints with loops in GEKKO

I want to learn python and gekko but i have an issue.我想学习 python 和壁虎,但我有一个问题。 I want to write this matlab code again with gekko, for learning gekko.我想用gekko再次编写这个matlab代码,用于学习gekko。 Here is matlab code that is working very well:这是运行良好的 matlab 代码:

%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这是我的 gekko 代码不起作用

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.我正在用 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.这是 Python gekko 中的一个版本,它简化了问题陈述和解决方案。

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.您可以定义m.Obj()m.Minimize()多次,gekko 添加它们以给出最终的目标值。
  • The constraints do not need to be in a list.约束不需要在列表中。 If you do have the equations in a list, use m.Equations() .如果列表中有方程式,请使用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.这是 Python 中的解决方案,它与 MATLAB 的目标 function 值相同,但在第一行和第二行(1 和 2 列)之间拆分了 200。 This is because the cost (11) is the same for these two so it is a degenerate solution with many possible optimal answers.这是因为这两个的成本 (11) 是相同的,所以它是一个退化的解决方案,有许多可能的最佳答案。

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.感谢您的帮助,我开始学习 GEKKO。 Now, I am solving a knapsak problem to learn, but this time I get the error "int 'object is not subscriptable".现在,我正在解决一个要学习的背包问题,但这次我收到错误“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?问题的根源是什么 我应该如何定义 1.10 矩阵?

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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