英文:
GEKKO gives @error: Solution Not Found when constraint has equality
问题
当我解决以下问题时,我得到:异常:@error:未找到解决方案
然后,我重新定义目标函数为 m.Maximize( sum((alpha[t]+beta[t]*P[t])(P[t]-MP[t]) for t in range (T))) 并消除约束,我得到解决方案。换句话说,我用约束替换了 D[t]。
请问您能解释一下错误背后的逻辑吗?平等约束有什么问题吗?
英文:
when I solve the following problem I get: Exception: @error: Solution Not Found
from gekko import GEKKO
import pandas as pd
import numpy as np
m = GEKKO(remote=True)
alpha=[6.97, 4.97, 6.55]
beta = [-0.04, -0.11, -0.1]
MP = [2707.2, 2213.3, 1904.1]
Plower = [1700, 1400, 1100]
Pupper = [5000, 5000, 5000]
h= 250 ; b= 150 ; c= 100
T= len(alpha)
D = m.Array(m.Var, T ,lb=-0, integer=False)
P = m.Array(m.Var, T ,lb=-0, integer=False)
m.Maximize( sum(D[t]*(P[t]-MP[t]) for t in range (T)))
m.Equations(D[t]==alpha[t]+beta[t]*P[t] for t in range (T))
#bound
m.Equations(P[t]>= Plower[t]for t in range (T))
m.Equations(P[t]<= Pupper[t] for t in range (T))
m.options.SOLVER=1
m.solve(disp=True) # solve on public server
then, I reformulate the objective function as m.Maximize( sum((alpha[t]+beta[t]*P[t])(P[t]-MP[t]) for t in range (T))) and eliminate the constraint and I get the solution. In other words, I replace D[t] using the constraint.
Can you please explain the logic behind the error. Is there any problem with equality constraint?
答案1
得分: 1
以下是您请求的翻译部分:
求解器通常可以通过简化模型来提高性能。这是一个简单改进的示例,以消除变量 D[t]
。在Gekko中另一种方法是使用一个Intermediate变量。
也可以使用 m.options.REDUCE=3
选项自动化模型中变量的消除。它会分析可以删除的简单方程,并在将问题传递给求解器之前通过预求解来固定变量值。这个选项对这个模型没有帮助。以下是三种方法:
1. 原始(不可行):
from gekko import GEKKO
import pandas as pd
import numpy as np
m = GEKKO(remote=True)
alpha=[6.97, 4.97, 6.55]
beta = [-0.04, -0.11, -0.1]
MP = [2707.2, 2213.3, 1904.1]
Plower = [1700, 1400, 1100]
Pupper = [5000, 5000, 5000]
h= 250 ; b= 150 ; c= 100
T= len(alpha)
D = m.Array(m.Var, T ,lb=-0, integer=False)
P = m.Array(m.Var, T ,lb=-0, integer=False)
m.Maximize( sum(D[t]*(P[t]-MP[t]) for t in range (T)))
m.Equations(D[t]==alpha[t]+beta[t]*P[t] for t in range (T))
m.Equations(P[t]>= Plower[t]for t in range (T))
m.Equations(P[t]<= Pupper[t] for t in range (T))
m.options.SOLVER=1
m.solve(disp=True) # 在公共服务器上求解
2. 目标函数替代(成功):
from gekko import GEKKO
import pandas as pd
import numpy as np
m = GEKKO(remote=True)
alpha=[6.97, 4.97, 6.55]
beta = [-0.04, -0.11, -0.1]
MP = [2707.2, 2213.3, 1904.1]
Plower = [1700, 1400, 1100]
Pupper = [5000, 5000, 5000]
h= 250 ; b= 150 ; c= 100
T= len(alpha)
D = m.Array(m.Var, T ,lb=-0, integer=False)
P = m.Array(m.Var, T ,lb=-0, integer=False)
m.Maximize(sum((alpha[t]+beta[t]*P[t])*(P[t]-MP[t]) for t in range (T)))
m.Equations(P[t]>=Plower[t] for t in range (T))
m.Equations(P[t]<=Pupper[t] for t in range (T))
m.options.SOLVER=1
m.solve(disp=True) # 在公共服务器上求解
3. Intermediate Variable(成功):
from gekko import GEKKO
import pandas as pd
import numpy as np
m = GEKKO(remote=True)
alpha=[6.97, 4.97, 6.55]
beta = [-0.04, -0.11, -0.1]
MP = [2707.2, 2213.3, 1904.1]
Plower = [1700, 1400, 1100]
Pupper = [5000, 5000, 5000]
h= 250 ; b= 150 ; c= 100
T= len(alpha)
D = m.Array(m.Var, T ,lb=-0, integer=False)
P = m.Array(m.Var, T ,lb=-0, integer=False)
D = [m.Intermediate(alpha[t]+beta[t]*P[t]) for t in range (T)]
m.Maximize(sum(D[t]*(P[t]-MP[t]) for t in range (T)))
m.Equations(P[t]>=Plower[t] for t in range (T))
m.Equations(P[t]<=Pupper[t] for t in range (T))
m.options.SOLVER=1
m.solve(disp=True) # 在公共服务器上求解
我更喜欢Intermediate变量,因为它们使模型更容易阅读和诊断,而不是使用非常长的方程或目标函数定义。
英文:
Solvers can often perform better with simplification of the model. This is an excellent example of a simple reformulation to eliminate variables D[t]
. Another way to do this in Gekko is to use an Intermediate variable.
D = [m.Intermediate(alpha[t]+beta[t]*P[t]) for t in range (T)]
m.Maximize(sum(D[t]*(P[t]-MP[t]) for t in range (T)))
There is also the m.options.REDUCE=3
option that automates the elimination of variables from the model. It analyzes simple equations that can be removed and variable values fixed with a pre-solve before the problem is given to the solver. This option wouldn't help with this model. Here are the three approaches:
1. Original (Infeasible):
from gekko import GEKKO
import pandas as pd
import numpy as np
m = GEKKO(remote=True)
alpha=[6.97, 4.97, 6.55]
beta = [-0.04, -0.11, -0.1]
MP = [2707.2, 2213.3, 1904.1]
Plower = [1700, 1400, 1100]
Pupper = [5000, 5000, 5000]
h= 250 ; b= 150 ; c= 100
T= len(alpha)
D = m.Array(m.Var, T ,lb=-0, integer=False)
P = m.Array(m.Var, T ,lb=-0, integer=False)
m.Maximize( sum(D[t]*(P[t]-MP[t]) for t in range (T)))
m.Equations(D[t]==alpha[t]+beta[t]*P[t] for t in range (T))
m.Equations(P[t]>= Plower[t]for t in range (T))
m.Equations(P[t]<= Pupper[t] for t in range (T))
m.options.SOLVER=1
m.solve(disp=True) # solve on public server
2. Objective Function Substitution (Successful):
from gekko import GEKKO
import pandas as pd
import numpy as np
m = GEKKO(remote=True)
alpha=[6.97, 4.97, 6.55]
beta = [-0.04, -0.11, -0.1]
MP = [2707.2, 2213.3, 1904.1]
Plower = [1700, 1400, 1100]
Pupper = [5000, 5000, 5000]
h= 250 ; b= 150 ; c= 100
T= len(alpha)
D = m.Array(m.Var, T ,lb=-0, integer=False)
P = m.Array(m.Var, T ,lb=-0, integer=False)
m.Maximize(sum((alpha[t]+beta[t]*P[t])*(P[t]-MP[t]) for t in range (T)))
m.Equations(P[t]>=Plower[t] for t in range (T))
m.Equations(P[t]<=Pupper[t] for t in range (T))
m.options.SOLVER=1
m.solve(disp=True) # solve on public server
3. Intermediate Variable (Successful):
from gekko import GEKKO
import pandas as pd
import numpy as np
m = GEKKO(remote=True)
alpha=[6.97, 4.97, 6.55]
beta = [-0.04, -0.11, -0.1]
MP = [2707.2, 2213.3, 1904.1]
Plower = [1700, 1400, 1100]
Pupper = [5000, 5000, 5000]
h= 250 ; b= 150 ; c= 100
T= len(alpha)
D = m.Array(m.Var, T ,lb=-0, integer=False)
P = m.Array(m.Var, T ,lb=-0, integer=False)
D = [m.Intermediate(alpha[t]+beta[t]*P[t]) for t in range (T)]
m.Maximize(sum(D[t]*(P[t]-MP[t]) for t in range (T)))
m.Equations(P[t]>=Plower[t] for t in range (T))
m.Equations(P[t]<=Pupper[t] for t in range (T))
m.options.SOLVER=1
m.solve(disp=True) # solve on public server
I prefer Intermediate variables because they keep the model easier to read and diagnose than using very long equations or objective function definitions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论