Reputation: 1
I am using cplex cpo model for optimisation problem in python. I am beginner and I am learning. I am facing an issue which is given below.
I have the following binary decision variables:
model.binary_var(name='gamma1')
model.binary_var(name='gamma2')
model.binary_var(name='gamma3')
model.binary_var(name='gamma4')`
I have another variable defined as:
d =
5gamma1 + 13gamma2 + 17gamma3 + 45gamma4 / sum(gamma1 +gamma2+gamma3)`
objective function = minmization problem depending on decision and many other values
When I solve the optmization problem, I can get the values of gamma by following
model.add(model.minimize(objectiveFunction))
solution = model.solve( )
all_variables = model.get_all_variables()
# Iterate over all variables
print("All Variables:")
for var in all_variables:
print(var.get_name(), "-", var.get_type(), "Variable", solution.get_value(var), "solution"
But I am unable to get value of "d ". Is there a way I can get this value from model? (not to recalculate like getting optimized gamma value and calling a function to calculate d)
I've tried
solution.get_var_solution(d)
solution.get_value(d)
Upvotes: 0
Views: 119
Reputation: 10062
You can use kpi for that.
See example at https://github.com/AlexFleischerParis/zoodocplex/blob/master/zookpicpo.py
from docplex.cp.model import CpoModel
mdl = CpoModel(name='buses')
nbbus40 = mdl.integer_var(0,1000,name='nbBus40')
nbbus30 = mdl.integer_var(0,1000,name='nbBus30')
nbbus=nbbus40+nbbus30
mdl.add_kpi(nbbus,"nbbus")
mdl.add(nbbus40*40 + nbbus30*30 >= 300)
mdl.minimize(nbbus40*500 + nbbus30*400)
msol=mdl.solve()
print(msol[nbbus40]," buses 40 seats")
print(msol[nbbus30]," buses 30 seats")
print("nbbus = ",msol["nbbus"])
Upvotes: 0