1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022010 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 


22  using System;


23  using HeuristicLab.Core;


24  using HeuristicLab.Data;


25  using HeuristicLab.Encodings.RealVectorEncoding;


26  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;


27 


28  namespace 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. 1139.


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. 169174, 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. 169174, Springer.")]


35  [StorableClass]


36  public class GriewankEvaluator : SingleObjectiveTestFunctionProblemEvaluator {


37  /// <summary>


38  /// Returns false as the Griewangk 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  /// <summary>


69  /// If dimension of the problem is less or equal than 100 the values of Math.Sqrt(i + 1) are precomputed.


70  /// </summary>


71  private double[] sqrts;


72 


73  /// <summary>


74  /// Evaluates the test function for a specific <paramref name="point"/>.


75  /// </summary>


76  /// <param name="point">Ndimensional point for which the test function should be evaluated.</param>


77  /// <returns>The result value of the Griewangk function at the given point.</returns>


78  public static double Apply(RealVector point) {


79  double result = 0;


80  double val = 0;


81 


82  for (int i = 0; i < point.Length; i++)


83  result += point[i] * point[i];


84  result = result / 4000;


85 


86  val = Math.Cos(point[0]);


87  for (int i = 1; i < point.Length; i++)


88  val *= Math.Cos(point[i] / Math.Sqrt(i + 1));


89 


90  result = result  val + 1;


91  return result;


92  }


93 


94  /// <summary>


95  /// 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


96  /// </summary>


97  /// <param name="point">Ndimensional point for which the test function should be evaluated.</param>


98  /// <param name="sqrts">The precomputed array of square roots.</param>


99  /// <returns>The result value of the Griewangk function at the given point.</returns>


100  private static double Apply(RealVector point, double[] sqrts) {


101  double result = 0;


102  double val = 0;


103 


104  for (int i = 0; i < point.Length; i++)


105  result += point[i] * point[i];


106  result = result / 4000;


107 


108  val = Math.Cos(point[0]);


109  for (int i = 1; i < point.Length; i++)


110  val *= Math.Cos(point[i] / sqrts[i]);


111 


112  result = result  val + 1;


113  return result;


114  }


115 


116  /// <summary>


117  /// Evaluates the test function for a specific <paramref name="point"/>.


118  /// </summary>


119  /// <remarks>Calls <see cref="Apply"/>.</remarks>


120  /// <param name="point">Ndimensional point for which the test function should be evaluated.</param>


121  /// <returns>The result value of the Griewangk function at the given point.</returns>


122  protected override double EvaluateFunction(RealVector point) {


123  if (point.Length > 100)


124  return Apply(point);


125  else {


126  if (sqrts == null  sqrts.Length < point.Length) ComputeSqrts(point.Length);


127  return Apply(point, sqrts);


128  }


129  }


130 


131  private void ComputeSqrts(int length) {


132  sqrts = new double[length];


133  for (int i = 0; i < length; i++) sqrts[i] = Math.Sqrt(i + 1);


134  }


135  }


136  }

