Clad.java
1package com.gams.examples.clad;
2 
3 import java.io.BufferedWriter;
4 import java.io.ByteArrayOutputStream;
5 import java.io.File;
6 import java.io.FileWriter;
7 import java.io.PrintStream;
8 import java.util.AbstractMap;
9 import java.util.ArrayList;
10 import java.util.List;
11 import java.util.Map;
12 
13 import com.gams.api.GAMSException;
14 import com.gams.api.GAMSJob;
15 import com.gams.api.GAMSOptions;
16 import com.gams.api.GAMSWorkspace;
18 
32 public class Clad {
33 
34  public static void main(String[] args) {
35  GAMSWorkspaceInfo wsInfo = new GAMSWorkspaceInfo();
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(5), "epgap 0.1" ) );
47  steps.add( new AbstractMap.SimpleEntry<Long, String>( new Long(10), "epgap 0.2" ));
48  steps.add( new AbstractMap.SimpleEntry<Long, String>( new Long(20), "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("epgap 0"); outfile.newLine();
56  outfile.write("interactive 1"); outfile.newLine();
57  outfile.write("iafile cplex.op2"); outfile.newLine();
58  outfile.close();
59  } catch (Exception e) {
60  e.printStackTrace();
61  System.exit(1);
62  }
63 
64  GAMSOptions opt = ws.addOptions();
65  opt.setMIP( "cplex" );
66  opt.setOptFile( 1 );
67  opt.setSolveLink( GAMSOptions.ESolveLink.LoadLibrary );
68 
69  ByteArrayOutputStream os = new ByteArrayOutputStream();
70  PrintStream ps = new PrintStream(os);
71 
72  Worker w = new Worker(job, opt, ps);
73  w.start();
74 
75  long prevStep = 0;
76  for (Map.Entry<Long, String> entry : steps) {
77  long diffInSeconds = entry.getKey().longValue() - prevStep ;
78  try {
79  System.out.println("** waiting "+ diffInSeconds * 1000+ " milliseconds");
80  w.join( diffInSeconds * 1000 );
81  } catch( Exception e ) {
82  System.out.println("** Exception has been caught : "+e);
83  break;
84  }
85 
86  prevStep = entry.getKey().longValue();
87  try {
88  File logFile = new File(ws.workingDirectory(), "cplex.op2");
89  BufferedWriter outfile = new BufferedWriter(new FileWriter(logFile));
90  outfile.write( entry.getValue() ); outfile.newLine();
91  outfile.close();
92  } catch (Exception e) {
93  e.printStackTrace();
94  System.exit(1);
95  }
96  job.interrupt();
97  System.out.println("** Interrupted Cplex to continue with new option: " + entry.getValue());
98  }
99  if (w.isAlive()) {
100  try {
101  w.join();
102  } catch (InterruptedException e) { }
103  }
104 
105  String log = os.toString();
106  if (!log.contains("Interrupted...")) {
107  System.out.println("** Solver log **");
108  System.out.println(log);
109  System.out.println();
110  System.out.println("** Expected the solver to be interrupted at least once.");
111  System.exit(1);
112  }
113  System.out.println("** Interrupted...");
114  System.exit(0);
115  }
116 
118  static class Worker extends Thread {
119  GAMSJob job;
120  PrintStream output;
121  GAMSOptions option;
122 
128  public Worker(GAMSJob jb, GAMSOptions opt, PrintStream out) {
129  job = jb;
130  option = opt;
131  output = out;
132  }
133 
136  public void run() throws GAMSException {
137  job.run(option, output);
138  }
139  }
140 }
void setSystemDirectory(String directory)
void setSolveLink(GAMSOptions.ESolveLink x)
GAMSJob addJobFromGamsLib(String modelName)
void setWorkingDirectory(String directory)
This example demonstrates how to implement a complex termination criterion for a difficult MIP using ...
Definition: Clad.java:32