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 
195  public Ipopt()
196  {
197  if( System.getProperty("os.name").toLowerCase().indexOf("win") >= 0 )
198  {
199  /* for Ipopt releases, it should be ipopt-3.dll
200  * for other intermediate versions, it should be ipopt-0.dll
201  * with MinGW, libtool adds a "lib" prefix
202  * finally, try also without version info
203  */
204  final String[] candidates = { "ipopt-3", "ipopt-0", "libipopt-3", "libipopt-0", "ipopt", "libipopt" };
205  boolean loadedlib = false;
206  for( String c : candidates )
207  {
208  try
209  {
210  /* This loads the Ipopt library with RTLD_LOCAL, which means that symbols loaded are not made available for future dlopen() calls.
211  * This causes a problem when using MKL, which loads an additional library at runtime, e.g., libmkl_avx2, because this lib references
212  * to symbols that could be resolved in previously load MKL libraries - but are not because of RTLD_LOCAL.
213  * TODO should one add some kind of workaround to load the Ipopt lib with RTLD_GLOBAL?, e.g.,
214  * https://stackoverflow.com/questions/5425034/java-load-shared-libraries-with-dependencies
215  * https://github.com/victor-paltz/global-load-library
216  */
217  System.loadLibrary(c);
218  loadedlib = true;
219  break;
220  }
221  catch( UnsatisfiedLinkError e )
222  { }
223  }
224  if( !loadedlib )
225  {
226  throw new UnsatisfiedLinkError("Could not load Ipopt library. Check your java.library.path.");
227  }
228  }
229  else
230  {
231  System.loadLibrary("ipopt");
232  }
233  }
234 
243  public Ipopt(
244  String DLL)
245  {
246  // Loads the library
247  System.loadLibrary(DLL);
248  }
249 
258  public Ipopt(
259  String path,
260  String DLL)
261  {
262  // Loads the library
263  File file = new File(path, System.mapLibraryName(DLL));
264  System.load(file.getAbsolutePath());
265  }
266 
267 
288  abstract protected boolean get_bounds_info(
289  int n,
290  double[] x_l,
291  double[] x_u,
292  int m,
293  double[] g_l,
294  double[] g_u
295  );
296 
321  abstract protected boolean get_starting_point(
322  int n,
323  boolean init_x,
324  double[] x,
325  boolean init_z,
326  double[] z_L,
327  double[] z_U,
328  int m,
329  boolean init_lambda,
330  double[] lambda
331  );
332 
344  abstract protected boolean eval_f(
345  int n,
346  double[] x,
347  boolean new_x,
348  double[] obj_value
349  );
350 
362  abstract protected boolean eval_grad_f(
363  int n,
364  double[] x,
365  boolean new_x,
366  double[] grad_f
367  );
368 
379  abstract protected boolean eval_g(
380  int n,
381  double[] x,
382  boolean new_x,
383  int m,
384  double[] g
385  );
386 
415  abstract protected boolean eval_jac_g(
416  int n,
417  double[] x,
418  boolean new_x,
419  int m,
420  int nele_jac,
421  int[] iRow,
422  int[] jCol,
423  double[] values
424  );
425 
426 
459  abstract protected boolean eval_h(
460  int n,
461  double[] x,
462  boolean new_x,
463  double obj_factor,
464  int m,
465  double[] lambda,
466  boolean new_lambda,
467  int nele_hess,
468  int[] iRow,
469  int[] jCol,
470  double[] values
471  );
472 
483  public void dispose()
484  {
485  // dispose the native implementation
486  if( ipopt != 0 )
487  {
489  ipopt = 0;
490  }
491  }
492 
493  @Deprecated
494  protected void finalize() throws Throwable
495  {
496  dispose();
497  }
498 
511  public boolean create(
512  int n,
513  int m,
514  int nele_jac,
515  int nele_hess,
516  int index_style)
517  {
518  // delete any previously created native memory
519  dispose();
520 
521  x = new double[n];
522  g = new double[m];
523 
524  // allocate the callback arguments
525  callback_grad_f = new double[n];
526  callback_jac_g = new double[nele_jac];
527  callback_hess = new double[nele_hess];
528 
529  // the multiplier
530  mult_x_U = new double[n];
531  mult_x_L = new double[n];
532  mult_g = new double[m];
533 
534  // create the optimization problem and return a pointer to it
535  ipopt = CreateIpoptProblem(n, m, nele_jac, nele_hess, index_style);
536 
537  //System.out.println("Finish Java Obj");
538  return ipopt == 0 ? false : true;
539  }
540 
549  public boolean setIntegerOption(
550  String keyword,
551  int val)
552  {
553  if( ipopt == 0 )
554  {
555  return false;
556  }
557 
558  return AddIpoptIntOption(ipopt, keyword, val);
559  }
560 
569  public boolean setNumericOption(
570  String keyword,
571  double val)
572  {
573  if( ipopt == 0 )
574  {
575  return false;
576  }
577 
578  return AddIpoptNumOption(ipopt, keyword, val);
579  }
580 
589  public boolean setStringOption(
590  String keyword,
591  String val)
592  {
593  if( ipopt == 0 )
594  {
595  return false;
596  }
597 
598  return AddIpoptStrOption(ipopt, keyword, val.toLowerCase());
599  }
600 
611  public int OptimizeNLP()
612  {
613  this.status = this.OptimizeTNLP(ipopt,
616 
617  return this.status;
618  }
619 
648  public boolean get_curr_iterate(
649  long ip_data,
650  long ip_cq,
651  boolean scaled,
652  int n,
653  double x[],
654  double z_L[],
655  double z_U[],
656  int m,
657  double g[],
658  double lambda[]
659  )
660  {
661  return this.GetCurrIterate(ipopt, ip_data, ip_cq, scaled, n, x, z_L, z_U, m, g, lambda);
662  }
663 
697  public boolean get_curr_violations(
698  long ip_data,
699  long ip_cq,
700  boolean scaled,
701  int n,
702  double x_L_violation[],
703  double x_U_violation[],
704  double compl_x_L[],
705  double compl_x_U[],
706  double grad_lag_x[],
707  int m,
708  double nlp_constraint_violation[],
709  double compl_g[]
710  )
711  {
712  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);
713  }
714 
718  public double[] getVariableValues()
719  {
720  return x;
721  }
722 
726  public double getObjectiveValue()
727  {
728  return obj_val[0];
729  }
730 
736  public int getStatus()
737  {
738  return status;
739  }
740 
744  public double[] getConstraintValues()
745  {
746  return g;
747  }
748 
752  public double[] getConstraintMultipliers()
753  {
754  return mult_g;
755  }
756 
760  public double[] getLowerBoundMultipliers()
761  {
762  return mult_x_L;
763  }
764 
768  public double[] getUpperBoundMultipliers()
769  {
770  return mult_x_U;
771  }
772 
797  public boolean intermediate_callback(
798  int algorithmmode,
799  int iter,
800  double obj_value,
801  double inf_pr,
802  double inf_du,
803  double mu,
804  double d_norm,
805  double regularization_size,
806  double alpha_du,
807  double alpha_pr,
808  int ls_trials,
809  long ip_data,
810  long ip_cq)
811  {
812  return true;
813  }
814 
829  public boolean get_scaling_parameters(
830  double[] obj_scaling,
831  int n,
832  double[] x_scaling,
833  int m,
834  double[] g_scaling,
835  boolean[] use_x_g_scaling)
836  {
837  return false;
838  }
839 
845  {
846  return -1;
847  }
848 
857  int num_nonlin_vars,
858  int[] pos_nonlin_vars)
859  {
860  return false;
861  }
862 }
double[] getVariableValues()
Gives primal variable values at final point.
Definition: Ipopt.java:718
static final int WALLTIME_EXCEEDED
Definition: Ipopt.java:144
abstract boolean eval_g(int n, double[] x, boolean new_x, int m, double[] g)
Method to request the constraint values.
static final int SOLVE_SUCCEEDED
Definition: Ipopt.java:134
boolean setStringOption(String keyword, String val)
Function for setting a string option.
Definition: Ipopt.java:589
double callback_hess[]
Definition: Ipopt.java:164
double[] getConstraintMultipliers()
Gives constraint dual multipliers in final point.
Definition: Ipopt.java:752
static final int DIVERGING_ITERATES
Definition: Ipopt.java:138
static final int INFEASIBLE_PROBLEM
Definition: Ipopt.java:136
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[])
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.
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:829
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.
native boolean AddIpoptIntOption(long ipopt, String keyword, int val)
double callback_jac_g[]
Definition: Ipopt.java:163
static final int RESTORATIONPHASEMODE
Definition: Ipopt.java:156
Ipopt(String DLL)
Creates a NLP Solver for the given DLL file.
Definition: Ipopt.java:243
Ipopt(String path, String DLL)
Creates a NLP Solver for the given DLL file and path.
Definition: Ipopt.java:258
static final int UNRECOVERABLE_EXCEPTION
Definition: Ipopt.java:149
double mult_x_U[]
Final multipliers for upper variable bounds.
Definition: Ipopt.java:179
static final int C_STYLE
Use C index style for iRow and jCol vectors.
Definition: Ipopt.java:128
double g[]
Values of constraint at final point.
Definition: Ipopt.java:173
double x[]
Final value of variable values.
Definition: Ipopt.java:167
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[])
native void FreeIpoptProblem(long ipopt)
int status
Status returned by the solver.
Definition: Ipopt.java:185
static final int INSUFFICIENT_MEMORY
Definition: Ipopt.java:151
static final int NOT_ENOUGH_DEGREES_OF_FRE
Definition: Ipopt.java:145
double[] getConstraintValues()
Gives constraint function values at final point.
Definition: Ipopt.java:744
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:797
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:697
static final int INVALID_PROBLEM_DEFINITION
Definition: Ipopt.java:146
double getObjectiveValue()
Gives objective function value at final point.
Definition: Ipopt.java:726
void dispose()
Dispose of the natively allocated memory.
Definition: Ipopt.java:483
static final int SEARCH_DIRECTION_TOO_SMALL
Definition: Ipopt.java:137
abstract boolean eval_grad_f(int n, double[] x, boolean new_x, double[] grad_f)
Method to request the gradient of the objective function.
boolean setIntegerOption(String keyword, int val)
Function for setting an integer option.
Definition: Ipopt.java:549
static final int ITERATION_EXCEEDED
Definition: Ipopt.java:140
double[] getUpperBoundMultipliers()
Gives dual multipliers for variable upper bounds in final point.
Definition: Ipopt.java:768
abstract boolean eval_f(int n, double[] x, boolean new_x, double[] obj_value)
Method to request the value of the objective function.
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 AddIpoptStrOption(long ipopt, String keyword, String val)
static final int INVALID_NUMBER_DETECTED
Definition: Ipopt.java:148
boolean create(int n, int m, int nele_jac, int nele_hess, int index_style)
Create a new problem.
Definition: Ipopt.java:511
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:856
double obj_val[]
Final value of objective function.
Definition: Ipopt.java:170
int getStatus()
Gives Ipopt status of last OptimizeNLP call.
Definition: Ipopt.java:736
static final int INVALID_OPTION
Definition: Ipopt.java:147
double[] getLowerBoundMultipliers()
Gives dual multipliers for variable lower bounds in final point.
Definition: Ipopt.java:760
static final int ACCEPTABLE_LEVEL
Definition: Ipopt.java:135
long ipopt
Pointer to the native optimization object.
Definition: Ipopt.java:159
double mult_g[]
Final multipliers for constraints.
Definition: Ipopt.java:182
Ipopt()
Creates a new NLP Solver using a default as the DLL name.
Definition: Ipopt.java:195
double mult_x_L[]
Final multipliers for lower variable bounds.
Definition: Ipopt.java:176
static final int ERROR_IN_STEP_COMPUTATION
Definition: Ipopt.java:142
static final int USER_REQUESTED_STOP
Definition: Ipopt.java:139
native boolean AddIpoptNumOption(long ipopt, String keyword, double val)
static final int NON_IPOPT_EXCEPTION
Definition: Ipopt.java:150
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[])
boolean setNumericOption(String keyword, double val)
Function for setting a number option.
Definition: Ipopt.java:569
static final int RESTORATION_FAILED
Definition: Ipopt.java:141
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:648
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.
static final int FORTRAN_STYLE
Use FORTRAN index style for iRow and jCol vectors.
Definition: Ipopt.java:131
native long CreateIpoptProblem(int n, int m, int nele_jac, int nele_hess, int index_style)
int OptimizeNLP()
This function actually solve the problem.
Definition: Ipopt.java:611
int get_number_of_nonlinear_variables()
When LBFGS hessian approximation is used, this method should be overloaded.
Definition: Ipopt.java:844
double callback_grad_f[]
Callback arguments.
Definition: Ipopt.java:162
static final int REGULARMODE
Definition: Ipopt.java:155
static final int CPUTIME_EXCEEDED
Definition: Ipopt.java:143
void finalize()
Definition: Ipopt.java:494
static final int INTERNAL_ERROR
Definition: Ipopt.java:152
This file contains a base class for all exceptions and a set of macros to help with exceptions.