We use Jupyter Notebook and python package [mip](https://www.python-mip.com/ # (Mixed-Integer Linear Programming).
import mip
I = range(N)
J = range(N)
d = [[0 for j in J] for i in I]
model = mip.model()
# x_i binary {0,1} for i in I
x = [model.add_var(name=f"{i}", lb=0, var_type=BINARY) for i in I]
y = [model.add_var(name=f"{i}{j}") for i in I for j in J]
# str(i) is the same as f"{i}" - we just have to format the i as a string
model.objective = mip.minimize(...)
#or
model.objective = mip.maximise(...)
m.add_constr( xsum(x[i] for i in range(n)) >= y)
other example:
for i in I:
for j in J:
model.add_constr(mip.xsum(x[str(i), j]))
other examples:
m.add_constr( x1 + x2 <= 1 )
x = model.add_var(name='x', var_type=INTEGER, lb=0, ub=10)
# Optimizing command
model.optimize()
# Optimal objective function value
model.objective.x
# Printing the variables values
for i in model.vars:
print(i.name, i.x)
The dual variables determine how the objective function changes with respect to a small increase in the right-hand side value of an active constraint in the optimal solution. In economic models, dual variables can be used to calculate pricing information for products or resources. For example the optimal value of the dual variable can be used to determine the maximum price a company would pay for additional production days per month.
pi
gives the unitary change in objective function due to a change in the right-hand side in each constraint.
print(model.constrs[3].pi);
The value amounts to the shadow price for the demand constraint.
Which is the maximum amount of money that the company would invest in advertising, so as to increase the demand of A3 by 100 square meters?
print("Dual value:", model.constrs[2].pi)