34 public static void main(String[] args) {
40 File workingDirectory =
new File(System.getProperty(
"user.dir"),
"Cutstock");
41 workingDirectory.mkdir();
49 opt.setAllModelTypes(
"Cplex");
52 opt.defines(
"pmax", String.valueOf(maxpattern));
53 opt.defines(
"solveMasterAs",
"RMIP");
56 Map<String, Double> d =
new HashMap<String, Double>();
58 d.put(
"i1", Double.valueOf(97) );
59 d.put(
"i2", Double.valueOf(610) );
60 d.put(
"i3", Double.valueOf(395) );
61 d.put(
"i4", Double.valueOf(211) );
63 Map<String, Double> w =
new HashMap<String, Double>();
65 w.put(
"i1", Double.valueOf(45) );
66 w.put(
"i2", Double.valueOf(36) );
67 w.put(
"i3", Double.valueOf(31) );
68 w.put(
"i4", Double.valueOf(14) );
78 rawWidth.addRecord().setValue( 100 );
79 for (String i : d.keySet())
81 for (Entry<String, Double> e : d.entrySet())
82 demand.addRecord( e.getKey() ).setValue( e.getValue() );
83 for (Entry<String, Double> e : w.entrySet())
84 width.addRecord( e.getKey() ).setValue( e.getValue() );
89 masterInitJob.
run(opt, masterCP, cutstockData);
91 GAMSJob masterJob = ws.
addJobFromString(
"execute_load 'csdata', aip, pp; solve master min z using %solveMasterAs%;", masterCP);
93 GAMSSet pattern = cutstockData.
addSet(
"pp", 1,
"pattern index");
98 for (Entry<String, Double> e : w.entrySet()) {
99 String[] keys =
new String[] { e.getKey(), pattern.addRecord( Integer.toString(++patternCount) ).getKey(0) };
100 patternData.addRecord(keys).setValue( (
int)(r / e.getValue()) );
114 boolean patternAdded =
true;
117 masterJob.
run(opt, masterCP, cutstockData);
121 demandDual.addRecord( dem.getKey(0) ).setValue( dem.getMarginal() );
126 if (patternCount == maxpattern) {
127 System.out.println(
"Out of pattern. Increase maxpattern (currently ["+maxpattern+
"])");
128 patternAdded =
false;
131 System.out.println(
"New pattern Value: " + subMI.
SyncDB().
getVariable(
"z").findRecord().getLevel());
132 GAMSSetRecord s = pattern.addRecord( Integer.toString(++patternCount) );
134 if (y.getLevel() > 0.5) {
135 String[] keys =
new String[] { y.getKey(0), s.getKey(0) };
136 patternData.addRecord( keys ).setValue( Math.round(y.getLevel()) );
141 patternAdded =
false;
146 opt.defines(
"solveMasterAs",
"MIP");
147 masterJob.
run(opt, cutstockData);
149 System.out.println(
"Optimal Solution: "+masterJob.
OutDB().
getVariable(
"z").findRecord().getLevel());
152 if (xp.getLevel() > 0.5) {
153 System.out.println(
" pattern ["+xp.getKey(0)+
"] ["+xp.getLevel()+
"] times: ");
155 @SuppressWarnings(
"unused")
157 Iterator<GAMSParameterRecord> it = param.iterator();
158 while (it.hasNext()) {
160 System.out.println(
" ["+rec.getKey(0)+
"] : ["+ rec.
getValue()+
"]");
162 System.out.println();
173 static String masterModel =
174 "$Title Cutting Stock - Master problem \n" +
185 "$if not set pmax $set pmax 1000 \n" +
186 "Set p possible patterns /1*%pmax%/ \n" +
187 " pp(p) dynamic subset of p \n" +
189 " aip(i,p) number of width i in pattern growing in p;\n" +
191 "* Master model \n" +
192 "Variable xp(p) patterns used \n" +
193 " z objective variable \n" +
194 "Integer variable xp; xp.up(p) = sum(i, d(i)); \n" +
196 "Equation numpat number of patterns used \n" +
197 " demand(i) meet demand; \n" +
199 "numpat.. z =e= sum(pp, xp(pp)); \n" +
200 "demand(i).. sum(pp, aip(i,pp)*xp(pp)) =g= d(i); \n" +
202 "model master /numpat, demand/; \n" +
205 static String subModel =
206 "$Title Cutting Stock - Pricing problem is a knapsack model \n" +
218 " demdual(i) duals of master demand constraint /#i eps/; \n" +
220 "Variable z, y(i) new pattern; \n" +
221 "Integer variable y; y.up(i) = ceil(r/w(i)); \n" +
223 "Equation defobj \n" +
226 "defobj.. z =e= 1 - sum(i, demdual(i)*y(i)); \n" +
227 "knapsack.. sum(i, w(i)*y(i)) =l= r; \n" +
228 "model pricing /defobj, knapsack/; \n" +