Ipopt  3.11.9
LuksanVlcek1.java
Go to the documentation of this file.
1 
9 package org.coinor.examples.scalable;
10 
17 public class LuksanVlcek1 extends Scalable
18 {
24  public LuksanVlcek1(String name, double gl, double gu)
25  {
26  super(name, gl, gu);
27  }
28 
29  @Override
30  public boolean initialize(int n)
31  {
32  if( n <= 2 )
33  {
34  System.out.print("N needs to be at least 3.\n");
35  return false;
36  }
37 
38  // The problem described in LuksanVlcek1.hpp has 4 variables, x[0] through x[3]
39  this.n = n;
40 
41  m = n - 2;
42 
43  nnz_jac_g = m * 3;
44 
45  nnz_h_lag = n + n-1;
46 
47  // use the C style numbering of matrix indices (starting at 0)
48  index_style = C_STYLE;
49 
50  // none of the variables have bounds
51  x_l = new double[n];
52  x_u = new double[n];
53  for( int i = 0; i < n; ++i )
54  {
55  x_l[i] = -1e20;
56  x_u[i] = 1e20;
57  }
58 
59  // Set the bounds for the constraints
60  g_l = new double[m];
61  g_u = new double[m];
62  for( int i = 0; i < m; ++i )
63  {
64  g_l[i] = gl;
65  g_u[i] = gu;
66  }
67 
68  // set the starting point
69  x = new double[n];
70  for( int i = 0; i < n/2; ++i )
71  {
72  x[2*i] = -1.2;
73  x[2*i+1] = 1.0;
74  }
75  if( n % 2 == 1 )
76  x[n-1] = -1.2;
77 
78  return true;
79  }
80 
81  protected boolean get_bounds_info(int n, double[] x_l, double[] x_u,
82  int m, double[] g_l, double[] g_u)
83  {
84  // none of the variables have bounds
85  for( int i = 0; i < n; ++i )
86  {
87  x_l[i] = -1e20;
88  x_u[i] = 1e20;
89  }
90 
91  // Set the bounds for the constraints
92  for( int i = 0; i < m; ++i )
93  {
94  g_l[i] = gl;
95  g_u[i] = gu;
96  }
97 
98  return true;
99  }
100 
101  protected boolean get_starting_point(int n, boolean init_x, double[] x,
102  boolean init_z, double[] z_L, double[] z_U,
103  int m, boolean init_lambda,double[] lambda)
104  {
105  for( int i = 0; i < n/2; ++i )
106  {
107  x[2*i] = -1.2;
108  x[2*i+1] = 1.0;
109  }
110  if( n % 2 == 1 )
111  x[n-1] = -1.2;
112 
113  return true;
114  }
115 
116  @Override
117  protected boolean eval_f(int n, double[] x, boolean new_x, double[] obj_value)
118  {
119  obj_value[0] = 0.0;
120  for( int i = 0; i < n-1; ++i )
121  {
122  double a1 = x[i] * x[i] - x[i+1];
123  double a2 = x[i] - 1.0;
124  obj_value[0] += 100.0 * a1 * a1 + a2 * a2;
125  }
126 
127  return true;
128  }
129 
130  @Override
131  protected boolean eval_g(int n, double[] x, boolean new_x, int m, double[] g)
132  {
133  for( int i = 0; i < n-2; ++i )
134  g[i] = 3.0 * Math.pow(x[i+1], 3.0) + 2.0 * x[i+2] - 5.0 + Math.sin(x[i+1]-x[i+2]) * Math.sin(x[i+1]+x[i+2])
135  + 4.0 * x[i+1] - x[i] * Math.exp(x[i] - x[i+1]) - 3.0;
136 
137  return true;
138  }
139 
140  @Override
141  protected boolean eval_grad_f(int n, double[] x, boolean new_x, double[] grad_f)
142  {
143  grad_f[0] = 0.0;
144  for( int i = 0; i < n-1; ++i )
145  {
146  grad_f[i] += 400.0 * x[i] * (x[i] * x[i] - x[i+1]) + 2.0 * (x[i] - 1.0);
147  grad_f[i+1] = -200.0 * (x[i] * x[i] - x[i+1]);
148  }
149 
150  return true;
151  }
152 
153  @Override
154  protected boolean eval_jac_g(int n, double[] x, boolean new_x, int m,
155  int nele_jac, int[] iRow, int[] jCol, double[] values)
156  {
157  if( values == null )
158  {
159  // return the structure of the jacobian
160  int ijac=0;
161  for( int i = 0; i < n-2; ++i )
162  {
163  iRow[ijac] = i;
164  jCol[ijac] = i;
165  ijac++;
166  iRow[ijac] = i;
167  jCol[ijac] = i+1;
168  ijac++;
169  iRow[ijac] = i;
170  jCol[ijac] = i+2;
171  ijac++;
172  }
173  }
174  else
175  {
176  // return the values of the jacobian of the constraints
177  int ijac=0;
178 
179  for( int i = 0; i < n-2; ++i )
180  {
181  // x[i]
182  values[ijac] = -(1.0 + x[i]) * Math.exp(x[i] - x[i+1]);
183  ijac++;
184  // x[i+1]
185  values[ijac] = 9.0 * x[i+1] * x[i+1]
186  + Math.cos(x[i+1] - x[i+2]) * Math.sin(x[i+1] + x[i+2])
187  + Math.sin(x[i+1] - x[i+2]) * Math.cos(x[i+1] + x[i+2])
188  + 4.0 + x[i] * Math.exp(x[i] - x[i+1]);
189  ijac++;
190  // x[i+2]
191  values[ijac] = 2.0
192  - Math.cos(x[i+1] - x[i+2]) * Math.sin(x[i+1] + x[i+2])
193  + Math.sin(x[i+1] - x[i+2]) * Math.cos(x[i+1] + x[i+2]);
194  ijac++;
195  }
196  }
197 
198  return true;
199  }
200 
201  @Override
202  protected boolean eval_h(int n, double[] x, boolean new_x,
203  double obj_factor, int m, double[] lambda, boolean new_lambda,
204  int nele_hess, int[] iRow, int[] jCol, double[] values)
205  {
206  if( values == null)
207  {
208  int ihes = 0;
209  for( int i = 0; i < n; ++i )
210  {
211  iRow[ihes] = i;
212  jCol[ihes] = i;
213  ++ihes;
214  if( i < n-1 )
215  {
216  iRow[ihes] = i;
217  jCol[ihes] = i+1;
218  ihes++;
219  }
220  }
221  assert ihes == nele_hess;
222  }
223  else
224  {
225  int ihes = 0;
226  for( int i = 0; i < n; ++i )
227  {
228  // x[i],x[i]
229  if( i < n-1 )
230  {
231  values[ihes] = obj_factor * (2.0 + 400.0 * (3.0 * x[i] * x[i] - x[i+1]));
232  if( i < n-2 )
233  values[ihes] -= lambda[i] * (2.0 + x[i]) * Math.exp(x[i] - x[i+1]);
234  }
235  else
236  values[ihes] = 0.;
237 
238  if( i > 0 )
239  {
240  // x[i+1]x[i+1]
241  values[ihes] += obj_factor * 200.0;
242  if( i < n-1 )
243  values[ihes] += lambda[i-1]* (18.0 * x[i]
244  - 2.0 * Math.sin(x[i] - x[i+1]) * Math.sin(x[i] + x[i+1])
245  + 2.0 * Math.cos(x[i] - x[i+1]) * Math.cos(x[i] + x[i+1])
246  - x[i-1] * Math.exp(x[i-1] - x[i]));
247  }
248  if( i > 1 )
249  // x[i+2]x[i+2]
250  values[ihes] += lambda[i-2] * (-2.0 * Math.sin(x[i-1] - x[i]) * Math.sin(x[i-1] + x[i])
251  - 2.0 * Math.cos(x[i-1] - x[i]) * Math.cos(x[i-1] + x[i]));
252  ihes++;
253 
254  if( i < n-1 )
255  {
256  // x[i],x[i+1]
257  values[ihes] = obj_factor * (-400.0 * x[i]);
258  if( i < n-2 )
259  values[ihes] += lambda[i]*(1.+x[i])*Math.exp(x[i]-x[i+1]);
260  /*
261  if (i>0) {
262  // x[i+1],x[i+2]
263  values[ihes] +=
264  lambda[i-1]*( sin(x[i]-x[i+1])*sin(x[i]+x[i+1])
265  + cos(x[i]-x[i+1])*cos(x[i]+x[i+1])
266  - cos(x[i]-x[i+1])*cos(x[i]+x[i+1])
267  - sin(x[i]-x[i+1])*sin(x[i]+x[i+1])
268  );
269  }
270  */
271  ihes++;
272  }
273  }
274  assert ihes == nele_hess;
275  }
276 
277  return true;
278  }
279 }
Number * x
Input: Starting point Output: Optimal solution.
Number Number Index Number Number Index Index nele_hess
Number of non-zero elements in Hessian of Lagrangian.
Number Number Index Number Number Index nele_jac
Number of non-zero elements in constraint Jacobian.
Number Number Index m
Number of constraints.
static final int C_STYLE
Use C index style for iRow and jCol vectors.
Definition: Ipopt.java:78
double g[]
Values of constraint at final point.
Definition: Ipopt.java:117
Implementation of Example 5.1 from "Sparse and Parially Separable Test Problems for Unconstrained and...
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 retrieving a starting point.
boolean eval_grad_f(int n, double[] x, boolean new_x, double[] grad_f)
Callback function for the objective function gradient.
boolean eval_g(int n, double[] x, boolean new_x, int m, double[] g)
Callback function for the constraints.
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.
boolean get_bounds_info(int n, double[] x_l, double[] x_u, int m, double[] g_l, double[] g_u)
Callback function for the variable bounds and constraint sides.
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.
boolean eval_f(int n, double[] x, boolean new_x, double[] obj_value)
Callback function for the objective function.
LuksanVlcek1(String name, double gl, double gu)
Constructor.
boolean initialize(int n)
In this function all problem sizes, bounds and initial guess should be initialized.
Abstract class for the scalable problems.
Definition: Scalable.java:24