Clad.java
1package com.gams.examples.clad;
2
3import java.io.BufferedWriter;
4import java.io.ByteArrayOutputStream;
5import java.io.File;
6import java.io.FileWriter;
7import java.io.PrintStream;
8import java.util.AbstractMap;
9import java.util.ArrayList;
10import java.util.List;
11import java.util.Map;
12
14import com.gams.api.GAMSJob;
15import com.gams.api.GAMSOptions;
18
32public class Clad {
33
34 public static void main(String[] args) {
36 if (args.length > 0)
37 wsInfo.setSystemDirectory( args[0] );
38
39 File workingDirectory = new File(System.getProperty("user.dir"), "Clad");
40 workingDirectory.mkdir();
41 wsInfo.setWorkingDirectory(workingDirectory.getAbsolutePath());
42
43 GAMSWorkspace ws = new GAMSWorkspace(wsInfo);
44
45 List<Map.Entry<Long,String>> steps = new ArrayList<Map.Entry<Long,String>>();
46 steps.add( new AbstractMap.SimpleEntry<Long, String>( new Long(10), "epgap 0.1" ) );
47 steps.add( new AbstractMap.SimpleEntry<Long, String>( new Long(20), "epgap 0.2" ));
48 steps.add( new AbstractMap.SimpleEntry<Long, String>( new Long(30), "epagap 1e9" ));
49
50 GAMSJob job = ws.addJobFromGamsLib("clad");
51
52 try {
53 File logFile = new File(ws.workingDirectory(), "cplex.opt");
54 BufferedWriter outfile = new BufferedWriter(new FileWriter(logFile));
55 outfile.write("interactive 1"); outfile.newLine();
56 outfile.write("iafile cplex.op2"); outfile.newLine();
57 outfile.close();
58 } catch (Exception e) {
59 e.printStackTrace();
60 System.exit(1);
61 }
62
63 GAMSOptions opt = ws.addOptions();
64 opt.setMIP( "cplex" );
65 opt.setOptFile( 1 );
66 opt.setSolveLink( GAMSOptions.ESolveLink.LoadLibrary );
67
68 ByteArrayOutputStream os = new ByteArrayOutputStream();
69 PrintStream ps = new PrintStream(os);
70
71 Worker w = new Worker(job, opt, ps);
72 w.start();
73
74 long prevStep = 0;
75 for (Map.Entry<Long, String> entry : steps) {
76 long diffInSeconds = entry.getKey().longValue() - prevStep ;
77 try {
78 w.join( diffInSeconds * 1000 );
79 } catch( Exception e ) { break; }
80
81 prevStep = entry.getKey().longValue();
82 try {
83 File logFile = new File(ws.workingDirectory(), "cplex.op2");
84 BufferedWriter outfile = new BufferedWriter(new FileWriter(logFile));
85 outfile.write( entry.getValue() ); outfile.newLine();
86 outfile.close();
87 } catch (Exception e) {
88 e.printStackTrace();
89 System.exit(1);
90 }
91 job.interrupt();
92 }
93 if (w.isAlive()) {
94 try {
95 w.join();
96 } catch (InterruptedException e) { }
97 }
98
99 String log = os.toString();
100 if (!log.contains("MIP status(113): aborted")) {
101 System.out.println("Expected the solver to be interrupted at least once.");
102 System.exit(1);
103 }
104 System.exit(0);
105 }
106
108 static class Worker extends Thread {
109 GAMSJob job;
110 PrintStream output;
111 GAMSOptions option;
112
118 public Worker(GAMSJob jb, GAMSOptions opt, PrintStream out) {
119 job = jb;
120 option = opt;
121 output = out;
122 }
123
126 public void run() throws GAMSException {
127 job.run(option, output);
128 }
129 }
130}
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS)...
&#160;
This example demonstrates how to implement a complex termination criterion for a difficult MIP using ...
Definition: Clad.java:32
void setSolveLink(GAMSOptions.ESolveLink x)
void setSystemDirectory(String directory)
GAMSJob addJobFromGamsLib(String modelName)
void setWorkingDirectory(String directory)