11#!/usr/bin/python
22# -*- coding: utf-8 -*-
33
4- from mip .model import *
4+ from mip .model import Model , xsum , Column
5+ from mip .constants import INTEGER
56
67EPS = 10e-4
7- SOLVER = GUROBI
8+
89
910def get_pricing (m , w , L ):
1011 # creating the pricing problem
11- pricing = Model (SOLVER )
12+ pricing = Model ()
1213
1314 # creating pricing variables
1415 a = []
1516 for i in range (m ):
16- a .append (pricing .add_var (obj = 0 , var_type = INTEGER , name = 'a_%d' % (i + 1 )))
17+ a .append (pricing .add_var (obj = 0 , var_type = INTEGER ,
18+ name = 'a_%d' % (i + 1 )))
1719
1820 # creating pricing constraint
1921 pricing .add_constr (xsum (w [i ] * a [i ] for i in range (m )) <= L , 'bar_length' )
@@ -22,6 +24,7 @@ def get_pricing(m, w, L):
2224
2325 return a , pricing
2426
27+
2528def cg ():
2629 """
2730 Simple column generation implementation for a Cutting Stock Problem
@@ -33,18 +36,20 @@ def cg():
3336 b = [1 , 2 , 2 , 1 ] # demand for each item
3437
3538 # creating models and auxiliary lists
36- master = Model (SOLVER )
39+ master = Model ()
3740 lambdas = []
3841 constraints = []
3942
4043 # creating an initial pattern (which cut one item per bar)
4144 # to provide the restricted master problem with a feasible solution
4245 for i in range (m ):
43- lambdas .append (master .add_var (obj = 1 , name = 'lambda_%d' % (len (lambdas ) + 1 )))
46+ lambdas .append (master .add_var (obj = 1 , name = 'lambda_%d' %
47+ (len (lambdas ) + 1 )))
4448
4549 # creating constraints
4650 for i in range (m ):
47- constraints .append (master .add_constr (lambdas [i ] >= b [i ], name = 'i_%d' % (i + 1 )))
51+ constraints .append (master .add_constr (lambdas [i ] >= b [i ],
52+ name = 'i_%d' % (i + 1 )))
4853
4954 new_vars = True
5055 while new_vars :
@@ -54,7 +59,6 @@ def cg():
5459 ##########
5560
5661 master .optimize ()
57- master .write ('master.lp' )
5862
5963 # printing dual values
6064 print_solution (master )
@@ -125,8 +129,8 @@ def kantorovich():
125129 # constraints
126130 for i in range (m ):
127131 model .add_constr (xsum (x [i , j ] for j in range (N )) >= b [i ])
128- for j in range (N ):
129- model .add_constr (xsum (w [i ] * x [i , j ] for i in range (m )) <= L * y [j ])
132+ for j in range (N ):
133+ model .add_constr (xsum (w [i ] * x [i , j ] for i in range (m )) <= L * y [j ])
130134
131135 # additional constraint to reduce symmetry
132136 for j in range (1 , N ):
@@ -145,7 +149,7 @@ def print_solution(model: Model):
145149 for v in model .vars :
146150 if v .x > EPS :
147151 print (' {v.name} = {v.x:.3}' .format (** locals ()))
148- print ('' )
152+ print ('' )
149153
150154
151155if __name__ == "__main__" :
0 commit comments