Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2745_EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/Problems/InfillProblem.cs @ 17915

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

#2745 updated persistence to HEAL.Attic

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