Free cookie consent management tool by TermsFeed Policy Generator

source: branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Operators/InfillSolver.cs @ 15064

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

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

File size: 5.4 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.RealVectorEncoding;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31
32namespace HeuristicLab.Algorithms.EGO {
33  /// <summary>
34  /// A base class for operators that manipulate real-valued vectors.
35  /// </summary>
36  [Item("InfillSolver", "A RealVectorCreator that creates candidates by optimizing an infill-subproblem")]
37  [StorableClass]
38  public class InfillSolver : RealVectorCreator {
39
40    public ILookupParameter<IAlgorithm> InfillOptimizationAlgorithmParamter => (ILookupParameter<IAlgorithm>)Parameters["InfillAlgorithm"];
41    public ILookupParameter<IRegressionSolution> ModelParameter => (ILookupParameter<IRegressionSolution>)Parameters["Model"];
42    public ILookupParameter<BoolValue> MaximizationParameter => (ILookupParameter<BoolValue>)Parameters["Maximization"];
43    public ILookupParameter<BoolValue> RemoveDuplicatesParameter => (ILookupParameter<BoolValue>)Parameters["RemoveDuplicates"];
44    public IFixedValueParameter<DoubleValue> DuplicateCutoffParameter => (IFixedValueParameter<DoubleValue>)Parameters["Duplicates Cutoff"];
45    public ILookupParameter<DoubleMatrix> InfillBoundsParameter => (ILookupParameter<DoubleMatrix>)Parameters["InfillBounds"];
46
47    [StorableConstructor]
48    protected InfillSolver(bool deserializing) : base(deserializing) { }
49    protected InfillSolver(InfillSolver original, Cloner cloner) : base(original, cloner) { }
50    public InfillSolver() {
51      Parameters.Add(new LookupParameter<IAlgorithm>("InfillAlgorithm", "The algorithm used to optimize the infill problem") { Hidden = true });
52      Parameters.Add(new LookupParameter<IRegressionSolution>("Model", "The RegressionSolution upon which the InfillProblem operates") { Hidden = true });
53      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Whether the original problem is a maximization problem") { Hidden = true });
54      Parameters.Add(new LookupParameter<BoolValue>("RemoveDuplicates", "Whether duplicates shall be removed") { Hidden = true });
55      Parameters.Add(new FixedValueParameter<DoubleValue>("Duplicates Cutoff", "The cut off radius for", new DoubleValue(0.01)) { Hidden = false });
56      Parameters.Add(new LookupParameter<DoubleMatrix>("InfillBounds", "The bounds applied for infill solving") { Hidden = true });
57    }
58
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new InfillSolver(this, cloner);
61    }
62
63    protected override RealVector Create(IRandom random, IntValue length, DoubleMatrix bounds) {
64      var infillBounds = InfillBoundsParameter.ActualValue;
65      if (infillBounds != null && infillBounds.Rows > 0) {
66        bounds = infillBounds;
67      }
68
69      var alg = InfillOptimizationAlgorithmParamter.ActualValue;
70      var model = ModelParameter.ActualValue;
71      var max = MaximizationParameter.ActualValue.Value;
72      var res = OptimizeInfillProblem(alg, model, max, bounds, length.Value, random);
73      var rad = DuplicateCutoffParameter.Value.Value;
74      if (!RemoveDuplicatesParameter.ActualValue.Value || !(GetMinDifference(model.ProblemData.Dataset, res) < rad * rad)) return res;
75      for (var i = 0; i < res.Length; i++) res[i] += random.NextDouble() * rad * 2;
76      return res;
77    }
78
79    public static RealVector OptimizeInfillProblem(IAlgorithm algorithm, IRegressionSolution model, bool maximization, DoubleMatrix bounds, int length, IRandom random) {
80      var infillProblem = algorithm.Problem as InfillProblem;
81      if (infillProblem == null) throw new ArgumentException("The algortihm has no InfillProblem to solve");
82      infillProblem.Encoding.Length = length;
83      infillProblem.Encoding.Bounds = bounds;
84      infillProblem.Initialize(model, maximization);
85      var res = EgoUtilities.SyncRunSubAlgorithm(algorithm, random.Next(int.MaxValue));
86      var v = res[InfillProblem.BestInfillSolutionResultName].Value as RealVector;
87      algorithm.Runs.Clear();
88      return v;
89    }
90
91    private static double GetMinDifference(IDataset data, RealVector r) {
92      var mind = double.MaxValue;
93      for (var i = 0; i < data.Rows; i++) {
94        var d = 0.0;
95        for (var j = 0; j < r.Length; j++) {
96          var d2 = data.GetDoubleValue("input" + j, i) - r[j];
97          d += d2 * d2;
98        }
99        if (!(d < mind)) continue;
100        mind = d;
101      }
102      return mind;
103    }
104
105
106  }
107}
Note: See TracBrowser for help on using the repository browser.