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