Free cookie consent management tool by TermsFeed Policy Generator

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

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

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

File size: 5.3 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.RealVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.DataAnalysis;
32
33namespace HeuristicLab.Algorithms.EGO {
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
38    public override bool Maximization => true;
39
40    #region ProblemResultNames
41    public const string BestInfillSolutionResultName = "BestInfillSolution";
42    public const string BestInfillQualityResultName = "BestInfillQuality";
43    #endregion
44
45    #region Properties
46    [Storable]
47    private IInfillCriterion infillCriterion;
48
49    public IInfillCriterion InfillCriterion
50    {
51      get { return infillCriterion; }
52      set
53      {
54        infillCriterion = value;
55        infillCriterion.Encoding = Encoding;
56      }
57    }
58    #endregion
59
60    #region Constructors
61    [StorableConstructor]
62    private InfillProblem(bool deserializing) : base(deserializing) { }
63    private InfillProblem(InfillProblem original, Cloner cloner) : base(original, cloner) {
64      infillCriterion = cloner.Clone(original.infillCriterion);
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) {
71      return !InBounds(individual.RealVector(), Encoding.Bounds) ? double.MinValue : InfillCriterion.Evaluate(individual.RealVector());
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);
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)));
80        return;
81      }
82      var qold = results[BestInfillQualityResultName].Value as DoubleValue;
83      if (qold == null) throw new ArgumentException("Old best quality is not a double value. Conflicting Analyzers?");
84      if (qold.Value >= newQuality) return;
85      results[BestInfillSolutionResultName].Value = (RealVector)individuals[best].RealVector().Clone();
86      qold.Value = newQuality;
87    }
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
120  }
121}
Note: See TracBrowser for help on using the repository browser.