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 
127  /* Native function should not be used directly */
128  private native void GetVersion(
129  int version[]
130  );
131 
133  public final static int C_STYLE = 0;
134 
136  public final static int FORTRAN_STYLE = 1;
137 
138  /* The possible Ipopt status return codes: should be kept in sync with Ipopt return codes */
139  public final static int SOLVE_SUCCEEDED = 0;
140  public final static int ACCEPTABLE_LEVEL = 1;
141  public final static int INFEASIBLE_PROBLEM = 2;
142  public final static int SEARCH_DIRECTION_TOO_SMALL = 3;
143  public final static int DIVERGING_ITERATES = 4;
144  public final static int USER_REQUESTED_STOP = 5;
145  public final static int ITERATION_EXCEEDED = -1;
146  public final static int RESTORATION_FAILED = -2;
147  public final static int ERROR_IN_STEP_COMPUTATION = -3;
148  public final static int CPUTIME_EXCEEDED = -4;
149  public final static int WALLTIME_EXCEEDED = -5;
150  public final static int NOT_ENOUGH_DEGREES_OF_FRE = -10;
151  public final static int INVALID_PROBLEM_DEFINITION = -11;
152  public final static int INVALID_OPTION = -12;
153  public final static int INVALID_NUMBER_DETECTED = -13;
154  public final static int UNRECOVERABLE_EXCEPTION = -100;
155  public final static int NON_IPOPT_EXCEPTION = -101;
156  public final static int INSUFFICIENT_MEMORY = -102;
157  public final static int INTERNAL_ERROR = -199;
158 
159  /* The possible algorithm modes (passed to intermediate_callback) */
160  public final static int REGULARMODE = 0;
161  public final static int RESTORATIONPHASEMODE = 1;
162 
164  private long ipopt;
165 
167  private double callback_grad_f[];
168  private double callback_jac_g[];
169  private double callback_hess[];
170 
172  private double x[];
173 
175  private double obj_val[] = {0};
176 
178  private double g[];
179 
181  private double mult_x_L[];
182 
184  private double mult_x_U[];
185 
187  private double mult_g[];
188 
191 
198  public Ipopt()
199  {
200  if( System.getProperty("os.name").toLowerCase().indexOf("win") >= 0 )
201  {
202  /* for Ipopt releases, it should be ipopt-3.dll
203  * for other intermediate versions, it should be ipopt-0.dll
204  * with MinGW, libtool adds a "lib" prefix
205  * finally, try also without version info
206  */
207  final String[] candidates = { "ipopt-3", "ipopt-0", "libipopt-3", "libipopt-0", "ipopt", "libipopt" };
208  boolean loadedlib = false;
209  for( String c : candidates )
210  {
211  try
212  {
213  System.loadLibrary(c);
214  loadedlib = true;
215  break;
216  }
217  catch( UnsatisfiedLinkError e )
218  { }
219  }
220  if( !loadedlib )
221  {
222  throw new UnsatisfiedLinkError("Could not load Ipopt library. Check your java.library.path.");
223  }
224  }
225  else
226  {
227  /* This loads the Ipopt library with RTLD_LOCAL, which means that symbols loaded are not made available for future dlopen() calls.
228  * This causes a problem when using MKL, which loads an additional library at runtime, e.g., libmkl_avx2, because this lib references
229  * to symbols that could be resolved in previously load MKL libraries - but are not because of RTLD_LOCAL.
230  * TODO should one add some kind of workaround to load the Ipopt lib with RTLD_GLOBAL?, e.g.,
231  * https://stackoverflow.com/questions/5425034/java-load-shared-libraries-with-dependencies
232  * https://github.com/victor-paltz/global-load-library
233  */
234  System.loadLibrary("ipopt");
235  }
236  }
237 
244  public Ipopt(
245  String DLL)
246  {
247  // Loads the library
248  System.loadLibrary(DLL);
249  }
250 
257  public Ipopt(
258  String path,
259  String DLL)
260  {
261  // Loads the library
262  File file = new File(path, System.mapLibraryName(DLL));
263  System.load(file.getAbsolutePath());
264  }
265 
286  abstract protected boolean get_bounds_info(
287  int n,
288  double[] x_l,
289  double[] x_u,
290  int m,
291  double[] g_l,
292  double[] g_u
293  );
294 
319  abstract protected boolean get_starting_point(
320  int n,
321  boolean init_x,
322  double[] x,
323  boolean init_z,
324  double[] z_L,
325  double[] z_U,
326  int m,
327  boolean init_lambda,
328  double[] lambda
329  );
330 
342  abstract protected boolean eval_f(
343  int n,
344  double[] x,
345  boolean new_x,
346  double[] obj_value
347  );
348 
360  abstract protected boolean eval_grad_f(
361  int n,
362  double[] x,
363  boolean new_x,
364  double[] grad_f
365  );
366 
377  abstract protected boolean eval_g(
378  int n,
379  double[] x,
380  boolean new_x,
381  int m,
382  double[] g
383  );
384 
413  abstract protected boolean eval_jac_g(
414  int n,
415  double[] x,
416  boolean new_x,
417  int m,
418  int nele_jac,
419  int[] iRow,
420  int[] jCol,
421  double[] values
422  );
423 
456  abstract protected boolean eval_h(
457  int n,
458  double[] x,
459  boolean new_x,
460  double obj_factor,
461  int m,
462  double[] lambda,
463  boolean new_lambda,
464  int nele_hess,
465  int[] iRow,
466  int[] jCol,
467  double[] values
468  );
469 
480  public void dispose()
481  {
482  // dispose the native implementation
483  if( ipopt != 0 )
484  {
486  ipopt = 0;
487  }
488  }
489 
490  @Deprecated
491  protected void finalize() throws Throwable
492  {
493  dispose();
494  }
495 
508  public boolean create(
509  int n,
510  int m,
511  int nele_jac,
512  int nele_hess,
513  int index_style)
514  {
515  // delete any previously created native memory
516  dispose();
517 
518  x = new double[n];
519  g = new double[m];
520 
521  // allocate the callback arguments
522  callback_grad_f = new double[n];
523  callback_jac_g = new double[nele_jac];
524  callback_hess = new double[nele_hess];
525 
526  // the multiplier
527  mult_x_U = new double[n];
528  mult_x_L = new double[n];
529  mult_g = new double[m];
530 
531  // create the optimization problem and return a pointer to it
532  ipopt = CreateIpoptProblem(n, m, nele_jac, nele_hess, index_style);
533 
534  //System.out.println("Finish Java Obj");
535  return ipopt == 0 ? false : true;
536  }
537 
546  public boolean setIntegerOption(
547  String keyword,
548  int val)
549  {
550  if( ipopt == 0 )
551  {
552  return false;
553  }
554 
555  return AddIpoptIntOption(ipopt, keyword, val);
556  }
557 
566  public boolean setNumericOption(
567  String keyword,
568  double val)
569  {
570  if( ipopt == 0 )
571  {
572  return false;
573  }
574 
575  return AddIpoptNumOption(ipopt, keyword, val);
576  }
577 
586  public boolean setStringOption(
587  String keyword,
588  String val)
589  {
590  if( ipopt == 0 )
591  {
592  return false;
593  }
594 
595  return AddIpoptStrOption(ipopt, keyword, val.toLowerCase());
596  }
597 
608  public int OptimizeNLP()
609  {
610  this.status = this.OptimizeTNLP(ipopt,
613 
614  return this.status;
615  }
616 
645  public boolean get_curr_iterate(
646  long ip_data,
647  long ip_cq,
648  boolean scaled,
649  int n,
650  double x[],
651  double z_L[],
652  double z_U[],
653  int m,
654  double g[],
655  double lambda[]
656  )
657  {
658  return this.GetCurrIterate(ipopt, ip_data, ip_cq, scaled, n, x, z_L, z_U, m, g, lambda);
659  }
660 
694  public boolean get_curr_violations(
695  long ip_data,
696  long ip_cq,
697  boolean scaled,
698  int n,
699  double x_L_violation[],
700  double x_U_violation[],
701  double compl_x_L[],
702  double compl_x_U[],
703  double grad_lag_x[],
704  int m,
705  double nlp_constraint_violation[],
706  double compl_g[]
707  )
708  {
709  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);
710  }
711 
715  public double[] getVariableValues()
716  {
717  return x;
718  }
719 
723  public double getObjectiveValue()
724  {
725  return obj_val[0];
726  }
727 
733  public int getStatus()
734  {
735  return status;
736  }
737 
741  public double[] getConstraintValues()
742  {
743  return g;
744  }
745 
749  public double[] getConstraintMultipliers()
750  {
751  return mult_g;
752  }
753 
757  public double[] getLowerBoundMultipliers()
758  {
759  return mult_x_L;
760  }
761 
765  public double[] getUpperBoundMultipliers()
766  {
767  return mult_x_U;
768  }
769 
794  public boolean intermediate_callback(
795  int algorithmmode,
796  int iter,
797  double obj_value,
798  double inf_pr,
799  double inf_du,
800  double mu,
801  double d_norm,
802  double regularization_size,
803  double alpha_du,
804  double alpha_pr,
805  int ls_trials,
806  long ip_data,
807  long ip_cq)
808  {
809  return true;
810  }
811 
826  public boolean get_scaling_parameters(
827  double[] obj_scaling,
828  int n,
829  double[] x_scaling,
830  int m,
831  double[] g_scaling,
832  boolean[] use_x_g_scaling)
833  {
834  return false;
835  }
836 
842  {
843  return -1;
844  }
845 
854  int num_nonlin_vars,
855  int[] pos_nonlin_vars)
856  {
857  return false;
858  }
859 
868  public void getVersion(
869  int[] version
870  )
871  {
872  GetVersion(version);
873  }
874 }
Ipopt()
Creates a new NLP Solver using a default as the DLL name.
Definition: Ipopt.java:198
void getVersion(int[] version)
Get version of Ipopt library.
Definition: Ipopt.java:868
double callback_jac_g[]
Definition: Ipopt.java:168
native void FreeIpoptProblem(long ipopt)
static final int RESTORATIONPHASEMODE
Definition: Ipopt.java:161
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:181
static final int INSUFFICIENT_MEMORY
Definition: Ipopt.java:156
static final int DIVERGING_ITERATES
Definition: Ipopt.java:143
int get_number_of_nonlinear_variables()
When LBFGS hessian approximation is used, this method should be overloaded.
Definition: Ipopt.java:841
static final int NOT_ENOUGH_DEGREES_OF_FRE
Definition: Ipopt.java:150
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:765
static final int USER_REQUESTED_STOP
Definition: Ipopt.java:144
double getObjectiveValue()
Gives objective function value at final point.
Definition: Ipopt.java:723
static final int INVALID_OPTION
Definition: Ipopt.java:152
static final int INTERNAL_ERROR
Definition: Ipopt.java:157
static final int ITERATION_EXCEEDED
Definition: Ipopt.java:145
int status
Status returned by the solver.
Definition: Ipopt.java:190
static final int INFEASIBLE_PROBLEM
Definition: Ipopt.java:141
static final int FORTRAN_STYLE
Use FORTRAN index style for iRow and jCol vectors.
Definition: Ipopt.java:136
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:154
double mult_x_U[]
Final multipliers for upper variable bounds.
Definition: Ipopt.java:184
native long CreateIpoptProblem(int n, int m, int nele_jac, int nele_hess, int index_style)
static final int WALLTIME_EXCEEDED
Definition: Ipopt.java:149
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:757
double [] getConstraintValues()
Gives constraint function values at final point.
Definition: Ipopt.java:741
double obj_val[]
Final value of objective function.
Definition: Ipopt.java:175
static final int RESTORATION_FAILED
Definition: Ipopt.java:146
void finalize()
Definition: Ipopt.java:491
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:645
boolean setIntegerOption(String keyword, int val)
Function for setting an integer option.
Definition: Ipopt.java:546
boolean setStringOption(String keyword, String val)
Function for setting a string option.
Definition: Ipopt.java:586
static final int CPUTIME_EXCEEDED
Definition: Ipopt.java:148
boolean setNumericOption(String keyword, double val)
Function for setting a number option.
Definition: Ipopt.java:566
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:794
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:244
boolean create(int n, int m, int nele_jac, int nele_hess, int index_style)
Create a new problem.
Definition: Ipopt.java:508
static final int NON_IPOPT_EXCEPTION
Definition: Ipopt.java:155
static final int ERROR_IN_STEP_COMPUTATION
Definition: Ipopt.java:147
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:257
double callback_grad_f[]
Callback arguments.
Definition: Ipopt.java:167
native boolean AddIpoptIntOption(long ipopt, String keyword, int val)
static final int SEARCH_DIRECTION_TOO_SMALL
Definition: Ipopt.java:142
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:172
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:153
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:826
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:178
int OptimizeNLP()
This function actually solve the problem.
Definition: Ipopt.java:608
double [] getVariableValues()
Gives primal variable values at final point.
Definition: Ipopt.java:715
long ipopt
Pointer to the native optimization object.
Definition: Ipopt.java:164
double [] getConstraintMultipliers()
Gives constraint dual multipliers in final point.
Definition: Ipopt.java:749
static final int ACCEPTABLE_LEVEL
Definition: Ipopt.java:140
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:187
int getStatus()
Gives Ipopt status of last OptimizeNLP call.
Definition: Ipopt.java:733
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:853
static final int C_STYLE
Use C index style for iRow and jCol vectors.
Definition: Ipopt.java:133
native boolean AddIpoptNumOption(long ipopt, String keyword, double val)
static final int REGULARMODE
Definition: Ipopt.java:160
void dispose()
Dispose of the natively allocated memory.
Definition: Ipopt.java:480
native void GetVersion(int version[])
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:694
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:169
static final int SOLVE_SUCCEEDED
Definition: Ipopt.java:139
static final int INVALID_PROBLEM_DEFINITION
Definition: Ipopt.java:151