Options.java
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package examples;
00026
00027 import static org.gecode.Gecode.*;
00028 import static org.gecode.GecodeEnumConstants.*;
00029
00030 import org.gecode.*;
00031 import org.gecode.gist.Gist;
00032 import org.gecode.gist.GistEventListener;
00033
00038 public class Options {
00039 public boolean
00040 gui = true,
00041 bab = false,
00042 naive = true,
00043 print = true;
00044 public int
00045 a_d = (int)Gecode.getDefault_a_d(),
00046 c_d = (int)Gecode.getDefault_c_d(),
00047 solutions = 1,
00048 iterations = 1,
00049 samples = 1,
00050 size = 1,
00051 timelimit = -1,
00052 faillimit = -1;
00053 public IntConLevel
00054 icl = ICL_DEF;
00055 public String name;
00056 public Mode mode = Mode.solution;
00057 public GistEventListener gel = null;
00058
00059 public Options() {}
00060
00061 public Options(String n) {
00062 name = n;
00063 }
00064
00065 public void parse(String[] args) {
00066 parse(args, -1);
00067 }
00068
00069 public void parse(String[] args, int sizemax) {
00070 int i = 0;
00071 try {
00072 while (i < args.length) {
00073 if (args[i].equalsIgnoreCase("-help") ||
00074 args[i].equalsIgnoreCase("--help")) {
00075 String modeline = "(";
00076 Mode[] ms = Mode.values();
00077 for (int j = 0; j < ms.length; ++j) {
00078 modeline += ms[j];
00079 if (j != ms.length-1) modeline += " ";
00080 }
00081 modeline += ") default: " + mode;
00082 System.out.println(
00083 "Options for example " + name + "\n" +
00084 "\t-gui default: " + gui + "\n" +
00085 "\t\t use the Gist for search\n" +
00086 "\t-nogui default: " + (!gui) + "\n" +
00087 "\t\tdo not use the Gist for search\n" +
00088 "\t-mode " + modeline + "\n" +
00089 "\t\tprint solutions or measure time\n"+
00090 "\t\t(applied when no gui is used)\n" +
00091 "\t-solutions (int) default: " +
00092 solutions + "\n" +
00093 "\t\tnumber of solutions\n" +
00094 "\t-iterations (int) default: " +
00095 iterations + "\n" +
00096 "\t\tnumber of iterations\n" +
00097 "\t-samples (int) default: " + samples + "\n" +
00098 "\t\tnumber of samples\n" +
00099 "\t(int)\n" +
00100 "\t-size (int) default: " + size + "\n" +
00101 "\t\tsize/instance of problem\n" +
00102 "\t-print (boolean) default: " + print+"\n"+
00103 "\t\tprint the solutions\n" +
00104 "\t-a_d (int) default: " + a_d + "\n" +
00105 "\t-c_d (int) default: " + c_d + "\n" +
00106 "\t\trecomputation distances\n" +
00107 "\t-bab (boolean) default: " + bab + "\n" +
00108 "\t\tuse optimizing search\n" +
00109 "\t-naive default: " + naive + "\n" +
00110 "\t\tuse naive model\n" +
00111 "\t-smart default: " + (!naive) + "\n" +
00112 "\t\tuse smart model\n" +
00113 "\t-icl (dom bnd val def) default: " +
00114 getCL(icl) + "\n" +
00115 "\t\twhich consistency level to us\ne" +
00116 "\t-time (int) default: " +
00117 timelimit + "\n" +
00118 "\t\tMaximum time to spend searching\n" +
00119 "\t-fails (int) default: " +
00120 faillimit + "\n" +
00121 "\t\tMaximum number of fails during search"
00122 );
00123 System.exit(0);
00124 } else if (args[i].equalsIgnoreCase("-gui")) {
00125 gui = true;
00126 } else if (args[i].equalsIgnoreCase("-nogui")) {
00127 gui = false;
00128 } else if (args[i].equalsIgnoreCase("-mode")) {
00129 mode = Mode.valueOf(args[++i].toLowerCase());
00130 } else if (args[i].equalsIgnoreCase("-solutions")) {
00131 solutions = Integer.parseInt(args[++i]);
00132 } else if (args[i].equalsIgnoreCase("-iterations")) {
00133 iterations = Integer.parseInt(args[++i]);
00134 } else if (args[i].equalsIgnoreCase("-samples")) {
00135 samples = Integer.parseInt(args[++i]);
00136 } else if (args[i].equalsIgnoreCase("-size")) {
00137 size = Integer.parseInt(args[++i]);
00138 } else if (args[i].equalsIgnoreCase("-print")) {
00139 print = Boolean.parseBoolean(args[++i]);
00140 } else if (args[i].equalsIgnoreCase("-a_d")) {
00141 a_d = Integer.parseInt(args[++i]);
00142 } else if (args[i].equalsIgnoreCase("-c_d")) {
00143 c_d = Integer.parseInt(args[++i]);
00144 } else if (args[i].equalsIgnoreCase("-bab")) {
00145 bab = Boolean.parseBoolean(args[++i]);
00146 } else if (args[i].equalsIgnoreCase("-naive")) {
00147 naive = true;
00148 } else if (args[i].equalsIgnoreCase("-smart")) {
00149 naive = false;
00150 } else if (args[i].equalsIgnoreCase("-icl")) {
00151 icl = getCL(args[++i]);
00152 } else if (args[i].equalsIgnoreCase("-time")) {
00153 timelimit = Integer.parseInt(args[++i]);
00154 } else if (args[i].equalsIgnoreCase("-fails")) {
00155 faillimit = Integer.parseInt(args[++i]);
00156 } else {
00157 try {
00158 size = Integer.parseInt(args[i]);
00159 } catch (NumberFormatException nfe) {
00160 System.err.println("Unrecognized option: " + args[i] +
00161 "\nUse -help for a list of options.");
00162 System.exit(1);
00163 }
00164 }
00165 ++i;
00166 }
00167 } catch(Exception ex) {
00168 System.err.println("Erroneous argument for " + args[i-1]);
00169 System.exit(1);
00170 }
00171
00172 if (sizemax > 0)
00173 if (size < 0 || size >= sizemax) {
00174 System.err.println("Size must be between 0 and " + (sizemax-1));
00175 System.exit(1);
00176 }
00177 }
00178
00181 public void doSearch(Space g) {
00182 doSearch(g, FTStop.create(faillimit, timelimit));
00183 }
00184
00190 public void doSearch(Space g, Stop stop) {
00191 if (g.getName() == "") {
00192 g.setName(name);
00193 }
00194
00195 if (!gui) {
00196 long startTime, time, nsols;
00197 switch (mode) {
00198 case solution:
00199 Statistics stat = null;
00200 nsols = solutions;
00201 startTime = System.nanoTime();
00202 if (bab) {
00203 BABSearch search = new BABSearch(g,a_d,c_d, stop);
00204 Space sol = (Space)search.next();
00205 while (sol != null) {
00206 if (print)
00207 System.out.println(sol.toString());
00208 if (--nsols == 0) break;
00209 sol = (Space)search.next();
00210 }
00211 stat = search.statistics();
00212 } else {
00213 DFSSearch search = new DFSSearch(g,a_d,c_d, stop);
00214 Space sol = (Space)search.next();
00215 while (sol != null) {
00216 if (print)
00217 System.out.println(sol.toString());
00218 if (--nsols == 0) break;
00219 sol = (Space)search.next();
00220 }
00221 stat = search.statistics();
00222 }
00223 time = System.nanoTime() - startTime;
00224 System.out.println("Summary:" +
00225 "\n\truntime: " + (time/1000000) +
00226 "\n\tsolutions: " + (solutions-nsols));
00227 if (stat != null) {
00228 System.out.println(" \tpropagations: " + stat.getPropagate() +
00229 "\n\tfailures: " + stat.getFail() +
00230 "\n\tclones: " + stat.getClone() +
00231 "\n\tcommits: " + stat.getCommit() +
00232 "\n\tpeak memory: " +
00233 ((stat.getMemory()+1023)/1024) + "KB");
00234 }
00235 break;
00236 case time:
00237 case timenogc:
00238 double[] ts = new double[samples];
00239 for (int sit = samples; sit-->0; ) {
00240 if (mode == Mode.timenogc)
00241 for (int k = 0; k < 3; ++k) {
00242 System.gc();
00243 System.runFinalization();
00244 System.gc();
00245 try {
00246 Thread.currentThread().sleep(50);
00247 } catch (Exception ex) {}
00248 }
00249 startTime = System.nanoTime();
00250 for (int kit = iterations; kit-->0; ) {
00251 nsols = solutions;
00252 if (bab) {
00253 BABSearch search = new BABSearch(g,a_d,c_d, stop);
00254 Space sol = (Space)search.next();
00255 while (sol != null) {
00256 if (--nsols == 0) break;
00257 sol = (Space)search.next();
00258 }
00259 } else {
00260 DFSSearch search = new DFSSearch(g,a_d,c_d, stop);
00261 Space sol = (Space)search.next();
00262 while (sol != null) {
00263 if (--nsols == 0) break;
00264 sol = (Space)search.next();
00265 }
00266 }
00267 }
00268 time = System.nanoTime() - startTime;
00269 ts[sit] = (((double)time)/iterations)/1000000.0;
00270 }
00271
00272 double m = arithmean(ts);
00273 double d = stddev(ts) * 100.0;
00274 System.out.printf(java.util.Locale.US,
00275 "\tRuntime: %.6fms (%.2f%% deviation)\n", m, d);
00276 break;
00277 }
00278 } else {
00279 Gist gist = new Gist(g, bab);
00280 if (gel != null)
00281 gist.addEventListener(gel);
00282 if (solutions <= 0) {
00283 gist.exploreAll();
00284 } else if (solutions > 0) {
00285 while (solutions-- != 0)
00286 gist.exploreOne();
00287 }
00288 }
00289 }
00290
00291
00292 double arithmean(double[] t) {
00293 if (t.length == 0) return 0.0;
00294 double res = 0.0;
00295 for (int i = t.length; i-->0; )
00296 res+= t[i];
00297 return res / t.length;
00298 }
00299
00300 double stddev(double[] t) {
00301 if (t.length < 2) return 0.0;
00302 double am = arithmean(t);
00303 double s = 0.0;
00304 for (int i = t.length; i-->0; ) {
00305 double d = t[i] - am;
00306 s += d*d;
00307 }
00308 return Math.sqrt(s / (t.length-1)) / am;
00309 }
00310
00311
00312 protected IntConLevel getCL(String icl) {
00313 if (icl.equalsIgnoreCase("dom"))
00314 return ICL_DOM;
00315 if (icl.equalsIgnoreCase("bnd"))
00316 return ICL_BND;
00317 if (icl.equalsIgnoreCase("val"))
00318 return ICL_VAL;
00319 if (icl.equalsIgnoreCase("def"))
00320 return ICL_DEF;
00321 throw new RuntimeException();
00322 }
00323
00324 protected String getCL(IntConLevel icl) {
00325 switch(icl) {
00326 case ICL_DOM: return "dom";
00327 case ICL_BND: return "bnd";
00328 case ICL_VAL: return "val";
00329 case ICL_DEF: return "def";
00330 }
00331 throw new RuntimeException();
00332 }
00333
00334
00335 enum Mode {solution, time, timenogc};
00336 }
00337
00338 class FTStop extends Stop {
00339 private TimeStop ts;
00340 private FailStop fs;
00341 private FTStop(int fails, int time) {
00342 ts = new TimeStop(time);
00343 fs = new FailStop(fails);
00344 }
00345 public boolean stop(Statistics s) {
00346 return fs.stop(s) || ts.stop(s);
00347 }
00348 public static Stop create(int fails, int time) {
00349 if (fails < 0 && time < 0) return null;
00350 if (fails < 0) return new TimeStop( time);
00351 if (time < 0) return new FailStop(fails);
00352 return new FTStop(fails, time);
00353 }
00354 }