File: LuksanVlcek1.java

package info (click to toggle)
coinor-ipopt 3.14.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,796 kB
  • sloc: cpp: 97,169; sh: 4,802; ansic: 2,537; java: 1,289; makefile: 821; fortran: 224; xml: 210
file content (337 lines) | stat: -rw-r--r-- 8,546 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* Copyright (C) 2007 VRTech Industrial Technologies - www.vrtech.com.br.
 * Copyright (C) 2007 Tong Kewei, Beihang University, - www.buaa.edu.cn.
 * All Rights Reserved.
 * This code is published under the Eclipse Public License.
 */

/** Implementation of Example 5.1 from "Sparse and Parially Separable Test Problems for Unconstrained and Equality Constrained Optimization" by L. Luksan and J. Vlcek.
 *
 * This code is based on an Ipopt example file with same name.
 *
 * @author Rafael de Pelegrini Soares, Tong Kewei
 */
public class LuksanVlcek1 extends Scalable
{
   /** Constructor.
    *
    * Here, gl and gu are the bounds for the constraints.
    * The original formulation is obtained by setting gl and gu to zero.
    * Using gl lower than gu allows the obtain a problem formulation with inequality
    * constraints.
    */
   public LuksanVlcek1(
      String name,
      double gl,
      double gu)
   {
      super(name, gl, gu);
   }

   @Override
   public boolean initialize(
      int n)
   {
      if( n <= 2 )
      {
         System.out.print("N needs to be at least 3.\n");
         return false;
      }

      // The problem described in LuksanVlcek1.hpp has 4 variables, x[0] through x[3]
      this.n = n;

      m = n - 2;

      nnz_jac_g = m * 3;

      nnz_h_lag = n + n - 1;

      // use the C style numbering of matrix indices (starting at 0)
      index_style = C_STYLE;

      // none of the variables have bounds
      x_l = new double[n];
      x_u = new double[n];
      for( int i = 0; i < n; ++i )
      {
         x_l[i] = -1e20;
         x_u[i] =  1e20;
      }

      // Set the bounds for the constraints
      g_l = new double[m];
      g_u = new double[m];
      for( int i = 0; i < m; ++i )
      {
         g_l[i] = gl;
         g_u[i] = gu;
      }

      // set the starting point
      x = new double[n];
      for( int i = 0; i < n / 2; ++i )
      {
         x[2 * i    ] = -1.2;
         x[2 * i + 1] =  1.0;
      }
      if (n % 2 == 1)
      {
         x[n - 1] = -1.2;
      }

      return true;
   }

   protected boolean get_bounds_info(
      int      n,
      double[] x_l,
      double[] x_u,
      int      m,
      double[] g_l,
      double[] g_u)
   {
      // none of the variables have bounds
      for( int i = 0; i < n; ++i )
      {
         x_l[i] = -1e20;
         x_u[i] =  1e20;
      }

      // Set the bounds for the constraints
      for( int i = 0; i < m; ++i )
      {
         g_l[i] = gl;
         g_u[i] = gu;
      }

      return true;
   }

