Free cookie consent management tool by TermsFeed Policy Generator

source: branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/SamplingMethods/LatinHyperCubeDesign.cs @ 15870

Last change on this file since 15870 was 14833, checked in by bwerth, 8 years ago

#2745 added LatinHyperCubeDesign as possible InitialSamplingPlan

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33// ReSharper disable once CheckNamespace
34namespace HeuristicLab.Algorithms.EGO {
35
36  [StorableClass]
37  [Item("LatinHyperCubeDesign", "A latin hypercube sampling strategy for real valued optimization")]
38  public class LatinHyperCubeDesign : ParameterizedNamedItem, IInitialSampling {
39
40    public const string DesigningAlgorithmParamterName = "DesignningAlgorithm";
41    public IValueParameter<IAlgorithm> DesigningAlgorithmParameter => Parameters[DesigningAlgorithmParamterName] as IValueParameter<IAlgorithm>;
42    private IAlgorithm DesigningAlgorithm => DesigningAlgorithmParameter.Value;
43
44    [StorableConstructor]
45    private LatinHyperCubeDesign(bool deserializing) : base(deserializing) { }
46    private LatinHyperCubeDesign(LatinHyperCubeDesign original, Cloner cloner) : base(original, cloner) { }
47    public LatinHyperCubeDesign() {
48      var es = new EvolutionStrategy.EvolutionStrategy {
49        PopulationSize = { Value = 50 },
50        Children = { Value = 100 },
51        MaximumGenerations = { Value = 300 }
52      };
53      Parameters.Add(new ValueParameter<IAlgorithm>(DesigningAlgorithmParamterName, "The Algorithm used for optimizing the Latin Hypercube (minimax) criterion.", es));
54    }
55    public override IDeepCloneable Clone(Cloner cloner) {
56      return new LatinHyperCubeDesign(this, cloner);
57    }
58
59    public RealVector[] GetSamples(int noSamples, RealVector[] existingSamples, RealVectorEncoding encoding, IRandom random) {
60      var lhsprob = new LHSProblem(noSamples, existingSamples, encoding);
61      DesigningAlgorithm.Problem = lhsprob;
62      EgoUtilities.SyncRunSubAlgorithm(DesigningAlgorithm, random.Next());
63      return lhsprob.BestSolution;
64    }
65  }
66
67  [StorableClass]
68  internal class LHSProblem : SingleObjectiveBasicProblem<RealVectorEncoding> {
69    [Storable]
70    private int NoVectors;
71    [Storable]
72    private int NoDimensions;
73    [Storable]
74    private RealVector[] ExistingSamples;
75
76    [Storable]
77    public double BestQuality = double.MinValue;
78    [Storable]
79    public RealVector[] BestSolution;
80
81    [StorableConstructor]
82    private LHSProblem(bool deserializing) : base(deserializing) { }
83
84    private LHSProblem(LHSProblem original, Cloner cloner) : base(original, cloner) {
85      NoDimensions = original.NoDimensions;
86      NoVectors = original.NoVectors;
87      ExistingSamples = original.ExistingSamples?.Select(cloner.Clone).ToArray();
88    }
89
90    public LHSProblem(int novectors, RealVector[] existingSamples, RealVectorEncoding encoding) {
91      NoVectors = novectors;
92      NoDimensions = encoding.Length;
93      ExistingSamples = existingSamples;
94      Encoding.Length = novectors * encoding.Length;
95      var b = new DoubleMatrix(Encoding.Length, 2);
96      for (int i = 0; i < novectors; i++) {
97        for (int j = 0; j < encoding.Length; j++) {
98          var k = j % encoding.Bounds.Rows;
99          b[i * encoding.Length + j, 0] = encoding.Bounds[k, 0];
100          b[i * encoding.Length + j, 1] = encoding.Bounds[k, 1];
101        }
102      }
103      Encoding.Bounds = b;
104
105    }
106    public override IDeepCloneable Clone(Cloner cloner) {
107      return new LHSProblem(this, cloner);
108    }
109
110    public override double Evaluate(Individual individual, IRandom random) {
111      return Enumerable.Range(0, NoVectors).Select(x => MinDistance(x, individual.RealVector())).Min();
112    }
113
114    //returns -1 if no distance can be calculated
115    private double MinDistance(int pointNumber, RealVector design) {
116      var min = double.MaxValue;
117      if (ExistingSamples != null && ExistingSamples.Length != 0) min = ExistingSamples.Select(x => Distance(x, ExtractPoint(pointNumber, design))).Min();
118      if (pointNumber == 0) return min;
119      var min2 = Enumerable.Range(0, pointNumber).Select(i => Distance(ExtractPoint(i, design), ExtractPoint(pointNumber, design))).Min();
120      return min < 0 ? min2 : Math.Min(min, min2);
121    }
122    private IEnumerable<double> ExtractPoint(int pointNumber, RealVector design) {
123      return design.Skip(NoDimensions * pointNumber).Take(NoDimensions);
124    }
125    private static double Distance(IEnumerable<double> a, IEnumerable<double> b) {
126      return a.Zip(b, (d, d1) => d - d1).Sum(d => d * d);
127    }
128
129    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
130      base.Analyze(individuals, qualities, results, random);
131      var i = qualities.ArgMin(x => x);
132      if (BestQuality > qualities[i]) return;
133      var r = individuals[i].RealVector();
134      BestSolution = Enumerable.Range(0, NoVectors).Select(x => new RealVector(ExtractPoint(x, r).ToArray())).ToArray();
135    }
136
137    public override bool Maximization => true;
138  }
139
140
141
142
143}
144
Note: See TracBrowser for help on using the repository browser.