Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2745 added discretized EGO-version for use with IntegerVectors

File size: 5.4 KB
RevLine 
[15064]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;
[15338]23using System.Threading;
[15064]24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.RealVectorEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.DataAnalysis;
32
33namespace HeuristicLab.Algorithms.EGO {
34  [Item("InfillSolver", "A RealVectorCreator that creates candidates by optimizing an infill-subproblem")]
35  [StorableClass]
[15338]36  public class InfillSolver : RealVectorCreator, ICancellableOperator {
[15064]37
38    public ILookupParameter<IAlgorithm> InfillOptimizationAlgorithmParamter => (ILookupParameter<IAlgorithm>)Parameters["InfillAlgorithm"];
39    public ILookupParameter<IRegressionSolution> ModelParameter => (ILookupParameter<IRegressionSolution>)Parameters["Model"];
40    public ILookupParameter<BoolValue> MaximizationParameter => (ILookupParameter<BoolValue>)Parameters["Maximization"];
41    public ILookupParameter<BoolValue> RemoveDuplicatesParameter => (ILookupParameter<BoolValue>)Parameters["RemoveDuplicates"];
42    public IFixedValueParameter<DoubleValue> DuplicateCutoffParameter => (IFixedValueParameter<DoubleValue>)Parameters["Duplicates Cutoff"];
43    public ILookupParameter<DoubleMatrix> InfillBoundsParameter => (ILookupParameter<DoubleMatrix>)Parameters["InfillBounds"];
44
[15338]45    public CancellationToken Cancellation { get; set; }
46
[15064]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
[15338]79    private RealVector OptimizeInfillProblem(IAlgorithm algorithm, IRegressionSolution model, bool maximization, DoubleMatrix bounds, int length, IRandom random) {
[15064]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);
[15338]85      var res = EgoUtilities.SyncRunSubAlgorithm(algorithm, random.Next(int.MaxValue), Cancellation);
[15064]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
[15338]106
[15064]107  }
108}
Note: See TracBrowser for help on using the repository browser.