1
$\begingroup$

For a CpModel using docplex, is there any function to evaluate a linear expression based on the variables values in the final solution?

For example, for expression = (x + 2y + z), with Gurobi this expression has type gurobipy.LinExpr and I can do expression.getValue(). For CP-SAT of OrTools I can do solver.Value(expression) where solver has type CpSolver.

$\endgroup$

2 Answers 2

0
$\begingroup$

After some more research, it seems that as of today there is no way to get the value of a general linear expression that was not previously defined in the model before solving it. I found the same question here https://community.ibm.com/community/user/ai-datascience/discussion/extract-the-solution-value-for-expression-in-cp and apparently the best solution is to add the expression as a pre-defined KPI to the model.

$\endgroup$
-1
$\begingroup$

Assuming your model object is mdl, when you create the constraints in docplex with add_constraint (or add_constraints), the generated constraint is returned and you can save that, similar to what you showed in your example.

For example:

ctr = mdl.add_constraint(x+y<=5, 'c1') If your constraint is not saved, you can access it after the solve by ctr = mdl.get_constraint_by_name('c1') as well.

Now, what's the value that you're after? If it's x+y, then you're after the lhs. So, to get that expression, you can say ctr.lhs (Similarly, you can access rhs using ctr.rhs). If you want to get the value, it's like getting any other value in docplex, by accessing solution_value. So, the value of x+y becomes ctr.lhs.solution_value. There might be other ways, but this is the one that I'm aware of.

$\endgroup$
1
  • $\begingroup$ Thank you! I was looking for something more generic though, not necessarily for an expression that is the lhs of a constraint. $\endgroup$
    – Nara
    Commented Sep 11, 2023 at 17:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.