Cplex优化程序返回结果为零。

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

Cplex optimization program returns results equal to zero

问题

我目前正在解决一个优化问题,其中一个湖有150单位的水。我每卖出一单位的水就得到3美元,但我需要保证月底时还剩下100单位的水,否则我就要为低于100单位的每一单位支付5美元。我知道雨水会带来125单位的水(稍后我会添加随机性的降雨)。

我的模型如下:

!pip install cplex
!pip install docplex
from docplex.mp.model import Model
from docplex.mp.environment import Environment
env = Environment()
env.print_information()

mdl = Model()

x = mdl.continuous_var(lb=None, ub=None, name=None )
y = mdl.continuous_var(lb=None, ub=None, name=None )

r1=mdl.add_constraint( 150-x+y+125 >= 100 )    
s = mdl.solve()

mdl.maximize( 3*x-5*y )
obj = mdl.objective_value

print(x.solution_value)
print(y.solution_value)

print("* best objective is: {:g}".format(obj))

mdl.export("modelo_determinista_bajo.lp")

其中x是卖出的水量,y是低于100单位的水量。

模型的输出对于x、y和收益都是零。

我看不出我做错了什么。有人可以帮帮我吗?
致以最诚挚的问候。

英文:

I am currently working on an optimization problem in which a lake has 150 units of water. I am paid 3$ for each unit of water sold, but I need to guarantee that 100 units of water will remain at the end of the month or pay 5$ for each unit below the threshold of 100. I know that rain will bring 125 units of water (later on I will add stochastic rain).

My model is as follows

!pip install cplex
!pip install docplex
from docplex.mp.model import Model
from docplex.mp.environment import Environment
env = Environment()
env.print_information()

mdl = Model()

x = mdl.continuous_var(lb=None, ub=None, name=None )
y = mdl.continuous_var(lb=None, ub=None, name=None )



r1=mdl.add_constraint( 150-x+y+125 >= 100  )    
s = mdl.solve()

mdl.maximize(  3*x-5*y )
obj = mdl.objective_value



print(x.solution_value)
print(y.solution_value)

print("* best objective is: {:g}".format(obj))

mdl.export("modelo_determinista_bajo.lp")

where x is the amount of water sold and y is the amount of water below the 100 units mark.

The output of the model is zero for x, y and the benefit.

I cannot see what I am doing wrong. Can someone help me?
Best regards.

答案1

得分: 1

from docplex.mp.model import Model
from docplex.mp.environment import Environment
env = Environment()
env.print_information()

mdl = Model()

x = mdl.continuous_var(lb=None, ub=None, name=None )
y = mdl.continuous_var(lb=None, ub=None, name=None )

r1=mdl.add_constraint( 150-x+y+125 >= 100  )    

mdl.maximize(  3*x-5*y )

s = mdl.solve()

obj = mdl.objective_value

print(x.solution_value)
print(y.solution_value)

print("* best objective is: {:g}".format(obj))

mdl.export("modelo_determinista_bajo.lp")
英文:
from docplex.mp.model import Model
from docplex.mp.environment import Environment
env = Environment()
env.print_information()

mdl = Model()

x = mdl.continuous_var(lb=None, ub=None, name=None )
y = mdl.continuous_var(lb=None, ub=None, name=None )



r1=mdl.add_constraint( 150-x+y+125 >= 100  )    

mdl.maximize(  3*x-5*y )


s = mdl.solve()

obj = mdl.objective_value

print(x.solution_value)
print(y.solution_value)

print("* best objective is: {:g}".format(obj))

mdl.export("modelo_determinista_bajo.lp")

gives

175.0
0
* best objective is: 525

答案2

得分: 0

  • Model.maximize()(或者minimize())简单地_设置_目标和方向,但不运行solve(),因此不会计算出(新的)解决方案。

  • 默认情况下,目标值为0,这并不像看起来那么愚蠢:CPLEX将尝试找到一个_可行的_解决方案。如果成功,目标值当然等于0。

  • 在调用mdl.solve()之前调用mdl.objective_value将导致运行时错误:没有解决方案存在,因此该属性为空,并且会引发异常;这很可能是你遇到的情况。实际上,你应该会得到一个DOcplexException

    docplex.mp.utils.DOcplexException: Model<docplex_model1>尚未解决

英文:

To complement Alex's code:

  • Model.maximize() (resp. minimize()) simply sets the objective and sense, but does not run solve(), therefore no (new) solution is computed.

  • By default, the objective is 0, which is not as silly as it may seem: CPLEX
    will try to find a feasible solution. If it succeeds, the objective value equals (of course) 0.

  • Calling mdl.objective_value before mdl.solve() will cause a runtime error: no solution is present, so the attribute is empty, and an exception is raised; this is likely what you have encountered.
    You should get actually a DOcplexException:

    docplex.mp.utils.DOcplexException: Model<docplex_model1> has not been solved yet

huangapple
  • 本文由 发表于 2023年3月1日 16:41:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75601314.html
匿名

发表评论

匿名网友

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

确定