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