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 
96  public final static int C_STYLE = 0;
97 
99  public final static int FORTRAN_STYLE = 1;
100 
102  public final static int SOLVE_SUCCEEDED = 0;
103  public final static int ACCEPTABLE_LEVEL = 1;
104  public final static int INFEASIBLE_PROBLEM = 2;
105  public final static int SEARCH_DIRECTION_TOO_SMALL = 3;
106  public final static int DIVERGING_ITERATES = 4;
107  public final static int USER_REQUESTED_STOP = 5;
108  public final static int ITERATION_EXCEEDED = -1;
109  public final static int RESTORATION_FAILED = -2;
110  public final static int ERROR_IN_STEP_COMPUTATION = -3;
111  public final static int CPUTIME_EXCEEDED = -4;
112  public final static int NOT_ENOUGH_DEGREES_OF_FRE = -10;
113  public final static int INVALID_PROBLEM_DEFINITION = -11;
114  public final static int INVALID_OPTION = -12;
115  public final static int INVALID_NUMBER_DETECTED = -13;
116  public final static int UNRECOVERABLE_EXCEPTION = -100;
117  public final static int NON_IPOPT_EXCEPTION = -101;
118  public final static int INSUFFICIENT_MEMORY = -102;
119  public final static int INTERNAL_ERROR = -199;
120 
122  private long ipopt;
123 
125  private double callback_grad_f[];
126  private double callback_jac_g[];
127  private double callback_hess[];
128 
130  private double x[];
131 
133  private double obj_val[] = {0.0};
134 
136  private double g[];
137 
139  private double mult_x_L[];
140 
142  private double mult_x_U[];
143 
145  private double mult_g[];
146 
149 
158  public Ipopt()
159  {
160  if( System.getProperty("os.name").toLowerCase().indexOf("win") >= 0 )
161  {
162  /* for Ipopt releases, it should be ipopt-3.dll
163  * for other intermediate versions, it should be ipopt-0.dll
164  * with MinGW, libtool adds a "lib" prefix
165  * finally, try also without version info
166  */
167  final String[] candidates = { "ipopt-3", "ipopt-0", "libipopt-3", "libipopt-0", "ipopt", "libipopt" };
168  boolean loadedlib = false;
169  for( String c : candidates )
170  {
171  try
172  {
173  System.loadLibrary(c);
174  loadedlib = true;
175  break;
176  }
177  catch( UnsatisfiedLinkError e )
178  { }
179  }
180  if( !loadedlib )
181  {
182  throw new UnsatisfiedLinkError("Could not load Ipopt library. Check your java.library.path.");
183  }
184  }
185  else
186  {
187  System.loadLibrary("ipopt");
188  }
189  }
190 
199  public Ipopt(
200  String DLL)
201  {
202  // Loads the library
203  System.loadLibrary(DLL);
204  }
205 
214  public Ipopt(
215  String path,
216  String DLL)
217  {
218  // Loads the library
219  File file = new File(path, System.mapLibraryName(DLL));
220  System.load(file.getAbsolutePath());
221  }
222 
223 
244  abstract protected boolean get_bounds_info(
245  int n,
246  double[] x_l,
247  double[] x_u,
248  int m,
249  double[] g_l,
250  double[] g_u
251  );
252 
277  abstract protected boolean get_starting_point(
278  int n,
279  boolean init_x,
280  double[] x,
281  boolean init_z,
282  double[] z_L,
283  double[] z_U,
284  int m,
285  boolean init_lambda,
286  double[] lambda
287  );
288 
300  abstract protected boolean eval_f(
301  int n,
302  double[] x,
303  boolean new_x,
304  double[] obj_value
305  );
306 
318  abstract protected boolean eval_grad_f(
319  int n,
320  double[] x,
321  boolean new_x,
322  double[] grad_f
323  );
324 
335  abstract protected boolean eval_g(
336  int n,
337  double[] x,
338  boolean new_x,
339  int m,
340  double[] g
341  );
342 
371  abstract protected boolean eval_jac_g(
372  int n,
373  double[] x,
374  boolean new_x,
375  int m,
376  int nele_jac,
377  int[] iRow,
378  int[] jCol,
379  double[] values
380  );
381 
382 
415  abstract protected boolean eval_h(
416  int n,
417  double[] x,
418  boolean new_x,
419  double obj_factor,
420  int m,
421  double[] lambda,
422  boolean new_lambda,
423  int nele_hess,
424  int[] iRow,
425  int[] jCol,
426  double[] values
427  );
428 
439  public void dispose()
440  {
441  // dispose the native implementation
442  if( ipopt != 0 )
443  {
445  ipopt = 0;
446  }
447  }
448 
449  @Deprecated
450  protected void finalize() throws Throwable
451  {
452  dispose();
453  }
454 
467  public boolean create(
468  int n,
469  int m,
470  int nele_jac,
471  int nele_hess,
472  int index_style)
473  {
474  // delete any previously created native memory
475  dispose();
476 
477  x = new double[n];
478  g = new double[m];
479 
480  // allocate the callback arguments
481  callback_grad_f = new double[n];
482  callback_jac_g = new double[nele_jac];
483  callback_hess = new double[nele_hess];
484 
485  // the multiplier
486  mult_x_U = new double[n];
487  mult_x_L = new double[n];
488  mult_g = new double[m];
489 
490  // create the optimization problem and return a pointer to it
491  ipopt = CreateIpoptProblem(n, m, nele_jac, nele_hess, index_style);
492 
493  //System.out.println("Finish Java Obj");
494  return ipopt == 0 ? false : true;
495  }
496 
505  public boolean setIntegerOption(
506  String keyword,
507  int val)
508  {
509  if( ipopt == 0 )
510  {
511  return false;
512  }
513 
514  return AddIpoptIntOption(ipopt, keyword, val);
515  }
516 
525  public boolean setNumericOption(
526  String keyword,
527  double val)
528  {
529  if( ipopt == 0 )
530  {
531  return false;
532  }
533 
534  return AddIpoptNumOption(ipopt, keyword, val);
535  }
536 
545  public boolean setStringOption(
546  String keyword,
547  String val)
548  {
549  if( ipopt == 0 )
550  {
551  return false;
552  }
553 
554  return AddIpoptStrOption(ipopt, keyword, val.toLowerCase());
555  }
556 
567  public int OptimizeNLP()
568  {
569  this.status = this.OptimizeTNLP(ipopt,
572 
573  return this.status;
574  }
575 
579  public double[] getVariableValues()
580  {
581  return x;
582  }
583 
587  public double getObjectiveValue()
588  {
589  return obj_val[0];
590  }
591 
597  public int getStatus()
598  {
599  return status;
600  }
601 
605  public double[] getConstraintValues()
606  {
607  return g;
608  }
609 
613  public double[] getConstraintMultipliers()
614  {
615  return mult_g;
616  }
617 
621  public double[] getLowerBoundMultipliers()
622  {
623  return mult_x_L;
624  }
625 
629  public double[] getUpperBoundMultipliers()
630  {
631  return mult_x_U;
632  }
633 
648  public boolean get_scaling_parameters(
649  double[] obj_scaling,
650  int n,
651  double[] x_scaling,
652  int m,
653  double[] g_scaling,
654  boolean[] use_x_g_scaling)
655  {
656  return false;
657  }
658 
664  {
665  return -1;
666  }
667 
676  int num_nonlin_vars,
677  int[] pos_nonlin_vars)
678  {
679  return false;
680  }
681 }
org.coinor.Ipopt.eval_grad_f
abstract boolean eval_grad_f(int n, double[] x, boolean new_x, double[] grad_f)
Method to request the gradient of the objective function.
org.coinor.Ipopt.INVALID_OPTION
static final int INVALID_OPTION
Definition: Ipopt.java:114
org.coinor.Ipopt.dispose
void dispose()
Dispose of the natively allocated memory.
Definition: Ipopt.java:439
org.coinor.Ipopt.setStringOption
boolean setStringOption(String keyword, String val)
Function for setting a string option.
Definition: Ipopt.java:545
org.coinor.Ipopt.CPUTIME_EXCEEDED
static final int CPUTIME_EXCEEDED
Definition: Ipopt.java:111
org.coinor.Ipopt.DIVERGING_ITERATES
static final int DIVERGING_ITERATES
Definition: Ipopt.java:106
org.coinor.Ipopt.FreeIpoptProblem
native void FreeIpoptProblem(long ipopt)
org.coinor.Ipopt.callback_grad_f
double callback_grad_f[]
Callback arguments.
Definition: Ipopt.java:125
org.coinor.Ipopt.C_STYLE
static final int C_STYLE
Use C index style for iRow and jCol vectors.
Definition: Ipopt.java:96
org.coinor.Ipopt.NON_IPOPT_EXCEPTION
static final int NON_IPOPT_EXCEPTION
Definition: Ipopt.java:117
org.coinor.Ipopt.ipopt
long ipopt
Pointer to the native optimization object.
Definition: Ipopt.java:122
org.coinor.Ipopt.getObjectiveValue
double getObjectiveValue()
Gives objective function value at final point.
Definition: Ipopt.java:587
org.coinor.Ipopt.SEARCH_DIRECTION_TOO_SMALL
static final int SEARCH_DIRECTION_TOO_SMALL
Definition: Ipopt.java:105
org.coinor.Ipopt.get_scaling_parameters
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:648
org.coinor.Ipopt.x
double x[]
Final value of variable values.
Definition: Ipopt.java:130
org.coinor.Ipopt.USER_REQUESTED_STOP
static final int USER_REQUESTED_STOP
Definition: Ipopt.java:107
org.coinor.Ipopt.g
double g[]
Values of constraint at final point.
Definition: Ipopt.java:136
org.coinor.Ipopt.INVALID_NUMBER_DETECTED
static final int INVALID_NUMBER_DETECTED
Definition: Ipopt.java:115
Ipopt
This file contains a base class for all exceptions and a set of macros to help with exceptions.
Definition: IpInexactAlgBuilder.hpp:13
org.coinor.Ipopt.RESTORATION_FAILED
static final int RESTORATION_FAILED
Definition: Ipopt.java:109
org.coinor.Ipopt.callback_jac_g
double callback_jac_g[]
Definition: Ipopt.java:126
org.coinor.Ipopt.AddIpoptNumOption
native boolean AddIpoptNumOption(long ipopt, String keyword, double val)
org.coinor.Ipopt.INSUFFICIENT_MEMORY
static final int INSUFFICIENT_MEMORY
Definition: Ipopt.java:118
org.coinor.Ipopt.finalize
void finalize()
Definition: Ipopt.java:450
org.coinor.Ipopt.NOT_ENOUGH_DEGREES_OF_FRE
static final int NOT_ENOUGH_DEGREES_OF_FRE
Definition: Ipopt.java:112
org.coinor.Ipopt.ACCEPTABLE_LEVEL
static final int ACCEPTABLE_LEVEL
Definition: Ipopt.java:103
org.coinor.Ipopt.UNRECOVERABLE_EXCEPTION
static final int UNRECOVERABLE_EXCEPTION
Definition: Ipopt.java:116
org.coinor.Ipopt.AddIpoptIntOption
native boolean AddIpoptIntOption(long ipopt, String keyword, int val)
org.coinor.Ipopt.INTERNAL_ERROR
static final int INTERNAL_ERROR
Definition: Ipopt.java:119
org.coinor.Ipopt.eval_g
abstract boolean eval_g(int n, double[] x, boolean new_x, int m, double[] g)
Method to request the constraint values.
org.coinor.Ipopt.INFEASIBLE_PROBLEM
static final int INFEASIBLE_PROBLEM
Definition: Ipopt.java:104
org.coinor.Ipopt.callback_hess
double callback_hess[]
Definition: Ipopt.java:127
org.coinor.Ipopt.get_starting_point
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.
org.coinor.Ipopt.setNumericOption
boolean setNumericOption(String keyword, double val)
Function for setting a number option.
Definition: Ipopt.java:525
org.coinor.Ipopt.ITERATION_EXCEEDED
static final int ITERATION_EXCEEDED
Definition: Ipopt.java:108
org.coinor.Ipopt.getStatus
int getStatus()
Gives Ipopt status of last OptimizeNLP call.
Definition: Ipopt.java:597
org.coinor.Ipopt.eval_h
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.
org.coinor.Ipopt.mult_x_L
double mult_x_L[]
Final multipliers for lower variable bounds.
Definition: Ipopt.java:139
org.coinor.Ipopt.FORTRAN_STYLE
static final int FORTRAN_STYLE
Use FORTRAN index style for iRow and jCol vectors.
Definition: Ipopt.java:99
org.coinor.Ipopt.mult_x_U
double mult_x_U[]
Final multipliers for upper variable bounds.
Definition: Ipopt.java:142
org.coinor.Ipopt.Ipopt
Ipopt(String path, String DLL)
Creates a NLP Solver for the given DLL file and path.
Definition: Ipopt.java:214
org.coinor.Ipopt.getUpperBoundMultipliers
double[] getUpperBoundMultipliers()
Gives dual multipliers for variable upper bounds in final point.
Definition: Ipopt.java:629
org.coinor.Ipopt.OptimizeTNLP
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[])
org.coinor.Ipopt.eval_jac_g
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.
org.coinor.Ipopt.create
boolean create(int n, int m, int nele_jac, int nele_hess, int index_style)
Create a new problem.
Definition: Ipopt.java:467
org.coinor.Ipopt.getVariableValues
double[] getVariableValues()
Gives primal variable values at final point.
Definition: Ipopt.java:579
org.coinor.Ipopt.getConstraintValues
double[] getConstraintValues()
Gives constraint function values at final point.
Definition: Ipopt.java:605
org.coinor.Ipopt.Ipopt
Ipopt(String DLL)
Creates a NLP Solver for the given DLL file.
Definition: Ipopt.java:199
org.coinor.Ipopt.OptimizeNLP
int OptimizeNLP()
This function actually solve the problem.
Definition: Ipopt.java:567
org.coinor.Ipopt.mult_g
double mult_g[]
Final multipliers for constraints.
Definition: Ipopt.java:145
org.coinor.Ipopt.obj_val
double obj_val[]
Final value of objective function.
Definition: Ipopt.java:133
org.coinor.Ipopt.CreateIpoptProblem
native long CreateIpoptProblem(int n, int m, int nele_jac, int nele_hess, int index_style)
org.coinor.Ipopt.getConstraintMultipliers
double[] getConstraintMultipliers()
Gives constraint dual multipliers in final point.
Definition: Ipopt.java:613
org.coinor.Ipopt.getLowerBoundMultipliers
double[] getLowerBoundMultipliers()
Gives dual multipliers for variable lower bounds in final point.
Definition: Ipopt.java:621
org.coinor.Ipopt.ERROR_IN_STEP_COMPUTATION
static final int ERROR_IN_STEP_COMPUTATION
Definition: Ipopt.java:110
org.coinor.Ipopt.get_list_of_nonlinear_variables
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:675
org.coinor.Ipopt.setIntegerOption
boolean setIntegerOption(String keyword, int val)
Function for setting an integer option.
Definition: Ipopt.java:505
org.coinor.Ipopt.INVALID_PROBLEM_DEFINITION
static final int INVALID_PROBLEM_DEFINITION
Definition: Ipopt.java:113
org.coinor.Ipopt.get_number_of_nonlinear_variables
int get_number_of_nonlinear_variables()
When LBFGS hessian approximation is used, this method should be overloaded.
Definition: Ipopt.java:663
org.coinor.Ipopt.status
int status
Status returned by the solver.
Definition: Ipopt.java:148
org.coinor.Ipopt.Ipopt
Ipopt()
Creates a new NLP Solver using a default as the DLL name.
Definition: Ipopt.java:158
org.coinor.Ipopt.AddIpoptStrOption
native boolean AddIpoptStrOption(long ipopt, String keyword, String val)
org.coinor.Ipopt.eval_f
abstract boolean eval_f(int n, double[] x, boolean new_x, double[] obj_value)
Method to request the value of the objective function.
org.coinor.Ipopt.SOLVE_SUCCEEDED
static final int SOLVE_SUCCEEDED
The possible Ipopt status return codes: should be kept in sync with Ipopt return codes.
Definition: Ipopt.java:102
org.coinor.Ipopt.get_bounds_info
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.