1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
7 | using System.Diagnostics;
|
---|
8 | using HeuristicLab.Core;
|
---|
9 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
10 | using HeuristicLab.Parameters;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Problems.TestFunctions.Evaluators {
|
---|
13 | [Item("MultinormalFunction", "Evaluates a random multinormal function on a given point.")]
|
---|
14 | [StorableClass]
|
---|
15 | public class MultinormalEvaluator : SingleObjectiveTestFunctionProblemEvaluator {
|
---|
16 |
|
---|
17 | private ItemList<RealVector> centers {
|
---|
18 | get { return (ItemList<RealVector>)Parameters["Centers"].ActualValue; }
|
---|
19 | set { Parameters["Centers"].ActualValue = value; }
|
---|
20 | }
|
---|
21 | private RealVector s_2s {
|
---|
22 | get { return (RealVector)Parameters["s^2s"].ActualValue; }
|
---|
23 | set { Parameters["s^2s"].ActualValue = value; }
|
---|
24 | }
|
---|
25 |
|
---|
26 | [StorableConstructor]
|
---|
27 | public MultinormalEvaluator(bool deserializing) { }
|
---|
28 |
|
---|
29 | public MultinormalEvaluator() {
|
---|
30 | Parameters.Add(new ValueParameter<ItemList<RealVector>>("Centers", "Centers of normal distributions"));
|
---|
31 | Parameters.Add(new ValueParameter<RealVector>("s^2s", "sigma^2 of normal distributions"));
|
---|
32 | centers = new ItemList<RealVector>() {
|
---|
33 | new RealVector(new double[] { -5.0, -5.0 }),
|
---|
34 | new RealVector(new double[] { 5.0, -5.0 }),
|
---|
35 | new RealVector(new double[] { -5.0, 5.0 }),
|
---|
36 | new RealVector(new double[] { 5.0, 5.0 }),
|
---|
37 | };
|
---|
38 | s_2s = new RealVector(new double[] { 0.2, 1, 1, 2 });
|
---|
39 | }
|
---|
40 |
|
---|
41 | private double FastFindOptimum(out RealVector bestSolution) {
|
---|
42 | var optima = centers.Select((c, i) => new { f = EvaluateFunction(c), i }).OrderBy(v => v.f).ToList();
|
---|
43 | if (optima.Count == 0) {
|
---|
44 | bestSolution = new RealVector();
|
---|
45 | return 0;
|
---|
46 | } else {
|
---|
47 | var best = optima.First();
|
---|
48 | bestSolution = centers[best.i];
|
---|
49 | return best.f;
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public static double N(RealVector x, RealVector x0, double s_2) {
|
---|
54 | Debug.Assert(x.Length == x0.Length);
|
---|
55 | double d = 0;
|
---|
56 | for (int i = 0; i < x.Length; i++) {
|
---|
57 | d += (x[i] - x0[i]) * (x[i] - x0[i]);
|
---|
58 | }
|
---|
59 | return Math.Exp(-d / (2 * s_2)) / (2 * Math.PI * s_2);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public override bool Maximization {
|
---|
63 | get { return false; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public override DoubleMatrix Bounds {
|
---|
67 | get { return new DoubleMatrix(new double[,] { { -10, 10 } }); }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public override double BestKnownQuality {
|
---|
71 | get {
|
---|
72 | RealVector bestSolution;
|
---|
73 | return FastFindOptimum(out bestSolution);
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | public override int MinimumProblemSize { get { return 1; } }
|
---|
78 |
|
---|
79 | public override int MaximumProblemSize { get { return 1000; } }
|
---|
80 |
|
---|
81 | private RealVector Shorten(RealVector x, int dimensions) {
|
---|
82 | return new RealVector(x.Take(dimensions).ToArray());
|
---|
83 | }
|
---|
84 |
|
---|
85 | public override RealVector GetBestKnownSolution(int dimension) {
|
---|
86 | RealVector bestSolution;
|
---|
87 | FastFindOptimum(out bestSolution);
|
---|
88 | return Shorten(bestSolution, dimension);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public double Evaluate(RealVector point) {
|
---|
92 | return EvaluateFunction(point);
|
---|
93 | }
|
---|
94 |
|
---|
95 | protected override double EvaluateFunction(RealVector point) {
|
---|
96 | double value = 0;
|
---|
97 | for (int i = 0; i < centers.Count; i++) {
|
---|
98 | value -= N(point, Shorten(centers[i], point.Length), s_2s[i]);
|
---|
99 | }
|
---|
100 | return value;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|