package com.imsl.test.example.math; import com.imsl.math.*; import com.imsl.stat.*; /** *
* Solves a constrained optimization problem using a direct search complex * method. *
** The constrained problem * $$ \min f(x_1,x_2) = 100.0(x_2-x_1^2)^2 + (1.0-x_1)^2 \\ * \text{subject to} \quad * \begin{array}{l} * -2 \le x_1 \le 0.5 \\ * -1 \le x_2 \le 2 * \end{array} $$ * is solved with an initial guess \((-1.2,1.0)\) and the solution is printed. * The analytical solution is \((x_1,x_2) = (0.5,0.25)\) with optimum value * \(0.25\). *
* * @see Code * @see Output */ public class NelderMeadEx2 { public static void main(String args[]) { NelderMead.Function fcn = new NelderMead.Function() { public double f(double x[]) { double fValue; double t1 = x[1] - x[0] * x[0]; double t2 = 1.0 - x[0]; fValue = 100.0 * t1 * t1 + t2 * t2; return fValue; } }; double ftol = 1.0e-7; // The initial guess is used as the first vertex of the random // initial complex. double xguess[] = {-1.2, 1.0}; double xlb[] = {-2.0, -1.0}; double xub[] = {0.5, 2.0}; // Define random object that is used internally in the computation // of the initial complex. Random r = new com.imsl.stat.Random(123457); r.setMultiplier(16807); NelderMead zs = new NelderMead(fcn, xlb, xub); zs.setTolerance(ftol); zs.setRandomObject(r); zs.setGuess(xguess); zs.solve(); double[] sol = zs.getSolution(); double obj = zs.getObjectiveValue(); System.out.println("Solution:"); for (int k = 0; k < sol.length; k++) { System.out.println("x[" + k + "] = " + sol[k]); } System.out.println(); System.out.println("Optimum value: " + obj); } }