|
| 1 | +import com.nag.routines.E04.E04RA; |
| 2 | +import com.nag.routines.E04.E04RH; |
| 3 | +import com.nag.routines.E04.E04RG; |
| 4 | +import com.nag.routines.E04.E04ZM; |
| 5 | +import com.nag.routines.E04.E04KF; |
| 6 | +import com.nag.routines.E04.E04KFU; |
| 7 | +import com.nag.routines.E04.E04RX; |
| 8 | +import com.nag.routines.E04.E04RZ; |
| 9 | +import com.nag.routines.X04.X04AC; |
| 10 | + |
| 11 | +import java.lang.Math; |
| 12 | + |
| 13 | +/** |
| 14 | + * E04KF example program text. |
| 15 | + */ |
| 16 | +public class E04KFJE { |
| 17 | + |
| 18 | + private static final int NMONIT = 9; |
| 19 | + private static final int NVAR = 2; |
| 20 | + |
| 21 | + public static void main(String[] args) { |
| 22 | + |
| 23 | + long cpuser = 0; |
| 24 | + int nu = 2 * NVAR; |
| 25 | + |
| 26 | + /* Header */ |
| 27 | + System.out.println(" E04KFJ Example Program Results\n"); |
| 28 | + |
| 29 | + // Define filename for monitoring output |
| 30 | + X04AC x04ac = new X04AC(); |
| 31 | + int ifail = 0; |
| 32 | + x04ac.eval(NMONIT, "e04kffe.mon", 1, ifail); |
| 33 | + |
| 34 | + // Initialize handle |
| 35 | + E04RA e04ra = new E04RA(); |
| 36 | + long handle = 0; |
| 37 | + ifail = 0; |
| 38 | + e04ra.eval(handle, NVAR, ifail); |
| 39 | + handle = e04ra.getHANDLE(); |
| 40 | + |
| 41 | + // Define initial guess point |
| 42 | + double[] x = new double[] { -1.5, 1.9 }; |
| 43 | + |
| 44 | + // Define Simple box bounds on X |
| 45 | + E04RH e04rh = new E04RH(); |
| 46 | + double[] blx = new double[] { -1.0, -2.0 }; |
| 47 | + double[] bux = new double[] { 0.8, 2.0 }; |
| 48 | + ifail = 0; |
| 49 | + e04rh.eval(handle, NVAR, blx, bux, ifail); |
| 50 | + |
| 51 | + // Add nonlinear objective information |
| 52 | + E04RG e04rg = new E04RG(); |
| 53 | + int iidx[] = new int[NVAR]; |
| 54 | + for (int i = 0; i < NVAR; i++) |
| 55 | + iidx[i] = i + 1; |
| 56 | + ifail = 0; |
| 57 | + e04rg.eval(handle, NVAR, iidx, ifail); |
| 58 | + |
| 59 | + // Add options |
| 60 | + E04ZM e04zm = new E04ZM(); |
| 61 | + ifail = 0; |
| 62 | + e04zm.eval(handle, "FOAS Print Frequency = 5", ifail); |
| 63 | + |
| 64 | + ifail = 0; |
| 65 | + e04zm.eval(handle, "Print Solution = yes", ifail); |
| 66 | + |
| 67 | + ifail = 0; |
| 68 | + e04zm.eval(handle, "Print Level = 1", ifail); |
| 69 | + |
| 70 | + ifail = 0; |
| 71 | + e04zm.eval(handle, "Monitoring File = " + NMONIT, ifail); |
| 72 | + |
| 73 | + ifail = 0; |
| 74 | + e04zm.eval(handle, "Monitoring Level = 3", ifail); |
| 75 | + |
| 76 | + // Solve the problem |
| 77 | + E04KF e04kf = new E04KF(); |
| 78 | + OBJFUN objfun = new OBJFUN(); |
| 79 | + OBJGRD objgrd = new OBJGRD(); |
| 80 | + MONIT monit = new MONIT(); |
| 81 | + double[] rinfo = new double[100]; |
| 82 | + double[] stats = new double[100]; |
| 83 | + int[] iuser = new int[0]; |
| 84 | + double[] ruser = new double[0]; |
| 85 | + ifail = -1; |
| 86 | + e04kf.eval(handle, objfun, objgrd, monit, NVAR, x, rinfo, stats, iuser, ruser, cpuser, ifail); |
| 87 | + |
| 88 | + ifail = e04kf.getIFAIL(); |
| 89 | + |
| 90 | + // Print objective value at solution |
| 91 | + if ((ifail == 0) || (ifail == 50)) { |
| 92 | + System.out.printf("\n\n Solution found:\n Objective function value at solution: %9.1E\n", rinfo[0]); |
| 93 | + // Retrieve Lagrange multipliers (FDX) |
| 94 | + E04RX e04rx = new E04RX(); |
| 95 | + double[] u = new double[nu]; |
| 96 | + e04rx.eval(handle, "U", 1, nu, u, ifail); |
| 97 | + if (ifail == 0) { |
| 98 | + System.out.printf(" Gradient at solution: %9.1E %9.1E\n\n", u[0] - u[1], u[2] - u[3]); |
| 99 | + System.out.printf(" Estimated Lagrange multipliers: blx %9.1E %9.1E\n", u[0], u[2]); |
| 100 | + System.out.printf(" Estimated Lagrange multipliers: bux %9.1E %9.1E\n", u[1], u[3]); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + System.out.println(); |
| 105 | + |
| 106 | + // Clean up |
| 107 | + E04RZ e04rz = new E04RZ(); |
| 108 | + ifail = 0; |
| 109 | + e04rz.eval(handle, ifail); |
| 110 | + } |
| 111 | + |
| 112 | + public static class OBJFUN extends E04KF.Abstract_E04KF_OBJFUN { |
| 113 | + public void eval() { |
| 114 | + // Rosenbrock function |
| 115 | + this.FX = Math.pow(1.0 - this.X[0], 2) + 100.0 * Math.pow(this.X[1] - Math.pow(this.X[0], 2), 2); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public static class OBJGRD extends E04KF.Abstract_E04KF_OBJGRD { |
| 120 | + public void eval() { |
| 121 | + this.FDX[0] = 2.0 * this.X[0] - 400.0 * this.X[0] * (this.X[1] - Math.pow(this.X[0], 2)) - 2.0; |
| 122 | + this.FDX[1] = 200.0 * (this.X[1] - Math.pow(this.X[0], 2)); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + public static class MONIT extends E04KF.Abstract_E04KF_MONIT { |
| 127 | + public void eval() { |
| 128 | + E04KFU e04kfu = new E04KFU(); |
| 129 | + e04kfu.eval(this.NVAR, this.X, this.INFORM, this.RINFO, this.STATS, this.IUSER, this.RUSER, this.CPUSER); |
| 130 | + this.INFORM = e04kfu.getINFORM(); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments