def get_pricing(m, w, L): # creating the pricing problem pricing = Model() # creating pricing variables a = [] for i in range(m): a.append( pricing.add_var(obj=0, var_type=INTEGER, name='a_%d' % (i + 1))) # creating pricing constraint pricing.add_constr(xsum(w[i] * a[i] for i in range(m)) <= L, 'bar_length') pricing.write('pricing.lp') return a, pricing
def cg(): """ Simple column generation implementation for a Cutting Stock Problem """ L = 250 # bar length m = 4 # number of requests w = [187, 119, 74, 90] # size of each item b = [1, 2, 2, 1] # demand for each item # creating models and auxiliary lists master = Model() lambdas = [] constraints = [] # creating an initial pattern (which cut one item per bar) # to provide the restricted master problem with a feasible solution for i in range(m): lambdas.append(master.add_var(obj=1, name='lambda_%d' % (len(lambdas) + 1))) # creating constraints for i in range(m): constraints.append(master.add_constr(lambdas[i] >= b[i], name='i_%d' % (i + 1))) # creating the pricing problem pricing = Model(SOLVER) # creating pricing variables a = [] for i in range(m): a.append(pricing.add_var(obj=0, var_type=INTEGER, name='a_%d' % (i + 1))) # creating pricing constraint pricing.add_constr(xsum(w[i] * a[i] for i in range(m)) <= L, 'bar_length') pricing.write('pricing.lp') new_vars = True while new_vars: ########## # STEP 1: solving restricted master problem ########## master.optimize() # printing dual values print_solution(master) print('pi = ', end='') print([constraints[i].pi for i in range(m)]) print('') ########## # STEP 2: updating pricing objective with dual values from master ########## pricing.objective = 1 for i in range(m): a[i].obj = -constraints[i].pi # solving pricing problem pricing.optimize() # printing pricing solution z_val = pricing.objective_value print('Pricing:') print(' z = {z_val}'.format(**locals())) print(' a = ', end='') print([v.x for v in pricing.vars]) print('') ########## # STEP 3: adding the new columns ########## # checking if columns with negative reduced cost were produced and # adding them into the restricted master problem if 1 + pricing.objective_value < - EPS: coeffs = [a[i].x for i in range(m)] column = Column(constraints, coeffs) lambdas.append(master.add_var(obj=1, column=column, name='lambda_%d' % (len(lambdas) + 1))) print('new pattern = {coeffs}'.format(**locals())) # if no column with negative reduced cost was produced, then linear # relaxation of the restricted master problem is solved else: new_vars = False pricing.write('pricing.lp') print_solution(master)