Free cookie consent management tool by TermsFeed Policy Generator

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

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

Merged cloning refactoring branch back into trunk (#922)

File size: 6.0 KB
RevLine 
[3150]1#region License Information
2/* HeuristicLab
[3154]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3150]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;
[4722]23using HeuristicLab.Common;
[3150]24using HeuristicLab.Core;
25using HeuristicLab.Data;
[3154]26using HeuristicLab.Encodings.RealVectorEncoding;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[3150]28
[3170]29namespace HeuristicLab.Problems.TestFunctions {
[3150]30  /// <summary>
[3315]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.
[3150]34  /// </summary>
[3315]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.")]
[3154]36  [StorableClass]
[3315]37  public class GriewankEvaluator : SingleObjectiveTestFunctionProblemEvaluator {
[3154]38    /// <summary>
[3318]39    /// Returns false as the Griewank function is a minimization problem.
[3154]40    /// </summary>
41    public override bool Maximization {
42      get { return false; }
[3150]43    }
[3154]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>
[3315]57    /// Gets the minimum problem size (1).
[3154]58    /// </summary>
59    public override int MinimumProblemSize {
[3315]60      get { return 1; }
[3154]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    }
[3150]68
[4722]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
[3781]78    public override RealVector GetBestKnownSolution(int dimension) {
79      return new RealVector(dimension);
80    }
[3150]81    /// <summary>
[3315]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>
[3150]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>
[3318]90    /// <returns>The result value of the Griewank function at the given point.</returns>
[3154]91    public static double Apply(RealVector point) {
[3150]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;
[3315]104      return result;
[3150]105    }
106
107    /// <summary>
[3315]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>
[3318]112    /// <returns>The result value of the Griewank function at the given point.</returns>
[3315]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>
[3150]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>
[3318]134    /// <returns>The result value of the Griewank function at the given point.</returns>
[3154]135    protected override double EvaluateFunction(RealVector point) {
[3315]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      }
[3150]142    }
[3315]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    }
[3150]148  }
149}
Note: See TracBrowser for help on using the repository browser.