1package com.gams.examples.transport;
 
    4import java.io.FileInputStream;
 
    5import java.io.IOException;
 
   25    public static void main(String[] args) 
throws IOException {
 
   29        String reader = 
"JXL";
 
   31        if (args.length > 0) {
 
   35           if (args.length > 1) {
 
   36              if (args[0].toUpperCase().equals(
"POI")) {
 
   37                 reader = args[0].toUpperCase();
 
   43        File workingDirectory = 
new File(System.getProperty(
"user.dir"), 
"Transport10");
 
   44        workingDirectory.mkdir();
 
   53        if (reader.equals(
"POI")) {
 
   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);
 
   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);
 
   72            } 
catch (jxl.read.biff.BiffException e) {
 
   78        t.
getopt().setAllModelTypes(
"xpress");
 
   85            System.out.println(
"x(" + rec.getKey(0) + 
"," + rec.getKey(1) + 
"): level=" + rec.getLevel() + 
" marginal=" + rec.getMarginal());
 
   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";
 
 
  105class readJXLFromFile {
 
  106    private jxl.Workbook w;
 
  113    public readJXLFromFile(String input) 
throws IOException, jxl.read.biff.BiffException {
 
  114        File inputFile = 
new File(input);
 
  115        w = jxl.Workbook.getWorkbook(inputFile);
 
  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() );
 
  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()) );
 
  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()) );
 
  148    public void close() {
 
  159class readPOIFromFile {
 
  160    private org.apache.poi.hssf.usermodel.HSSFWorkbook workbook;
 
  161    private FileInputStream file;
 
  167    public readPOIFromFile(String input) 
throws IOException {
 
  168        file = 
new FileInputStream(
new File(input));
 
  169        workbook = 
new org.apache.poi.hssf.usermodel.HSSFWorkbook(file);
 
  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()) );
 
  186    public void read(GAMSParameter param, String fromWorksheet, 
boolean twoDimensioned) {
 
  187        org.apache.poi.hssf.usermodel.HSSFSheet sheet = workbook.getSheet(fromWorksheet);
 
  189           fillTwoDimensionedParameterFromSheet(param, sheet);
 
  191            fillParameterFromSheet( param, sheet );
 
  195    public void close() throws IOException {
 
  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;
 
  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() );
 
  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;
 
  219                param.addRecord( firstRow.getCell(idx).getStringCellValue() ).setValue( Double.valueOf( cell.getStringCellValue() )); 
break;
 
  221                param.addRecord( firstRow.getCell(idx).getStringCellValue() ).setValue( cell.getBooleanCellValue() ? 1 : 0 ); 
break;
 
  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()) {
 
  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()) {
 
  238                         param.addRecord( row.getCell(0).getStringCellValue() , 
 
  239                                          sheet.getRow(0).getCell( j ).getStringCellValue()  
 
  240                                        ).setValue( cell.getNumericCellValue() ); 
 
  243                         param.addRecord( row.getCell(0).getStringCellValue() , sheet.getRow( 0 ).getCell( j ).getStringCellValue() ).setValue( Double.valueOf( cell.getStringCellValue() ));
 
  246                         param.addRecord( row.getCell(0).getStringCellValue() , sheet.getRow( 0 ).getCell( j ).getStringCellValue() ).setValue( cell.getBooleanCellValue() ? 1 : 0 );
 
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
 
GAMSSet geti()
i: canning plants