Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Problems.TestFunctions/3.3/Evaluators/MultinormalEvaluator.cs @ 6036

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

Merged cloning refactoring branch back into trunk (#922)

File size: 6.3 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Diagnostics;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.RealVectorEncoding;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Problems.TestFunctions.Evaluators {
34  [Item("MultinormalFunction", "Evaluates a random multinormal function on a given point.")]
35  [StorableClass]
36  public class MultinormalEvaluator : SingleObjectiveTestFunctionProblemEvaluator {
37
38    private ItemList<RealVector> centers {
39      get { return (ItemList<RealVector>)Parameters["Centers"].ActualValue; }
40      set { Parameters["Centers"].ActualValue = value; }
41    }
42    private RealVector s_2s {
43      get { return (RealVector)Parameters["s^2s"].ActualValue; }
44      set { Parameters["s^2s"].ActualValue = value; }
45    }
46    private static Random Random = new Random();
47
48    private Dictionary<int, List<RealVector>> stdCenters;
49    public IEnumerable<RealVector> Centers(int nDim) {
50      if (stdCenters == null)
51        stdCenters = new Dictionary<int, List<RealVector>>();
52      if (!stdCenters.ContainsKey(nDim))
53        stdCenters[nDim] = GetCenters(nDim).ToList();
54      return stdCenters[nDim];
55    }
56
57    private IEnumerable<RealVector> GetCenters(int nDim) {
58      RealVector r0 = new RealVector(nDim);
59      for (int i = 0; i < r0.Length; i++)
60        r0[i] = 5;
61      yield return r0;
62      for (int i = 1; i < 1 << nDim; i++) {
63        RealVector r = new RealVector(nDim);
64        for (int j = 0; j < nDim; j++) {
65          r[j] = (i >> j) % 2 == 0 ? Random.NextDouble() + 4.5 : Random.NextDouble() + 14.5;
66        }
67        yield return r;
68      }
69    }
70
71    private Dictionary<int, List<double>> stdSigma_2s;
72    public IEnumerable<double> Sigma_2s(int nDim) {
73      if (stdSigma_2s == null)
74        stdSigma_2s = new Dictionary<int, List<double>>();
75      if (!stdSigma_2s.ContainsKey(nDim))
76        stdSigma_2s[nDim] = GetSigma_2s(nDim).ToList();
77      return stdSigma_2s[nDim];
78    }
79    private IEnumerable<double> GetSigma_2s(int nDim) {
80      yield return 0.2;
81      for (int i = 1; i < (1 << nDim) - 1; i++) {
82        yield return Random.NextDouble() * 0.5 + 0.75;
83      }
84      yield return 2;
85    }
86
87    [StorableConstructor]
88    protected MultinormalEvaluator(bool deserializing) : base(deserializing) { }
89    protected MultinormalEvaluator(MultinormalEvaluator original, Cloner cloner) : base(original, cloner) { }
90    public MultinormalEvaluator() {
91      Parameters.Add(new ValueParameter<ItemList<RealVector>>("Centers", "Centers of normal distributions"));
92      Parameters.Add(new ValueParameter<RealVector>("s^2s", "sigma^2 of normal distributions"));
93      Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator"));
94      centers = new ItemList<RealVector>();
95      s_2s = new RealVector();
96    }
97
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new MultinormalEvaluator(this, cloner);
100    }
101
102    private double FastFindOptimum(out RealVector bestSolution) {
103      var optima = centers.Select((c, i) => new { f = EvaluateFunction(c), i }).OrderBy(v => v.f).ToList();
104      if (optima.Count == 0) {
105        bestSolution = new RealVector();
106        return 0;
107      } else {
108        var best = optima.First();
109        bestSolution = centers[best.i];
110        return best.f;
111      }
112    }
113
114    public static double N(RealVector x, RealVector x0, double s_2) {
115      Debug.Assert(x.Length == x0.Length);
116      double d = 0;
117      for (int i = 0; i < x.Length; i++) {
118        d += (x[i] - x0[i]) * (x[i] - x0[i]);
119      }
120      return Math.Exp(-d / (2 * s_2)) / (2 * Math.PI * s_2);
121    }
122
123    public override bool Maximization {
124      get { return false; }
125    }
126
127    public override DoubleMatrix Bounds {
128      get { return new DoubleMatrix(new double[,] { { 0, 20 } }); }
129    }
130
131    public override double BestKnownQuality {
132      get {
133        if (centers.Count == 0) {
134          return -1 / (2 * Math.PI * 0.2);
135        } else {
136          RealVector bestSolution;
137          return FastFindOptimum(out bestSolution);
138        }
139      }
140    }
141
142    public override int MinimumProblemSize { get { return 1; } }
143
144    public override int MaximumProblemSize { get { return 100; } }
145
146    private RealVector Shorten(RealVector x, int dimensions) {
147      return new RealVector(x.Take(dimensions).ToArray());
148    }
149
150    public override RealVector GetBestKnownSolution(int dimension) {
151      if (centers.Count == 0) {
152        RealVector r = new RealVector(dimension);
153        for (int i = 0; i < r.Length; i++)
154          r[i] = 5;
155        return r;
156      } else {
157        RealVector bestSolution;
158        FastFindOptimum(out bestSolution);
159        return Shorten(bestSolution, dimension);
160      }
161    }
162
163    public double Evaluate(RealVector point) {
164      return EvaluateFunction(point);
165    }
166
167    protected override double EvaluateFunction(RealVector point) {
168      double value = 0;
169      if (centers.Count == 0) {
170        var c = Centers(point.Length).GetEnumerator();
171        var s = Sigma_2s(point.Length).GetEnumerator();
172        while (c.MoveNext() && s.MoveNext()) {
173          value -= N(point, c.Current, s.Current);
174        }
175      } else {
176        for (int i = 0; i < centers.Count; i++) {
177          value -= N(point, centers[i], s_2s[i]);
178        }
179      }
180      return value;
181    }
182  }
183}
Note: See TracBrowser for help on using the repository browser.