CutstockModel.java
1package com.gams.examples.cutstock;
2 
3 import java.io.PrintStream;
4 
5 import com.gams.api.GAMSDatabase;
7 import com.gams.api.GAMSJob;
8 import com.gams.api.GAMSOptions;
10 import com.gams.api.GAMSSet;
11 import com.gams.api.GAMSWorkspace;
12 
17 public class CutstockModel {
18  private GAMSSet fWidths;
19  private GAMSParameter fRawWidth;
20  private GAMSParameter fDemand;
21  private GAMSParameter fWidth;
22  private GAMSParameter fPatRep;
23 
24  private GAMSWorkspace fws;
25  private GAMSDatabase fCutstockData, fDbOut;
26  private GAMSOptions fopt;
27  private GAMSJob job;
28 
33  fws = ws;
34  fopt = ws.addOptions();
35 
36  fCutstockData = ws.addDatabase("gdxincname");
37 
38  fopt.defines("gdxincname", "gdxincname");
39  fopt.setSolveLink( GAMSOptions.ESolveLink.LoadLibrary );
40  fopt.defines("dbOut1", "dbOut1");
41 
42  fWidths = fCutstockData.addSet("i", "widths");
43  fRawWidth = fCutstockData.addParameter("r", "raw width");
44  fDemand = fCutstockData.addParameter("d", "demand", fWidths);
45  fWidth = fCutstockData.addParameter("w", "width", fWidths);
46 
47  job = ws.addJobFromString( this.getModelSource() );
48  }
49 
51  public void run() {
52  this.run(null);
53  }
54 
58  public void run(PrintStream output) {
59  if (!fCutstockData.checkDomains())
60  throw new GAMSException("Domain Errors in Cutstock Database");
61 
62  job.run(fopt, null, output, false, fCutstockData);
63  fDbOut = fws.addDatabaseFromGDX(fopt.getDefinitionOf("dbOut1") + ".gdx");
64  fPatRep = fDbOut.getParameter("patrep");
65  }
66 
68  public GAMSSet getWidths() { return fWidths; }
69 
71  public GAMSParameter getRawWidth() { return fRawWidth; }
72 
74  public GAMSParameter getDemand() { return fDemand; }
75 
77  public GAMSParameter getWidth() { return fWidth; }
78 
80  public GAMSParameter getPatRep() { return fPatRep; }
81 
83  public GAMSOptions getOpt() { return fopt; }
84 
86  public String getModelSource() { return model; }
87 
88  static String model =
89  "$Title Cutting Stock - A Column Generation Approach (CUTSTOCK,SEQ=294) \n" +
90  " \n" +
91  "$ontext \n" +
92  "The task is to cut out some paper products of different sizes from a \n" +
93  "large raw paper roll, in order to meet a customer's order. The objective \n" +
94  "is to minimize the required number of paper rolls. \n" +
95  " \n" +
96  "P. C. Gilmore and R. E. Gomory, A linear programming approach to the \n" +
97  "cutting stock problem, Part I, Operations Research 9 (1961), 849-859. \n" +
98  " \n" +
99  "P. C. Gilmore and R. E. Gomory, A linear programming approach to the \n" +
100  "cutting stock problem, Part II, Operations Research 11 (1963), 863-888. \n" +
101  "$offtext \n" +
102  " \n" +
103  "Set i widths \n" +
104  "Parameter \n" +
105  " r raw width \n" +
106  " w(i) width \n" +
107  " d(i) demand ; \n" +
108  " \n" +
109  "$if not set gdxincname $abort 'no include file name for data file provided'\n" +
110  "$gdxin %gdxincname% \n" +
111  "$load r i w d \n" +
112  "$gdxin \n" +
113  " \n" +
114  "* Gilmore-Gomory column generation algorithm \n" +
115  " \n" +
116  "Set p possible patterns /p1*p1000/ \n" +
117  " pp(p) dynamic subset of p \n" +
118  "Parameter \n" +
119  " aip(i,p) number of width i in pattern growing in p; \n" +
120  " \n" +
121  "* Master model \n" +
122  "Variable xp(p) patterns used \n" +
123  " z objective variable \n" +
124  "Integer variable xp; xp.up(p) = sum(i, d(i)); \n" +
125  " \n" +
126  "Equation numpat number of patterns used \n" +
127  " demand(i) meet demand; \n" +
128  " \n" +
129  "numpat.. z =e= sum(pp, xp(pp)); \n" +
130  "demand(i).. sum(pp, aip(i,pp)*xp(pp)) =g= d(i); \n" +
131  " \n" +
132  "model master /numpat, demand/; \n" +
133  " \n" +
134  "* Pricing problem - Knapsack model \n" +
135  "Variable y(i) new pattern; \n" +
136  "Integer variable y; y.up(i) = ceil(r/w(i)); \n" +
137  " \n" +
138  "Equation defobj \n" +
139  " knapsack knapsack constraint; \n" +
140  " \n" +
141  "defobj.. z =e= 1 - sum(i, demand.m(i)*y(i)); \n" +
142  "knapsack.. sum(i, w(i)*y(i)) =l= r; \n" +
143  " \n" +
144  "model pricing /defobj, knapsack/; \n" +
145  " \n" +
146  "* Initialization - the initial patterns have a single width \n" +
147  "pp(p) = ord(p)<=card(i); \n" +
148  "aip(i,pp(p))$(ord(i)=ord(p)) = floor(r/w(i)); \n" +
149  "*display aip; \n" +
150  " \n" +
151  "Scalar done loop indicator /0/ \n" +
152  "Set pi(p) set of the last pattern; pi(p) = ord(p)=card(pp)+1; \n" +
153  " \n" +
154  "option optcr=0,limrow=0,limcol=0,solprint=off; \n" +
155  " \n" +
156  "While(not done and card(pp)<card(p), \n" +
157  " solve master using rmip minimizing z; \n" +
158  " solve pricing using mip minimizing z; \n" +
159  " \n" +
160  "* pattern that might improve the master model found? \n" +
161  " if(z.l < -0.001, \n" +
162  " aip(i,pi) = round(y.l(i)); \n" +
163  " pp(pi) = yes; pi(p) = pi(p-1); \n" +
164  " else \n" +
165  " done = 1; \n" +
166  " ); \n" +
167  "); \n" +
168  "display 'lower bound for number of rolls', master.objval; \n" +
169  " \n" +
170  "option solprint=on; \n" +
171  "solve master using mip minimizing z; \n" +
172  " \n" +
173  "Parameter patrep Solution pattern report \n" +
174  " demrep Solution demand supply report; \n" +
175  " \n" +
176  "patrep('# produced',p) = round(xp.l(p)); \n" +
177  "patrep(i,p)$patrep('# produced',p) = aip(i,p); \n" +
178  "patrep(i,'total') = sum(p, patrep(i,p)); \n" +
179  "patrep('# produced','total') = sum(p, patrep('# produced',p)); \n" +
180  " \n" +
181  "demrep(i,'produced') = sum(p,patrep(i,p)*patrep('# produced',p)); \n" +
182  "demrep(i,'demand') = d(i); \n" +
183  "demrep(i,'over') = demrep(i,'produced') - demrep(i,'demand'); \n" +
184  " \n" +
185  "display patrep, demrep; \n" +
186  "$if not set dbOut1 $abort 'no file name for out-database 1 file provided' \n" +
187  "execute_unload '%dbOut1%', patrep; \n" +
188  " ";
189 }
String getDefinitionOf(String str)
String getModelSource()
get the source of cutstock model
GAMSParameter getPatRep()
get an output symbol, patrep : Solution pattern report
GAMSParameter getParameter(String identifier)
void run()
Executes the cutstock model.
CutstockModel(GAMSWorkspace ws)
CutstockModel constructor.
GAMSParameter getRawWidth()
get an input symbol, r : raw width
GAMSParameter getWidth()
get an input symbol, w : width
void run(PrintStream output)
Executes the cutstock model.
void defines(String defStr, String asStr)
GAMSDatabase addDatabaseFromGDX(String gdxFileName)
GAMSSet getWidths()
get an input symbol, i : widths
void setSolveLink(GAMSOptions.ESolveLink x)
GAMSParameter addParameter(String identifier, int dimension)
GAMSSet addSet(String identifier, int dimension)
GAMSJob addJobFromString(String source)
GAMSParameter getDemand()
get an input symbol, d : demand
This example shows the wrapper model of a cutstock problem based on the simple GAMS [cutstock] model ...
GAMSOptions getOpt()
get Options for the execution of the cutstock model