Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.TestFunctions/3.3/Evaluators/MultinormalEvaluator.cs @ 17234

Last change on this file since 17234 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

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