Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2745 migrated EGO from Connected Vehicles to its own branch, several modifications/simplifications along the way

File size: 4.6 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 HeuristicLab.Algorithms.EGO;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.RealVectorEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31
32namespace HeuristicLab.Problems.SurrogateProblem {
33  [StorableClass]
34  [Item("InfillProblem", "A problem for finding the most interesing potential new sampling Points by optimizing some InfillCriterion")]
35  public sealed class InfillProblem : SingleObjectiveBasicProblem<RealVectorEncoding> {
36
37    [Storable]
38    public override bool Maximization => true;
39
40    #region Properties;
41    [Storable]
42    private IInfillCriterion infillCriterion;
43    [Storable]
44    private SingleObjectiveBasicProblem<IEncoding> problem;
45    [Storable]
46    private IRegressionSolution regressionSolution;
47
48
49    public IInfillCriterion InfillCriterion
50    {
51      get { return infillCriterion; }
52      set { infillCriterion = value; }
53    }
54    public SingleObjectiveBasicProblem<IEncoding> Problem
55    {
56      get { return problem; }
57      set
58      {
59        problem = value;
60        if (problem == null) return;
61        Encoding = problem.Encoding as RealVectorEncoding;
62        if (Encoding == null) throw new ArgumentException("EGO can not be performed on non-RealVectorEncodings");
63      }
64    }
65    public IRegressionSolution RegressionSolution
66    {
67      get { return regressionSolution; }
68      set { regressionSolution = value; }
69    }
70    #endregion
71
72    #region HLConstructors
73    [StorableConstructor]
74    private InfillProblem(bool deserializing) : base(deserializing) { }
75    private InfillProblem(InfillProblem original, Cloner cloner) : base(original, cloner) {
76      infillCriterion = cloner.Clone(original.InfillCriterion);
77      problem = cloner.Clone(original.Problem);
78      regressionSolution = cloner.Clone(original.regressionSolution);
79    }
80    public InfillProblem() { }
81    public override IDeepCloneable Clone(Cloner cloner) { return new InfillProblem(this, cloner); }
82    #endregion
83
84    public override double Evaluate(Individual individual, IRandom r) {
85      var q = InfillCriterion.Evaluate(RegressionSolution, individual.RealVector(), Problem.Maximization);
86      return InfillCriterion.Maximization(Problem.Maximization) ? q : -q; //This is necessary because Maximization is not supposed to change on a normal problem
87    }
88    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
89      base.Analyze(individuals, qualities, results, random);
90      var best = qualities.ArgMax(x => x);
91      var qnew = InfillCriterion.Maximization(Problem.Maximization) ? qualities[best] : -qualities[best];
92      //var best = Maximization ? qualities.ArgMax(x => x) : qualities.ArgMin(x => x);
93      const string qname = EfficientGlobalOptimizationAlgorithm.BestInfillQualityResultName;
94      const string sname = EfficientGlobalOptimizationAlgorithm.BestInfillSolutionResultName;
95      if (!results.ContainsKey(EfficientGlobalOptimizationAlgorithm.BestInfillQualityResultName)) {
96        results.Add(new Result(sname, (RealVector)individuals[best].RealVector().Clone()));
97        results.Add(new Result(qname, new DoubleValue(qnew)));
98        return;
99      }
100      var qold = results[qname].Value as DoubleValue;
101      if (qold == null) throw new ArgumentException("Old best quality is not a double value. Conflicting Analyzers?");
102      if (qold.Value >= qnew == InfillCriterion.Maximization(Problem.Maximization)) return;
103      results[sname].Value = (RealVector)individuals[best].RealVector().Clone();
104      qold.Value = qnew;
105
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.