1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.TestFunctions {
|
---|
30 | /// <summary>
|
---|
31 | /// The Griewank function is introduced in Griewank, A.O. 1981. Generalized descent for global optimization. Journal of Optimization Theory and Applications 34, pp. 11-39.
|
---|
32 | /// It is a multimodal fitness function in the range [-600,600]^n.
|
---|
33 | /// Here it is implemented as described (without the modifications) in Locatelli, M. 2003. A note on the Griewank test function. Journal of Global Optimization 25, pp. 169-174, Springer.
|
---|
34 | /// </summary>
|
---|
35 | [Item("GriewankEvaluator", "Evaluates the Griewank function on a given point. The optimum of this function is 0 at the origin. It is introduced by Griewank A.O. 1981 and implemented as described (without the modifications) in Locatelli, M. 2003. A note on the Griewank test function. Journal of Global Optimization 25, pp. 169-174, Springer.")]
|
---|
36 | [StorableClass]
|
---|
37 | public class GriewankEvaluator : SingleObjectiveTestFunctionProblemEvaluator {
|
---|
38 | /// <summary>
|
---|
39 | /// Returns false as the Griewank function is a minimization problem.
|
---|
40 | /// </summary>
|
---|
41 | public override bool Maximization {
|
---|
42 | get { return false; }
|
---|
43 | }
|
---|
44 | /// <summary>
|
---|
45 | /// Gets the optimum function value (0).
|
---|
46 | /// </summary>
|
---|
47 | public override double BestKnownQuality {
|
---|
48 | get { return 0; }
|
---|
49 | }
|
---|
50 | /// <summary>
|
---|
51 | /// Gets the lower and upper bound of the function.
|
---|
52 | /// </summary>
|
---|
53 | public override DoubleMatrix Bounds {
|
---|
54 | get { return new DoubleMatrix(new double[,] { { -600, 600 } }); }
|
---|
55 | }
|
---|
56 | /// <summary>
|
---|
57 | /// Gets the minimum problem size (1).
|
---|
58 | /// </summary>
|
---|
59 | public override int MinimumProblemSize {
|
---|
60 | get { return 1; }
|
---|
61 | }
|
---|
62 | /// <summary>
|
---|
63 | /// Gets the (theoretical) maximum problem size (2^31 - 1).
|
---|
64 | /// </summary>
|
---|
65 | public override int MaximumProblemSize {
|
---|
66 | get { return int.MaxValue; }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public override RealVector GetBestKnownSolution(int dimension) {
|
---|
70 | return new RealVector(dimension);
|
---|
71 | }
|
---|
72 | /// <summary>
|
---|
73 | /// If dimension of the problem is less or equal than 100 the values of Math.Sqrt(i + 1) are precomputed.
|
---|
74 | /// </summary>
|
---|
75 | private double[] sqrts;
|
---|
76 |
|
---|
77 | /// <summary>
|
---|
78 | /// Evaluates the test function for a specific <paramref name="point"/>.
|
---|
79 | /// </summary>
|
---|
80 | /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
|
---|
81 | /// <returns>The result value of the Griewank function at the given point.</returns>
|
---|
82 | public static double Apply(RealVector point) {
|
---|
83 | double result = 0;
|
---|
84 | double val = 0;
|
---|
85 |
|
---|
86 | for (int i = 0; i < point.Length; i++)
|
---|
87 | result += point[i] * point[i];
|
---|
88 | result = result / 4000;
|
---|
89 |
|
---|
90 | val = Math.Cos(point[0]);
|
---|
91 | for (int i = 1; i < point.Length; i++)
|
---|
92 | val *= Math.Cos(point[i] / Math.Sqrt(i + 1));
|
---|
93 |
|
---|
94 | result = result - val + 1;
|
---|
95 | return result;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /// <summary>
|
---|
99 | /// Evaluates the test function for a specific <paramref name="point"/>. It uses an array of precomputed values for Math.Sqrt(i + 1) with i = 0..N
|
---|
100 | /// </summary>
|
---|
101 | /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
|
---|
102 | /// <param name="sqrts">The precomputed array of square roots.</param>
|
---|
103 | /// <returns>The result value of the Griewank function at the given point.</returns>
|
---|
104 | private static double Apply(RealVector point, double[] sqrts) {
|
---|
105 | double result = 0;
|
---|
106 | double val = 0;
|
---|
107 |
|
---|
108 | for (int i = 0; i < point.Length; i++)
|
---|
109 | result += point[i] * point[i];
|
---|
110 | result = result / 4000;
|
---|
111 |
|
---|
112 | val = Math.Cos(point[0]);
|
---|
113 | for (int i = 1; i < point.Length; i++)
|
---|
114 | val *= Math.Cos(point[i] / sqrts[i]);
|
---|
115 |
|
---|
116 | result = result - val + 1;
|
---|
117 | return result;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /// <summary>
|
---|
121 | /// Evaluates the test function for a specific <paramref name="point"/>.
|
---|
122 | /// </summary>
|
---|
123 | /// <remarks>Calls <see cref="Apply"/>.</remarks>
|
---|
124 | /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
|
---|
125 | /// <returns>The result value of the Griewank function at the given point.</returns>
|
---|
126 | protected override double EvaluateFunction(RealVector point) {
|
---|
127 | if (point.Length > 100)
|
---|
128 | return Apply(point);
|
---|
129 | else {
|
---|
130 | if (sqrts == null || sqrts.Length < point.Length) ComputeSqrts(point.Length);
|
---|
131 | return Apply(point, sqrts);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | private void ComputeSqrts(int length) {
|
---|
136 | sqrts = new double[length];
|
---|
137 | for (int i = 0; i < length; i++) sqrts[i] = Math.Sqrt(i + 1);
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|