Python – How to display the duality of a (raw) linear program defined in PuLP

How to display the duality of a (raw) linear program defined in PuLP… here is a solution to the problem.

How to display the duality of a (raw) linear program defined in PuLP

The first question about Stack overflow… Love this site….

I use PuLP on Python. Based on the input variables, objective function, and constraints, I try to see the dual variables/values associated with the optimal solution of the initial LP problem (the original problem). I’m having trouble searching in the docs, I’m just trying to figure it out. Is there a way to do this with PuLP? The following sample code shows the raw input.

Note: I edited my question from asking for double results to asking for double results (based on feedback).

Note 2: Further edits based on feedback for clarification.

import pulp
lpProb = pulp. LpProblem("example", pulp. LpMinimize)
x1 = pulp. LpVariable("x1", 0, None, pulp. LpContinuous)
x2 = pulp. LpVariable("x2", 0, None, pulp. LpContinuous)
x3 = pulp. LpVariable("x3", 0, None, pulp. LpContinuous)
lpProb += 3*x1 + 4*x2 + 2*x3
lpProb += x1 + 2*x2 + x3 >= 42
lpProb += 5*x1 + 7*x2 + 4*x3 >= 68
lpProb.solve()
print(pulp. LpStatus[lpProb.status])
for i in lpProb.variables():
    print("Variable {0} = {1}".format(i.name, i.varValue))
print("Objective function z = {0}".format(pulp.value(lpProb.objective)))

Solution

@StuartMitchell The following suggestions were made in the comments on this issue. It prints the name, dual value, and relaxation of each constraint.

for name, c in list(lpProb.constraints.items()):
    print(name, ":", c, "\t", c.pi, "\t\t", c.slack)

Related Problems and Solutions