IBMDecisionOptimization / docplex-examples

These samples demonstrate how to use the DOcplex library to model and solve optimization problems.
https://ibmdecisionoptimization.github.io/
Apache License 2.0
396 stars 228 forks source link

How to get the solution of dual problem directly by Docplex #70

Open Dogggggggggg opened 2 years ago

Dogggggggggg commented 2 years ago

Dear CPLEX developers, I want to get the solutions of LP model and its dual problem simultaneously instead of transforming the primal problem into a dual problem.For example:

from docplex.mp.model import Model model = Model()

x1 = model.continuous_var(lb=0, name='x1') x2 = model.continuous_var(lb=0, name='x2')

model.add_constraint(2x1 + 3x2 <= 24) model.add_constraint(3x1 + 4x2 <= 30) model.add_constraint(3x1 + 2x2 <= 26)

f = 4x1 + 3x2

model.maximize(f)

How can I get the solution of its dual problem directly.

vberaudi commented 2 years ago

if I don't mistake, you only have to do:

    model.solve()

    c = model.get_cplex()
    values = c.solution.get_dual_values([c1.index, c2.index, c3.index])
    print(values)

which will internally call the getpi function based on your input. See https://www.ibm.com/docs/en/icos/12.8.0.0?topic=SSSA5P_12.8.0/ilog.odms.cplex.help/refpythoncplex/html/cplex._internal._subinterfaces.SolutionInterface-class.html#get_dual_values for example.