Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/Evaluators/GriewankEvaluator.cs @ 4068

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 5.6 KB
Line 
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
22using System;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Encodings.RealVectorEncoding;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Problems.TestFunctions {
29  /// <summary>
30  /// 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.
31  /// It is a multimodal fitness function in the range [-600,600]^n.
32  /// 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.
33  /// </summary>
34  [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.")]
35  [StorableClass]
36  public class GriewankEvaluator : SingleObjectiveTestFunctionProblemEvaluator {
37    /// <summary>
38    /// Returns false as the Griewank function is a minimization problem.
39    /// </summary>
40    public override bool Maximization {
41      get { return false; }
42    }
43    /// <summary>
44    /// Gets the optimum function value (0).
45    /// </summary>
46    public override double BestKnownQuality {
47      get { return 0; }
48    }
49    /// <summary>
50    /// Gets the lower and upper bound of the function.
51    /// </summary>
52    public override DoubleMatrix Bounds {
53      get { return new DoubleMatrix(new double[,] { { -600, 600 } }); }
54    }
55    /// <summary>
56    /// Gets the minimum problem size (1).
57    /// </summary>
58    public override int MinimumProblemSize {
59      get { return 1; }
60    }
61    /// <summary>
62    /// Gets the (theoretical) maximum problem size (2^31 - 1).
63    /// </summary>
64    public override int MaximumProblemSize {
65      get { return int.MaxValue; }
66    }
67
68    public override RealVector GetBestKnownSolution(int dimension) {
69      return new RealVector(dimension);
70    }
71    /// <summary>
72    /// If dimension of the problem is less or equal than 100 the values of Math.Sqrt(i + 1) are precomputed.
73    /// </summary>
74    private double[] sqrts;
75
76    /// <summary>
77    /// Evaluates the test function for a specific <paramref name="point"/>.
78    /// </summary>
79    /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
80    /// <returns>The result value of the Griewank function at the given point.</returns>
81    public static double Apply(RealVector point) {
82      double result = 0;
83      double val = 0;
84
85      for (int i = 0; i < point.Length; i++)
86        result += point[i] * point[i];
87      result = result / 4000;
88
89      val = Math.Cos(point[0]);
90      for (int i = 1; i < point.Length; i++)
91        val *= Math.Cos(point[i] / Math.Sqrt(i + 1));
92
93      result = result - val + 1;
94      return result;
95    }
96
97    /// <summary>
98    /// 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
99    /// </summary>
100    /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
101    /// <param name="sqrts">The precomputed array of square roots.</param>
102    /// <returns>The result value of the Griewank function at the given point.</returns>
103    private static double Apply(RealVector point, double[] sqrts) {
104      double result = 0;
105      double val = 0;
106
107      for (int i = 0; i < point.Length; i++)
108        result += point[i] * point[i];
109      result = result / 4000;
110
111      val = Math.Cos(point[0]);
112      for (int i = 1; i < point.Length; i++)
113        val *= Math.Cos(point[i] / sqrts[i]);
114
115      result = result - val + 1;
116      return result;
117    }
118
119    /// <summary>
120    /// Evaluates the test function for a specific <paramref name="point"/>.
121    /// </summary>
122    /// <remarks>Calls <see cref="Apply"/>.</remarks>
123    /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
124    /// <returns>The result value of the Griewank function at the given point.</returns>
125    protected override double EvaluateFunction(RealVector point) {
126      if (point.Length > 100)
127        return Apply(point);
128      else {
129        if (sqrts == null || sqrts.Length < point.Length) ComputeSqrts(point.Length);
130        return Apply(point, sqrts);
131      }
132    }
133
134    private void ComputeSqrts(int length) {
135      sqrts = new double[length];
136      for (int i = 0; i < length; i++) sqrts[i] = Math.Sqrt(i + 1);
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.