英文:
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="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')
#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
>=0,"volatility_constraint"
)
#Capacity constraint
Supply=m.add_constraint(X1+X2+X3+X4+X5+X6+X7+X8==1225,"Supply")
#Rail, Truck & Union constraints
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")
#Constraints
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()
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 <cell line: 49>()
47 m.print_information()
48 s=m.solve()
---> 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 """ 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 """
-> 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("Model<{0}> has not been solved yet", self.name)
5188 else:
-> 5189 self.fatal("Model<{0}> did not solve successfully", 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):
-> 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()
--> 210 raise DOcplexException(resolved_message)
DOcplexException: Model<bedford> 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 'Supply' is dependent and infeasible.
And then if you remove
Supply=m.add_constraint(X1+X2+X3+X4+X5+X6+X7+X8==1225,"Supply")
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="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')
#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
>=0,"volatility_constraint"
)
#Capacity constraint
Supply=m.add_constraint(X1+X2+X3+X4+X5+X6+X7+X8==1225,"Supply")
#Rail, Truck & Union constraints
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")
#Constraints
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()
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论