Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11049 was 9980, checked in by abeham, 11 years ago

#1909:

  • Integrated TestFunctionInstances branch into trunk
  • Removed the branch
File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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    public override string FunctionName { get { return "Griewank"; } }
39    /// <summary>
40    /// Returns false as the Griewank function is a minimization problem.
41    /// </summary>
42    public override bool Maximization {
43      get { return false; }
44    }
45    /// <summary>
46    /// Gets the optimum function value (0).
47    /// </summary>
48    public override double BestKnownQuality {
49      get { return 0; }
50    }
51    /// <summary>
52    /// Gets the lower and upper bound of the function.
53    /// </summary>
54    public override DoubleMatrix Bounds {
55      get { return new DoubleMatrix(new double[,] { { -600, 600 } }); }
56    }
57    /// <summary>
58    /// Gets the minimum problem size (1).
59    /// </summary>
60    public override int MinimumProblemSize {
61      get { return 1; }
62    }
63    /// <summary>
64    /// Gets the (theoretical) maximum problem size (2^31 - 1).
65    /// </summary>
66    public override int MaximumProblemSize {
67      get { return int.MaxValue; }
68    }
69
70    [StorableConstructor]
71    protected GriewankEvaluator(bool deserializing) : base(deserializing) { }
72    protected GriewankEvaluator(GriewankEvaluator original, Cloner cloner) : base(original, cloner) { }
73    public GriewankEvaluator() : base() { }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new GriewankEvaluator(this, cloner);
77    }
78
79    public override RealVector GetBestKnownSolution(int dimension) {
80      return new RealVector(dimension);
81    }
82    /// <summary>
83    /// If dimension of the problem is less or equal than 100 the values of Math.Sqrt(i + 1) are precomputed.
84    /// </summary>
85    private double[] sqrts;
86
87    /// <summary>
88    /// Evaluates the test function for a specific <paramref name="point"/>.
89    /// </summary>
90    /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
91    /// <returns>The result value of the Griewank function at the given point.</returns>
92    public static double Apply(RealVector point) {
93      double result = 0;
94      double val = 0;
95
96      for (int i = 0; i < point.Length; i++)
97        result += point[i] * point[i];
98      result = result / 4000;
99
100      val = Math.Cos(point[0]);
101      for (int i = 1; i < point.Length; i++)
102        val *= Math.Cos(point[i] / Math.Sqrt(i + 1));
103
104      result = result - val + 1;
105      return result;
106    }
107
108    /// <summary>
109    /// 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
110    /// </summary>
111    /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
112    /// <param name="sqrts">The precomputed array of square roots.</param>
113    /// <returns>The result value of the Griewank function at the given point.</returns>
114    private static double Apply(RealVector point, double[] sqrts) {
115      double result = 0;
116      double val = 0;
117
118      for (int i = 0; i < point.Length; i++)
119        result += point[i] * point[i];
120      result = result / 4000;
121
122      val = Math.Cos(point[0]);
123      for (int i = 1; i < point.Length; i++)
124        val *= Math.Cos(point[i] / sqrts[i]);
125
126      result = result - val + 1;
127      return result;
128    }
129
130    /// <summary>
131    /// Evaluates the test function for a specific <paramref name="point"/>.
132    /// </summary>
133    /// <remarks>Calls <see cref="Apply"/>.</remarks>
134    /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
135    /// <returns>The result value of the Griewank function at the given point.</returns>
136    public override double Evaluate(RealVector point) {
137      if (point.Length > 100)
138        return Apply(point);
139      else {
140        if (sqrts == null || sqrts.Length < point.Length) ComputeSqrts(point.Length);
141        return Apply(point, sqrts);
142      }
143    }
144
145    private void ComputeSqrts(int length) {
146      sqrts = new double[length];
147      for (int i = 0; i < length; i++) sqrts[i] = Math.Sqrt(i + 1);
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.