有人能修复我的Python代码和docplex吗?

huangapple go评论80阅读模式
英文:

Can someone fix my python code & docplex?

问题

以下是您的代码的翻译部分:

我需要为一个项目编写Python代码但在运行时出现了问题我尝试修复它但始终出现相同的问题它显示相同的错误代码...
# 这是我的代码

from docplex.mp.model import Model
m = Model(name="bedford")
X1 = m.continuous_var(name='Ashley')
X2 = m.continuous_var(name='Bedford')
X3 = m.continuous_var(name='Cosnol')
X4 = m.continuous_var(name='Dunby')
X5 = m.continuous_var(name='Earlam')
X6 = m.continuous_var(name='Florence')
X7 = m.continuous_var(name='Gaston')
X8 = m.continuous_var(name='Hopt')
# 平均波动性
avgvol = 19
# 波动性约束
Volatility = m.add_constraint((15 - avgvol) * X1 +
                              (16 - avgvol) * X2 +
                              (18 - avgvol) * X3 +
                              (20 - avgvol) * X4 +
                              (21 - avgvol) * X5 +
                              (22 - avgvol) * X6 +
                              (23 - avgvol) * X7 +
                              (24 - avgvol) * X8
                              >= 0, "volatility_constraint"
                              )
# 容量约束
Supply = m.add_constraint(X1 + X2 + X3 + X4 + X5 + X6 + X7 + X8 == 1225, "Supply")
# 铁路、卡车和工会约束
Truck = m.add_constraint(X2 + X4 + X5 + X6 == 720, "Truck")
Rail = m.add_constraint(X1 + X3 + X7 + X8 == 650, "Rail")
Union = m.add_constraint(X1 + X2 - X3 + X4 - X5 + X6 - X7 - X8 >= 0, "Union")
# 约束
Ashley = m.add_constraint(X1 <= 300, "Ashley")
Bedford = m.add_constraint(X2 <= 600, "Bedford")
Cosnol = m.add_constraint(X3 <= 500, "Cosnol")
Dunby = m add_constraint(X4 <= 655, "Dunby")
Earlam = m.add_constraint(X5 <= 575, "Earlam")
Florence = m.add_constraint(X6 <= 680, "Florence")
Gaston = m.add_constraint(X7 <= 450, "Gaston")
Hopt = m.add_constraint(X8 <= 490, "Hopt")
m.minimize(X1 * 49.5 + X2 * 50 + X3 * 61 + X4 * 63.5 + X5 * 66.5 + X6 * 71 + X7 * 72.5 + X8 * 80)
m.print_information()
s = m.solve()
m.print_solution()

请注意,翻译中保留了原始代码的格式。如果您需要帮助解决错误,请提供有关错误的更多详细信息。

英文:

I have to do a python code for a project but when i run it, i have a problem with it. I try to fix it but it's always the same problem. It said the same code error...

this is my code

