TransportGUI.java
1package com.gams.examples.transport;
2 
3 import java.awt.Component;
4 import java.awt.ComponentOrientation;
5 import java.awt.Font;
6 import java.awt.GridBagConstraints;
7 import java.awt.GridBagLayout;
8 import java.awt.Insets;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.ActionListener;
11 import java.io.File;
12 import java.io.IOException;
13 import java.io.OutputStream;
14 import java.io.PrintStream;
15 import java.text.DecimalFormat;
16 
17 import javax.swing.BorderFactory;
18 import javax.swing.Box;
19 import javax.swing.BoxLayout;
20 import javax.swing.JButton;
21 import javax.swing.JFileChooser;
22 import javax.swing.JFrame;
23 import javax.swing.JLabel;
24 import javax.swing.JPanel;
25 import javax.swing.JScrollPane;
26 import javax.swing.JTabbedPane;
27 import javax.swing.JTable;
28 import javax.swing.JTextArea;
29 import javax.swing.JTextField;
30 import javax.swing.SwingConstants;
31 import javax.swing.SwingUtilities;
32 import javax.swing.border.EmptyBorder;
33 import javax.swing.filechooser.FileNameExtensionFilter;
34 import javax.swing.table.AbstractTableModel;
35 import javax.swing.table.DefaultTableCellRenderer;
36 import javax.swing.table.TableColumnModel;
37 
38 import com.gams.api.GAMSDatabase;
39 import com.gams.api.GAMSException;
40 import com.gams.api.GAMSJob;
41 import com.gams.api.GAMSOptions;
42 import com.gams.api.GAMSParameter;
44 import com.gams.api.GAMSVariable;
46 import com.gams.api.GAMSWorkspace;
48 
54 public class TransportGUI extends JFrame implements ActionListener {
55  private final GridBagConstraints constraints;
56  private final TransportTableModel capacityTableModel, demandTableModel, distanceTableModel, shipmentTableModel;
57  private final JTable capacityTable, demandTable, distanceTable, shipmentTable;
58  private final JTextArea logTextArea;
59  private final JScrollPane logScrollPanel;
60  private final JTextField freightCost, transportCost;
61  private final JButton loadButton, saveButton, runButton, exitButton;
62  private final JFileChooser fc = new JFileChooser();
63  private GAMSWorkspace ws;
64  private GAMSJob job;
65  private GAMSDatabase inDB;
66  private PrintStream printStream;
67 
68  public static void main(String[] args) {
69  SwingUtilities.invokeLater(new Runnable() {
70  public void run() {
71  new TransportGUI();
72  }
73  });
74  }
75 
76  public TransportGUI() {
77  super("Transport - GAMS Java API Example");
78  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
79 
80  // initialize GAMS Workspace and Job
81  GAMSWorkspaceInfo wsInfo = new GAMSWorkspaceInfo();
82  File workingDirectory = new File(System.getProperty("user.dir"), "TransportGUI");
83  workingDirectory.mkdir();
84  wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
85  ws = new GAMSWorkspace(wsInfo);
86 
87  // Create and run a job from static data file
88  job = ws.addJobFromString(data);
89  job.run();
90  inDB = job.OutDB();
91 
92  // Create components and set their layouts
93  JPanel dataPane = new JPanel();
94  dataPane.setLayout(new GridBagLayout());
95  dataPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
96  dataPane.setBorder(BorderFactory.createTitledBorder("Data"));
97 
98  getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
99  getContentPane().add(Box.createHorizontalGlue());
100  getContentPane().add(dataPane);
101  getContentPane().add(Box.createHorizontalGlue());
102 
103  constraints = new GridBagConstraints();
104  constraints.insets = new Insets(5, 10, 5, 10);
105 
106  JLabel capacityLabel = new JLabel("Capacity:");
107  capacityLabel.setToolTipText("capacity of plant");
108  constraints.anchor = GridBagConstraints.WEST;
109  constraints.gridx = 0;
110  constraints.gridy = 0;
111  dataPane.add(capacityLabel, constraints);
112 
113  capacityTableModel = new TransportTableModel(inDB.getParameter("a"), capacityLabel.getText());
114  capacityTable = new JTable( capacityTableModel );
115  capacityTable.setPreferredScrollableViewportSize(capacityTable.getPreferredSize());
116  capacityTable.setFillsViewportHeight(true);
117  capacityTable.setAlignmentX(Component.RIGHT_ALIGNMENT);
118  JScrollPane capacityScrollPane = new JScrollPane(capacityTable);
119  capacityScrollPane.setMinimumSize(capacityTable.getPreferredSize());
120  capacityScrollPane.setAutoscrolls(true);
121  constraints.anchor = GridBagConstraints.WEST;
122  constraints.gridx = 0;
123  constraints.gridy = 1;
124  dataPane.add(capacityScrollPane, constraints);
125 
126  NumberCellRenderer renderer = new NumberCellRenderer() ;
127  renderer.setHorizontalAlignment(JLabel.RIGHT);
128  TableColumnModel columnModel = capacityTable.getColumnModel();
129  for(int i=0; i<2; i++)
130  columnModel.getColumn(i).setCellRenderer(renderer);
131 
132  JLabel demandLabel = new JLabel("Demand:");
133  demandLabel.setToolTipText("demand at market");
134  constraints.anchor = GridBagConstraints.WEST;
135  constraints.gridx = 0;
136  constraints.gridy = 2;
137  dataPane.add(demandLabel, constraints);
138 
139  demandTableModel = new TransportTableModel(inDB.getParameter("b"), demandLabel.getText());
140  demandTable = new JTable(demandTableModel);
141  demandTable.setPreferredScrollableViewportSize(demandTable.getPreferredSize());
142  demandTable.setFillsViewportHeight(true);
143  JScrollPane demandScrollPane = new JScrollPane(demandTable);
144  demandScrollPane.setMinimumSize(capacityTable.getPreferredSize());
145  demandScrollPane.setAutoscrolls(true);
146  constraints.anchor = GridBagConstraints.WEST;
147  constraints.gridx = 0;
148  constraints.gridy = 3;
149  dataPane.add(demandScrollPane, constraints);
150 
151  columnModel = demandTable.getColumnModel();
152  for(int i=0; i<3; i++)
153  columnModel.getColumn(i).setCellRenderer(renderer);
154 
155  JLabel distanceLabel = new JLabel("Distance:");
156  distanceLabel.setToolTipText("distance in thousands of miles ");
157  constraints.anchor = GridBagConstraints.WEST;
158  constraints.gridx = 0;
159  constraints.gridy = 4;
160  dataPane.add(distanceLabel, constraints);
161 
162  distanceTableModel = new TransportTableModel(inDB.getParameter("d"), distanceLabel.getText());
163  distanceTable = new JTable(distanceTableModel);
164  distanceTable.setPreferredScrollableViewportSize(distanceTable.getPreferredSize());
165  distanceTable.setFillsViewportHeight(true);
166  JScrollPane distanceScrollPane = new JScrollPane(distanceTable);
167  distanceScrollPane.setMinimumSize(distanceTable.getPreferredSize());
168  distanceScrollPane.setAutoscrolls(true);
169  constraints.anchor = GridBagConstraints.WEST;
170  constraints.gridx = 0;
171  constraints.gridy = 5;
172  dataPane.add(distanceScrollPane, constraints);
173 
174  columnModel = distanceTable.getColumnModel();
175  columnModel.getColumn(2).setCellRenderer(renderer);
176 
177  JLabel costLabel = new JLabel("Freight Cost:");
178  costLabel.setToolTipText("freight in dollars per case per thousand miles");
179  constraints.anchor = GridBagConstraints.WEST;
180  constraints.gridx = 0;
181  constraints.gridy = 6;
182  dataPane.add(costLabel, constraints);
183 
184  freightCost = new JTextField(8);
185  freightCost.setHorizontalAlignment(SwingConstants.RIGHT);
186  freightCost.setText(Double.toString(inDB.getParameter("f").getFirstRecord().getValue() ));
187  freightCost.setPreferredSize(freightCost.getPreferredSize());
188  constraints.anchor = GridBagConstraints.WEST;
189  constraints.fill = GridBagConstraints.HORIZONTAL;
190  constraints.gridx = 0;
191  constraints.gridy = 7;
192  dataPane.add(freightCost, constraints);
193 
194  // Create a text area to display running job's log
195  logTextArea = new JTextArea(15, 50);
196  logTextArea.setLineWrap(true);
197  logTextArea.setWrapStyleWord(true);
198  logTextArea.setEditable(false);
199  Font font = new Font( "Monospaced", Font.PLAIN, 12 );
200  logTextArea.setFont( font );
201 
202  // Create a scrollable Text Panel to output the running job's log
203  printStream = new PrintStream(new LogOutputStream(logTextArea));
204  System.setOut(printStream);
205  System.setErr(printStream);
206 
207  logScrollPanel = new JScrollPane(logTextArea);
208  logScrollPanel.setMinimumSize(logTextArea.getPreferredSize());
209  logScrollPanel.setAutoscrolls(true);
210 
211  JPanel runPane = new JPanel();
212  runPane.setLayout(new GridBagLayout());
213  runPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
214  runPane.setBorder(new EmptyBorder(5,5,5,5));
215 
216  loadButton = makeButton("Load Data");
217  loadButton.setToolTipText("Load Data from gdx file");
218  constraints.anchor = GridBagConstraints.CENTER;
219  constraints.fill = GridBagConstraints.HORIZONTAL;
220  constraints.gridx = 0;
221  constraints.gridy = 0;
222  runPane.add(loadButton, constraints);
223 
224  saveButton = makeButton("Save Data");
225  saveButton.setToolTipText("Save Data to gdx file");
226  constraints.gridx = 1;
227  runPane.add(saveButton, constraints);
228 
229  runButton = makeButton("Run");
230  runButton.setToolTipText("Run Transport Model");
231  constraints.gridx = 2;
232  runPane.add(runButton, constraints);
233 
234  exitButton = makeButton("Exit");
235  runButton.setToolTipText("Exit Transport Example");
236  constraints.gridx = 3;
237  runPane.add(exitButton, constraints);
238  getContentPane().add(runPane);
239 
240  // Create Output panel
241  JPanel outputPane = new JPanel();
242  outputPane.setBorder(BorderFactory.createTitledBorder("Output"));
243  getContentPane().add(outputPane);
244 
245  JTabbedPane resultPane = new JTabbedPane();
246 
247  JPanel resultInnerPane = new JPanel();
248  resultInnerPane.setLayout(new GridBagLayout());
249  resultInnerPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
250 
251  JLabel transportCostLabel = new JLabel("Transportation Cost:");
252  transportCostLabel.setToolTipText("total transportation costs in thousands of dollars");
253  constraints.anchor = GridBagConstraints.WEST;
254  constraints.gridx = 0;
255  constraints.gridy = 1;
256  resultInnerPane.add(transportCostLabel, constraints);
257 
258  transportCost = new JTextField(12);
259  transportCost.setHorizontalAlignment(SwingConstants.RIGHT);
260  transportCost.setText("");
261  constraints.anchor = GridBagConstraints.WEST;
262  constraints.gridx = 0;
263  constraints.gridy = 2;
264  resultInnerPane.add(transportCost, constraints);
265 
266  JLabel shipmentLabel = new JLabel("Shipment:");
267  shipmentLabel.setToolTipText("shipment quantities");
268  constraints.anchor = GridBagConstraints.WEST;
269  constraints.gridx = 0;
270  constraints.gridy = 3;
271  resultInnerPane.add(shipmentLabel, constraints);
272 
273  shipmentTableModel = new TransportTableModel((GAMSVariable)null, shipmentLabel.getText());
274  shipmentTable = new JTable(shipmentTableModel);
275  shipmentTable.setPreferredScrollableViewportSize(shipmentTable.getPreferredSize());
276  shipmentTable.setFillsViewportHeight(true);
277  JScrollPane shipmentScrollPane = new JScrollPane(shipmentTable);
278  constraints.anchor = GridBagConstraints.WEST;
279  constraints.gridx = 0;
280  constraints.gridy = 4;
281  resultInnerPane.add(shipmentScrollPane, constraints);
282 
283  columnModel = shipmentTable.getColumnModel();
284  for(int i=2; i<4; i++)
285  columnModel.getColumn(i).setCellRenderer(renderer);
286 
287  resultPane.setAutoscrolls(true);
288  resultPane.addTab("Results", resultInnerPane);
289  resultPane.addTab("log",logScrollPanel);
290 
291  outputPane.add(resultPane);
292  outputPane.add(Box.createHorizontalGlue());
293 
294  // Display the window
295  pack();
296  setVisible(true);
297  }
298 
300  private JButton makeButton(String caption) {
301  JButton b = new JButton(caption);
302  b.setActionCommand(caption);
303  b.addActionListener(this);
304  getContentPane().add(b, constraints);
305  return b;
306  }
307 
309  private void updateDB(GAMSDatabase db) {
310  GAMSParameter capacity = db.getParameter("a");
311  capacity.findRecord("seattle").setValue(
312  (capacityTable.getValueAt(0,0) instanceof Double) ? ((Double)capacityTable.getValueAt(0,0)).doubleValue() : Double.parseDouble((String)capacityTable.getValueAt(0,0))
313  );
314  capacity.findRecord("san-diego").setValue(
315  (capacityTable.getValueAt(0,1) instanceof Double) ? ((Double)capacityTable.getValueAt(0,1)).doubleValue() : Double.parseDouble((String)capacityTable.getValueAt(0,1))
316  );
317  GAMSParameter demand = db.getParameter("b");
318  demand.findRecord("new-york").setValue(
319  (demandTable.getValueAt(0,0) instanceof Double) ? ((Double)demandTable.getValueAt(0,0)).doubleValue() : Double.parseDouble((String)demandTable.getValueAt(0,0))
320  );
321  demand.findRecord("chicago").setValue(
322  (demandTable.getValueAt(0,1) instanceof Double) ? ((Double)demandTable.getValueAt(0,1)).doubleValue() : Double.parseDouble((String)demandTable.getValueAt(0,1))
323  );
324  demand.findRecord("topeka").setValue(
325  (demandTable.getValueAt(0,2) instanceof Double) ? ((Double)demandTable.getValueAt(0,2)).doubleValue() : Double.parseDouble((String)demandTable.getValueAt(0,2))
326  );
327  GAMSParameter distance = db.getParameter("d");
328  distance.findRecord("seattle", "new-york").setValue(
329  (distanceTable.getValueAt(0,2) instanceof Double) ? ((Double)distanceTable.getValueAt(0,2)).doubleValue() : Double.parseDouble((String)distanceTable.getValueAt(0,2))
330  );
331  distance.findRecord("seattle", "chicago").setValue(
332  (distanceTable.getValueAt(1,2) instanceof Double) ? ((Double)distanceTable.getValueAt(1,2)).doubleValue() : Double.parseDouble((String)distanceTable.getValueAt(1,2))
333  );
334  distance.findRecord("seattle", "topeka").setValue(
335  (distanceTable.getValueAt(2,2) instanceof Double) ? ((Double)distanceTable.getValueAt(2,2)).doubleValue() : Double.parseDouble((String)distanceTable.getValueAt(2,2))
336  );
337  distance.findRecord("san-diego", "new-york").setValue(
338  (distanceTable.getValueAt(3,2) instanceof Double) ? ((Double)distanceTable.getValueAt(3,2)).doubleValue() : Double.parseDouble((String)distanceTable.getValueAt(3,2))
339  );
340  distance.findRecord("san-diego", "chicago").setValue(
341  (distanceTable.getValueAt(4,2) instanceof Double) ? ((Double)distanceTable.getValueAt(4,2)).doubleValue() : Double.parseDouble((String)distanceTable.getValueAt(4,2))
342  );
343  distance.findRecord("san-diego", "topeka").setValue(
344  (distanceTable.getValueAt(5,2) instanceof Double) ? ((Double)distanceTable.getValueAt(5,2)).doubleValue() : Double.parseDouble((String)distanceTable.getValueAt(5,2))
345  );
346  GAMSParameter f = db.getParameter("f");
347  f.getFirstRecord().setValue( Double.parseDouble(freightCost.getText()) );
348  }
349 
350  @Override
351  public void actionPerformed(ActionEvent event) {
352  if (event.getSource() == runButton) {
353  runButton.setEnabled(false);
354  try {
355  updateDB(inDB);
356  job = ws.addJobFromString(model);
357  GAMSOptions opt = ws.addOptions();
358  opt.defines("gdxincname", inDB.getName());
359  job.run(opt, printStream, inDB);
360  } catch(Exception e) {
361  e.printStackTrace();
362  } finally {
363  runButton.setEnabled(true);
364  if (job.OutDB() != null) {
365  double roundedValue = Math.round(job.OutDB().getVariable("z").getFirstRecord().getLevel() * 100D) / 100D;
366  transportCost.setText( Double.toString( roundedValue ));
367  int i = 0;
368  for(GAMSVariableRecord rec: job.OutDB().getVariable("x")) {
369  shipmentTableModel.setValueAt(rec.getKey(0), i, 0);
370  shipmentTableModel.setValueAt(rec.getKey(1), i, 1);
371  shipmentTableModel.setValueAt(new Double(rec.getLevel()), i, 2);
372  shipmentTableModel.setValueAt(new Double(rec.getMarginal()), i, 3);
373  i++;
374  }
375  }
376  }
377  } else if (event.getSource() == loadButton) {
378  runButton.setEnabled(true);
379  FileNameExtensionFilter gdxfilter = new FileNameExtensionFilter( "GAMS Data eXchange files (*.gdx)", "gdx");
380  fc.setDialogTitle("Load data from .gdx file");
381  fc.setFileFilter(gdxfilter);
382  int returnVal = fc.showOpenDialog(this);
383  if (returnVal == JFileChooser.APPROVE_OPTION) {
384  File file = fc.getSelectedFile();
385  try {
386  inDB = ws.addDatabaseFromGDX(file.getAbsolutePath());
387  int j = 0;
388  for(GAMSParameterRecord rec : inDB.getParameter("a")) {
389  capacityTableModel.setValueAt(new Double(rec.getValue()), 0, j++);
390  }
391  j = 0;
392  for(GAMSParameterRecord rec : inDB.getParameter("b")) {
393  demandTableModel.setValueAt(new Double(rec.getValue()), 0, j++);
394  }
395  int i = 0;
396  for(GAMSParameterRecord rec : inDB.getParameter("d")) {
397  distanceTableModel.setValueAt(rec.getKey(0), i, 0);
398  distanceTableModel.setValueAt(rec.getKey(1), i, 1);
399  distanceTableModel.setValueAt(new Double(rec.getValue()), i, 2);
400  i++;
401  }
402 
403  transportCost.setText("");
404  for(i=0; i<6; i++) {
405  shipmentTableModel.setValueAt("", i, 0);
406  shipmentTableModel.setValueAt("", i, 1);
407  shipmentTableModel.setValueAt("", i, 2);
408  shipmentTableModel.setValueAt("", i, 3);
409  }
410  System.out.println("--- Data loaded from [" + file.getAbsolutePath() + "].\n");
411  } catch(GAMSException e) {
412  System.out.println("--- Data could not be loaded from [" + file.getAbsolutePath() + "]");
413  System.out.println(e.getMessage());
414  }
415  } else {
416  System.out.println("--- Load Data command cancelled by user.\n");
417  }
418  } else if (event.getSource() == saveButton) {
419  runButton.setEnabled(true);
420  FileNameExtensionFilter gdxfilter = new FileNameExtensionFilter( "GAMS Data eXchange files (*.gdx)", "gdx");
421  fc.setFileFilter(gdxfilter);
422  fc.setDialogTitle("Save Data to .gdx file");
423  int returnVal = fc.showSaveDialog(null);
424  if (returnVal == JFileChooser.APPROVE_OPTION) {
425  File file = fc.getSelectedFile();
426  try {
427  updateDB(inDB);
428  inDB.export(file.getAbsolutePath());
429  System.out.println("--- Data saved to ["+file.getAbsolutePath()+"].\n");
430  } catch(GAMSException e) {
431  System.out.println("--- Data could not be saved to [" + file.getAbsolutePath() + "]\n");
432  System.out.println(e.getMessage());
433  }
434  } else {
435  System.out.println("--- Save Data command cancelled by user.\n");
436  }
437  } else if (event.getSource() == exitButton) {
438  System.out.println("--- Closing Transport GUI Example.\n");
439  System.exit(0);
440  }
441  }
442 
443  static String data =
444  "Sets \n" +
445  " i canning plants / seattle, san-diego / \n" +
446  " j markets / new-york, chicago, topeka / ; \n" +
447  "Parameters \n" +
448  " \n" +
449  " a(i) capacity of plant i in cases \n" +
450  " / seattle 350 \n" +
451  " san-diego 600 / \n" +
452  " \n" +
453  " b(j) demand at market j in cases \n" +
454  " / new-york 325 \n" +
455  " chicago 300 \n" +
456  " topeka 275 / ; \n" +
457  " \n" +
458  "Table d(i,j) distance in thousands of miles \n" +
459  " new-york chicago topeka \n" +
460  " seattle 2.5 1.7 1.8 \n" +
461  " san-diego 2.5 1.8 1.4 ; \n" +
462  " \n" +
463  "Scalar f freight in dollars per case per thousand miles /90/ ; \n ";
464 
465  static String model = "Sets \n" +
466  " i canning plants \n" +
467  " j markets \n" +
468  " \n" +
469  " Parameters \n" +
470  " a(i) capacity of plant i in cases \n" +
471  " b(j) demand at market j in cases \n" +
472  " d(i,j) distance in thousands of miles \n" +
473  " Scalar f freight in dollars per case per thousand miles; \n" +
474  " \n" +
475  "$if not set gdxincname $abort 'no include file name for data file provided'\n" +
476  "$gdxin %gdxincname% \n" +
477  "$load i j a b d f \n" +
478  "$gdxin \n" +
479  " \n" +
480  " Parameter c(i,j) transport cost in thousands of dollars per case ; \n" +
481  " \n" +
482  " c(i,j) = f * d(i,j) / 1000 ; \n" +
483  " \n" +
484  " Variables \n" +
485  " x(i,j) shipment quantities in cases \n" +
486  " z total transportation costs in thousands of dollars ; \n" +
487  " \n" +
488  " Positive Variable x ; \n" +
489  " \n" +
490  " Equations \n" +
491  " \n" +
492  " cost define objective function \n" +
493  " supply(i) observe supply limit at plant i \n" +
494  " demand(j) satisfy demand at market j ; \n" +
495  " \n" +
496  " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n" +
497  " \n" +
498  " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n" +
499  " \n" +
500  " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n" +
501  " \n" +
502  " Model transport /all/ ; \n" +
503  " \n" +
504  " Solve transport using lp minimizing z ; \n" +
505  " \n" +
506  " Display x.l, x.m ; \n" +
507  " \n";
508 }
509 
511 class LogOutputStream extends OutputStream {
512  private JTextArea textArea;
513 
514  public LogOutputStream(JTextArea textArea) {
515  this.textArea = textArea;
516  }
517 
518  @Override
519  public void write(int b) throws IOException {
520  textArea.append(String.valueOf((char)b));
521  textArea.setCaretPosition(textArea.getDocument().getLength());
522  }
523 }
524 
526 class TransportTableModel extends AbstractTableModel {
527  String tableName;
528  String[] columnNames;
529  Object[][] data;
530 
531  public TransportTableModel (GAMSVariable var, String name) {
532  tableName = name;
533  setTableValue(var);
534  }
535 
536  public TransportTableModel (GAMSParameter param, String name) {
537  tableName = name;
538  setTableValue(param);
539  }
540 
541  public void setTableValue(GAMSVariable var) {
542  columnNames = new String[]{ "From", "To", "Quantity", "Shadow Price"};
543  data = new Object[6][4];
544  if (var == null) {
545  for(int i=0; i<6; i++)
546  for(int j=0; j<columnNames.length; j++)
547  data[i][j] = "";
548  } else if (var.getDimension() == 2) {
549  int i =0;
550  for (GAMSVariableRecord rec : var) {
551  setValueAt(rec.getKey(0), i, 0);
552  setValueAt(rec.getKey(1), i, 1);
553  setValueAt(new Double(rec.getLevel()), i, 2);
554  setValueAt(new Double(rec.getMarginal()), i, 3);
555  i++;
556  }
557  }
558  }
559 
560  public void setTableValue(GAMSParameter param) {
561  int j=0;
562  if (param.getDimension() == 1) {
563  columnNames = new String[param.getNumberOfRecords()];
564  data = new Object[1][columnNames.length];
565  for(GAMSParameterRecord rec : param) {
566  columnNames[j] = rec.getKey(0);
567  setValueAt(new Double(rec.getValue()), 0, j);
568  j++;
569  }
570  } else if (param.getName().equals("d")) {
571  columnNames = new String[] { "Plants", "Markets", "Distance" };
572  data = new Object[param.getNumberOfRecords()][3];
573  int i = 0;
574  for(GAMSParameterRecord rec : param){
575  setValueAt(rec.getKey(0), i, 0);
576  setValueAt(rec.getKey(1), i, 1);
577  setValueAt(new Double(rec.getValue()), i, 2);
578  i++;
579  }
580  }
581  }
582 
583  public String getName() { return tableName; }
584  public int getRowCount() { return data.length; }
585  public int getColumnCount() { return columnNames.length; }
586  public String getColumnName(int col) { return columnNames[col]; }
587  public Object getValueAt( int row, int col ) { return data[row][col]; }
588  public boolean isCellEditable(int row, int col) {
589  if ((tableName.equals("Distance:") && (col<2)) )
590  return false;
591  else
592  return true;
593  }
594  public void setValueAt(Object value, int row, int col) {
595  data[row][col] = value;
596  fireTableCellUpdated(row, col);
597  }
598 }
599 
601 class NumberCellRenderer extends DefaultTableCellRenderer {
602  DecimalFormat formatter = new DecimalFormat( "#.00" );
603 
604  @Override
605  public Component getTableCellRendererComponent(JTable jTable, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
606  Component c = super.getTableCellRendererComponent(jTable, value, isSelected, hasFocus, row, column);
607  if (value instanceof Number)
608  value = formatter.format((Number)value);
609  return c;
610  }
611 }
GAMSParameter getParameter(String identifier)
void defines(String defStr, String asStr)
GAMSDatabase addDatabaseFromGDX(String gdxFileName)
GAMSJob addJobFromString(String source)
In this example a transportation is solved.
void setWorkingDirectory(String directory)
GAMSDatabase OutDB()
GAMSVariable getVariable(String identifier)