Ipopt Documentation  
Ipopt.java
Go to the documentation of this file.
1 /* Copyright (C) 2007 VRTech Industrial Technologies - www.vrtech.com.br.
2  * Copyright (C) 2007 Tong Kewei, Beihang University, - www.buaa.edu.cn.
3  * All Rights Reserved.
4  * This code is published under the Eclipse Public License.
5  */
6 
7 package org.coinor;
8 
9 import java.io.File;
10 
44 public abstract class Ipopt
45 {
46  /* Native function should not be used directly */
47  private native boolean AddIpoptIntOption(
48  long ipopt,
49  String keyword,
50  int val
51  );
52 
53  /* Native function should not be used directly */
54  private native boolean AddIpoptNumOption(
55  long ipopt,
56  String keyword,
57  double val
58  );
59 
60  /* Native function should not be used directly */
61  private native boolean AddIpoptStrOption(
62  long ipopt,
63  String keyword,
64  String val
65  );
66 
67  /* Native function should not be used directly */
68  private native long CreateIpoptProblem(
69  int n,
70  int m,
71  int nele_jac,
72  int nele_hess,
73  int index_style
74  );
75 
76  /* Native function should not be used directly */
77  private native void FreeIpoptProblem(
78  long ipopt
79  );
80 
81  /* Native function should not be used directly */
82  private native int OptimizeTNLP(
83  long ipopt,
84  double x[],
85  double g[],
86  double obj_val[],
87  double mult_g[],
88  double mult_x_L[],
89  double mult_x_U[],
90  double callback_grad_f[],
91  double callback_jac_g[],
92  double callback_hess[]
93  );
94 
95  /* Native function should not be used directly */
96  private native boolean GetCurrIterate(
97  long ipopt,
98  long ip_data,
99  long ip_cq,
100  boolean scaled,
101  int n,
102  double x[],
103  double z_L[],
104  double z_U[],
105  int m,
106  double g[],
107  double lambda[]
108  );
109 
110  /* Native function should not be used directly */
111  private native boolean GetCurrViolations(
112  long ipopt,
113  long ip_data,
114  long ip_cq,
115  boolean scaled,
116  int n,
117  double x_L_violation[],
118  double x_U_violation[],
119  double compl_x_L[],
120  double compl_x_U[],
121  double grad_lag_x[],
122  int m,
123  double nlp_constraint_violation[],
124  double compl_g[]
125  );
126 
128  public final static int C_STYLE = 0;
129 
131  public final static int FORTRAN_STYLE = 1;
132 
133  /* The possible Ipopt status return codes: should be kept in sync with Ipopt return codes */
134  public final static int SOLVE_SUCCEEDED = 0;
135  public final static int ACCEPTABLE_LEVEL = 1;
136  public final static int INFEASIBLE_PROBLEM = 2;
137  public final static int SEARCH_DIRECTION_TOO_SMALL = 3;
138  public final static int DIVERGING_ITERATES = 4;
139  public final static int USER_REQUESTED_STOP = 5;
140  public final static int ITERATION_EXCEEDED = -1;
141  public final static int RESTORATION_FAILED = -2;
142  public final static int ERROR_IN_STEP_COMPUTATION = -3;
143  public final static int CPUTIME_EXCEEDED = -4;
144  public final static int WALLTIME_EXCEEDED = -5;
145  public final static int NOT_ENOUGH_DEGREES_OF_FRE = -10;
146  public final static int INVALID_PROBLEM_DEFINITION = -11;
147  public final static int INVALID_OPTION = -12;
148  public final static int INVALID_NUMBER_DETECTED = -13;
149  public final static int UNRECOVERABLE_EXCEPTION = -100;
150  public final static int NON_IPOPT_EXCEPTION = -101;
151  public final static int INSUFFICIENT_MEMORY = -102;
152  public final static int INTERNAL_ERROR = -199;
153 
154  /* The possible algorithm modes (passed to intermediate_callback) */
155  public final static int REGULARMODE = 0;
156  public final static int RESTORATIONPHASEMODE = 1;
157 
159  private long ipopt;
160 
162  private double callback_grad_f[];
163  private double callback_jac_g[];
164  private double callback_hess[];
165 
167  private double x[];
168 
170  private double obj_val[] = {0};
171 
173  private double g[];
174 
176  private double mult_x_L[];
177 
179  private double mult_x_U[];
180 
182  private double mult_g[];
183 
186 
193  public Ipopt()
194  {
195  if( System.getProperty("os.name").toLowerCase().indexOf("win") >= 0 )
196  {
197  /* for Ipopt releases, it should be ipopt-3.dll
198  * for other intermediate versions, it should be ipopt-0.dll
199  * with MinGW, libtool adds a "lib" prefix
200  * finally, try also without version info
201  */
202  final String[] candidates = { "ipopt-3", "ipopt-0", "libipopt-3", "libipopt-0", "ipopt", "libipopt" };
203  boolean loadedlib = false;
204  for( String c : candidates )
205  {
206  try
207  {
208  /* This loads the Ipopt library with RTLD_LOCAL, which means that symbols loaded are not made available for future dlopen() calls.
209  * This causes a problem when using MKL, which loads an additional library at runtime, e.g., libmkl_avx2, because this lib references
210  * to symbols that could be resolved in previously load MKL libraries - but are not because of RTLD_LOCAL.
211  * TODO should one add some kind of workaround to load the Ipopt lib with RTLD_GLOBAL?, e.g.,
212  * https://stackoverflow.com/questions/5425034/java-load-shared-libraries-with-dependencies
213  * https://github.com/victor-paltz/global-load-library
214  */
215  System.loadLibrary(c);
216  loadedlib = true;
217  break;
218  }
219  catch( UnsatisfiedLinkError e )
220  { }
221  }
222  if( !loadedlib )
223  {
224  throw new UnsatisfiedLinkError("Could not load Ipopt library. Check your java.library.path.");
225  }
226  }
227  else
228  {
229  System.loadLibrary("ipopt");
230  }
231  }
232 
239  public Ipopt(
240  String DLL)
241  {
242  // Loads the library
243  System.loadLibrary(DLL);
244  }
245 
252  public Ipopt(
253  String path,
254  String DLL)
255  {
256  // Loads the library
257  File file = new File(path, System.mapLibraryName(DLL));
258  System.load(file.getAbsolutePath());
259  }
260 
281  abstract protected boolean get_bounds_info(
282  int n,
283  double[] x_l,
284  double[] x_u,
285  int m,
286  double[] g_l,
287  double[] g_u
288  );
289 
314  abstract protected boolean get_starting_point(
315  int n,
316  boolean init_x,
317  double[] x,
318  boolean init_z,
319  double[] z_L,
320  double[] z_U,
321  int m,
322  boolean init_lambda,
323  double[] lambda
324  );
325 
337  abstract protected boolean eval_f(
338  int n,
339  double[] x,
340  boolean new_x,
341  double[] obj_value
342  );
343 
355  abstract protected boolean eval_grad_f(
356  int n,
357  double[] x,
358  boolean new_x,
359  double[] grad_f
360  );
361 
372  abstract protected boolean eval_g(
373  int n,
374  double[] x,
375  boolean new_x,
376  int m,
377  double[] g
378  );
379 
408  abstract protected boolean eval_jac_g(
409  int n,
410  double[] x,
411  boolean new_x,
412  int m,
413  int nele_jac,
414  int[] iRow,
415  int[] jCol,
416  double[] values
417  );
418 
451  abstract protected boolean eval_h(
452  int n,
453  double[] x,
454  boolean new_x,
455  double obj_factor,
456  int m,
457  double[] lambda,
458  boolean new_lambda,
459  int nele_hess,
460  int[] iRow,
461  int[] jCol,
462  double[] values
463  );
464 
475  public void dispose()
476  {
477  // dispose the native implementation
478  if( ipopt != 0 )
479  {
481  ipopt = 0;
482  }
483  }
484 
485  @Deprecated
486  protected void finalize() throws Throwable
487  {
488  dispose();
489  }
490 
503  public boolean create(
504  int n,
505  int m,
506  int nele_jac,
507  int nele_hess,
508  int index_style)
509  {
510  // delete any previously created native memory
511  dispose();
512 
513  x = new double[n];
514  g = new double[m];
515 
516  // allocate the callback arguments
517  callback_grad_f = new double[n];
518  callback_jac_g = new double[nele_jac];
519  callback_hess = new double[nele_hess];
520 
521  // the multiplier
522  mult_x_U = new double[n];
523  mult_x_L = new double[n];
524  mult_g = new double[m];
525 
526  // create the optimization problem and return a pointer to it
527  ipopt = CreateIpoptProblem(n, m, nele_jac, nele_hess, index_style);
528 
529  //System.out.println("Finish Java Obj");
530  return ipopt == 0 ? false : true;
531  }
532 
541  public boolean setIntegerOption(
542  String keyword,
543  int val)
544  {
545  if( ipopt == 0 )
546  {
547  return false;
548  }
549 
550  return AddIpoptIntOption(ipopt, keyword, val);
551  }
552 
561  public boolean setNumericOption(
562  String keyword,
563  double val)
564  {
565  if( ipopt == 0 )
566  {
567  return false;
568  }
569 
570  return AddIpoptNumOption(ipopt, keyword, val);
571  }
572 
581  public boolean setStringOption(
582  String keyword,
583  String val)
584  {
585  if( ipopt == 0 )
586  {
587  return false;
588  }
589 
590  return AddIpoptStrOption(ipopt, keyword, val.toLowerCase());
591  }
592 
603  public int OptimizeNLP()
604  {
605  this.status = this.OptimizeTNLP(ipopt,
608 
609  return this.status;
610  }
611 
640  public boolean get_curr_iterate(
641  long ip_data,
642  long ip_cq,
643  boolean scaled,
644  int n,
645  double x[],
646  double z_L[],
647  double z_U[],
648  int m,
649  double g[],
650  double lambda[]
651  )
652  {
653  return this.GetCurrIterate(ipopt, ip_data, ip_cq, scaled, n, x, z_L, z_U, m, g, lambda);
654  }
655 
689  public boolean get_curr_violations(
690  long ip_data,
691  long ip_cq,
692  boolean scaled,
693  int n,
694  double x_L_violation[],
695  double x_U_violation[],
696  double compl_x_L[],
697  double compl_x_U[],
698  double grad_lag_x[],
699  int m,
700  double nlp_constraint_violation[],
701  double compl_g[]
702  )
703  {
704  return this.GetCurrViolations(ipopt, ip_data, ip_cq, scaled, n, x_L_violation, x_U_violation, compl_x_L, compl_x_U, grad_lag_x, m, nlp_constraint_violation, compl_g);
705  }
706 
710  public double[] getVariableValues()
711  {
712  return x;
713  }
714 
718  public double getObjectiveValue()
719  {
720  return obj_val[0];
721  }
722 
728  public int getStatus()
729  {
730  return status;
731  }
732 
736  public double[] getConstraintValues()
737  {
738  return g;
739  }
740 
744  public double[] getConstraintMultipliers()
745  {
746  return mult_g;
747  }
748 
752  public double[] getLowerBoundMultipliers()
753  {
754  return mult_x_L;
755  }
756 
760  public double[] getUpperBoundMultipliers()
761  {
762  return mult_x_U;
763  }
764 
789  public boolean intermediate_callback(
790  int algorithmmode,
791  int iter,
792  double obj_value,
793  double inf_pr,
794  double inf_du,
795  double mu,
796  double d_norm,
797  double regularization_size,
798  double alpha_du,
799  double alpha_pr,
800  int ls_trials,
801  long ip_data,
802  long ip_cq)
803  {
804  return true;
805  }
806 
821  public boolean get_scaling_parameters(
822  double[] obj_scaling,
823  int n,
824  double[] x_scaling,
825  int m,
826  double[] g_scaling,
827  boolean[] use_x_g_scaling)
828  {
829  return false;
830  }
831 
837  {
838  return -1;
839  }
840 
849  int num_nonlin_vars,
850  int[] pos_nonlin_vars)
851  {
852  return false;
853  }
854 }
Ipopt()
Creates a new NLP Solver using a default as the DLL name.
Definition: Ipopt.java:193
double callback_jac_g[]
Definition: Ipopt.java:163
native void FreeIpoptProblem(long ipopt)
static final int RESTORATIONPHASEMODE
Definition: Ipopt.java:156
abstract boolean get_bounds_info(int n, double[] x_l, double[] x_u, int m, double[] g_l, double[] g_u)
Method to request bounds on the variables and constraints.
double mult_x_L[]
Final multipliers for lower variable bounds.
Definition: Ipopt.java:176
static final int INSUFFICIENT_MEMORY
Definition: Ipopt.java:151
static final int DIVERGING_ITERATES
Definition: Ipopt.java:138
int get_number_of_nonlinear_variables()
When LBFGS hessian approximation is used, this method should be overloaded.
Definition: Ipopt.java:836
static final int NOT_ENOUGH_DEGREES_OF_FRE
Definition: Ipopt.java:145
native int OptimizeTNLP(long ipopt, double x[], double g[], double obj_val[], double mult_g[], double mult_x_L[], double mult_x_U[], double callback_grad_f[], double callback_jac_g[], double callback_hess[])
double [] getUpperBoundMultipliers()
Gives dual multipliers for variable upper bounds in final point.
Definition: Ipopt.java:760
static final int USER_REQUESTED_STOP
Definition: Ipopt.java:139
double getObjectiveValue()
Gives objective function value at final point.
Definition: Ipopt.java:718
static final int INVALID_OPTION
Definition: Ipopt.java:147
static final int INTERNAL_ERROR
Definition: Ipopt.java:152
static final int ITERATION_EXCEEDED
Definition: Ipopt.java:140
int status
Status returned by the solver.
Definition: Ipopt.java:185
static final int INFEASIBLE_PROBLEM
Definition: Ipopt.java:136
static final int FORTRAN_STYLE
Use FORTRAN index style for iRow and jCol vectors.
Definition: Ipopt.java:131
abstract boolean get_starting_point(int n, boolean init_x, double[] x, boolean init_z, double[] z_L, double[] z_U, int m, boolean init_lambda, double[] lambda)
Method to request the starting point before iterating.
static final int UNRECOVERABLE_EXCEPTION
Definition: Ipopt.java:149
double mult_x_U[]
Final multipliers for upper variable bounds.
Definition: Ipopt.java:179
native long CreateIpoptProblem(int n, int m, int nele_jac, int nele_hess, int index_style)
static final int WALLTIME_EXCEEDED
Definition: Ipopt.java:144
This file contains a base class for all exceptions and a set of macros to help with exceptions...
double [] getLowerBoundMultipliers()
Gives dual multipliers for variable lower bounds in final point.
Definition: Ipopt.java:752
double [] getConstraintValues()
Gives constraint function values at final point.
Definition: Ipopt.java:736
double obj_val[]
Final value of objective function.
Definition: Ipopt.java:170
static final int RESTORATION_FAILED
Definition: Ipopt.java:141
void finalize()
Definition: Ipopt.java:486
boolean get_curr_iterate(long ip_data, long ip_cq, boolean scaled, int n, double x[], double z_L[], double z_U[], int m, double g[], double lambda[])
Get primal and dual variable values of the current iterate.
Definition: Ipopt.java:640
boolean setIntegerOption(String keyword, int val)
Function for setting an integer option.
Definition: Ipopt.java:541
boolean setStringOption(String keyword, String val)
Function for setting a string option.
Definition: Ipopt.java:581
static final int CPUTIME_EXCEEDED
Definition: Ipopt.java:143
boolean setNumericOption(String keyword, double val)
Function for setting a number option.
Definition: Ipopt.java:561
boolean intermediate_callback(int algorithmmode, int iter, double obj_value, double inf_pr, double inf_du, double mu, double d_norm, double regularization_size, double alpha_du, double alpha_pr, int ls_trials, long ip_data, long ip_cq)
Intermediate Callback method for the user.
Definition: Ipopt.java:789
abstract boolean eval_g(int n, double[] x, boolean new_x, int m, double[] g)
Method to request the constraint values.
Ipopt(String DLL)
Creates a NLP Solver for the given DLL file.
Definition: Ipopt.java:239
boolean create(int n, int m, int nele_jac, int nele_hess, int index_style)
Create a new problem.
Definition: Ipopt.java:503
static final int NON_IPOPT_EXCEPTION
Definition: Ipopt.java:150
static final int ERROR_IN_STEP_COMPUTATION
Definition: Ipopt.java:142
native boolean AddIpoptStrOption(long ipopt, String keyword, String val)
Ipopt(String path, String DLL)
Creates a NLP Solver for the given DLL file and path.
Definition: Ipopt.java:252
double callback_grad_f[]
Callback arguments.
Definition: Ipopt.java:162
native boolean AddIpoptIntOption(long ipopt, String keyword, int val)
static final int SEARCH_DIRECTION_TOO_SMALL
Definition: Ipopt.java:137
abstract boolean eval_jac_g(int n, double[] x, boolean new_x, int m, int nele_jac, int[] iRow, int[] jCol, double[] values)
Method to request either the sparsity structure or the values of the Jacobian of the constraints...
double x[]
Final value of variable values.
Definition: Ipopt.java:167
native boolean GetCurrIterate(long ipopt, long ip_data, long ip_cq, boolean scaled, int n, double x[], double z_L[], double z_U[], int m, double g[], double lambda[])
static final int INVALID_NUMBER_DETECTED
Definition: Ipopt.java:148
boolean get_scaling_parameters(double[] obj_scaling, int n, double[] x_scaling, int m, double[] g_scaling, boolean[] use_x_g_scaling)
If you using_scaling_parameters = true, this method should be overloaded.
Definition: Ipopt.java:821
abstract 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)
Method to request either the sparsity structure or the values of the Hessian of the Lagrangian...
native boolean GetCurrViolations(long ipopt, long ip_data, long ip_cq, boolean scaled, int n, double x_L_violation[], double x_U_violation[], double compl_x_L[], double compl_x_U[], double grad_lag_x[], int m, double nlp_constraint_violation[], double compl_g[])
double g[]
Values of constraint at final point.
Definition: Ipopt.java:173
int OptimizeNLP()
This function actually solve the problem.
Definition: Ipopt.java:603
double [] getVariableValues()
Gives primal variable values at final point.
Definition: Ipopt.java:710
long ipopt
Pointer to the native optimization object.
Definition: Ipopt.java:159
double [] getConstraintMultipliers()
Gives constraint dual multipliers in final point.
Definition: Ipopt.java:744
static final int ACCEPTABLE_LEVEL
Definition: Ipopt.java:135
abstract boolean eval_grad_f(int n, double[] x, boolean new_x, double[] grad_f)
Method to request the gradient of the objective function.
double mult_g[]
Final multipliers for constraints.
Definition: Ipopt.java:182
int getStatus()
Gives Ipopt status of last OptimizeNLP call.
Definition: Ipopt.java:728
boolean get_list_of_nonlinear_variables(int num_nonlin_vars, int[] pos_nonlin_vars)
When LBFGS hessian approximation is used, this method should be overloaded.
Definition: Ipopt.java:848
static final int C_STYLE
Use C index style for iRow and jCol vectors.
Definition: Ipopt.java:128
native boolean AddIpoptNumOption(long ipopt, String keyword, double val)
static final int REGULARMODE
Definition: Ipopt.java:155
void dispose()
Dispose of the natively allocated memory.
Definition: Ipopt.java:475
boolean get_curr_violations(long ip_data, long ip_cq, boolean scaled, int n, double x_L_violation[], double x_U_violation[], double compl_x_L[], double compl_x_U[], double grad_lag_x[], int m, double nlp_constraint_violation[], double compl_g[])
Get primal and dual infeasibility of the current iterate.
Definition: Ipopt.java:689
abstract boolean eval_f(int n, double[] x, boolean new_x, double[] obj_value)
Method to request the value of the objective function.
double callback_hess[]
Definition: Ipopt.java:164
static final int SOLVE_SUCCEEDED
Definition: Ipopt.java:134
static final int INVALID_PROBLEM_DEFINITION
Definition: Ipopt.java:146