Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Problems.TestFunctions/3.3/Evaluators/GriewankEvaluator.cs @ 12147

Last change on this file since 12147 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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    [StorableConstructor]
70    protected GriewankEvaluator(bool deserializing) : base(deserializing) { }
71    protected GriewankEvaluator(GriewankEvaluator original, Cloner cloner) : base(original, cloner) { }
72    public GriewankEvaluator() : base() { }
73
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new GriewankEvaluator(this, cloner);
76    }
77
78    public override RealVector GetBestKnownSolution(int dimension) {
79      return new RealVector(dimension);
80    }
81    /// <summary>
82    /// If dimension of the problem is less or equal than 100 the values of Math.Sqrt(i + 1) are precomputed.
83    /// </summary>
84    private double[] sqrts;
85
86    /// <summary>
87    /// Evaluates the test function for a specific <paramref name="point"/>.
88    /// </summary>
89    /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
90    /// <returns>The result value of the Griewank function at the given point.</returns>
91    public static double Apply(RealVector point) {
92      double result = 0;
93      double val = 0;
94
95      for (int i = 0; i < point.Length; i++)
96        result += point[i] * point[i];
97      result = result / 4000;
98
99      val = Math.Cos(point[0]);
100      for (int i = 1; i < point.Length; i++)
101        val *= Math.Cos(point[i] / Math.Sqrt(i + 1));
102
103      result = result - val + 1;
104      return result;
105    }
106
107    /// <summary>
108    /// 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
109    /// </summary>
110    /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
111    /// <param name="sqrts">The precomputed array of square roots.</param>
112    /// <returns>The result value of the Griewank function at the given point.</returns>
113    private static double Apply(RealVector point, double[] sqrts) {
114      double result = 0;
115      double val = 0;
116
117      for (int i = 0; i < point.Length; i++)
118        result += point[i] * point[i];
119      result = result / 4000;
120
121      val = Math.Cos(point[0]);
122      for (int i = 1; i < point.Length; i++)
123        val *= Math.Cos(point[i] / sqrts[i]);
124
125      result = result - val + 1;
126      return result;
127    }
128
129    /// <summary>
130    /// Evaluates the test function for a specific <paramref name="point"/>.
131    /// </summary>
132    /// <remarks>Calls <see cref="Apply"/>.</remarks>
133    /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
134    /// <returns>The result value of the Griewank function at the given point.</returns>
135    protected override double EvaluateFunction(RealVector point) {
136      if (point.Length > 100)
137        return Apply(point);
138      else {
139        if (sqrts == null || sqrts.Length < point.Length) ComputeSqrts(point.Length);
140        return Apply(point, sqrts);
141      }
142    }
143
144    private void ComputeSqrts(int length) {
145      sqrts = new double[length];
146      for (int i = 0; i < length; i++) sqrts[i] = Math.Sqrt(i + 1);
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.