Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2745_EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/SamplingMethods/LatinHyperCubeDesignOLD.cs @ 17146

Last change on this file since 17146 was 15064, checked in by bwerth, 8 years ago

#2745 implemented EGO as EngineAlgorithm + some simplifications in the IInfillCriterion interface

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