// run the job public void run() { try { runX(); } catch (Exception e) { System.err.println(e.toString()); } }
// run optimzer public void optimize(RTContext ctxt, OptimResults r, OptimCallbacks cbs) throws Xcept { // initialization res = r; args = res.args; this.cbs = cbs; if (Util.hasNaNs(args.xmin) || Util.hasNaNs(args.xmax)) throw new Xcept(this, "xmin & xmax required"); // xstart defaults to min+max/2 for (int i = 0; i < args.nx(); i++) if (Double.isNaN(args.xstart[i])) args.xstart[i] = (args.xmax[i] + args.xmin[i]) / 2; // results and work area allocation int n = args.nx(); int[] nfout = new int[2]; double[] tlout = new double[2]; int[] istat = new int[1]; double[] p = new double[n * (n + 1)]; double[] y = new double[n + 1]; int[] fnums = new int[n + 1]; double[] pbar = new double[n]; double[] pstar = new double[n]; double[] pdstar = new double[n]; // call native method (fortran) try { int threadInx = (ctxt == null) ? 0 : ctxt.threadInx; simplx( threadInx, ctxt, n, // int != jint, must calloc args.xstart, args.xmin, args.xmax, args.xistep, new double[] {args.stepTol}, // saves calloc args.maxCalls, // int != jint, must calloc new double[] {args.errTol}, // saves calloc res.bestX, nfout, tlout, istat, p, y, fnums, pbar, pstar, pdstar); // get simplex specific results res.status = istat[0]; res.finalStep = tlout[1]; switch (istat[0]) { case 4: res.status = OptimResults.ERROR; if (Util.isBlank(res.termMsg)) res.termMsg = "Optimiation canceled by user"; break; case 3: res.status = OptimResults.NORMAL; res.termMsg = "Met # calls stopping criterion"; break; case 2: res.status = OptimResults.NORMAL; res.termMsg = "All vertices have same func value"; break; case 1: res.status = OptimResults.NORMAL; res.termMsg = "Met step size stopping criterion"; break; case 0: res.status = OptimResults.NORMAL; res.termMsg = "Met mean sqr error stopping criterion"; break; case -1: res.status = OptimResults.ERROR; res.termMsg = "Nonsensical parameters supplied"; break; case -2: res.status = OptimResults.CANCEL; res.termMsg = "Error midway through optimization"; break; case -3: res.status = OptimResults.ERROR; res.termMsg = "Error during first func evaluation"; break; case -4: res.status = OptimResults.ERROR; res.termMsg = "Canceled during first func evaluation"; break; default: res.status = OptimResults.ERROR; res.termMsg = "Undocumented error code (" + istat[0] + ")"; break; } // pretty up any errors } catch (Exception e) { if (e instanceof Xcept) { Xcept xe = (Xcept) e; res.termMsg = xe.cleanMessage(); throw xe; } else { res.termMsg = e.getMessage(); throw new Xcept(this, res.termMsg); } } if (res.status == OptimResults.ERROR) throw new Xcept("Simplex optimizer: " + res.termMsg); }