Loading...
Searching...
No Matches
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(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 opt.setThreads( 1 );
69
70 ByteArrayOutputStream os = new ByteArrayOutputStream();
71 PrintStream ps = new PrintStream(os);
72
73 Worker w = new Worker(job, opt, ps);
74 w.start();
75
76 long prevStep = 0;
77 for (Map.Entry<Long, String> entry : steps) {
78 long diffInSeconds = entry.getKey().longValue() - prevStep ;
79 try {
80 System.out.println("** waiting "+ diffInSeconds * 1000+ " milliseconds");
81 w.join( diffInSeconds * 1000 );
82 } catch( Exception e ) {
83 System.out.println("** Exception has been caught : "+e);
84 break;
85 }
86
87 prevStep = entry.getKey().longValue();
88 try {
89 File logFile = new File(ws.workingDirectory(), "cplex.op2");
90 BufferedWriter outfile = new BufferedWriter(new FileWriter(logFile));
91 outfile.write( entry.getValue() ); outfile.newLine();
92 outfile.close();
93 } catch (Exception e) {
94 e.printStackTrace();
95 System.exit(1);
96 }
97 job.interrupt();
98 System.out.println("** Interrupted Cplex to continue with new option: " + entry.getValue());
99 }
100 if (w.isAlive()) {
101 try {
102 w.join();
103 } catch (InterruptedException e) { }
104 }
105
106 String log = os.toString();
107 if (!log.contains("Interrupted...")) {
108 System.out.println("** Solver log **");
109 System.out.println(log);
110 System.out.println();
111 System.out.println("** Expected the solver to be interrupted at least once.");
112 System.exit(1);
113 }
114 System.out.println("** Interrupted...");
115 System.exit(0);
116 }
117
119 static class Worker extends Thread {
120 GAMSJob job;
121 PrintStream output;
122 GAMSOptions option;
123
129 public Worker(GAMSJob jb, GAMSOptions opt, PrintStream out) {
130 job = jb;
131 option = opt;
132 output = out;
133 }
134
137 public void run() throws GAMSException {
138 job.run(option, output);
139 }
140 }
141}
void setSolveLink(GAMSOptions.ESolveLink x)
void setSystemDirectory(String directory)
void setWorkingDirectory(String directory)
GAMSJob addJobFromGamsLib(String modelName)
This example demonstrates how to implement a complex termination criterion for a difficult MIP using ...
Definition: Clad.java:32
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).
&#160;