Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Problems.TestFunctions/3.3/Functions/Multinormal.cs @ 15071

Last change on this file since 15071 was 13403, checked in by abeham, 9 years ago

#2521:

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