Transport14.java
1package com.gams.examples.transport;
2
3import java.io.File;
4
7import com.gams.api.GAMSJob;
12
18public class Transport14 {
19 public static void main(String[] args) {
20 initializeWorkspace(args);
21 double[] bmultlist = new double[] { 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 };
22 Optimizer[] optim = new Optimizer[bmultlist.length];
23 for (int i=0; i<bmultlist.length; i++) {
24 optim[i] = new Optimizer(bmultlist[i]);
25 optim[i].start();
26 }
27 }
28
29 static void initializeWorkspace(String[] args) {
31 File workingDirectory = new File(System.getProperty("user.dir"), "Transport14");
32 workingDirectory.mkdir();
33 wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
34 if (args.length > 0)
35 wsInfo.setSystemDirectory(args[0]);
36 Optimizer.setWorkspace( new GAMSWorkspace(wsInfo) );
37 }
38}
39
43class Optimizer extends Thread {
44 static GAMSWorkspace workspace = null;
45 double bmult = 0.0;
46
47 static void setWorkspace(GAMSWorkspace ws) {
48 workspace = ws;
49 }
50
51 public Optimizer(double mult) {
52 if (workspace == null)
53 throw new GAMSException("no workspace information, initialize workspace before creating an Optimizer");
54 bmult = mult;
55 }
56
57 public void run() {
58 GAMSDatabase gDb = workspace.addDatabase();
59
60 GAMSParameter f = gDb.addParameter("f", "freight in dollars per case per thousand miles");
61 f.addRecord().setValue( 90 * bmult );
62
63 GAMSJob gModJob = workspace.addJobFromString( model );
64
65 GAMSOptions gOption = workspace.addOptions();
66 gOption.defines("gdxincname", gDb.getName());
67 gModJob.run(gOption, gDb);
68
69 double obj = gModJob.OutDB().getVariable("z").getFirstRecord().getLevel();
70 System.out.println("Scenario bmult=" + bmult + ", Obj=" + obj);
71 }
72
73 static String model =
74 "Sets \n"+
75 " i canning plants / seattle, san-diego / \n"+
76 " j markets / new-york, chicago, topeka / ; \n"+
77 " \n"+
78 "Parameters \n"+
79 " \n"+
80 " a(i) capacity of plant i in cases \n"+
81 " / seattle 350 \n"+
82 " san-diego 600 / \n"+
83 " \n"+
84 " b(j) demand at market j in cases \n"+
85 " / new-york 325 \n"+
86 " chicago 300 \n"+
87 " topeka 275 / ; \n"+
88 " \n"+
89 "Table d(i,j) distance in thousands of miles \n"+
90 " new-york chicago topeka \n"+
91 " seattle 2.5 1.7 1.8 \n"+
92 " san-diego 2.5 1.8 1.4 ; \n"+
93 " \n"+
94 "Scalar f freight in dollars per case per thousand miles; \n"+
95 " \n"+
96 "$if not set gdxincname $abort 'no include file name for data file provided' \n"+
97 "$gdxin %gdxincname% \n"+
98 "$load f \n"+
99 "$gdxin \n"+
100 " \n"+
101 "Parameter c(i,j) transport cost in thousands of dollars per case ; \n"+
102 " \n"+
103 " c(i,j) = f * d(i,j) / 1000 ; \n"+
104 " \n"+
105 "Variables \n"+
106 " x(i,j) shipment quantities in cases \n"+
107 " z total transportation costs in thousands of dollars ; \n"+
108 " \n"+
109 "Positive Variable x ; \n"+
110 " \n"+
111 "Equations \n"+
112 " cost define objective function \n"+
113 " supply(i) observe supply limit at plant i \n"+
114 " demand(j) satisfy demand at market j ; \n"+
115 " \n"+
116 "cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"+
117 " \n"+
118 "supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"+
119 " \n"+
120 "demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n"+
121 " \n"+
122 "Model transport /all/ ; \n"+
123 " \n"+
124 "Solve transport using lp minimizing z ; \n"+
125 " \n"+
126 "Display x.l, x.m ; \n"+
127 " \n";
128}
This example shows how to run multiple GAMSJobs in parallel each using different scenario.
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS)...
GAMSVariable getVariable(String identifier)
GAMSParameter addParameter(String identifier, int dimension)
&#160;
void defines(String defStr, String asStr)
void setSystemDirectory(String directory)
T addRecord(Vector< String > keys)
GAMSDatabase OutDB()
GAMSJob addJobFromString(String source)
void setWorkingDirectory(String directory)