1package com.gams.examples.transport;
3import java.awt.Component;
4import java.awt.ComponentOrientation;
6import java.awt.GridBagConstraints;
7import java.awt.GridBagLayout;
9import java.awt.event.ActionEvent;
10import java.awt.event.ActionListener;
12import java.io.IOException;
13import java.io.OutputStream;
14import java.io.PrintStream;
15import java.text.DecimalFormat;
17import javax.swing.BorderFactory;
18import javax.swing.Box;
19import javax.swing.BoxLayout;
20import javax.swing.JButton;
21import javax.swing.JFileChooser;
22import javax.swing.JFrame;
23import javax.swing.JLabel;
24import javax.swing.JPanel;
25import javax.swing.JScrollPane;
26import javax.swing.JTabbedPane;
27import javax.swing.JTable;
28import javax.swing.JTextArea;
29import javax.swing.JTextField;
30import javax.swing.SwingConstants;
31import javax.swing.SwingUtilities;
32import javax.swing.border.EmptyBorder;
33import javax.swing.filechooser.FileNameExtensionFilter;
34import javax.swing.table.AbstractTableModel;
35import javax.swing.table.DefaultTableCellRenderer;
36import javax.swing.table.TableColumnModel;
54public 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();
66 private PrintStream printStream;
68 public static void main(String[] args) {
69 SwingUtilities.invokeLater(
new Runnable() {
77 super(
"Transport - GAMS Java API Example");
78 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
82 File workingDirectory =
new File(System.getProperty(
"user.dir"),
"TransportGUI");
83 workingDirectory.mkdir();
93 JPanel dataPane =
new JPanel();
94 dataPane.setLayout(
new GridBagLayout());
95 dataPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
96 dataPane.setBorder(BorderFactory.createTitledBorder(
"Data"));
98 getContentPane().setLayout(
new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
99 getContentPane().add(Box.createHorizontalGlue());
100 getContentPane().add(dataPane);
101 getContentPane().add(Box.createHorizontalGlue());
103 constraints =
new GridBagConstraints();
104 constraints.insets =
new Insets(5, 10, 5, 10);
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);
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);
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);
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);
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);
151 columnModel = demandTable.getColumnModel();
152 for(
int i=0; i<3; i++)
153 columnModel.getColumn(i).setCellRenderer(renderer);
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);
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);
174 columnModel = distanceTable.getColumnModel();
175 columnModel.getColumn(2).setCellRenderer(renderer);
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);
184 freightCost =
new JTextField(8);
185 freightCost.setHorizontalAlignment(SwingConstants.RIGHT);
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);
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 );
203 printStream =
new PrintStream(
new LogOutputStream(logTextArea));
204 System.setOut(printStream);
205 System.setErr(printStream);
207 logScrollPanel =
new JScrollPane(logTextArea);
208 logScrollPanel.setMinimumSize(logTextArea.getPreferredSize());
209 logScrollPanel.setAutoscrolls(
true);
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));
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);
224 saveButton = makeButton(
"Save Data");
225 saveButton.setToolTipText(
"Save Data to gdx file");
226 constraints.gridx = 1;
227 runPane.add(saveButton, constraints);
229 runButton = makeButton(
"Run");
230 runButton.setToolTipText(
"Run Transport Model");
231 constraints.gridx = 2;
232 runPane.add(runButton, constraints);
234 exitButton = makeButton(
"Exit");
235 runButton.setToolTipText(
"Exit Transport Example");
236 constraints.gridx = 3;
237 runPane.add(exitButton, constraints);
238 getContentPane().add(runPane);
241 JPanel outputPane =
new JPanel();
242 outputPane.setBorder(BorderFactory.createTitledBorder(
"Output"));
243 getContentPane().add(outputPane);
245 JTabbedPane resultPane =
new JTabbedPane();
247 JPanel resultInnerPane =
new JPanel();
248 resultInnerPane.setLayout(
new GridBagLayout());
249 resultInnerPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
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);
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);
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);
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);
283 columnModel = shipmentTable.getColumnModel();
284 for(
int i=2; i<4; i++)
285 columnModel.getColumn(i).setCellRenderer(renderer);
287 resultPane.setAutoscrolls(
true);
288 resultPane.addTab(
"Results", resultInnerPane);
289 resultPane.addTab(
"log",logScrollPanel);
291 outputPane.add(resultPane);
292 outputPane.add(Box.createHorizontalGlue());
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);
312 (capacityTable.getValueAt(0,0) instanceof Double) ? ((Double)capacityTable.getValueAt(0,0)).doubleValue() : Double.parseDouble((String)capacityTable.getValueAt(0,0))
315 (capacityTable.getValueAt(0,1) instanceof Double) ? ((Double)capacityTable.getValueAt(0,1)).doubleValue() : Double.parseDouble((String)capacityTable.getValueAt(0,1))
319 (demandTable.getValueAt(0,0) instanceof Double) ? ((Double)demandTable.getValueAt(0,0)).doubleValue() : Double.parseDouble((String)demandTable.getValueAt(0,0))
322 (demandTable.getValueAt(0,1) instanceof Double) ? ((Double)demandTable.getValueAt(0,1)).doubleValue() : Double.parseDouble((String)demandTable.getValueAt(0,1))
325 (demandTable.getValueAt(0,2) instanceof Double) ? ((Double)demandTable.getValueAt(0,2)).doubleValue() : Double.parseDouble((String)demandTable.getValueAt(0,2))
329 (distanceTable.getValueAt(0,2) instanceof Double) ? ((Double)distanceTable.getValueAt(0,2)).doubleValue() : Double.parseDouble((String)distanceTable.getValueAt(0,2))
332 (distanceTable.getValueAt(1,2) instanceof Double) ? ((Double)distanceTable.getValueAt(1,2)).doubleValue() : Double.parseDouble((String)distanceTable.getValueAt(1,2))
335 (distanceTable.getValueAt(2,2) instanceof Double) ? ((Double)distanceTable.getValueAt(2,2)).doubleValue() : Double.parseDouble((String)distanceTable.getValueAt(2,2))
338 (distanceTable.getValueAt(3,2) instanceof Double) ? ((Double)distanceTable.getValueAt(3,2)).doubleValue() : Double.parseDouble((String)distanceTable.getValueAt(3,2))
341 (distanceTable.getValueAt(4,2) instanceof Double) ? ((Double)distanceTable.getValueAt(4,2)).doubleValue() : Double.parseDouble((String)distanceTable.getValueAt(4,2))
344 (distanceTable.getValueAt(5,2) instanceof Double) ? ((Double)distanceTable.getValueAt(5,2)).doubleValue() : Double.parseDouble((String)distanceTable.getValueAt(5,2))
351 public void actionPerformed(ActionEvent event) {
352 if (event.getSource() == runButton) {
353 runButton.setEnabled(
false);
359 job.
run(opt, printStream, inDB);
360 }
catch(Exception e) {
363 runButton.setEnabled(
true);
364 if (job.
OutDB() !=
null) {
366 transportCost.setText( Double.toString( roundedValue ));
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);
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();
389 capacityTableModel.setValueAt(
new Double(rec.getValue()), 0, j++);
393 demandTableModel.setValueAt(
new Double(rec.getValue()), 0, j++);
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);
403 transportCost.setText(
"");
405 shipmentTableModel.setValueAt(
"", i, 0);
406 shipmentTableModel.setValueAt(
"", i, 1);
407 shipmentTableModel.setValueAt(
"", i, 2);
408 shipmentTableModel.setValueAt(
"", i, 3);
410 System.out.println(
"--- Data loaded from [" + file.getAbsolutePath() +
"].\n");
412 System.out.println(
"--- Data could not be loaded from [" + file.getAbsolutePath() +
"]");
416 System.out.println(
"--- Load Data command cancelled by user.\n");
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();
428 inDB.
export(file.getAbsolutePath());
429 System.out.println(
"--- Data saved to ["+file.getAbsolutePath()+
"].\n");
431 System.out.println(
"--- Data could not be saved to [" + file.getAbsolutePath() +
"]\n");
435 System.out.println(
"--- Save Data command cancelled by user.\n");
437 }
else if (event.getSource() == exitButton) {
438 System.out.println(
"--- Closing Transport GUI Example.\n");
445 " i canning plants / seattle, san-diego / \n" +
446 " j markets / new-york, chicago, topeka / ; \n" +
449 " a(i) capacity of plant i in cases \n" +
450 " / seattle 350 \n" +
451 " san-diego 600 / \n" +
453 " b(j) demand at market j in cases \n" +
454 " / new-york 325 \n" +
456 " topeka 275 / ; \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" +
463 "Scalar f freight in dollars per case per thousand miles /90/ ; \n ";
465 static String model =
"Sets \n" +
466 " i canning plants \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" +
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" +
480 " Parameter c(i,j) transport cost in thousands of dollars per case ; \n" +
482 " c(i,j) = f * d(i,j) / 1000 ; \n" +
485 " x(i,j) shipment quantities in cases \n" +
486 " z total transportation costs in thousands of dollars ; \n" +
488 " Positive Variable x ; \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" +
496 " cost .. z =e= sum((i,j), c(i,j)*x(i,j)) ; \n" +
498 " supply(i) .. sum(j, x(i,j)) =l= a(i) ; \n" +
500 " demand(j) .. sum(i, x(i,j)) =g= b(j) ; \n" +
502 " Model transport /all/ ; \n" +
504 " Solve transport using lp minimizing z ; \n" +
506 " Display x.l, x.m ; \n" +
511class LogOutputStream
extends OutputStream {
512 private JTextArea textArea;
514 public LogOutputStream(JTextArea textArea) {
515 this.textArea = textArea;
519 public void write(
int b)
throws IOException {
520 textArea.append(String.valueOf((
char)b));
521 textArea.setCaretPosition(textArea.getDocument().getLength());
526class TransportTableModel
extends AbstractTableModel {
528 String[] columnNames;
531 public TransportTableModel (GAMSVariable var, String name) {
536 public TransportTableModel (GAMSParameter param, String name) {
538 setTableValue(param);
541 public void setTableValue(GAMSVariable var) {
542 columnNames =
new String[]{
"From",
"To",
"Quantity",
"Shadow Price"};
543 data =
new Object[6][4];
545 for(
int i=0; i<6; i++)
546 for(
int j=0; j<columnNames.length; j++)
548 }
else if (var.getDimension() == 2) {
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);
560 public void setTableValue(GAMSParameter param) {
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);
570 }
else if (param.getName().equals(
"d")) {
571 columnNames =
new String[] {
"Plants",
"Markets",
"Distance" };
572 data =
new Object[param.getNumberOfRecords()][3];
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);
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)) )
594 public void setValueAt(Object value,
int row,
int col) {
595 data[row][col] = value;
596 fireTableCellUpdated(row, col);
601class NumberCellRenderer
extends DefaultTableCellRenderer {
602 DecimalFormat formatter =
new DecimalFormat(
"#.00" );
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);
GAMSParameter getParameter(String identifier)
GAMSVariable getVariable(String identifier)
void defines(String defStr, String asStr)
void setValue(double value)
T findRecord(String ... keys)
void setWorkingDirectory(String directory)
GAMSDatabase addDatabaseFromGDX(String gdxFileName)
GAMSJob addJobFromString(String source)
In this example a transportation is solved.
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).