Warehouse.java
1package com.gams.examples.warehouse;
2 
3 import java.io.File;
4 import java.util.HashMap;
5 import java.util.Map;
6 
9 import com.gams.api.GAMSGlobals;
10 import com.gams.api.GAMSDatabase;
11 import com.gams.api.GAMSJob;
12 import com.gams.api.GAMSOptions;
14 import com.gams.api.GAMSWorkspace;
16 
24 public class Warehouse {
25 
26  static int numberOfThreads = 16;
27  static int status = 0;
28 
29  public static void main(String[] args) {
30  // check workspace info from command line arguments
31  GAMSWorkspaceInfo wsInfo = new GAMSWorkspaceInfo();
32  if (args.length > 0)
33  wsInfo.setSystemDirectory( args[0] );
34  // create a directory
35  File workingDirectory = new File(System.getProperty("user.dir"), "Warehouse");
36  workingDirectory.mkdir();
37  wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
38  // create a workspace
39  GAMSWorkspace ws = new GAMSWorkspace( wsInfo );
40 
41  // create a GAMSDatabase for the results
42  GAMSDatabase resultDB = ws.addDatabase();
43  resultDB.addParameter("objrep",1,"Objective value");
44  resultDB.addSet("supplyMap",3,"Supply connection with level");
45 
46  try {
47  // run multiple parallel jobs
48  Object dbLock = new Object();
49  Map<String, WarehouseThread> warehousesMap = new HashMap<String, WarehouseThread>();
50  for (int i=10; i<=numberOfThreads; i++) {
51  WarehouseThread wh = new WarehouseThread(ws, i, resultDB, dbLock);
52  warehousesMap.put(Integer.toString(i), wh);
53  wh.start();
54  }
55 
56  // join all threads
57  for (WarehouseThread wh : warehousesMap.values()) {
58  try {
59  wh.join();
60  } catch (InterruptedException e) {
61  e.printStackTrace();
62  }
63  }
64 
65  // export the result database to a GDX file
66  resultDB.export(ws.workingDirectory() + GAMSGlobals.FILE_SEPARATOR + "result.gdx");
67  } catch (Exception e) {
68  e.printStackTrace();
69  } finally {
70  resultDB.dispose();
71  }
72 
73  System.exit(status);
74  }
75 
76  public static void notifyException(GAMSException e) {
77  if (e instanceof GAMSExecutionException)
78  status = ((GAMSExecutionException)e).getExitCode();
79  else
80  status = -1;
81  }
82 
83  static class WarehouseThread extends Thread {
84  GAMSWorkspace workspace;
85  GAMSDatabase result;
86  Object lockObject;
87  int numberOfWarehouses;
88 
89  public WarehouseThread(GAMSWorkspace ws, int number, GAMSDatabase db, Object lockObj) {
90  workspace = ws;
91  numberOfWarehouses = number;
92  result = db;
93  lockObject = lockObj;
94  }
95 
96  public void run() {
97  try {
98  // instantiate GAMSOptions and define some scalars
99  GAMSOptions opt = workspace.addOptions();
100  opt.setAllModelTypes( "cplex" );
101  opt.defines("Warehouse", Integer.toString(numberOfWarehouses));
102  opt.defines("Store", "65");
103  opt.defines("fixed", "22");
104  opt.defines("disaggregate", "0");
105  opt.setOptCR( 0.0 ); // Solve to optimality
106 
107  // create a GAMSJob from string and write results to the result database
108  GAMSJob job = workspace.addJobFromString(model);
109  job.run(opt, System.out); // job.run(opt);
110 
111  // need to lock database write operations
112  synchronized (lockObject) {
113  result.getParameter("objrep").addRecord(Integer.toString(numberOfWarehouses)).setValue( job.OutDB().getVariable("obj").findRecord().getLevel() );
114  }
115 
116  for (GAMSVariableRecord supplyRec : job.OutDB().getVariable("supply")) {
117  if (supplyRec.getLevel() > 0.5)
118  synchronized (lockObject) {
119  String[] keys = new String[] { Integer.toString(numberOfWarehouses), supplyRec.getKey(0), supplyRec.getKey(1) };
120  result.getSet("supplyMap").addRecord( keys );
121  }
122  }
123  } catch (GAMSException e) {
124  e.printStackTrace();
125  Warehouse.notifyException(e) ;
126  }
127  }
128  }
129 
130  static String model =
131  "$title Warehouse.gms \n" +
132  " \n" +
133  "$eolcom // \n" +
134  "$SetDDList warehouse store fixed disaggregate // acceptable defines \n" +
135  "$if not set warehouse $set warehouse 10 \n" +
136  "$if not set store $set store 50 \n" +
137  "$if not set fixed $set fixed 20 \n" +
138  "$if not set disaggregate $set disaggregate 1 // indicator for tighter bigM constraint \n" +
139  "$ife %store%<=%warehouse% $abort Increase number of stores (>%warehouse) \n" +
140  " \n" +
141  "Sets Warehouse /w1*w%warehouse% / \n" +
142  " Store /s1*s%store% / \n" +
143  "Alias (Warehouse,w), (Store,s); \n" +
144  "Scalar \n" +
145  " fixed fixed cost for opening a warehouse / %fixed% / \n" +
146  "Parameter \n" +
147  " capacity(WareHouse) \n" +
148  " supplyCost(Store,Warehouse); \n" +
149  " \n" +
150  "$eval storeDIVwarehouse trunc(card(store)/card(warehouse)) \n" +
151  "capacity(w) = %storeDIVwarehouse% + mod(ord(w),%storeDIVwarehouse%);\n" +
152  "supplyCost(s,w) = 1+mod(ord(s)+10*ord(w), 100); \n" +
153  " \n" +
154  "Variables \n" +
155  " open(Warehouse) \n" +
156  " supply(Store,Warehouse) \n" +
157  " obj; \n" +
158  "Binary variables open, supply; \n" +
159  " \n" +
160  "Equations \n" +
161  " defobj \n" +
162  " oneWarehouse(s) \n" +
163  " defopen(w); \n" +
164  " \n" +
165  "defobj.. obj =e= sum(w, fixed*open(w)) + sum((w,s), supplyCost(s,w)*supply(s,w)); \n" +
166  " \n" +
167  "oneWarehouse(s).. sum(w, supply(s,w)) =e= 1; \n" +
168  " \n" +
169  "defopen(w).. sum(s, supply(s,w)) =l= open(w)*capacity(w); \n" +
170  " \n" +
171  "$ifthen %disaggregate%==1 \n" +
172  "Equations \n" +
173  " defopen2(s,w); \n" +
174  "defopen2(s,w).. supply(s,w) =l= open(w); \n" +
175  "$endif \n" +
176  " \n" +
177  "model distrib /all/; \n" +
178  "solve distrib min obj using mip; \n" +
179  "abort$(distrib.solvestat<>%SolveStat.NormalCompletion% or \n" +
180  " distrib.modelstat<>%ModelStat.Optimal% and \n" +
181  " distrib.modelstat<>%ModelStat.IntegerSolution%) 'No solution!'; \n" +
182  " \n";
183 }
GAMSParameter getParameter(String identifier)
void defines(String defStr, String asStr)
void setSystemDirectory(String directory)
This example demonstrates how to solve a simple GAMS model to assign stores to warehouses for differe...
Definition: Warehouse.java:24
GAMSParameter addParameter(String identifier, int dimension)
GAMSSet addSet(String identifier, int dimension)
GAMSSet getSet(String identifier)
GAMSJob addJobFromString(String source)
void setWorkingDirectory(String directory)
static final String FILE_SEPARATOR
GAMSDatabase OutDB()
GAMSVariable getVariable(String identifier)
T addRecord(Vector< String > keys)
void setAllModelTypes(String value)