from docplex.mp.model import Model
m=Model(name=&quot;bedford&quot;)
X1=m.continuous_var(name=&#39;Ashley&#39;)
X2=m.continuous_var(name=&#39;Bedford&#39;)
X3=m.continuous_var(name=&#39;Cosnol&#39;)
X4=m.continuous_var(name=&#39;Dunby&#39;)
X5=m.continuous_var(name=&#39;Earlam&#39;)
X6=m.continuous_var(name=&#39;Florence&#39;)
X7=m.continuous_var(name=&#39;Gaston&#39;)
X8=m.continuous_var(name=&#39;Hopt&#39;)
#average volatility
avgvol=19
#volatility constraint
Volatility=m.add_constraint((15-avgvol)*X1+ 
(16-avgvol)*X2+ 
(18-avgvol)*X3+ 
(20-avgvol)*X4+ 
(21-avgvol)*X5+ 
(22-avgvol)*X6+ 
(23-avgvol)*X7+ 
(24-avgvol)*X8 
&gt;=0,&quot;volatility_constraint&quot;
)
#Capacity constraint
Supply=m.add_constraint(X1+X2+X3+X4+X5+X6+X7+X8==1225,&quot;Supply&quot;)
#Rail, Truck &amp; Union constraints
Truck=m.add_constraint(X2+X4+X5+X6==720,&quot;Truck&quot;)
Rail=m.add_constraint(X1+X3+X7+X8==650,&quot;Rail&quot;)
Union=m.add_constraint(X1+X2-X3+X4-X5+X6-X7-X8&gt;=0,&quot;Union&quot;)
#Constraints
Ashley=m.add_constraint(X1&lt;= 300,&quot;Ashley&quot;)
Bedford=m.add_constraint(X2&lt;= 600,&quot;Bedford&quot;)
Cosnol=m.add_constraint(X3&lt;= 500,&quot;Cosnol&quot;)
Dunby=m.add_constraint(X4&lt;= 655,&quot;Dunby&quot;)
Earlam=m.add_constraint(X5&lt;= 575,&quot;Earlam&quot;)
Florence=m.add_constraint(X6&lt;= 680,&quot;Florence&quot;)
Gaston=m.add_constraint(X7&lt;= 450,&quot;Gaston&quot;)
Hopt=m.add_constraint(X8&lt;=490,&quot;Hopt&quot;)
m.minimize(X1*49.5+X2*50+X3*61+X4*63.5+X5*66.5+X6*71+X7*72.5+X8*80)
m.print_information()
s=m.solve()
m.print_solution()

and the error code: I tried to understand but at the final it's always the same problem, I need a help of someone

DOcplexException                          Traceback (most recent call last)
Input In [6], in &lt;cell line: 49&gt;()
47 m.print_information()
48 s=m.solve()
---&gt; 49 m.print_solution()
File ~/opt/anaconda3/lib/python3.9/site-packages/docplex/mp/model.py:6081, in         Model.print_solution(self, print_zeros, solution_header_fmt, var_value_fmt, **kwargs)
6063 def print_solution(self, print_zeros=False,
6064                    solution_header_fmt=None,
6065                    var_value_fmt=None,
6066                    **kwargs):
6067     &quot;&quot;&quot;  Prints the values of the model variables after a solve.
6068 
6069     Only valid after a successful solve. If the model has not been solved successfully, an
(...)
6079         :func:`docplex.mp.solution.SolveSolution.display`
6080     &quot;&quot;&quot;
-&gt; 6081     self._check_has_solution()
6082     if var_value_fmt is None:
6083         if self._has_username_with_spaces():
File ~/opt/anaconda3/lib/python3.9/site-packages/docplex/mp/model.py:5189, in     Model._check_has_solution(self)
5187     self.fatal(&quot;Model&lt;{0}&gt; has not been solved yet&quot;, self.name)
5188 else:
-&gt; 5189     self.fatal(&quot;Model&lt;{0}&gt; did not solve successfully&quot;, self.name)
File ~/opt/anaconda3/lib/python3.9/site-packages/docplex/mp/model.py:1080, in Model.fatal(self, msg, *args)
1079 def fatal(self, msg, *args):
-&gt; 1080     self._error_handler.fatal(msg, args)
File ~/opt/anaconda3/lib/python3.9/site-packages/docplex/mp/error_handler.py:210, in AbstractErrorHandler.fatal(self, msg, args)
208 resolved_message = resolve_pattern(msg, args)
209 docplex_error_stop_here()
--&gt; 210 raise DOcplexException(resolved_message)
DOcplexException: Model&lt;bedford&gt; did not solve successfully

答案1

得分: 0

如果您要求记录登录信息

m.solve(log_output=True)

然后您将得到

行 'Supply' 有依赖性且不可行。

然后,如果您移除

Supply=m.add_constraint(X1+X2+X3+X4+X5+X6+X7+X8==1225, "Supply")

您将得到一个解决方案

目标值: 82353.000
Ashley=200.000
Bedford=488.000
Earlam=232.000
Gaston=450.000

如果您想要查看类似于 https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoorelaxationandconflict.py 中的冲突

from docplex.mp.model import Model
from docplex.mp.conflict_refiner import ConflictRefiner

m=Model(name="bedford")
X1=m.continuous_var(name='Ashley')
X2=m.continuous_var(name='Bedford')
X3=m.continuous_var(name='Cosnol')
X4=m.continuous_var(name='Dunby')
X5=m.continuous_var(name='Earlam')
X6=m.continuous_var(name='Florence')
X7=m.continuous_var(name='Gaston')
X8=m.continuous_var(name='Hopt')
#平均波动性
avgvol=19
#波动性约束
Volatility=m.add_constraint((15-avgvol)*X1+
                            (16-avgvol)*X2+
                            (18-avgvol)*X3+
                            (20-avgvol)*X4+
                            (21-avgvol)*X5+
                            (22-avgvol)*X6+
                            (23-avgvol)*X7+
                            (24-avgvol)*X8
                            >=0, "volatility_constraint"
                           )
#容量约束
Supply=m.add_constraint(X1+X2+X3+X4+X5+X6+X7+X8==1225, "Supply")
#卡车,卡车和工会约束
Truck=m.add_constraint(X2+X4+X5+X6==720, "Truck")
Rail=m.add_constraint(X1+X3+X7+X8==650, "Rail")
Union=m.add_constraint(X1+X2-X3+X4-X5+X6-X7-X8>=0, "Union")
#约束
Ashley=m.add_constraint(X1<= 300, "Ashley")
Bedford=m.add_constraint(X2<= 600, "Bedford")
Cosnol=m.add_constraint(X3<= 500, "Cosnol")
Dunby=m.add_constraint(X4<= 655, "Dunby")
Earlam=m.add_constraint(X5<= 575, "Earlam")
Florence=m.add_constraint(X6<= 680, "Florence")
Gaston=m.add_constraint(X7<= 450, "Gaston")
Hopt=m.add_constraint(X8<=490, "Hopt")
m.minimize(X1*49.5+X2*50+X3*61+X4*63.5+X5*66.5+X6*71+X7*72.5+X8*80)
m.print_information()
#m.solve(log_output=True)

cr=ConflictRefiner()
conflicts=cr.refine_conflict(m)
conflicts.display()
m.print_solution()

然后您将得到

冲突数:3
- 状态:成员,线性约束:Supply: Ashley + Bedford + Cosnol + Dunby + Earlam + Flo.. == 1225
- 状态:成员,线性约束:Truck: Bedford + Dunby + Earlam + Florence == 720
- 状态:成员,线性约束:Rail: Ashley + Cosnol + Gaston + Hopt == 650
英文:

if you ask for the log in

m.solve(log_output=True)

then you will get

Row &#39;Supply&#39; is dependent and infeasible.

And then if you remove

Supply=m.add_constraint(X1+X2+X3+X4+X5+X6+X7+X8==1225,&quot;Supply&quot;)

you will get a solution

objective: 82353.000
Ashley=200.000
Bedford=488.000
Earlam=232.000
Gaston=450.000

If you sant to see conflicts like in https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoorelaxationandconflict.py

from docplex.mp.model import Model
from docplex.mp.conflict_refiner import ConflictRefiner
m=Model(name=&quot;bedford&quot;)
X1=m.continuous_var(name=&#39;Ashley&#39;)
X2=m.continuous_var(name=&#39;Bedford&#39;)
X3=m.continuous_var(name=&#39;Cosnol&#39;)
X4=m.continuous_var(name=&#39;Dunby&#39;)
X5=m.continuous_var(name=&#39;Earlam&#39;)
X6=m.continuous_var(name=&#39;Florence&#39;)
X7=m.continuous_var(name=&#39;Gaston&#39;)
X8=m.continuous_var(name=&#39;Hopt&#39;)
#average volatility
avgvol=19
#volatility constraint
Volatility=m.add_constraint((15-avgvol)*X1+ 
(16-avgvol)*X2+ 
(18-avgvol)*X3+ 
(20-avgvol)*X4+ 
(21-avgvol)*X5+ 
(22-avgvol)*X6+ 
(23-avgvol)*X7+ 
(24-avgvol)*X8 
&gt;=0,&quot;volatility_constraint&quot;
)
#Capacity constraint
Supply=m.add_constraint(X1+X2+X3+X4+X5+X6+X7+X8==1225,&quot;Supply&quot;)
#Rail, Truck &amp; Union constraints
Truck=m.add_constraint(X2+X4+X5+X6==720,&quot;Truck&quot;)
Rail=m.add_constraint(X1+X3+X7+X8==650,&quot;Rail&quot;)
Union=m.add_constraint(X1+X2-X3+X4-X5+X6-X7-X8&gt;=0,&quot;Union&quot;)
#Constraints
Ashley=m.add_constraint(X1&lt;= 300,&quot;Ashley&quot;)
Bedford=m.add_constraint(X2&lt;= 600,&quot;Bedford&quot;)
Cosnol=m.add_constraint(X3&lt;= 500,&quot;Cosnol&quot;)
Dunby=m.add_constraint(X4&lt;= 655,&quot;Dunby&quot;)
Earlam=m.add_constraint(X5&lt;= 575,&quot;Earlam&quot;)
Florence=m.add_constraint(X6&lt;= 680,&quot;Florence&quot;)
Gaston=m.add_constraint(X7&lt;= 450,&quot;Gaston&quot;)
Hopt=m.add_constraint(X8&lt;=490,&quot;Hopt&quot;)
m.minimize(X1*49.5+X2*50+X3*61+X4*63.5+X5*66.5+X6*71+X7*72.5+X8*80)
m.print_information()
#m.solve(log_output=True)
cr=ConflictRefiner()
conflicts=cr.refine_conflict(m)
conflicts.display()
m.print_solution()

and you will get

conflict(s): 3
- status: Member, LinearConstraint: Supply: Ashley + Bedford + Cosnol + Dunby + Earlam + Flo.. == 1225
- status: Member, LinearConstraint: Truck: Bedford + Dunby + Earlam + Florence == 720
- status: Member, LinearConstraint: Rail: Ashley + Cosnol + Gaston + Hopt == 650

huangapple
  • 本文由 发表于 2023年2月23日 19:26:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定