00001
00009 package org.coinor.examples;
00010 import org.coinor.Ipopt;
00011
00024 public class HS071 extends Ipopt
00025 {
00026
00027 int n;
00028 int m;
00029 int nele_jac;
00030 int nele_hess;
00031
00032 int count_bounds = 0;
00033 int dcount_start = 0;
00034
00036 public HS071()
00037 {
00038
00039 nele_jac = 8;
00040
00041
00042
00043 nele_hess = 10;
00044
00045
00046 n = 4;
00047
00048
00049 m = 2;
00050
00051
00052 int index_style = Ipopt.C_STYLE;
00053
00054
00055 create(n, m, nele_jac, nele_hess, index_style);
00056 }
00057
00059 protected boolean get_bounds_info(int n, double[] x_L, double[] x_U,
00060 int m, double[] g_L, double[] g_U)
00061 {
00062 assert n == this.n;
00063 assert m == this.m;
00064
00065
00066 for( int i = 0; i < n; ++i )
00067 {
00068 x_L[i] = 1.0;
00069 x_U[i] = 5.0;
00070 }
00071
00072
00073 g_L[0] = 25.0; g_U[0] = 2e19;
00074 g_L[1] = 40.0; g_U[1] = 40.0;
00075
00076 return true;
00077 }
00078
00080 protected boolean get_starting_point(int n, boolean init_x, double[] x,
00081 boolean init_z, double[] z_L, double[] z_U,
00082 int m, boolean init_lambda,double[] lambda)
00083 {
00084 assert init_z == false;
00085 assert init_lambda = false;
00086
00087 if( init_x )
00088 {
00089 x[0] = 1.0;
00090 x[1] = 5.0;
00091 x[2] = 5.0;
00092 x[3] = 1.0;
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 return true;
00116 }
00117
00118 protected boolean eval_f(int n, double[] x, boolean new_x, double[] obj_value)
00119 {
00120 assert n == this.n;
00121
00122 obj_value[0] = x[0] * x[3] * (x[0] + x[1] + x[2]) + x[2];
00123
00124 return true;
00125 }
00126
00127 protected boolean eval_grad_f(int n, double[] x, boolean new_x, double[] grad_f)
00128 {
00129 assert n == this.n;
00130
00131 grad_f[0] = x[0] * x[3] + x[3] * (x[0] + x[1] + x[2]);
00132 grad_f[1] = x[0] * x[3];
00133 grad_f[2] = x[0] * x[3] + 1;
00134 grad_f[3] = x[0] * (x[0] + x[1] + x[2]);
00135
00136 return true;
00137 }
00138
00139 protected boolean eval_g(int n, double[] x, boolean new_x, int m, double[] g)
00140 {
00141 assert n == this.n;
00142 assert m == this.m;
00143
00144 g[0] = x[0] * x[1] * x[2] * x[3];
00145 g[1] = x[0]*x[0] + x[1]*x[1] + x[2]*x[2] + x[3]*x[3];
00146
00147 return true;
00148 }
00149
00150 protected boolean eval_jac_g(int n, double[] x, boolean new_x,
00151 int m, int nele_jac, int[] iRow, int[] jCol, double[] values)
00152 {
00153 assert n == this.n;
00154 assert m == this.m;
00155
00156 if( values == null )
00157 {
00158
00159
00160
00161 iRow[0] = 0; jCol[0] = 0;
00162 iRow[1] = 0; jCol[1] = 1;
00163 iRow[2] = 0; jCol[2] = 2;
00164 iRow[3] = 0; jCol[3] = 3;
00165 iRow[4] = 1; jCol[4] = 0;
00166 iRow[5] = 1; jCol[5] = 1;
00167 iRow[6] = 1; jCol[6] = 2;
00168 iRow[7] = 1; jCol[7] = 3;
00169 }
00170 else
00171 {
00172
00173
00174 values[0] = x[1]*x[2]*x[3];
00175 values[1] = x[0]*x[2]*x[3];
00176 values[2] = x[0]*x[1]*x[3];
00177 values[3] = x[0]*x[1]*x[2];
00178
00179 values[4] = 2*x[0];
00180 values[5] = 2*x[1];
00181 values[6] = 2*x[2];
00182 values[7] = 2*x[3];
00183 }
00184
00185 return true;
00186 }
00187
00188 protected boolean eval_h(int n, double[] x, boolean new_x, double obj_factor, int m, double[] lambda, boolean new_lambda, int nele_hess, int[] iRow, int[] jCol, double[] values)
00189 {
00190 assert n == this.n;
00191 assert m == this.m;
00192
00193 int idx = 0;
00194 int row = 0;
00195 int col = 0;
00196 if( values == null )
00197 {
00198
00199
00200
00201 idx = 0;
00202 for( row = 0; row < n; ++row )
00203 {
00204 for( col = 0; col <= row; ++col)
00205 {
00206 iRow[idx] = row;
00207 jCol[idx] = col;
00208 idx++;
00209 }
00210 }
00211
00212 assert idx == nele_hess;
00213 assert nele_hess == this.nele_hess;
00214 }
00215 else
00216 {
00217
00218
00219
00220 values[0] = obj_factor * (2*x[3]);
00221 values[1] = obj_factor * (x[3]);
00222 values[2] = 0.0;
00223 values[3] = obj_factor * (x[3]);
00224 values[4] = 0.0;
00225 values[5] = 0.0;
00226 values[6] = obj_factor * (2*x[0] + x[1] + x[2]);
00227 values[7] = obj_factor * (x[0]);
00228 values[8] = obj_factor * (x[0]);
00229 values[9] = 0.0;
00230
00231
00232 values[1] += lambda[0] * (x[2] * x[3]);
00233 values[3] += lambda[0] * (x[1] * x[3]);
00234 values[4] += lambda[0] * (x[0] * x[3]);
00235 values[6] += lambda[0] * (x[1] * x[2]);
00236 values[7] += lambda[0] * (x[0] * x[2]);
00237 values[8] += lambda[0] * (x[0] * x[1]);
00238
00239
00240 values[0] += lambda[1] * 2.0;
00241 values[2] += lambda[1] * 2.0;
00242 values[5] += lambda[1] * 2.0;
00243 values[9] += lambda[1] * 2.0;
00244 }
00245
00246 return true;
00247 }
00248
00249 public boolean get_scaling_parameters(double[] obj_scaling,
00250 int n, double[] x_scaling,
00251 int m, double[] g_scaling,
00252 boolean[] use_x_g_scaling)
00253 {
00254 return false;
00255 }
00256
00257
00258 public void print(double[] x, String str)
00259 {
00260 System.out.println(str);
00261 for( int i = 0; i < x.length; ++i )
00262 System.out.println(x[i]);
00263 System.out.println();
00264 }
00265
00269 public static void main(String []args)
00270 {
00271
00272 HS071 hs071 = new HS071();
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 int status = hs071.OptimizeNLP();
00287
00288
00289 if( status == SOLVE_SUCCEEDED )
00290 System.out.println("\n\n*** The problem solved!");
00291 else
00292 System.out.println("\n\n*** The problem was not solved successfully!");
00293
00294 double obj = hs071.getObjectiveValue();
00295 System.out.println("\nObjective Value = " + obj + "\n");
00296
00297 double x[] = hs071.getVariableValues();
00298 hs071.print(x, "Primal Variable Values:");
00299
00300 double constraints[] = hs071.getConstraintValues();
00301 hs071.print(constraints, "Constraint Values:");
00302
00303 double MLB[] = hs071.getLowerBoundMultipliers();
00304 hs071.print(MLB, "Dual Multipliers for Variable Lower Bounds:");
00305
00306 double MUB[] = hs071.getUpperBoundMultipliers();
00307 hs071.print(MUB, "Dual Multipliers for Variable Upper Bounds:");
00308
00309 double lam[] = hs071.getConstraintMultipliers();
00310 hs071.print(lam, "Dual Multipliers for Constraints:");
00311 }
00312 }