Free cookie consent management tool by TermsFeed Policy Generator

source: branches/EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Problems/InfillProblem.cs @ 15689

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

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

File size: 5.3 KB
RevLine 
[14741]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;
[15064]23using System.Collections.Generic;
24using System.Linq;
[14741]25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[15064]31using HeuristicLab.Problems.DataAnalysis;
[14741]32
[14768]33namespace HeuristicLab.Algorithms.EGO {
[14741]34  [StorableClass]
35  [Item("InfillProblem", "A problem for finding the most interesing potential new sampling Points by optimizing some InfillCriterion")]
36  public sealed class InfillProblem : SingleObjectiveBasicProblem<RealVectorEncoding> {
37
[15064]38    public override bool Maximization => true;
[14741]39
[15064]40    #region ProblemResultNames
41    public const string BestInfillSolutionResultName = "BestInfillSolution";
42    public const string BestInfillQualityResultName = "BestInfillQuality";
43    #endregion
44
45    #region Properties
[14741]46    [Storable]
47    private IInfillCriterion infillCriterion;
48
49    public IInfillCriterion InfillCriterion
50    {
51      get { return infillCriterion; }
52      set
53      {
[15064]54        infillCriterion = value;
55        infillCriterion.Encoding = Encoding;
[14741]56      }
57    }
58    #endregion
59
[15064]60    #region Constructors
[14741]61    [StorableConstructor]
62    private InfillProblem(bool deserializing) : base(deserializing) { }
63    private InfillProblem(InfillProblem original, Cloner cloner) : base(original, cloner) {
[15064]64      infillCriterion = cloner.Clone(original.infillCriterion);
[14741]65    }
66    public InfillProblem() { }
67    public override IDeepCloneable Clone(Cloner cloner) { return new InfillProblem(this, cloner); }
68    #endregion
69
70    public override double Evaluate(Individual individual, IRandom r) {
[15064]71      return !InBounds(individual.RealVector(), Encoding.Bounds) ? double.MinValue : InfillCriterion.Evaluate(individual.RealVector());
[14741]72    }
73    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
74      base.Analyze(individuals, qualities, results, random);
75      var best = qualities.ArgMax(x => x);
[15064]76      var newQuality = qualities[best];
77      if (!results.ContainsKey(BestInfillQualityResultName)) {
78        results.Add(new Result(BestInfillSolutionResultName, (RealVector)individuals[best].RealVector().Clone()));
79        results.Add(new Result(BestInfillQualityResultName, new DoubleValue(newQuality)));
[14741]80        return;
81      }
[15064]82      var qold = results[BestInfillQualityResultName].Value as DoubleValue;
[14741]83      if (qold == null) throw new ArgumentException("Old best quality is not a double value. Conflicting Analyzers?");
[15064]84      if (qold.Value >= newQuality) return;
85      results[BestInfillSolutionResultName].Value = (RealVector)individuals[best].RealVector().Clone();
86      qold.Value = newQuality;
[14741]87    }
[15064]88    public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
89      var bounds = Encoding.Bounds;
90      var michalewiczIteration = 0;
91      while (true) {
92        var neighbour = individual.Copy();
93        var r = neighbour.RealVector();
94        switch (random.Next(5)) {
95          case 0: UniformOnePositionManipulator.Apply(random, r, bounds); break;
96          case 1: UniformOnePositionManipulator.Apply(random, r, bounds); break;//FixedNormalAllPositionsManipulator.Apply(random, r, new RealVector(new[] { 0.1 })); break;
97          case 2: MichalewiczNonUniformAllPositionsManipulator.Apply(random, r, bounds, new IntValue(michalewiczIteration++), new IntValue(10000), new DoubleValue(5.0)); break;
98          case 3: MichalewiczNonUniformOnePositionManipulator.Apply(random, r, bounds, new IntValue(michalewiczIteration++), new IntValue(10000), new DoubleValue(5.0)); break;
99          case 4: BreederGeneticAlgorithmManipulator.Apply(random, r, bounds, new DoubleValue(0.1)); break;
100          default: throw new NotImplementedException();
101        }
102        yield return neighbour;
103        michalewiczIteration %= 10000;
104      }
105    }
106
107    public void Initialize(IRegressionSolution model, bool expensiveMaximization) {
108      infillCriterion.RegressionSolution = model;
109      infillCriterion.ExpensiveMaximization = expensiveMaximization;
110      infillCriterion.Encoding = Encoding;
111      infillCriterion.Initialize();
112    }
113
114    #region helpers
115    private static bool InBounds(RealVector r, DoubleMatrix bounds) {
116      return !r.Where((t, i) => t < bounds[i % bounds.Rows, 0] || t > bounds[i % bounds.Rows, 1]).Any();
117    }
118    #endregion
119
[14741]120  }
121}
Note: See TracBrowser for help on using the repository browser.