   protected 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)
   {
      for( int i = 0; i < n / 2; ++i )
      {
         x[2 * i    ] = -1.2;
         x[2 * i + 1] =  1.0;
      }
      if (n % 2 == 1)
      {
         x[n - 1] = -1.2;
      }

      return true;
   }

   @Override
   protected boolean eval_f(
      int      n,
      double[] x,
      boolean  new_x,
      double[] obj_value)
   {
      obj_value[0] = 0.0;
      for( int i = 0; i < n - 1; ++i )
      {
         double a1 = x[i] * x[i] - x[i + 1];
         double a2 = x[i] - 1.0;
         obj_value[0] += 100.0 * a1 * a1 + a2 * a2;
      }

      return true;
   }

   @Override
   protected boolean eval_g(
      int      n,
      double[] x,
      boolean  new_x,
      int      m,
      double[] g)
   {
      for( int i = 0; i < n - 2; ++i )
         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])
                + 4.0 * x[i + 1]
                - x[i] * Math.exp(x[i] - x[i + 1])
                - 3.0;

      return true;
   }

   @Override
   protected boolean eval_grad_f(
      int      n,
      double[] x,
      boolean  new_x,
      double[] grad_f)
   {
      grad_f[0] = 0.0;
      for( int i = 0; i < n - 1; ++i )
      {
         grad_f[i] += 400.0 * x[i] * (x[i] * x[i] - x[i + 1]) + 2.0 * (x[i] - 1.0);
         grad_f[i + 1] = -200.0 * (x[i] * x[i] - x[i + 1]);
      }

      return true;
   }

   @Override
   protected boolean eval_jac_g(
      int      n,
      double[] x,
      boolean  new_x,
      int      m,
      int      nele_jac,
      int[]    iRow,
      int[]    jCol,
      double[] values)
   {
      if( values == null )
      {
         // return the structure of the jacobian
         int ijac = 0;
         for( int i = 0; i < n - 2; ++i )
         {
            iRow[ijac] = i;
            jCol[ijac] = i;
            ijac++;
            iRow[ijac] = i;
            jCol[ijac] = i + 1;
            ijac++;
            iRow[ijac] = i;
            jCol[ijac] = i + 2;
            ijac++;
         }
      }
      else
      {
         // return the values of the jacobian of the constraints
         int ijac = 0;

         for( int i = 0; i < n - 2; ++i )
         {
            // x[i]
            values[ijac] = -(1.0 + x[i]) * Math.exp(x[i] - x[i + 1]);
            ijac++;

            // x[i+1]
            values[ijac] = 9.0 * x[i + 1] * x[i + 1]
                           + Math.cos(x[i + 1] - x[i + 2]) * Math.sin(x[i + 1] + x[i + 2])
                           + Math.sin(x[i + 1] - x[i + 2]) * Math.cos(x[i + 1] + x[i + 2])
                           + 4.0
                           + x[i] * Math.exp(x[i] - x[i + 1]);
            ijac++;

            // x[i+2]
            values[ijac] = 2.0
                           - Math.cos(x[i + 1] - x[i + 2]) * Math.sin(x[i + 1] + x[i + 2])
                           + Math.sin(x[i + 1] - x[i + 2]) * Math.cos(x[i + 1] + x[i + 2]);
            ijac++;
         }
      }

      return true;
   }

   @Override
   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)
   {
      if( values == null )
      {
         int ihes = 0;
         for( int i = 0; i < n; ++i )
         {
            iRow[ihes] = i;
            jCol[ihes] = i;
            ++ihes;
            if( i < n - 1 )
            {
               iRow[ihes] = i;
               jCol[ihes] = i + 1;
               ihes++;
            }
         }
         assert ihes == nele_hess;
      }
      else
      {
         int ihes = 0;
         for( int i = 0; i < n; ++i )
         {
            // x[i],x[i]
            if( i < n - 1 )
            {
               values[ihes] = obj_factor * (2.0 + 400.0 * (3.0 * x[i] * x[i] - x[i + 1]));
               if (i < n - 2)
               {
                  values[ihes] -= lambda[i] * (2.0 + x[i]) * Math.exp(x[i] - x[i + 1]);
               }
            }
            else
            {
               values[ihes] = 0.;
            }

            if( i > 0 )
            {
               // x[i+1]x[i+1]
               values[ihes] += obj_factor * 200.0;
               if( i < n - 1 )
                  values[ihes] += lambda[i - 1] * (18.0 * x[i]
                                                   - 2.0 * Math.sin(x[i] - x[i + 1]) * Math.sin(x[i] + x[i + 1])
                                                   + 2.0 * Math.cos(x[i] - x[i + 1]) * Math.cos(x[i] + x[i + 1])
                                                   - x[i - 1] * Math.exp(x[i - 1] - x[i]));
            }

            if( i > 1 )
               // x[i+2]x[i+2]
               values[ihes] += lambda[i - 2] * (-2.0 * Math.sin(x[i - 1] - x[i]) * Math.sin(x[i - 1] + x[i])
                                                - 2.0 * Math.cos(x[i - 1] - x[i]) * Math.cos(x[i - 1] + x[i]));
            ihes++;

            if( i < n - 1 )
            {
               // x[i],x[i+1]
               values[ihes] = obj_factor * (-400.0) * x[i];
               if( i < n - 2 )
               {
                  values[ihes] += lambda[i] * (1.0 + x[i]) * Math.exp(x[i] - x[i + 1]);
               }
               /*
                * if (i>0) { // x[i+1],x[i+2] values[ihes] += lambda[i-1]*(
                * sin(x[i]-x[i+1])*sin(x[i]+x[i+1]) +
                * cos(x[i]-x[i+1])*cos(x[i]+x[i+1]) -
                * cos(x[i]-x[i+1])*cos(x[i]+x[i+1]) -
                * sin(x[i]-x[i+1])*sin(x[i]+x[i+1]) ); }
                */
               ihes++;
            }
         }
         assert ihes == nele_hess;
      }

      return true;
   }
}