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

Last change on this file since 15338 was 15338, checked in by bwerth, 5 years ago

#2745 fixed bug concerning new Start and StartAsync methods; passed CancellationToken to sub algorithms

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