Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3530 was 3376, checked in by swagner, 15 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

File size: 5.5 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.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.RealVectorEncoding;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace 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    /// <summary>
70    /// If dimension of the problem is less or equal than 100 the values of Math.Sqrt(i + 1) are precomputed.
71    /// </summary>
72    private double[] sqrts;
73
74    /// <summary>
75    /// Evaluates the test function for a specific <paramref name="point"/>.
76    /// </summary>
77    /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
78    /// <returns>The result value of the Griewank function at the given point.</returns>
79    public static double Apply(RealVector point) {
80      double result = 0;
81      double val = 0;
82
83      for (int i = 0; i < point.Length; i++)
84        result += point[i] * point[i];
85      result = result / 4000;
86
87      val = Math.Cos(point[0]);
88      for (int i = 1; i < point.Length; i++)
89        val *= Math.Cos(point[i] / Math.Sqrt(i + 1));
90
91      result = result - val + 1;
92      return result;
93    }
94
95    /// <summary>
96    /// 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
97    /// </summary>
98    /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
99    /// <param name="sqrts">The precomputed array of square roots.</param>
100    /// <returns>The result value of the Griewank function at the given point.</returns>
101    private static double Apply(RealVector point, double[] sqrts) {
102      double result = 0;
103      double val = 0;
104
105      for (int i = 0; i < point.Length; i++)
106        result += point[i] * point[i];
107      result = result / 4000;
108
109      val = Math.Cos(point[0]);
110      for (int i = 1; i < point.Length; i++)
111        val *= Math.Cos(point[i] / sqrts[i]);
112
113      result = result - val + 1;
114      return result;
115    }
116
117    /// <summary>
118    /// Evaluates the test function for a specific <paramref name="point"/>.
119    /// </summary>
120    /// <remarks>Calls <see cref="Apply"/>.</remarks>
121    /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
122    /// <returns>The result value of the Griewank function at the given point.</returns>
123    protected override double EvaluateFunction(RealVector point) {
124      if (point.Length > 100)
125        return Apply(point);
126      else {
127        if (sqrts == null || sqrts.Length < point.Length) ComputeSqrts(point.Length);
128        return Apply(point, sqrts);
129      }
130    }
131
132    private void ComputeSqrts(int length) {
133      sqrts = new double[length];
134      for (int i = 0; i < length; i++) sqrts[i] = Math.Sqrt(i + 1);
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.