Cplex optimization program returns results equal to zero

Question:

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.

Asked By: slow_learner

||

Answers:

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
Answered By: Alex Fleischer

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

Answered By: Philippe Couronne