Skip to content

Commit a1b26d0

Browse files
authored
Merge pull request #3 from spoorendonk/cutting_stock_cbc
Cutting stock cbc Former-commit-id: ea62b49 [formerly 9433678] Former-commit-id: 0a88318
2 parents d4b2efa + 62a89ae commit a1b26d0

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

examples/cutting_stock.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
#!/usr/bin/python
22
# -*- coding: utf-8 -*-
33

4-
import pdb
54
from mip.model import *
65

76
EPS = 10e-4
87
SOLVER = GUROBI
98

9+
def get_pricing(m, w, L):
10+
# creating the pricing problem
11+
pricing = Model(SOLVER)
12+
13+
# creating pricing variables
14+
a = []
15+
for i in range(m):
16+
a.append(pricing.add_var(obj=0, var_type=INTEGER, name='a_%d' % (i + 1)))
17+
18+
# creating pricing constraint
19+
pricing.add_constr(xsum(w[i] * a[i] for i in range(m)) <= L, 'bar_length')
20+
21+
pricing.write('pricing.lp')
22+
23+
return a, pricing
1024

1125
def cg():
1226
"""
@@ -32,19 +46,6 @@ def cg():
3246
for i in range(m):
3347
constraints.append(master.add_constr(lambdas[i] >= b[i], name='i_%d' % (i + 1)))
3448

35-
# creating the pricing problem
36-
pricing = Model(SOLVER)
37-
38-
# creating pricing variables
39-
a = []
40-
for i in range(m):
41-
a.append(pricing.add_var(obj=0, var_type=INTEGER, name='a_%d' % (i + 1)))
42-
43-
# creating pricing constraint
44-
pricing.add_constr(xsum(w[i] * a[i] for i in range(m)) <= L, 'bar_length')
45-
46-
pricing.write('pricing.lp')
47-
4849
new_vars = True
4950
while new_vars:
5051

@@ -65,6 +66,8 @@ def cg():
6566
# STEP 2: updating pricing objective with dual values from master
6667
##########
6768

69+
a, pricing = get_pricing(m, w, L)
70+
6871
pricing.objective = 1
6972
for i in range(m):
7073
a[i].obj = -constraints[i].pi
@@ -86,7 +89,7 @@ def cg():
8689

8790
# checking if columns with negative reduced cost were produced and
8891
# adding them into the restricted master problem
89-
if pricing.objective_value < - EPS:
92+
if 1 + pricing.objective_value < - EPS:
9093
coeffs = [a[i].x for i in range(m)]
9194
column = Column(constraints, coeffs)
9295
lambdas.append(master.add_var(obj=1, column=column, name='lambda_%d' % (len(lambdas) + 1)))
@@ -99,7 +102,6 @@ def cg():
99102
new_vars = False
100103

101104
pricing.write('pricing.lp')
102-
pdb.set_trace()
103105

104106
print_solution(master)
105107

mip/cbc.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@
332332
cbc_progress_callback prgcbc, void *appData);
333333
334334
void Cbc_clearCallBack(Cbc_Model *model);
335+
336+
const double *Cbc_getRowPrice(Cbc_Model *model);
337+
338+
const double *Osi_getRowPrice(void *osi);
335339
""")
336340

337341
CHAR_ONE = "{}".format(chr(1)).encode("utf-8")
@@ -680,12 +684,18 @@ def var_get_lb(self, var: "Var") -> float:
680684
raise Exception('Error while getting lower bound of variables')
681685
return float(lb[var.idx])
682686

687+
def var_set_lb(self, var: "Var", value: float):
688+
cbclib.Cbc_setColLower(self._model, var.idx, value)
689+
683690
def var_get_ub(self, var: "Var") -> float:
684691
ub = cbclib.Cbc_getColUpper(self._model)
685692
if ub == ffi.NULL:
686693
raise Exception('Error while getting upper bound of variables')
687694
return float(ub[var.idx])
688695

696+
def var_set_ub(self, var: "Var", value: float):
697+
cbclib.Cbc_setColUpper(self._model, var.idx, value)
698+
689699
def var_get_name(self, idx: int) -> str:
690700
namep = self.__name_space
691701
cbclib.Cbc_getColName(self._model, idx, namep, MAX_NAME_SIZE)
@@ -906,6 +916,12 @@ def get_pump_passes(self) -> int:
906916
def set_pump_passes(self, passes: int):
907917
self.__pumpp = passes
908918

919+
def constr_get_pi(self, constr: Constr) -> float:
920+
rp = cbclib.Cbc_getRowPrice(self._model)
921+
if rp == ffi.NULL:
922+
raise Exception('row prices not available')
923+
return float(rp[constr.idx])
924+
909925

910926
class ModelOsi(Model):
911927
def __init__(self, osi_ptr: CData):

0 commit comments

Comments
 (0)