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 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HEAL.Attic;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
31 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 |
|
---|
34 | // ReSharper disable once CheckNamespace
|
---|
35 | namespace HeuristicLab.Algorithms.EGO {
|
---|
36 |
|
---|
37 | [StorableType("03d03890-d122-4730-b7bf-378a40d90a22")]
|
---|
38 | [Item("LatinHyperCubeDesignCreator", "A latin hypercube sampling strategy for real valued optimization")]
|
---|
39 | public class LatinHyperCubeDesignCreator : RealVectorCreator, IInitialSampling<RealVector> {
|
---|
40 | private IValueLookupParameter<IntValue> DesignSize => (IValueLookupParameter<IntValue>)Parameters["DesignSize"];
|
---|
41 | [Storable]
|
---|
42 | private Queue<RealVector> design = new Queue<RealVector>();
|
---|
43 |
|
---|
44 | [StorableConstructor]
|
---|
45 | protected LatinHyperCubeDesignCreator(StorableConstructorFlag deserializing) : base(deserializing) { }
|
---|
46 |
|
---|
47 | protected LatinHyperCubeDesignCreator(LatinHyperCubeDesignCreator original, Cloner cloner) : base(original, cloner) {
|
---|
48 | design = new Queue<RealVector>(original.design.Select(cloner.Clone));
|
---|
49 | }
|
---|
50 | public LatinHyperCubeDesignCreator() {
|
---|
51 | Parameters.Add(new ValueLookupParameter<IntValue>("DesignSize", "The size of the LHS-design produced by this creator"));
|
---|
52 | }
|
---|
53 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
54 | return new LatinHyperCubeDesignCreator(this, cloner);
|
---|
55 | }
|
---|
56 |
|
---|
57 | public RealVector[] GetSamples(int noSamples, RealVector[] existingSamples, IEncoding encoding, IRandom random) {
|
---|
58 | var enc = encoding as RealVectorEncoding;
|
---|
59 |
|
---|
60 |
|
---|
61 | return GetSamples(random, enc.Length, enc.Bounds, noSamples);
|
---|
62 | }
|
---|
63 |
|
---|
64 | public RealVector[] GetSamples(IRandom random, int dimensions, DoubleMatrix bounds, int noSamples) {
|
---|
65 | if (noSamples < 1) throw new ArgumentException("Designs need to have at least 1 Point");
|
---|
66 | var samples = new RealVector[noSamples];
|
---|
67 | for (var i = 0; i < noSamples; i++) samples[i] = new RealVector(dimensions);
|
---|
68 |
|
---|
69 | for (var i = 0; i < dimensions; i++) {
|
---|
70 | var permutations = new Permutation(PermutationTypes.Absolute, noSamples, random);
|
---|
71 | var row = i % bounds.Rows;
|
---|
72 | var offset = bounds[row, 0];
|
---|
73 | var range = (bounds[row, 1] - offset) / noSamples;
|
---|
74 | for (var j = 0; j < noSamples; j++) samples[j][i] = offset + (permutations[j] + random.NextDouble()) * range;
|
---|
75 | }
|
---|
76 | return samples;
|
---|
77 | }
|
---|
78 |
|
---|
79 | protected override RealVector Create(IRandom random, IntValue length, DoubleMatrix bounds) {
|
---|
80 | if (design.Count == 0) design = new Queue<RealVector>(GetSamples(random, length.Value, bounds, DesignSize.ActualValue.Value));
|
---|
81 | return design.Dequeue();
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|