ampl / mp

An open-source library for mathematical programming
https://mp.ampl.com
Other
229 stars 42 forks source link

Duals from CBC #214

Open glebbelov opened 1 year ago

glebbelov commented 1 year ago

Cbc installable via coin does not return duals. Check this model:

%%ampl_eval

set CHIPS;

param profits{CHIPS};
param copper{CHIPS};

var x{CHIPS} >= 0;

maximize Profit: sum {c in CHIPS} profits[c] * x[c];

s.t. Silicon: x['logic'] <= 1000;
s.t. Germanium: x['memory'] <= 1500;
s.t. Plastic: sum {c in CHIPS} x[c] <= 1750;
s.t. Copper: sum {c in CHIPS} copper[c] * x[c] <= 4800;
chips = ["logic", "memory"]
profits = {"logic": 12, "memory": 9}
copper = {"logic": 4, "memory": 2}

ampl.set['CHIPS'] = chips
ampl.param['profits'] = profits
ampl.param['copper'] = copper

ampl.option["solver"] = "cbc"
ampl.solve()

x = ampl.get_variable('x').get_values().to_list()

print(f'x = ({x})')
print(f'optimal value = {ampl.obj["Profit"].value():.2f}')
def ShowDuals(ampl):
    import fractions

    # display all duals
    print("The dual variable corresponding to:\n")
    for c in ampl.get_constraints():
        print("- the constraint on", c[0],
              "is equal to ",
              str(c[1].dual()))
              #str(fractions.Fraction(c[1].get_value())), end="")

ShowDuals(ampl)