[4722] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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;
|
---|
[13403] | 32 | using HeuristicLab.Random;
|
---|
[3869] | 33 |
|
---|
| 34 | namespace HeuristicLab.Problems.TestFunctions.Evaluators {
|
---|
[13403] | 35 | [Item("Multinormal", "Evaluates a random multinormal function on a given point.")]
|
---|
[3869] | 36 | [StorableClass]
|
---|
[13403] | 37 | public class Multinormal : SingleObjectiveTestFunction {
|
---|
[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 | }
|
---|
[3869] | 47 |
|
---|
[13403] | 48 | public IRandom Random {
|
---|
| 49 | get { return ((ValueParameter<IRandom>)Parameters["Random"]).Value; }
|
---|
| 50 | set { ((ValueParameter<IRandom>)Parameters["Random"]).Value = value; }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[3912] | 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;
|
---|
[4068] | 86 | for (int i = 1; i < (1 << nDim) - 1; i++) {
|
---|
[3912] | 87 | yield return Random.NextDouble() * 0.5 + 0.75;
|
---|
| 88 | }
|
---|
| 89 | yield return 2;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[4722] | 92 | [StorableConstructor]
|
---|
[13403] | 93 | protected Multinormal(bool deserializing) : base(deserializing) { }
|
---|
| 94 | protected Multinormal(Multinormal original, Cloner cloner) : base(original, cloner) { }
|
---|
| 95 | public Multinormal() {
|
---|
[3869] | 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"));
|
---|
[13403] | 98 | Parameters.Add(new ValueParameter<IRandom>("Random", "The random number generator that will make random instances", new MersenneTwister(0)));
|
---|
[3912] | 99 | centers = new ItemList<RealVector>();
|
---|
| 100 | s_2s = new RealVector();
|
---|
[3869] | 101 | }
|
---|
[4068] | 102 |
|
---|
[4722] | 103 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[13403] | 104 | return new Multinormal(this, cloner);
|
---|
[4722] | 105 | }
|
---|
| 106 |
|
---|
[3869] | 107 | private double FastFindOptimum(out RealVector bestSolution) {
|
---|
[9407] | 108 | var optima = centers.Select((c, i) => new { f = Evaluate(c), i }).OrderBy(v => v.f).ToList();
|
---|
[3869] | 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 {
|
---|
[3912] | 133 | get { return new DoubleMatrix(new double[,] { { 0, 20 } }); }
|
---|
[3869] | 134 | }
|
---|
| 135 |
|
---|
| 136 | public override double BestKnownQuality {
|
---|
| 137 | get {
|
---|
[3912] | 138 | if (centers.Count == 0) {
|
---|
[4068] | 139 | return -1 / (2 * Math.PI * 0.2);
|
---|
[3912] | 140 | } else {
|
---|
| 141 | RealVector bestSolution;
|
---|
| 142 | return FastFindOptimum(out bestSolution);
|
---|
| 143 | }
|
---|
[3869] | 144 | }
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | public override int MinimumProblemSize { get { return 1; } }
|
---|
| 148 |
|
---|
[3912] | 149 | public override int MaximumProblemSize { get { return 100; } }
|
---|
[3869] | 150 |
|
---|
| 151 | private RealVector Shorten(RealVector x, int dimensions) {
|
---|
[3912] | 152 | return new RealVector(x.Take(dimensions).ToArray());
|
---|
[3869] | 153 | }
|
---|
| 154 |
|
---|
| 155 | public override RealVector GetBestKnownSolution(int dimension) {
|
---|
[3912] | 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 | }
|
---|
[3869] | 166 | }
|
---|
| 167 |
|
---|
[9407] | 168 | public override double Evaluate(RealVector point) {
|
---|
[3869] | 169 | double value = 0;
|
---|
[3912] | 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 | }
|
---|
[3869] | 180 | }
|
---|
| 181 | return value;
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | }
|
---|