[4722] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[4722] | 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;
|
---|
[3869] | 23 | using System.Collections.Generic;
|
---|
[4068] | 24 | using System.Diagnostics;
|
---|
[3869] | 25 | using System.Linq;
|
---|
[4722] | 26 | using HeuristicLab.Common;
|
---|
[4068] | 27 | using HeuristicLab.Core;
|
---|
[3869] | 28 | using HeuristicLab.Data;
|
---|
| 29 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
[4068] | 30 | using HeuristicLab.Parameters;
|
---|
[3869] | 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.TestFunctions.Evaluators {
|
---|
| 34 | [Item("MultinormalFunction", "Evaluates a random multinormal function on a given point.")]
|
---|
| 35 | [StorableClass]
|
---|
| 36 | public class MultinormalEvaluator : SingleObjectiveTestFunctionProblemEvaluator {
|
---|
[9990] | 37 | public override string FunctionName { get { return "Multinormal"; } }
|
---|
[4068] | 38 |
|
---|
[3869] | 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; }
|
---|
[3912] | 46 | }
|
---|
[8587] | 47 | private static System.Random Random = new System.Random();
|
---|
[3869] | 48 |
|
---|
[3912] | 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;
|
---|
[4068] | 82 | for (int i = 1; i < (1 << nDim) - 1; i++) {
|
---|
[3912] | 83 | yield return Random.NextDouble() * 0.5 + 0.75;
|
---|
| 84 | }
|
---|
| 85 | yield return 2;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[4722] | 88 | [StorableConstructor]
|
---|
| 89 | protected MultinormalEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 90 | protected MultinormalEvaluator(MultinormalEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
[3912] | 91 | public MultinormalEvaluator() {
|
---|
[3869] | 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"));
|
---|
[3912] | 94 | Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator"));
|
---|
| 95 | centers = new ItemList<RealVector>();
|
---|
| 96 | s_2s = new RealVector();
|
---|
[3869] | 97 | }
|
---|
[4068] | 98 |
|
---|
[4722] | 99 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 100 | return new MultinormalEvaluator(this, cloner);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[3869] | 103 | private double FastFindOptimum(out RealVector bestSolution) {
|
---|
[9407] | 104 | var optima = centers.Select((c, i) => new { f = Evaluate(c), i }).OrderBy(v => v.f).ToList();
|
---|
[3869] | 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 {
|
---|
[3912] | 129 | get { return new DoubleMatrix(new double[,] { { 0, 20 } }); }
|
---|
[3869] | 130 | }
|
---|
| 131 |
|
---|
| 132 | public override double BestKnownQuality {
|
---|
| 133 | get {
|
---|
[3912] | 134 | if (centers.Count == 0) {
|
---|
[4068] | 135 | return -1 / (2 * Math.PI * 0.2);
|
---|
[3912] | 136 | } else {
|
---|
| 137 | RealVector bestSolution;
|
---|
| 138 | return FastFindOptimum(out bestSolution);
|
---|
| 139 | }
|
---|
[3869] | 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | public override int MinimumProblemSize { get { return 1; } }
|
---|
| 144 |
|
---|
[3912] | 145 | public override int MaximumProblemSize { get { return 100; } }
|
---|
[3869] | 146 |
|
---|
| 147 | private RealVector Shorten(RealVector x, int dimensions) {
|
---|
[3912] | 148 | return new RealVector(x.Take(dimensions).ToArray());
|
---|
[3869] | 149 | }
|
---|
| 150 |
|
---|
| 151 | public override RealVector GetBestKnownSolution(int dimension) {
|
---|
[3912] | 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 | }
|
---|
[3869] | 162 | }
|
---|
| 163 |
|
---|
[9407] | 164 | public override double Evaluate(RealVector point) {
|
---|
[3869] | 165 | double value = 0;
|
---|
[3912] | 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 | }
|
---|
[3869] | 176 | }
|
---|
| 177 | return value;
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | }
|
---|