Example 3: Solving a general nonlinear programming problem with logging

A general nonlinear programming problem is solved using a finite difference gradient. Intermediate output is captured in a file named MinConNLPlog.txt. The level of output requested is FINE. 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.

import com.imsl.math.*;
import java.util.logging.Logger;
import java.util.logging.Level;
import java.util.logging.Handler;

public class MinConNLPEx3 implements MinConNLP.Function{
    
    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.};
        double  x[] = {0.};
        MinConNLP minconnon = new MinConNLP(m, me, n);
        minconnon.setGuess(xinit);
        MinConNLPEx3 fcn = new MinConNLPEx3();
        Logger logger = minconnon.getLogger();
        Handler h = new java.util.logging.FileHandler("MinConNLPlog.txt");
        logger.addHandler(h);
        logger.setLevel(Level.FINE);
        h.setFormatter(new MinConNLP.Formatter());
        x = minconnon.solve(fcn);
        System.out.println("x is "+x[0] +" "+x[1]);
    }
}

Output

x is 0.8228756555325116 0.9114378277662559
Contents of the file MinConNLPlog.txt after execution:


  ITSTEP= 1  FX= 0.0   UPSI= 5.0  B2N=-1.0  UMI= 0.0  NR= 2  SI= -1   


  ITSTEP= 2  FX= 0.47222222222222204   UPSI= 0.8055555555555558  B2N=7.447602459741819E-16  UMI= 0.0  NR= 2  SI= -1   


  ITSTEP= 3  FX= 1.2261822533163689   UPSI= 0.09653353175869195  B2N=3.3306690738754696E-16  UMI= 0.0  NR= 2  SI= -1   


  ITSTEP= 4  FX= 1.393242278445973   UPSI= 1.2061157826948055E-4  B2N=1.336885555457667E-15  UMI= 0.0  NR= 2  SI= -1   

     N= 2     M= 2    ME= 1

  EPSX= 1.0E-5     SIGSM= 1.4901161193847656E-8

STARTVALUE
 0.02.0

   EPS= 2.220446049250313E-16   TOL= 2.2250738585072014E-308   DEL0= 0.5   DELM= 5.0E-7  TAU0= 1.0 
   TAU= 0.1   SD= 0.1   SW= 5.4782007307014466E-33   RHO= 1.0E-6   RHO1=1.0E-10
 SCFM= 10000.0   C1D= 0.01   EPDI= 2.220446049250313E-16
  NRE= 2  ANAL= false
 VBND= 1.0   EFCN= 2.220446049250313E-16   DIFF= 1
TERMINATION REASON:
KT-CONDITIONS SATISFIED, NO FURTHER CORRECTION COMPUTED
EVALUATIONS OF F                                    18
EVALUATIONS OF GRAD F                                0
EVALUATIONS OF CONSTRAINTS                          48
EVALUATIONS OF GRADS OF CONSTRAINTS                  0
FINAL SCALING OF OBJECTIVE                          1.0
NORM OF GRAD(F)                                     2.360902457120518
LAGRANGIAN VIOLATION                                9.992007221626409E-16
FEASIBILITY VIOLATION                               2.866595849582154E-13
DUAL FEASIBILITY VIOLATION                          0.0
OPTIMIZER RUNTIME SEC S

OPTIMAL VALUE OF F = 1.3934649806887736

OPTIMAL SOLUTION X = 
0.8228756555325116   0.9114378277662559
MULTIPLIERS ARE RELATIVE TO SCF=1
NR.     CONSTRAINT          NORMGRAD (OR 1)     MULTIPLIER
 1  -2.220446049250313E-16  2.23606797749979  -1.5944911588359063
 2  -2.864375403532904E-13  1.8687312653198707  1.8465915320074269
EVALUATIONS OF RESTRICTIONS AND THEIR GRADIENTS
 ( 24.0, 0.0 )
 ( 24.0, 0.0 )
LAST ESTIMATE OF CONDITION OF ACTIVE GRADIENTS  1.958467797854007
LAST ESTIMATE OF CONDITION OF APPROX. HESSIAN    1.3588763739672172
 ITERATIVE STEPS TOTAL          4
 # OF RESTARTS                  0
 # OF FULL REGULAR UPDATES      3
 # OF UPDATES                   3
 # OF FULL REGULARIZED SQP-STEPS 0
  FX= 1  SCF= 5.0  PSI= 1.8687312653198707  UPS= 1.8465915320074269
 DEL= 5.0E-5  B20= 0.0  B2N= -1.0  NR= 2
      SI= -1            U-= 0.0  C-R= 1.5365907428821477  C-D= 1.0
      XN= 2.8284271247461903  DN= 1.0671873729054746  PHA= -1            CL= 0
      SKM= 0.0  SIG= 1.0  CF+= 0.0          DIR= -5.0
      DSC= 0.0  COS= 1.0  VIO= 0.0
      UPD= 0           TK= 0.0  XSI= 0.0
  FX= 2  SCF= 0.8055555555555558  PSI= 0.0  UPS= 0
 DEL= 0.05  B20= 0.0  B2N= 7.447602459741819E-16  NR= 2
      SI= -1            U-= 0.0  C-R= 1.4798927762262672  C-D= 1.0
      XN= 1.7716909687891085  DN= 0.49125734684608885  PHA= 1            CL= 1
      SKM= 1.4727272299765986  SIG= 1.0  CF+= 1.0          DIR= -0.6737373565183514
      DSC= 1.4727272299765986  COS= 1.0  VIO= 0.9079593845004515
      UPD= 1           TK= 0.24133378083025844  XSI= 0.0
  FX= 3  SCF= 0.09653353175869195  PSI= 0.0  UPS= 0
 DEL= 0.05  B20= 0.0  B2N= 3.3306690738754696E-16  NR= 2
      SI= -1            U-= 0.0  C-R= 1.9355267257931226  C-D= 1.4591929871177434
      XN= 1.302259296758884  DN= 0.07742644541830818  PHA= 1            CL= 1
      SKM= 3.4500000422411627  SIG= 1.0  CF+= 2.0          DIR= -0.17617369749845635
      DSC= 3.4500000422411627  COS= 1.0  VIO= 1.0000000000000002
      UPD= 1           TK= 0.005994854450114255  XSI= 0.0
  FX= 4  SCF= 1.2061157826948055E-4  PSI= 0.0  UPS= 0
 DEL= 0.05  B20= 0.0  B2N= 1.336885555457667E-15  NR= 2
      SI= -1            U-= 0.0  C-R= 1.958467797854007  C-D= 1.3588763739672172
      XN= 1.2280376253662906  DN= 1.0192836585976224E-4  PHA= 2            CL= 1
      SKM= 3.892584026079591  SIG= 1.0  CF+= 2.0          DIR= -2.468065092929623E-4
      DSC= 3.892584026079591  COS= 1.0  VIO= 1.0000000000000002
      UPD= 1           TK= 1.0389391766841544E-8  XSI= 0.0
Link to Java source.