Skip to content

Commit ff8a803

Browse files
committed
cutting stock
Former-commit-id: 63b89ec [formerly 6a03196] Former-commit-id: 827a175
1 parent a1b26d0 commit ff8a803

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

examples/cutting_stock.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
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

67
EPS = 10e-4
7-
SOLVER = GUROBI
8+
89

910
def 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+
2528
def 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

151155
if __name__ == "__main__":

mip/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from enum import Enum
44

5-
VERSION = '1.3.11'
5+
VERSION = '1.3.12'
66

77
# epsilon number (practical zero)
88
EPS = 10e-6

0 commit comments

Comments
 (0)