Ipopt
trunk
|
00001 00009 package org.coinor; 00010 00011 import java.io.File; 00012 00047 public abstract class Ipopt 00048 { 00050 private native boolean AddIpoptIntOption(long ipopt, String keyword, int val); 00051 00053 private native boolean AddIpoptNumOption(long ipopt, String keyword, double val); 00054 00056 private native boolean AddIpoptStrOption(long ipopt, String keyword, String val); 00057 00059 private native long CreateIpoptProblem(int n,int m, 00060 int nele_jac, int nele_hess, int index_style); 00061 00063 private native void FreeIpoptProblem(long ipopt); 00064 00066 private native int OptimizeTNLP(long ipopt, 00067 double x[], double g[], 00068 double obj_val[], double mult_g[], double mult_x_L[], double mult_x_U[], 00069 double callback_grad_f[], double callback_jac_g[], double callback_hess[]); 00070 00071 00073 public static final String DLLNAME = "jipopt"; 00075 public static final String DLLPATH = "lib"; 00076 00078 public final static int C_STYLE = 0; 00080 public final static int FORTRAN_STYLE = 1; 00081 00083 public final static int SOLVE_SUCCEEDED = 0; 00084 public final static int ACCEPTABLE_LEVEL = 1; 00085 public final static int INFEASIBLE_PROBLEM = 2; 00086 public final static int SEARCH_DIRECTION_TOO_SMALL = 3; 00087 public final static int DIVERGING_ITERATES = 4; 00088 public final static int USER_REQUESTED_STOP = 5; 00089 public final static int ITERATION_EXCEEDED = -1; 00090 public final static int RESTORATION_FAILED = -2; 00091 public final static int ERROR_IN_STEP_COMPUTATION = -3; 00092 public final static int CPUTIME_EXCEEDED = -4; 00093 public final static int NOT_ENOUGH_DEGREES_OF_FRE = -10; 00094 public final static int INVALID_PROBLEM_DEFINITION = -11; 00095 public final static int INVALID_OPTION = -12; 00096 public final static int INVALID_NUMBER_DETECTED = -13; 00097 public final static int UNRECOVERABLE_EXCEPTION = -100; 00098 public final static int NON_IPOPT_EXCEPTION = -101; 00099 public final static int INSUFFICIENT_MEMORY = -102; 00100 public final static int INTERNAL_ERROR = -199; 00101 00103 private long ipopt; 00104 00106 private double callback_grad_f[]; 00107 private double callback_jac_g[]; 00108 private double callback_hess[]; 00109 00111 private double x[]; 00112 00114 private double obj_val[] = {0.0}; 00115 00117 private double g[]; 00118 00120 private double mult_x_L[]; 00121 00123 private double mult_x_U[]; 00124 00126 private double mult_g[]; 00127 00129 private int status = INVALID_PROBLEM_DEFINITION; 00130 00135 public Ipopt() 00136 { 00137 this(DLLPATH, DLLNAME); 00138 } 00139 00148 public Ipopt(String path, String DLL) 00149 { 00150 // Loads the library 00151 File file = new File(path, System.mapLibraryName(DLL)); 00152 System.load(file.getAbsolutePath()); 00153 } 00154 00156 abstract protected boolean get_bounds_info(int n, double[] x_l, double[] x_u, 00157 int m, double[] g_l, double[] g_u); 00158 00160 abstract protected boolean get_starting_point(int n, boolean init_x, double[] x, 00161 boolean init_z, double[] z_L, double[] z_U, 00162 int m, boolean init_lambda, double[] lambda); 00163 00165 abstract protected boolean eval_f(int n, double[] x, boolean new_x, double[] obj_value); 00166 00168 abstract protected boolean eval_grad_f(int n, double[] x, boolean new_x, double[] grad_f); 00169 00171 abstract protected boolean eval_g(int n, double[] x, boolean new_x, int m, double[] g); 00172 00174 abstract protected boolean eval_jac_g(int n, double[] x, boolean new_x, 00175 int m, int nele_jac, int[] iRow, int[] jCol, double[] values); 00176 00178 abstract protected boolean eval_h(int n, double[] x, boolean new_x, double obj_factor, 00179 int m, double[] lambda, boolean new_lambda, 00180 int nele_hess, int[] iRow, int[] jCol, 00181 double[] values); 00182 00192 public void dispose() 00193 { 00194 // dispose the native implementation 00195 if( ipopt != 0 ) 00196 { 00197 FreeIpoptProblem(ipopt); 00198 ipopt = 0; 00199 } 00200 } 00201 00202 protected void finalize() throws Throwable 00203 { 00204 dispose(); 00205 } 00206 00217 public boolean create(int n, int m, int nele_jac, int nele_hess, int index_style) 00218 { 00219 // delete any previously created native memory 00220 dispose(); 00221 00222 x = new double[n]; 00223 // allocate the callback arguments 00224 callback_grad_f = new double[n]; 00225 callback_jac_g = new double[nele_jac]; 00226 callback_hess = new double[nele_hess]; 00227 00228 // the multiplier 00229 mult_x_U = new double[n]; 00230 mult_x_L = new double[n]; 00231 g = new double[m]; 00232 mult_g = new double[m]; 00233 00234 // Create the optimization problem and return a pointer to it 00235 ipopt = CreateIpoptProblem(n, m, nele_jac, nele_hess, index_style); 00236 00237 //System.out.println("Finish Java Obj"); 00238 return ipopt == 0 ? false : true; 00239 } 00240 00249 public boolean setIntegerOption(String keyword, int val) 00250 { 00251 return ipopt == 0 ? false : AddIpoptIntOption(ipopt, keyword, val); 00252 } 00253 00262 public boolean setNumericOption(String keyword, double val) 00263 { 00264 return ipopt == 0 ? false : AddIpoptNumOption(ipopt, keyword, val); 00265 } 00266 00275 public boolean setStringOption(String keyword, String val) 00276 { 00277 return ipopt == 0 ? false : AddIpoptStrOption(ipopt, keyword, val.toLowerCase()); 00278 } 00279 00290 public int OptimizeNLP() 00291 { 00292 this.status = this.OptimizeTNLP(ipopt, 00293 x, g, obj_val, mult_g, mult_x_L, mult_x_U, 00294 callback_grad_f, callback_jac_g, callback_hess); 00295 00296 return this.status; 00297 } 00298 00302 public double[] getVariableValues() 00303 { 00304 return x; 00305 } 00306 00310 public double getObjectiveValue() 00311 { 00312 return obj_val[0]; 00313 } 00314 00320 public int getStatus() 00321 { 00322 return status; 00323 } 00324 00328 public double[] getConstraintValues() 00329 { 00330 return g; 00331 } 00332 00336 public double[] getConstraintMultipliers() 00337 { 00338 return mult_g; 00339 } 00340 00344 public double[] getLowerBoundMultipliers() 00345 { 00346 return mult_x_L; 00347 } 00348 00352 public double[] getUpperBoundMultipliers() 00353 { 00354 return mult_x_U; 00355 } 00356 00371 public boolean get_scaling_parameters(double[] obj_scaling, 00372 int n, double[] x_scaling, 00373 int m, double[] g_scaling, 00374 boolean[] use_x_g_scaling) 00375 { 00376 return false; 00377 } 00378 00383 public int get_number_of_nonlinear_variables() 00384 { 00385 return -1; 00386 } 00387 00395 public boolean get_list_of_nonlinear_variables(int num_nonlin_vars, 00396 int[] pos_nonlin_vars) 00397 { 00398 return false; 00399 } 00400 }