Loading...
Searching...
No Matches
Transport8.java
1package com.gams.examples.transport;
2
3import java.io.File;
4import java.util.Arrays;
5import java.util.LinkedList;
6import java.util.Queue;
7
9import com.gams.api.GAMSJob;
12import com.gams.api.GAMSOptions;
16
22public class Transport8 {
23
24 public static void main(String[] args) {
25 // check workspace info from command line arguments
27 if (args.length > 0)
28 wsInfo.setSystemDirectory( args[0] );
29 // create a directory
30 File workingDirectory = new File(System.getProperty("user.dir"), "Transport8");
31 workingDirectory.mkdir();
32 wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
33 // create a workspace
34 GAMSWorkspace ws = new GAMSWorkspace(wsInfo);
35 // create a checkpoint
37
38 // initialize a checkpoint by running a job
39 GAMSJob t8 = ws.addJobFromString(model);
40 t8.run(cp);
41
42 Queue<Double> bmultQueue = new LinkedList<Double>(
43 Arrays.asList( Double.valueOf(0.6), Double.valueOf(0.7), Double.valueOf(0.8), Double.valueOf(0.9),
44 Double.valueOf(1.0), Double.valueOf(1.1), Double.valueOf(1.2), Double.valueOf(1.3) )
45 );
46
47 // solve multiple model instances in parallel
48 Object IOLockObject = new Object();
49 int numberOfWorkers = 2;
50 Scenarios[] scenarios = new Scenarios[numberOfWorkers];
51 for (int i=0; i<numberOfWorkers; i++) {
52 scenarios[i] = new Scenarios( ws, cp, bmultQueue, IOLockObject, i );
53 scenarios[i].start();
54 }
55 for (int i=0; i<numberOfWorkers; i++) {
56 try {
57 scenarios[i].join();
58 } catch (InterruptedException e) {
59 e.printStackTrace();
60 }
61 }
62 }
63
65 static class Scenarios extends Thread {
66 GAMSWorkspace workspace;
67 GAMSCheckpoint checkpoint;
68 Object IOLockObject;
69 Queue<Double> bmultQueue;
70 int workerNumber;
71
79 public Scenarios(GAMSWorkspace ws, GAMSCheckpoint cp, Queue<Double> que, Object IOLockObj, int i) {
80 workspace = ws;
81 checkpoint = cp;
82 IOLockObject = IOLockObj;
83 bmultQueue = que;
84 workerNumber = i;
85 }
86
88 public void run() {
89 GAMSModelInstance mi = null;
90 synchronized (bmultQueue) {
91 mi = checkpoint.addModelInstance();
92 }
93
94 GAMSParameter bmult = mi.SyncDB().addParameter("bmult", "demand multiplier");
95
96 GAMSOptions opt = workspace.addOptions();
97 opt.setAllModelTypes("cplex");
98
99 // instantiate the GAMSModelInstance and pass a model definition and GAMSModifier to declare bmult mutable
100 mi.instantiate("transport use lp min z", opt, new GAMSModifier(bmult));
101
102 bmult.addRecord().setValue( 1.0 );
103
104 while (true) {
105 double b = 0.0;
106 // dynamically get a bmult value from the queue instead of passing it to the different threads at creation time
107 synchronized (bmultQueue) {
108 if (bmultQueue.isEmpty())
109 break;
110 else
111 b = bmultQueue.remove();
112 }
113 bmult.getFirstRecord().setValue(b);
114 mi.solve();
115 // we need to make the output a critical section to avoid messed up report informations
116 synchronized (IOLockObject) {
117 System.out.println("#"+workerNumber+":Scenario bmult=" + b + ":");
118 System.out.println(" Modelstatus: " + mi.getModelStatus());
119 System.out.println(" Solvestatus: " + mi.getSolveStatus());
120 System.out.println(" Obj: " + mi.SyncDB().getVariable("z").findRecord().getLevel());
121 }
122 }
123
124 // dispose option and model instance at the end of the run
125 opt.dispose();
126 mi.dispose();
127 }
128 }
129
130 static String model =
131 "Sets \n" +
132 " i canning plants / seattle, san-diego / \n" +
133 " j markets / new-york, chicago, topeka / ; \n" +
134 " \n" +
135 "Parameters \n" +
136 " a(i) capacity of plant i in cases \n" +
137 " / seattle 350 \n" +
138 " san-diego 600 / \n" +
139 " \n" +
140 " b(j) demand at market j in cases \n" +
141 " / new-york 325 \n" +
142 " chicago 300 \n" +
143 " topeka 275 / ; \n" +
144 " \n" +
145 "Table d(i,j) distance in thousands of miles \n" +
146 " new-york chicago topeka \n" +
147 "seattle 2.5 1.7 1.8 \n" +
148 "san-diego 2.5 1.8 1.4 ; \n" +
149 " \n" +
150 "Scalar f freight in dollars per case per thousand miles /90/ ; \n" +
151 "Scalar bmult demand multiplier /1/; \n" +
152 " \n" +
153 "Parameter c(i,j) transport cost in thousands of dollars per case ; \n" +
154 " c(i,j) = f * d(i,j) / 1000 ; \n" +
155 " \n" +
156 "Variables \n" +
157 " x(i,j) shipment quantities in cases \n" +
158 " z total transportation costs in thousands of dollars ; \n" +
159 " \n" +
160 "Positive Variable x ; \n" +
161 " \n" +
162 "Equations \n" +
163 " cost define objective function \n" +
164 " supply(i) observe supply limit at plant i \n" +
165 " demand(j) satisfy demand at market j ; \n" +
166 " \n" +
167 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n" +
168 " \n" +
169 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n" +
170 " \n" +
171 " demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ; \n" +
172 " \n" +
173 "Model transport /all/ ; \n" +
174 " \n";
175
176}
GAMSModelInstance addModelInstance()
GAMSParameter addParameter(String identifier, int dimension)
GAMSVariable getVariable(String identifier)
GAMSGlobals.ModelStat getModelStatus()
GAMSGlobals.SolveStat getSolveStatus()
void instantiate(String modelDefinition, GAMSModifier ... modifiers)
void setAllModelTypes(String value)
T findRecord(String ... keys)
T addRecord(Vector< String > keys)
void setSystemDirectory(String directory)
void setWorkingDirectory(String directory)
GAMSJob addJobFromString(String source)
GAMSCheckpoint addCheckpoint()
This example shows how to use a queue to solve multiple GAMSModelInstances in parallel.
Definition: Transport8.java:22
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).
&#160;