Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.TestFunctions/3.3/Evaluators/MultinormalEvaluator.cs @ 4068

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

Sorted usings and removed unused usings in entire solution (#1094)

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