Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2745_EfficientGlobalOptimization/HeuristicLab.Algorithms.EGO/DiscreteEGO/DiscreteInfillProblem.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.7 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.IntegerVectorEncoding;
30using HeuristicLab.Encodings.RealVectorEncoding;
31using HeuristicLab.Optimization;
32using HeuristicLab.Problems.DataAnalysis;
33
34namespace HeuristicLab.Algorithms.EGO {
35    [StorableType("7a98818c-8434-4a33-8230-2b2486772af6")]
36    [Item("DiscreteInfillProblem", "A problem for finding the most interesing potential new sampling Points by optimizing some InfillCriterion")]
37  public sealed class DiscreteInfillProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding> {
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      get { return infillCriterion; }
52      set {
53        infillCriterion = value;
54        infillCriterion.Encoding = GetRealVectorEncoding(Encoding);
55      }
56    }
57    #endregion
58
59    #region Constructors
60    [StorableConstructor]
61    private DiscreteInfillProblem(StorableConstructorFlag deserializing) : base(deserializing) { }
62    private DiscreteInfillProblem(DiscreteInfillProblem original, Cloner cloner) : base(original, cloner) {
63      infillCriterion = cloner.Clone(original.infillCriterion);
64    }
65    public DiscreteInfillProblem() { }
66    public override IDeepCloneable Clone(Cloner cloner) { return new DiscreteInfillProblem(this, cloner); }
67    #endregion
68
69    public override double Evaluate(Individual individual, IRandom r) {
70      return !InBounds(individual.IntegerVector(), Encoding.Bounds) ? double.MinValue : InfillCriterion.Evaluate(individual.IntegerVector().ToRealVector());
71    }
72    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
73      base.Analyze(individuals, qualities, results, random);
74      var best = qualities.ArgMax(x => x);
75      var newQuality = qualities[best];
76      if (!results.ContainsKey(BestInfillQualityResultName)) {
77        results.Add(new Result(BestInfillSolutionResultName, (IntegerVector)individuals[best].IntegerVector().Clone()));
78        results.Add(new Result(BestInfillQualityResultName, new DoubleValue(newQuality)));
79        return;
80      }
81      var qold = results[BestInfillQualityResultName].Value as DoubleValue;
82      if (qold == null) throw new ArgumentException("Old best quality is not a double value. Conflicting Analyzers?");
83      if (qold.Value >= newQuality) return;
84      results[BestInfillSolutionResultName].Value = (IntegerVector)individuals[best].IntegerVector().Clone();
85      qold.Value = newQuality;
86    }
87    public override IEnumerable<Individual> GetNeighbors(Individual individual, IRandom random) {
88      var bounds = Encoding.Bounds;
89      var michalewiczIteration = 0;
90      var sigma = new DoubleArray(new double[] { 1.0 });
91
92      while (true) {
93        var neighbour = individual.Copy();
94        var r = neighbour.IntegerVector();
95        switch (random.Next(3) % 3) {
96          case 0: HeuristicLab.Encodings.IntegerVectorEncoding.UniformOnePositionManipulator.Apply(random, r, bounds); break;
97          case 1: HeuristicLab.Encodings.IntegerVectorEncoding.RoundedNormalAllPositionsManipulator.Apply(random, r, bounds, sigma); break;//FixedNormalAllPositionsManipulator.Apply(random, r, new RealVector(new[] { 0.1 })); break;
98          case 2: HeuristicLab.Encodings.IntegerVectorEncoding.UniformSomePositionsManipulator.Apply(random, r, bounds, 0.1); break;
99          default: throw new NotImplementedException();
100        }
101        yield return neighbour;
102        michalewiczIteration %= 10000;
103      }
104    }
105
106    public void Initialize(IRegressionSolution model, bool expensiveMaximization) {
107      infillCriterion.RegressionSolution = model;
108      infillCriterion.ExpensiveMaximization = expensiveMaximization;
109      infillCriterion.Encoding = GetRealVectorEncoding(Encoding);
110      infillCriterion.Initialize();
111    }
112
113    #region helpers
114    private static bool InBounds(IntegerVector r, IntMatrix bounds) {
115      return !r.Where((t, i) => t < bounds[i % bounds.Rows, 0] || t > bounds[i % bounds.Rows, 1]).Any();
116    }
117
118    private static RealVectorEncoding GetRealVectorEncoding(IntegerVectorEncoding enc) {
119      var res = new RealVectorEncoding(enc.Length);
120      res.Bounds = new DoubleMatrix(enc.Bounds.Rows, enc.Bounds.Columns);
121      for (int r = 0; r < res.Bounds.Rows; r++)
122        for (int c = 0; c < res.Bounds.Columns; c++)
123          res.Bounds[r, c] = enc.Bounds[r, c];
124      return res;
125
126
127    }
128    #endregion
129
130  }
131}
Note: See TracBrowser for help on using the repository browser.