@alex Thank you for the response. So you are suggesting that in CP, for calculating an expression, we must define it as a KPI "before" solving the model.
However in MP, we can find the expression value after the solution.
For example in MP, we can can calculate the expression nbbus40-nbbus30
after solving the model, while it was "Not" defined as a KPI.
from docplex.mp.model import Model
mdl = Model(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(msol[nbbus]," nbbus"
)
print((nbbus40-nbbus30).solution_value," nbbus subtraction")
6.0 buses 40 seats
2.0 buses 30 seats
8.0 nbbus
4.0 nbbus subtraction
Is it possible to do something similar in CP? Calculating an expression without defining it as KPI?
------------------------------
Mohammad Gorji-Sefidmazgi
------------------------------
Original Message:
Sent: Mon July 10, 2023 02:07 AM
From: ALEX FLEISCHER
Subject: Extract the solution value for expression in CP
Hi,
you can use the msol object.
small example at https://github.com/AlexFleischerParis/zoodocplex/blob/master/zookpicpo.py
from docplex.cp.model import CpoModelmdl = CpoModel(name='buses')nbbus40 = mdl.integer_var(0,1000,name='nbBus40')nbbus30 = mdl.integer_var(0,1000,name='nbBus30')nbbus=nbbus40+nbbus30mdl.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"])"""6 buses 40 seats2 buses 30 seatsnbbus = 8"""
------------------------------
[Alex] [Fleischer]
[Data and AI Technical Sales]
[IBM]
Original Message:
Sent: Fri July 07, 2023 11:23 AM
From: Mohammad Gorji-Sefidmazgi
Subject: Extract the solution value for expression in CP
Given a CP optimization like this which can be solved:
mdl = CpoModel()
x = mdl.integer_var( min = 0, max = 2, name = 'x')
y= mdl.integer_var( min = 0, max = 3, name = 'y')
obj =mdl.maximize(x+y)
mdl.add(obj)
msol = mdl.solve()
msol.print_solution()
Variables:
x = 2
y = 3
and when we define a KPI in the form of an expression (type docplex.cp.expression.CpoFunctionCall
)
kpi = x**2+y**
2
How can we extract the solution value for this expression? For example in this case it should be 2^2+3^2=13
------------------------------
Mohammad Gorji-Sefidmazgi
------------------------------