-
Notifications
You must be signed in to change notification settings - Fork 0
/
cutstock_pyomo.py
65 lines (55 loc) · 1.92 KB
/
cutstock_pyomo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from pyomo.core import *
import pyomo.opt
from cutstock_util import*
import pyomo.environ as pyo
# Reading in Data using the cutstock_util
cutcount = getCutCount()
patcount = getPatCount()
Cuts = getCuts()
Patterns = getPatterns()
PriceSheet = getPriceSheetData()
SheetsAvail = getSheetsAvail()
CutDemand = getCutDemand()
CutsInPattern = getCutsInPattern()
########################################
#CutsInPattern = makeDict([Cuts,Patterns],CutsInPattern)
tmp = {}
for i in range(len(Cuts)):
tmp[Cuts[i]] = {}
for j in range(len(CutsInPattern[i])):
tmp[Cuts[i]][Patterns[j]] = CutsInPattern[i][j]
CutsInPattern = tmp
########################################
#CutDemand = makeDict([Cuts],CutDemand)
tmp = {}
for i in range(len(Cuts)):
tmp[Cuts[i]] = CutDemand[i]
CutDemand = tmp
model = ConcreteModel(name="CutStock Problem")
#Defining Variables
model.SheetsCut = Var()
model.TotalCost = Var()
model.PatternCount = Var(Patterns, bounds=(0,None))
model.ExcessCuts = Var(Cuts, bounds=(0,None))
#objective
model.objective = Objective(expr=1.0*model.TotalCost)
#Constraints
model.TotCost = Constraint(expr = model.TotalCost == PriceSheet* model.SheetsCut)
model.RawAvail = Constraint(expr = model.SheetsCut <= SheetsAvail)
model.Sheets = Constraint(expr = summation(model.PatternCount) == model.SheetsCut)
model.CutReq = Constraint(Cuts)
for c in Cuts:
model.CutReq.add(c, expr=sum(CutsInPattern[c][p]*model.PatternCount[p] for p in Patterns) == CutDemand[c] + model.ExcessCuts[c])
# instance = model.create_instance()
# opt = pyomo.opt.SolverFactory('glpk')
opt = pyo.SolverFactory('glpk')
# results = opt.solve(instance)
results = opt.solve(model)
# instance.load(results)
print( "Status:", results.solver.status)
print("Minimum total cost:", value(model.objective))
for v in model.component_objects(Var,active=True):
for index in v:
if (value(v[index]) > 0):
print(v.name,':',value(v[index]))
# model.display()