package com.imsl.test.example.math; import com.imsl.math.*; /** *

* Solves a nonlinear programming problem using a finite * difference gradient.

* * A general nonlinear programming problem is solved using a finite difference * gradient. The problem *

* $${\rm {min}} \,\, F(x) = (x_1 - 2)^2 + (x_2 - 1)^2$$

*

* subject to

* *

* $$g_1(x) = x_1 - 2x_2 + 1 = 0$$
* $$g_2(x) = -x_1^2 / 4 - x_2^2 + 1 \ge 0$$

*

* is solved.

* * @see Code * @see Output */ public class MinConNLPEx1 implements MinConNLP.Function { /** * Defines the objective function and constraints. * *

* This function is called by MinConNLP. *

* * * @param x a double array containing the variable values * @param iact an int specifying the return value. If * iact=0 this function returns the objective function * evaluated at x. If iact=\(1, 2, 3,...\), this * function returns the constraint with that index evaluated at * x. * @param ierr a boolean array of length 1, where * ierr[0]=false when no error or undesirable condition occurs * during evaluation, and ierr[0]=true indicates some issue * during evaluation * @return a double the value specified by iact * */ @Override public double f(double[] x, int iact, boolean[] ierr) { double result; ierr[0] = false; if (iact == 0) { result = (x[0] - 2.e0) * (x[0] - 2.e0) + (x[1] - 1.e0) * (x[1] - 1.e0); return result; } else { switch (iact) { case 1: result = (x[0] - 2.e0 * x[1] + 1.e0); return result; case 2: result = (-(x[0] * x[0]) / 4.e0 - (x[1] * x[1]) + 1.e0); return result; default: ierr[0] = true; return 0.e0; } } } public static void main(String args[]) throws Exception { int m = 2; int me = 1; int n = 2; double xinit[] = {2., 2.}; MinConNLP minconnon = new MinConNLP(m, me, n); minconnon.setGuess(xinit); MinConNLPEx1 fcn = new MinConNLPEx1(); double[] x = minconnon.solve(fcn); System.out.println("x is " + x[0] + " " + x[1]); } }