Ipopt  3.12.9
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
HS071.java
Go to the documentation of this file.
1 
9 package org.coinor.examples;
10 import org.coinor.Ipopt;
11 
24 public class HS071 extends Ipopt
25 {
26  // Problem sizes
27  int n;
28  int m;
29  int nele_jac;
30  int nele_hess;
31 
32  int count_bounds = 0;
33  int dcount_start = 0;
34 
36  public HS071()
37  {
38  /* Number of nonzeros in the Jacobian of the constraints */
39  nele_jac = 8;
40 
41  /* Number of nonzeros in the Hessian of the Lagrangian (lower or
42  * upper triangual part only) */
43  nele_hess = 10;
44 
45  /* Number of variables */
46  n = 4;
47 
48  /* Number of constraints */
49  m = 2;
50 
51  /* Index style for the irow/jcol elements */
53 
54  /* create the IpoptProblem */
55  create(n, m, nele_jac, nele_hess, index_style);
56  }
57 
59  protected boolean get_bounds_info(int n, double[] x_L, double[] x_U,
60  int m, double[] g_L, double[] g_U)
61  {
62  assert n == this.n;
63  assert m == this.m;
64 
65  /* set the values of the variable bounds */
66  for( int i = 0; i < n; ++i )
67  {
68  x_L[i] = 1.0;
69  x_U[i] = 5.0;
70  }
71 
72  /* set the values of the constraint bounds */
73  g_L[0] = 25.0; g_U[0] = 2e19;
74  g_L[1] = 40.0; g_U[1] = 40.0;
75 
76  return true;
77  }
78 
80  protected boolean get_starting_point(int n, boolean init_x, double[] x,
81  boolean init_z, double[] z_L, double[] z_U,
82  int m, boolean init_lambda,double[] lambda)
83  {
84  assert init_z == false;
85  assert init_lambda = false;
86 
87  if( init_x )
88  {
89  x[0] = 1.0;
90  x[1] = 5.0;
91  x[2] = 5.0;
92  x[3] = 1.0;
93  }
94 
95  /*
96  x[0] = 0.9999999923240762;
97  x[1] = 4.742999641809297;
98  x[2] = 3.8211499817883072;
99  x[3] = 1.3794082897556983;
100 
101  z_L[0] = 1.0878712258676539e+00;
102  z_L[1] = 0;
103  z_L[2] = 0;
104  z_L[3] = 0;
105 
106  z_U[0] = 0;
107  z_U[1] = 0;
108  z_U[2] = 0;
109  z_U[3] = 0;
110 
111  lambda[0] = -0.552293195627571;
112  lambda[1] = 0.16146777361782;
113  */
114 
115  return true;
116  }
117 
118  protected boolean eval_f(int n, double[] x, boolean new_x, double[] obj_value)
119  {
120  assert n == this.n;
121 
122  obj_value[0] = x[0] * x[3] * (x[0] + x[1] + x[2]) + x[2];
123 
124  return true;
125  }
126 
127  protected boolean eval_grad_f(int n, double[] x, boolean new_x, double[] grad_f)
128  {
129  assert n == this.n;
130 
131  grad_f[0] = x[0] * x[3] + x[3] * (x[0] + x[1] + x[2]);
132  grad_f[1] = x[0] * x[3];
133  grad_f[2] = x[0] * x[3] + 1;
134  grad_f[3] = x[0] * (x[0] + x[1] + x[2]);
135 
136  return true;
137  }
138 
139  protected boolean eval_g(int n, double[] x, boolean new_x, int m, double[] g)
140  {
141  assert n == this.n;
142  assert m == this.m;
143 
144  g[0] = x[0] * x[1] * x[2] * x[3];
145  g[1] = x[0]*x[0] + x[1]*x[1] + x[2]*x[2] + x[3]*x[3];
146 
147  return true;
148  }
149 
150  protected boolean eval_jac_g(int n, double[] x, boolean new_x,
151  int m, int nele_jac, int[] iRow, int[] jCol, double[] values)
152  {
153  assert n == this.n;
154  assert m == this.m;
155 
156  if( values == null )
157  {
158  /* return the structure of the jacobian */
159 
160  /* this particular jacobian is dense */
161  iRow[0] = 0; jCol[0] = 0;
162  iRow[1] = 0; jCol[1] = 1;
163  iRow[2] = 0; jCol[2] = 2;
164  iRow[3] = 0; jCol[3] = 3;
165  iRow[4] = 1; jCol[4] = 0;
166  iRow[5] = 1; jCol[5] = 1;
167  iRow[6] = 1; jCol[6] = 2;
168  iRow[7] = 1; jCol[7] = 3;
169  }
170  else
171  {
172  /* return the values of the jacobian of the constraints */
173 
174  values[0] = x[1]*x[2]*x[3]; /* 0,0 */
175  values[1] = x[0]*x[2]*x[3]; /* 0,1 */
176  values[2] = x[0]*x[1]*x[3]; /* 0,2 */
177  values[3] = x[0]*x[1]*x[2]; /* 0,3 */
178 
179  values[4] = 2*x[0]; /* 1,0 */
180  values[5] = 2*x[1]; /* 1,1 */
181  values[6] = 2*x[2]; /* 1,2 */
182  values[7] = 2*x[3]; /* 1,3 */
183  }
184 
185  return true;
186  }
187 
188  protected 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)
189  {
190  assert n == this.n;
191  assert m == this.m;
192 
193  int idx = 0; /* nonzero element counter */
194  int row = 0; /* row counter for loop */
195  int col = 0; /* col counter for loop */
196  if( values == null )
197  {
198  /* return the structure. This is a symmetric matrix, fill the lower left triangle only. */
199 
200  /* the hessian for this problem is actually dense */
201  idx = 0;
202  for( row = 0; row < n; ++row )
203  {
204  for( col = 0; col <= row; ++col)
205  {
206  iRow[idx] = row;
207  jCol[idx] = col;
208  idx++;
209  }
210  }
211 
212  assert idx == nele_hess;
213  assert nele_hess == this.nele_hess;
214  }
215  else
216  {
217  /* return the values. This is a symmetric matrix, fill the lower left triangle only */
218 
219  /* fill the objective portion */
220  values[0] = obj_factor * (2*x[3]); /* 0,0 */
221  values[1] = obj_factor * (x[3]); /* 1,0 */
222  values[2] = 0.0; /* 1,1 */
223  values[3] = obj_factor * (x[3]); /* 2,0 */
224  values[4] = 0.0; /* 2,1 */
225  values[5] = 0.0; /* 2,2 */
226  values[6] = obj_factor * (2*x[0] + x[1] + x[2]); /* 3,0 */
227  values[7] = obj_factor * (x[0]); /* 3,1 */
228  values[8] = obj_factor * (x[0]); /* 3,2 */
229  values[9] = 0.0; /* 3,3 */
230 
231  /* add the portion for the first constraint */
232  values[1] += lambda[0] * (x[2] * x[3]); /* 1,0 */
233  values[3] += lambda[0] * (x[1] * x[3]); /* 2,0 */
234  values[4] += lambda[0] * (x[0] * x[3]); /* 2,1 */
235  values[6] += lambda[0] * (x[1] * x[2]); /* 3,0 */
236  values[7] += lambda[0] * (x[0] * x[2]); /* 3,1 */
237  values[8] += lambda[0] * (x[0] * x[1]); /* 3,2 */
238 
239  /* add the portion for the second constraint */
240  values[0] += lambda[1] * 2.0; /* 0,0 */
241  values[2] += lambda[1] * 2.0; /* 1,1 */
242  values[5] += lambda[1] * 2.0; /* 2,2 */
243  values[9] += lambda[1] * 2.0; /* 3,3 */
244  }
245 
246  return true;
247  }
248 
249  public boolean get_scaling_parameters(double[] obj_scaling,
250  int n, double[] x_scaling,
251  int m, double[] g_scaling,
252  boolean[] use_x_g_scaling)
253  {
254  return false;
255  }
256 
257 
258  public void print(double[] x, String str)
259  {
260  System.out.println(str);
261  for( int i = 0; i < x.length; ++i )
262  System.out.println(x[i]);
263  System.out.println();
264  }
265 
269  public static void main(String []args)
270  {
271  //Create the problem
272  HS071 hs071 = new HS071();
273 
274  //Set some options
275  //hs071.setNumericOption("tol",1E-7);
276  //hs071.setStringOption("nlp_scaling_method","user-scaling");
277  //hs071.setStringOption("print_options_documentation","yes");
278  //hs071.setStringOption("warm_start_init_point","yes");
279  //hs071.setNumericOption("warm_start_bound_push",1e-9);
280  //hs071.setNumericOption("warm_start_bound_frac",1e-9);
281  //hs071.setNumericOption("warm_start_slack_bound_frac",1e-9);
282  //hs071.setNumericOption("warm_start_slack_bound_push",1e-9);
283  //hs071.setNumericOption("warm_start_mult_bound_push",1e-9);
284 
285  //Solve the problem
286  int status = hs071.OptimizeNLP();
287 
288  //Print the solution
289  if( status == SOLVE_SUCCEEDED )
290  System.out.println("\n\n*** The problem solved!");
291  else
292  System.out.println("\n\n*** The problem was not solved successfully!");
293 
294  double obj = hs071.getObjectiveValue();
295  System.out.println("\nObjective Value = " + obj + "\n");
296 
297  double x[] = hs071.getVariableValues();
298  hs071.print(x, "Primal Variable Values:");
299 
300  double constraints[] = hs071.getConstraintValues();
301  hs071.print(constraints, "Constraint Values:");
302 
303  double MLB[] = hs071.getLowerBoundMultipliers();
304  hs071.print(MLB, "Dual Multipliers for Variable Lower Bounds:");
305 
306  double MUB[] = hs071.getUpperBoundMultipliers();
307  hs071.print(MUB, "Dual Multipliers for Variable Upper Bounds:");
308 
309  double lam[] = hs071.getConstraintMultipliers();
310  hs071.print(lam, "Dual Multipliers for Constraints:");
311  }
312 }
Java example for interfacing with IPOPT.
Definition: HS071.java:24
Number Number Index Number Number Index Index Index index_style
indexing style for iRow &amp; jCol, 0 for C style, 1 for Fortran style
Number Number Index m
Number of constraints.
boolean eval_g(int n, double[] x, boolean new_x, int m, double[] g)
Callback function for the constraints.
Definition: HS071.java:139
boolean eval_jac_g(int n, double[] x, boolean new_x, int m, int nele_jac, int[] iRow, int[] jCol, double[] values)
Callback function for the constraints Jacobian.
Definition: HS071.java:150
boolean eval_grad_f(int n, double[] x, boolean new_x, double[] grad_f)
Callback function for the objective function gradient.
Definition: HS071.java:127
int status
Status returned by the solver.
Definition: Ipopt.java:129
void print(double[] x, String str)
Definition: HS071.java:258
boolean eval_f(int n, double[] x, boolean new_x, double[] obj_value)
Callback function for the objective function.
Definition: HS071.java:118
Number Number Index Number Number * g_U
Upper bounds on constraints.
HS071()
Creates a new instance of HS071cpp.
Definition: HS071.java:36
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)
Callback function for the starting point.
Definition: HS071.java:80
boolean create(int n, int m, int nele_jac, int nele_hess, int index_style)
Create a new problem.
Definition: Ipopt.java:217
Number * x_L
Lower bounds on variables.
double x[]
Final value of variable values.
Definition: Ipopt.java:111
Number Number Index Number * g_L
Lower bounds on constraints.
Number Number * x_U
Upper bounds on variables.
Number Number * x_scaling
double g[]
Values of constraint at final point.
Definition: Ipopt.java:117
Number Number Index Number Number Index Index nele_hess
Number of non-zero elements in Hessian of Lagrangian.
Number Number Number * g_scaling
boolean get_scaling_parameters(double[] obj_scaling, int n, double[] x_scaling, int m, double[] g_scaling, boolean[] use_x_g_scaling)
Definition: HS071.java:249
static final int C_STYLE
Use C index style for iRow and jCol vectors.
Definition: Ipopt.java:78
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)
Callback function for the hessian.
Definition: HS071.java:188
A Java Native Interface for the Ipopt optimization solver.
Definition: Ipopt.java:47
Number obj_scaling
static void main(String[]args)
Main function for running this example.
Definition: HS071.java:269
static final int SOLVE_SUCCEEDED
The possible Ipopt status return codes: should be kept in sync with Ipopt return codes.
Definition: Ipopt.java:83
boolean get_bounds_info(int n, double[] x_L, double[] x_U, int m, double[] g_L, double[] g_U)
Callback function for variable bounds and constraint sides.
Definition: HS071.java:59