Loading...
Searching...
No Matches
Transport10.java
1package com.gams.examples.transport;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.IOException;
6
9import com.gams.api.GAMSSet;
13
23public class Transport10 {
24
25 public static void main(String[] args) throws IOException {
26 // create a workspace info
28 // set default a spreadsheet reader
29 String reader = "JXL";
30
31 if (args.length > 0) {
32 // check system directory info from the first command line argument
33 wsInfo.setSystemDirectory( args[0] );
34 // check spreadsheet reader info from the second command line argument
35 if (args.length > 1) {
36 if (args[0].toUpperCase().equals("POI")) {
37 reader = args[0].toUpperCase();
38 }
39 }
40 }
41
42 // create a working directory
43 File workingDirectory = new File(System.getProperty("user.dir"), "Transport10");
44 workingDirectory.mkdir();
45 wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
46
47 // create a workspace
48 GAMSWorkspace ws = new GAMSWorkspace(wsInfo);
49
50 // create a transport model wrapper
52
53 if (reader.equals("POI")) {
54 // create a POI spreadsheet reader
55 readPOIFromFile poiReader = new readPOIFromFile(getInputString(ws));
56 poiReader.read(t.geti(), "capacity");
57 poiReader.read(t.geta(), "capacity", false);
58 poiReader.read(t.getj(), "demand");
59 poiReader.read(t.getb(), "demand", false);
60 poiReader.read(t.getd(), "distance", true);
61 poiReader.close();
62 } else {
63 // create a JXL spreadsheet reader
64 try {
65 readJXLFromFile jxlReader = new readJXLFromFile(getInputString(ws));
66 jxlReader.read(t.geti(), "capacity");
67 jxlReader.read(t.geta(), "capacity", false);
68 jxlReader.read(t.getj(), "demand");
69 jxlReader.read(t.getb(), "demand", false);
70 jxlReader.read(t.getd(), "distance", true);
71 jxlReader.close();
72 } catch (jxl.read.biff.BiffException e) {
73 e.printStackTrace();
74 }
75 }
76
77 // set option of all model types for xpress
78 t.getopt().setAllModelTypes("xpress");
79
80 // run the model
81 t.run(System.out);
82
83 // retrieve GAMSVariable "x" from the model wrapper
84 for (GAMSVariableRecord rec : t.getx())
85 System.out.println("x(" + rec.getKey(0) + "," + rec.getKey(1) + "): level=" + rec.getLevel() + " marginal=" + rec.getMarginal());
86 }
87
88 private static String getInputString(GAMSWorkspace ws) {
89 // the directory [path/to/gams] where GAMS has been installed
90 String gamsdir = ws.systemDirectory();
91
92 // read input data from workbook "[path/to/gams]/apifiles/Data/transport.xls"
93 if (!gamsdir.endsWith(GAMSGlobals.FILE_SEPARATOR))
94 gamsdir += GAMSGlobals.FILE_SEPARATOR;
95 return gamsdir + "apifiles" + GAMSGlobals.FILE_SEPARATOR + "Data"
96 + GAMSGlobals.FILE_SEPARATOR + "transport.xls";
97 }
98}
99
105class readJXLFromFile {
106 private jxl.Workbook w;
107
113 public readJXLFromFile(String input) throws IOException, jxl.read.biff.BiffException {
114 File inputFile = new File(input);
115 w = jxl.Workbook.getWorkbook(inputFile);
116 }
117
122 public void read(GAMSSet set, String fromWorksheet) {
123 jxl.Sheet sheet = w.getSheet(fromWorksheet);
124 for(jxl.Cell cell : sheet.getRow(0))
125 set.addRecord( cell.getContents() );
126 }
127
133 public void read(GAMSParameter param, String fromWorksheet, boolean twoDimensioned) {
134 jxl.Sheet sheet = w.getSheet(fromWorksheet);
135 if (twoDimensioned) {
136 for (int j = 1; j < sheet.getColumns(); j++)
137 for (int i = 1; i < sheet.getRows(); i++)
138 param.addRecord( sheet.getCell(0,i).getContents(),
139 sheet.getCell(j,0).getContents()
140 ).setValue( Double.valueOf(sheet.getCell(j,i).getContents()) );
141 } else {
142 for (int j = 0; j < sheet.getColumns(); j++)
143 param.addRecord( sheet.getCell(j, 0).getContents() ).setValue( Double.valueOf(sheet.getCell(j,1).getContents()) );
144 }
145 }
146
148 public void close() {
149 if (w!=null)
150 w.close();
151 }
152}
153
159class readPOIFromFile {
160 private org.apache.poi.hssf.usermodel.HSSFWorkbook workbook;
161 private FileInputStream file;
162
167 public readPOIFromFile(String input) throws IOException {
168 file = new FileInputStream(new File(input));
169 workbook = new org.apache.poi.hssf.usermodel.HSSFWorkbook(file);
170 }
171
176 public void read(GAMSSet set, String fromWorksheet) {
177 org.apache.poi.hssf.usermodel.HSSFSheet sheet = workbook.getSheet(fromWorksheet);
178 fillSetFromRow( set, sheet.getRow( sheet.getFirstRowNum()) );
179 }
180
186 public void read(GAMSParameter param, String fromWorksheet, boolean twoDimensioned) {
187 org.apache.poi.hssf.usermodel.HSSFSheet sheet = workbook.getSheet(fromWorksheet);
188 if (twoDimensioned)
189 fillTwoDimensionedParameterFromSheet(param, sheet);
190 else
191 fillParameterFromSheet( param, sheet );
192 }
193
195 public void close() throws IOException {
196 if (file!=null)
197 file.close();
198 }
199
200 private void fillSetFromRow(GAMSSet set, org.apache.poi.hssf.usermodel.HSSFRow row) {
201 for(org.apache.poi.ss.usermodel.Cell cell : row) {
202 switch(cell.getCellTypeEnum()) {
203 case BOOLEAN: set.addRecord( String.valueOf( cell.getBooleanCellValue() ) ); break;
204 case NUMERIC: set.addRecord( String.valueOf( cell.getNumericCellValue() ) ); break;
205 case STRING: set.addRecord( cell.getStringCellValue() ); break;
206 default: break;
207 }
208 }
209 }
210
211 private void fillParameterFromSheet(GAMSParameter param, org.apache.poi.hssf.usermodel.HSSFSheet sheet) {
212 org.apache.poi.hssf.usermodel.HSSFRow firstRow = sheet.getRow( sheet.getFirstRowNum() );
213 org.apache.poi.hssf.usermodel.HSSFRow row = sheet.getRow( sheet.getLastRowNum() );
214 int idx = 0;
215 for (org.apache.poi.ss.usermodel.Cell cell : row) {
216 switch(cell.getCellTypeEnum()) {
217 case NUMERIC: param.addRecord( firstRow.getCell(idx).getStringCellValue() ).setValue( cell.getNumericCellValue() ); break;
218 case STRING:
219 param.addRecord( firstRow.getCell(idx).getStringCellValue() ).setValue( Double.valueOf( cell.getStringCellValue() )); break;
220 case BOOLEAN:
221 param.addRecord( firstRow.getCell(idx).getStringCellValue() ).setValue( cell.getBooleanCellValue() ? 1 : 0 ); break;
222 default: break;
223 }
224 idx++;
225 }
226 }
227
228 private void fillTwoDimensionedParameterFromSheet(GAMSParameter param, org.apache.poi.hssf.usermodel.HSSFSheet sheet) {
229 for (org.apache.poi.ss.usermodel.Row row : sheet) {
230 if (row.getRowNum() == sheet.getFirstRowNum()) {
231 continue;
232 } else {
233 org.apache.poi.ss.usermodel.Cell cell = row.getCell( 0 );
234 for (short j = 1; j<row.getLastCellNum() ; j++) {
235 cell = row.getCell( j );
236 switch(cell.getCellTypeEnum()) {
237 case NUMERIC:
238 param.addRecord( row.getCell(0).getStringCellValue() ,
239 sheet.getRow(0).getCell( j ).getStringCellValue()
240 ).setValue( cell.getNumericCellValue() );
241 break;
242 case STRING:
243 param.addRecord( row.getCell(0).getStringCellValue() , sheet.getRow( 0 ).getCell( j ).getStringCellValue() ).setValue( Double.valueOf( cell.getStringCellValue() ));
244 break;
245 case BOOLEAN:
246 param.addRecord( row.getCell(0).getStringCellValue() , sheet.getRow( 0 ).getCell( j ).getStringCellValue() ).setValue( cell.getBooleanCellValue() ? 1 : 0 );
247 break;
248 default:
249 break;
250 }
251 }
252 }
253 }
254 }
255}
static final String FILE_SEPARATOR
void setAllModelTypes(String value)
void setSystemDirectory(String directory)
void setWorkingDirectory(String directory)
This example demonstrates how to retrieve an input for GAMS Transport Model from an Excel file (trans...
This example shows the wrapper model of a transportation problem based on the simple GAMS [trnsport] ...
GAMSOptions getopt()
Options for the execution of the trnsport model.
void run(GAMSCheckpoint checkpoint)
Executes the trnsport model.
GAMSVariable getx()
x(i,j): shipment quantities in cases
GAMSParameter getb()
b(i): demand at market j in cases
GAMSParameter geta()
a(i): capacity of plant i in cases
GAMSParameter getd()
d(i,j): distance in thousands of miles
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).
&#160;