View Javadoc
1   /*
2    * Licensed to the Hipparchus project under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The Hipparchus project licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.hipparchus.optim.nonlinear.vector.constrained;
18  
19  import org.hipparchus.linear.Array2DRowRealMatrix;
20  import org.hipparchus.linear.ArrayRealVector;
21  import org.hipparchus.linear.RealMatrix;
22  import org.hipparchus.linear.RealVector;
23  
24  public class HockSchittkowskiFunction77 extends TwiceDifferentiableFunction {
25  
26      @Override
27      public int dim() {
28          return 5;
29      }
30  
31      @Override
32      public double value(RealVector x) {
33          double x1 = x.getEntry(0);
34          double x2 = x.getEntry(1);
35          double x3 = x.getEntry(2);
36          double x4 = x.getEntry(3);
37          double x5 = x.getEntry(4);
38          // obj = (x[1]-1)^2 + (x[1] - x[2])^2 + (x[3]-1)^2 + (x[4]-1)^4 + (x[5]-1)^6
39          return (x1-1)*(x1-1) + (x1 - x2)*(x1 - x2) + (x3-1)*(x3-1) + (x4-1)*(x4-1)*(x4-1)*(x4-1) + (x5-1)*(x5-1)*(x5-1)*(x5-1)*(x5-1)*(x5-1);
40      }
41  
42      @Override
43      public RealVector gradient(RealVector x) {
44          RealVector grad=new ArrayRealVector(x.getDimension());
45          double x1 = x.getEntry(0);
46          double x2 = x.getEntry(1);
47          double x3 = x.getEntry(2);
48          double x4 = x.getEntry(3);
49          double x5 = x.getEntry(4);
50          grad.setEntry(0,2.0*(x1-1.0)+2.0*(x1-x2));
51          grad.setEntry(1,-2.0*(x1-x2));
52          grad.setEntry(2,2.0*(x3-1));
53           grad.setEntry(3,4.0*(x4-1)*(x4-1)*(x4-1));
54            grad.setEntry(4,6.0*(x5-1)*(x5-1)*(x5-1)*(x5-1)*(x5-1));
55          return grad;
56      }
57  
58      @Override
59      public RealMatrix hessian(RealVector x) {
60          RealMatrix h=new Array2DRowRealMatrix(x.getDimension(),x.getDimension());
61          double x4 = x.getEntry(3);
62          double x5 = x.getEntry(4);
63          h.setEntry(0,0, 4.0);
64          h.setEntry(0,1, -2.0);
65          h.setEntry(0,2, 0);
66          h.setEntry(0,3, 0);
67          h.setEntry(0,4, 0);
68  
69           h.setEntry(1,0, -2.0);
70          h.setEntry(1,1, 2.0);
71          h.setEntry(1,2, 0);
72          h.setEntry(1,3, 0);
73          h.setEntry(1,4, 0);
74  
75          h.setEntry(2,0, 0);
76          h.setEntry(2,1, 0);
77          h.setEntry(2,2, 2.0);
78          h.setEntry(2,3, 0);
79          h.setEntry(2,4, 0);
80  
81          h.setEntry(3,0, 0);
82          h.setEntry(3,1, 0);
83          h.setEntry(3,2, 0.0);
84          h.setEntry(3,3, 12.0*(x4-1)*(x4-1));
85          h.setEntry(3,4, 0);
86  
87          h.setEntry(4,0, 0);
88          h.setEntry(4,1, 0);
89          h.setEntry(4,2, 0.0);
90          h.setEntry(4,3, 0.0);
91          h.setEntry(4,4, 30.0*(x5-1)*(x5-1)*(x5-1)*(x5-1));
92  
93          return h;
94      }
95  
96  }