Skip to content

Latest commit

 

History

History
104 lines (65 loc) · 2.23 KB

File metadata and controls

104 lines (65 loc) · 2.23 KB

Python Labs

We use Jupyter Notebook and python package [mip](https://www.python-mip.com/ # (Mixed-Integer Linear Programming).

MIP library

import mip

Declare I and J as a range

I = range(N)
J = range(N)

Declare d as a NxN matrix

d = [[0 for j in J] for i in I]

Declare the model

model = mip.model()

Declare variables

# 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

Define the objective function

model.objective = mip.minimize(...) 
#or 
model.objective = mip.maximise(...) 

Define constraints

$\sum_{i=0}^{n-1} x_i \ge y$ is 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)

Optimization

# Optimizing command
model.optimize()

# Optimal objective function value
model.objective.x

# Printing the variables values
for i in model.vars:
	print(i.namei.x)

Sensitivity analysis

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.

Property pi

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)