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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 |
|
---|
30 | namespace HeuristicLab.Problems.TestFunctions {
|
---|
31 | /// <summary>
|
---|
32 | /// The sphere function is a unimodal function that has its optimum at the origin.
|
---|
33 | /// It is implemented as described in Beyer, H.-G. and Schwefel, H.-P. 2002. Evolution Strategies - A Comprehensive Introduction Natural Computing, 1, pp. 3-52.
|
---|
34 | /// </summary>
|
---|
35 | [Item("SphereEvaluator", "Evaluates the Sphere function y = C * ||X||^Alpha on a given point. The optimum of this function is 0 at the origin. It is implemented as described in Beyer, H.-G. and Schwefel, H.-P. 2002. Evolution Strategies - A Comprehensive Introduction Natural Computing, 1, pp. 3-52.")]
|
---|
36 | [StorableClass]
|
---|
37 | public class SphereEvaluator : SingleObjectiveTestFunctionProblemEvaluator {
|
---|
38 | /// <summary>
|
---|
39 | /// Returns false as the Sphere 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[,] { { -5.12, 5.12 } }); }
|
---|
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 | public override RealVector GetBestKnownSolution(int dimension) {
|
---|
70 | return new RealVector(dimension);
|
---|
71 | }
|
---|
72 |
|
---|
73 | /// <summary>
|
---|
74 | /// The parameter C modifies the steepness of the objective function y = C * ||X||^Alpha. Default is C = 1.
|
---|
75 | /// </summary>
|
---|
76 | public ValueParameter<DoubleValue> CParameter {
|
---|
77 | get { return (ValueParameter<DoubleValue>)Parameters["C"]; }
|
---|
78 | }
|
---|
79 | /// <summary>
|
---|
80 | /// The parameter Alpha modifies the steepness of the objective function y = C * ||X||^Alpha. Default is Alpha = 2.
|
---|
81 | /// </summary>
|
---|
82 | public ValueParameter<DoubleValue> AlphaParameter {
|
---|
83 | get { return (ValueParameter<DoubleValue>)Parameters["Alpha"]; }
|
---|
84 | }
|
---|
85 | /// <summary>
|
---|
86 | /// The parameter C modifies the steepness of the objective function y = C * ||X||^Alpha. Default is C = 1.
|
---|
87 | /// </summary>
|
---|
88 | public DoubleValue C {
|
---|
89 | get { return CParameter.Value; }
|
---|
90 | set { if (value != null) CParameter.Value = value; }
|
---|
91 | }
|
---|
92 | /// <summary>
|
---|
93 | /// The parameter Alpha modifies the steepness of the objective function y = C * ||X||^Alpha. Default is Alpha = 2.
|
---|
94 | /// </summary>
|
---|
95 | public DoubleValue Alpha {
|
---|
96 | get { return AlphaParameter.Value; }
|
---|
97 | set { if (value != null) AlphaParameter.Value = value; }
|
---|
98 | }
|
---|
99 |
|
---|
100 | /// <summary>
|
---|
101 | /// Initializes a new instance of the SphereEvaluator with two parameters (<c>C</c> and <c>Alpha</c>).
|
---|
102 | /// </summary>
|
---|
103 | public SphereEvaluator()
|
---|
104 | : base() {
|
---|
105 | Parameters.Add(new ValueParameter<DoubleValue>("C", "The parameter C modifies the steepness of the objective function y = C * ||X||^Alpha. Default is C = 1.", new DoubleValue(1)));
|
---|
106 | Parameters.Add(new ValueParameter<DoubleValue>("Alpha", "The parameter Alpha modifies the steepness of the objective function y = C * ||X||^Alpha. Default is Alpha = 2.", new DoubleValue(2)));
|
---|
107 | }
|
---|
108 | /// <summary>
|
---|
109 | /// Evaluates the test function for a specific <paramref name="point"/>.
|
---|
110 | /// </summary>
|
---|
111 | /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
|
---|
112 | /// <returns>The result value of the Sphere function at the given point.</returns>
|
---|
113 | public static double Apply(RealVector point, double c, double alpha) {
|
---|
114 | double result = 0;
|
---|
115 | for (int i = 0; i < point.Length; i++)
|
---|
116 | result += point[i] * point[i];
|
---|
117 | if (alpha != 2) result = Math.Pow(Math.Sqrt(result), alpha);
|
---|
118 | return c * result;
|
---|
119 | }
|
---|
120 |
|
---|
121 | /// <summary>
|
---|
122 | /// Evaluates the test function for a specific <paramref name="point"/>.
|
---|
123 | /// </summary>
|
---|
124 | /// <remarks>Calls <see cref="Apply"/>.</remarks>
|
---|
125 | /// <param name="point">N-dimensional point for which the test function should be evaluated.</param>
|
---|
126 | /// <returns>The result value of the Sphere function at the given point.</returns>
|
---|
127 | protected override double EvaluateFunction(RealVector point) {
|
---|
128 | return Apply(point, C.Value, Alpha.Value);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|