Loading...
Searching...
No Matches
Transport6.java
1package com.gams.examples.transport;
2
3import java.io.File;
4
7import com.gams.api.GAMSJob;
10
15public class Transport6 {
16
17 public static void main(String[] args) {
18 // check workspace info from command line arguments
20 if (args.length > 0)
21 wsInfo.setSystemDirectory( args[0] );
22 // create a directory
23 File workingDirectory = new File(System.getProperty("user.dir"), "Transport6");
24 workingDirectory.mkdir();
25 wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
26 // create a workspace
27 GAMSWorkspace ws = new GAMSWorkspace(wsInfo);
28 // create a checkpoint
30
31 // initialize a checkpoint by running a job
32 GAMSJob t6 = ws.addJobFromString(model);
33 t6.run(cp);
34
35 double[] bmultlist = new double[] { 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 };
36
37 // run multiple parallel jobs using the created checkpoint
38 Object lockObject = new Object();
39 Scenario[] scenarios = new Scenario[bmultlist.length];
40 for (int i=0; i<bmultlist.length; i++) {
41 scenarios[i] = new Scenario(ws, cp, lockObject, bmultlist[i]);
42 scenarios[i].start();
43 }
44 for (int i=0; i<bmultlist.length; i++) {
45 try {
46 scenarios[i].join();
47 } catch (InterruptedException e) {
48 e.printStackTrace();
49 }
50 }
51 }
52
53 static class Scenario extends Thread {
54 GAMSWorkspace workspace;
55 GAMSCheckpoint checkpoint;
56 Object lockObject;
57 double bmult;
58
59 public Scenario(GAMSWorkspace ws, GAMSCheckpoint cp, Object lockObj, double b) {
60 workspace = ws;
61 checkpoint = cp;
62 lockObject = lockObj;
63 bmult = b;
64 }
65
66 public void run() {
67 GAMSJob t6 = workspace.addJobFromString("bmult=" + bmult + "; solve transport min z use lp; ms=transport.modelstat; ss=transport.solvestat;", checkpoint);
68 t6.run();
69
70 // we need to make the output a critical section to avoid messed up report information
71 synchronized (lockObject) {
72 System.out.println("Scenario bmult=" + bmult + ":");
73 System.out.println(" Modelstatus: " + GAMSGlobals.ModelStat.lookup( (int) t6.OutDB().getParameter("ms").findRecord().getValue() ));
74 System.out.println(" Solvestatus: " + GAMSGlobals.SolveStat.lookup( (int)t6.OutDB().getParameter("ss").findRecord().getValue() ));
75 System.out.println(" Obj: " + t6.OutDB().getVariable("z").findRecord().getLevel());
76 }
77 // dispose output database at the end of the run
78 t6.OutDB().dispose();
79 }
80 }
81
82 static String model =
83 "Sets \n" +
84 " i canning plants / seattle, san-diego / \n" +
85 " j markets / new-york, chicago, topeka / ; \n" +
86 " \n" +
87 "Parameters \n" +
88 " a(i) capacity of plant i in cases \n" +
89 " / seattle 350 \n" +
90 " san-diego 600 / \n" +
91 " \n" +
92 " b(j) demand at market j in cases \n" +
93 " / new-york 325 \n" +
94 " chicago 300 \n" +
95 " topeka 275 / ; \n" +
96 " \n" +
97 "Table d(i,j) distance in thousands of miles \n" +
98 " new-york chicago topeka \n" +
99 "seattle 2.5 1.7 1.8 \n" +
100 "san-diego 2.5 1.8 1.4 ; \n" +
101 " \n" +
102 "Scalar f freight in dollars per case per thousand miles /90/ ; \n" +
103 "Scalar bmult demand multiplier /1/; \n" +
104 " \n" +
105 "Parameter c(i,j) transport cost in thousands of dollars per case ; \n" +
106 " c(i,j) = f * d(i,j) / 1000 ; \n" +
107 " \n" +
108 "Variables \n" +
109 " x(i,j) shipment quantities in cases \n" +
110 " z total transportation costs in thousands of dollars ; \n" +
111 " \n" +
112 "Positive Variable x ; \n" +
113 " \n" +
114 "Equations \n" +
115 " cost define objective function \n" +
116 " supply(i) observe supply limit at plant i \n" +
117 " demand(j) satisfy demand at market j ; \n" +
118 " \n" +
119 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n" +
120 " \n" +
121 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n" +
122 " \n" +
123 " demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ; \n" +
124 " \n" +
125 "Model transport /all/ ; \n" +
126 "Scalar ms 'model status', ss 'solve status'; \n" +
127 " \n";
128}
GAMSParameter getParameter(String identifier)
GAMSVariable getVariable(String identifier)
GAMSDatabase OutDB()
T findRecord(String ... keys)
void setSystemDirectory(String directory)
void setWorkingDirectory(String directory)
GAMSJob addJobFromString(String source)
GAMSCheckpoint addCheckpoint()
This example shows how to run multiple GAMSJobs in parallel using a GAMSCheckpoint.
Definition: Transport6.java:15
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).
&#160;