Ipopt
trunk
|
00001 00009 package org.coinor.examples; 00010 import org.coinor.Ipopt; 00011 00024 public class HS071 extends Ipopt 00025 { 00026 // Problem sizes 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 /* Number of nonzeros in the Jacobian of the constraints */ 00039 nele_jac = 8; 00040 00041 /* Number of nonzeros in the Hessian of the Lagrangian (lower or 00042 * upper triangual part only) */ 00043 nele_hess = 10; 00044 00045 /* Number of variables */ 00046 n = 4; 00047 00048 /* Number of constraints */ 00049 m = 2; 00050 00051 /* Index style for the irow/jcol elements */ 00052 int index_style = Ipopt.C_STYLE; 00053 00054 /* create the IpoptProblem */ 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 /* set the values of the variable bounds */ 00066 for( int i = 0; i < n; ++i ) 00067 { 00068 x_L[i] = 1.0; 00069 x_U[i] = 5.0; 00070 } 00071 00072 /* set the values of the constraint bounds */ 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 x[0] = 0.9999999923240762; 00097 x[1] = 4.742999641809297; 00098 x[2] = 3.8211499817883072; 00099 x[3] = 1.3794082897556983; 00100 00101 z_L[0] = 1.0878712258676539e+00; 00102 z_L[1] = 0; 00103 z_L[2] = 0; 00104 z_L[3] = 0; 00105 00106 z_U[0] = 0; 00107 z_U[1] = 0; 00108 z_U[2] = 0; 00109 z_U[3] = 0; 00110 00111 lambda[0] = -0.552293195627571; 00112 lambda[1] = 0.16146777361782; 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 /* return the structure of the jacobian */ 00159 00160 /* this particular jacobian is dense */ 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 /* return the values of the jacobian of the constraints */ 00173 00174 values[0] = x[1]*x[2]*x[3]; /* 0,0 */ 00175 values[1] = x[0]*x[2]*x[3]; /* 0,1 */ 00176 values[2] = x[0]*x[1]*x[3]; /* 0,2 */ 00177 values[3] = x[0]*x[1]*x[2]; /* 0,3 */ 00178 00179 values[4] = 2*x[0]; /* 1,0 */ 00180 values[5] = 2*x[1]; /* 1,1 */ 00181 values[6] = 2*x[2]; /* 1,2 */ 00182 values[7] = 2*x[3]; /* 1,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; /* nonzero element counter */ 00194 int row = 0; /* row counter for loop */ 00195 int col = 0; /* col counter for loop */ 00196 if( values == null ) 00197 { 00198 /* return the structure. This is a symmetric matrix, fill the lower left triangle only. */ 00199 00200 /* the hessian for this problem is actually dense */ 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 /* return the values. This is a symmetric matrix, fill the lower left triangle only */ 00218 00219 /* fill the objective portion */ 00220 values[0] = obj_factor * (2*x[3]); /* 0,0 */ 00221 values[1] = obj_factor * (x[3]); /* 1,0 */ 00222 values[2] = 0.0; /* 1,1 */ 00223 values[3] = obj_factor * (x[3]); /* 2,0 */ 00224 values[4] = 0.0; /* 2,1 */ 00225 values[5] = 0.0; /* 2,2 */ 00226 values[6] = obj_factor * (2*x[0] + x[1] + x[2]); /* 3,0 */ 00227 values[7] = obj_factor * (x[0]); /* 3,1 */ 00228 values[8] = obj_factor * (x[0]); /* 3,2 */ 00229 values[9] = 0.0; /* 3,3 */ 00230 00231 /* add the portion for the first constraint */ 00232 values[1] += lambda[0] * (x[2] * x[3]); /* 1,0 */ 00233 values[3] += lambda[0] * (x[1] * x[3]); /* 2,0 */ 00234 values[4] += lambda[0] * (x[0] * x[3]); /* 2,1 */ 00235 values[6] += lambda[0] * (x[1] * x[2]); /* 3,0 */ 00236 values[7] += lambda[0] * (x[0] * x[2]); /* 3,1 */ 00237 values[8] += lambda[0] * (x[0] * x[1]); /* 3,2 */ 00238 00239 /* add the portion for the second constraint */ 00240 values[0] += lambda[1] * 2.0; /* 0,0 */ 00241 values[2] += lambda[1] * 2.0; /* 1,1 */ 00242 values[5] += lambda[1] * 2.0; /* 2,2 */ 00243 values[9] += lambda[1] * 2.0; /* 3,3 */ 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 //Create the problem 00272 HS071 hs071 = new HS071(); 00273 00274 //Set some options 00275 //hs071.setNumericOption("tol",1E-7); 00276 //hs071.setStringOption("nlp_scaling_method","user-scaling"); 00277 //hs071.setStringOption("print_options_documentation","yes"); 00278 //hs071.setStringOption("warm_start_init_point","yes"); 00279 //hs071.setNumericOption("warm_start_bound_push",1e-9); 00280 //hs071.setNumericOption("warm_start_bound_frac",1e-9); 00281 //hs071.setNumericOption("warm_start_slack_bound_frac",1e-9); 00282 //hs071.setNumericOption("warm_start_slack_bound_push",1e-9); 00283 //hs071.setNumericOption("warm_start_mult_bound_push",1e-9); 00284 00285 //Solve the problem 00286 int status = hs071.OptimizeNLP(); 00287 00288 //Print the solution 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 }