Loading...
Searching...
No Matches
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 // dispose option and databases
73 gOption.dispose();
74 gDb.dispose();
75 gModJob.OutDB().dispose();
76 }
77
78 static String model =
79 "Sets \n"+
80 " i canning plants / seattle, san-diego / \n"+
81 " j markets / new-york, chicago, topeka / ; \n"+
82 " \n"+
83 "Parameters \n"+
84 " \n"+
85 " a(i) capacity of plant i in cases \n"+
86 " / seattle 350 \n"+
87 " san-diego 600 / \n"+
88 " \n"+
89 " b(j) demand at market j in cases \n"+
90 " / new-york 325 \n"+
91 " chicago 300 \n"+
92 " topeka 275 / ; \n"+
93 " \n"+
94 "Table d(i,j) distance in thousands of miles \n"+
95 " new-york chicago topeka \n"+
96 " seattle 2.5 1.7 1.8 \n"+
97 " san-diego 2.5 1.8 1.4 ; \n"+
98 " \n"+
99 "Scalar f freight in dollars per case per thousand miles; \n"+
100 " \n"+
101 "$if not set gdxincname $abort 'no include file name for data file provided' \n"+
102 "$gdxin %gdxincname% \n"+
103 "$load f \n"+
104 "$gdxin \n"+
105 " \n"+
106 "Parameter c(i,j) transport cost in thousands of dollars per case ; \n"+
107 " \n"+
108 " c(i,j) = f * d(i,j) / 1000 ; \n"+
109 " \n"+
110 "Variables \n"+
111 " x(i,j) shipment quantities in cases \n"+
112 " z total transportation costs in thousands of dollars ; \n"+
113 " \n"+
114 "Positive Variable x ; \n"+
115 " \n"+
116 "Equations \n"+
117 " cost define objective function \n"+
118 " supply(i) observe supply limit at plant i \n"+
119 " demand(j) satisfy demand at market j ; \n"+
120 " \n"+
121 "cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n"+
122 " \n"+
123 "supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n"+
124 " \n"+
125 "demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n"+
126 " \n"+
127 "Model transport /all/ ; \n"+
128 " \n"+
129 "Solve transport using lp minimizing z ; \n"+
130 " \n"+
131 "Display x.l, x.m ; \n"+
132 " \n";
133}
GAMSParameter addParameter(String identifier, int dimension)
void defines(String defStr, String asStr)
T addRecord(Vector< String > keys)
void setSystemDirectory(String directory)
void setWorkingDirectory(String directory)
GAMSJob addJobFromString(String source)
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).
&#160;