Loading...
Searching...
No Matches
Transport7.java
1package com.gams.examples.transport;
2
3import java.io.File;
4
7import com.gams.api.GAMSJob;
10import com.gams.api.GAMSOptions;
16
23public class Transport7 {
24
25 public static void main(String[] args) {
26 // check workspace info from command line arguments
28 if (args.length > 0)
29 wsInfo.setSystemDirectory( args[0] );
30 // create a directory
31 File workingDirectory = new File(System.getProperty("user.dir"), "Transport7");
32 workingDirectory.mkdir();
33 wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
34 // create a workspace
35 GAMSWorkspace ws = new GAMSWorkspace(wsInfo);
36 // create a checkpoint
38
39 // initialize a checkpoint by running a job
40 GAMSJob t7 = ws.addJobFromString(model);
41 t7.run(cp);
42
43 // create a MoelInstance and solve it multiple times with different scalar bmult
45 GAMSParameter bmult = mi.SyncDB().addParameter("bmult", "demand multiplier");
46 GAMSOptions opt = ws.addOptions();
47 opt.setAllModelTypes("cplex");
48
49 // instantiate the ModelInstance and pass a model definition and Modifier to declare bmult mutable
50 mi.instantiate("transport use lp min z", opt, new GAMSModifier(bmult));
51
52 bmult.addRecord().setValue( 1.0 );
53 double[] bmultlist = new double[] { 0.6, 0.7 , 0.8, 0.9, 1.0, 1.1, 1.2, 1.3 };
54
55 for (double b : bmultlist) {
56 bmult.getFirstRecord().setValue( b );
57 mi.solve();
58 System.out.println("Scenario bmult=" + b + ":");
59 System.out.println(" Modelstatus: " + mi.getModelStatus());
60 System.out.println(" Solvestatus: " + mi.getSolveStatus());
61 System.out.println(" Obj: " + mi.SyncDB().getVariable("z").findRecord().getLevel());
62 }
63
64 // create a ModelInstance and solve it with single links in the network blocked
65 mi = cp.addModelInstance();
66
67 GAMSVariable x = mi.SyncDB().addVariable("x", 2, GAMSGlobals.VarType.POSITIVE, "");
68 GAMSParameter xup = mi.SyncDB().addParameter("xup", 2, "upper bound on x");
69
70 // instantiate the ModelInstance and pass a model definition and Modifier to declare upper bound of X mutable
71 mi.instantiate("transport use lp min z", new GAMSModifier(x, GAMSGlobals.UpdateAction.UPPER, xup));
72
73 for (GAMSSetRecord i : t7.OutDB().getSet("i")) {
74 for (GAMSSetRecord j : t7.OutDB().getSet("j")) {
75 xup.clear();
76 String[] keys = { i.getKey(0), j.getKey(0) };
77 xup.addRecord(keys).setValue(0);
78 mi.solve();
79 System.out.println("Scenario link blocked: " + i.getKey(0) + " - " + j.getKey(0));
80 System.out.println(" Modelstatus: " + mi.getModelStatus());
81 System.out.println(" Solvestatus: " + mi.getSolveStatus());
82 System.out.println(" Obj: " + mi.SyncDB().getVariable("z").findRecord().getLevel());
83 }
84 }
85 }
86
87
88 static String model =
89 "Sets \n" +
90 " i canning plants / seattle, san-diego / \n" +
91 " j markets / new-york, chicago, topeka / ; \n" +
92 " \n" +
93 "Parameters \n" +
94 " a(i) capacity of plant i in cases \n" +
95 " / seattle 350 \n" +
96 " san-diego 600 / \n" +
97 " \n" +
98 " b(j) demand at market j in cases \n" +
99 " / new-york 325 \n" +
100 " chicago 300 \n" +
101 " topeka 275 / ; \n" +
102 " \n" +
103 "Table d(i,j) distance in thousands of miles \n" +
104 " new-york chicago topeka \n" +
105 "seattle 2.5 1.7 1.8 \n" +
106 "san-diego 2.5 1.8 1.4 ; \n" +
107 " \n" +
108 "Scalar f freight in dollars per case per thousand miles /90/ ; \n" +
109 "Scalar bmult demand multiplier /1/; \n" +
110 " \n" +
111 "Parameter c(i,j) transport cost in thousands of dollars per case ; \n" +
112 " c(i,j) = f * d(i,j) / 1000 ; \n" +
113 " \n" +
114 "Variables \n" +
115 " x(i,j) shipment quantities in cases \n" +
116 " z total transportation costs in thousands of dollars ; \n" +
117 " \n" +
118 "Positive Variable x ; \n" +
119 " \n" +
120 "Equations \n" +
121 " cost define objective function \n" +
122 " supply(i) observe supply limit at plant i \n" +
123 " demand(j) satisfy demand at market j ; \n" +
124 " \n" +
125 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n" +
126 " \n" +
127 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n" +
128 " \n" +
129 " demand(j) .. sum(i, x(i,j)) =g= bmult*b(j) ; \n" +
130 " \n" +
131 "Model transport /all/ ; \n" +
132 " \n";
133}
GAMSModelInstance addModelInstance()
GAMSVariable addVariable(String identifier, int dimension, GAMSGlobals.VarType varType)
GAMSParameter addParameter(String identifier, int dimension)
GAMSVariable getVariable(String identifier)
GAMSSet getSet(String identifier)
GAMSDatabase OutDB()
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 create a GAMSModelInstance from a GAMSCheckpoint and how to modify a parame...
Definition: Transport7.java:23
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).
&#160;