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 System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Threading;
|
---|
26 | using HeuristicLab.Analysis;
|
---|
27 | using HeuristicLab.Common;
|
---|
28 | using HeuristicLab.Core;
|
---|
29 | using HeuristicLab.Data;
|
---|
30 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
31 | using HeuristicLab.Optimization;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.Problems.DataAnalysis;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Algorithms.SAPBA.Strategies {
|
---|
36 | [StorableClass]
|
---|
37 | public abstract class StrategyBase : ParameterizedNamedItem, ISurrogateStrategy {
|
---|
38 | #region Properties
|
---|
39 | [Storable]
|
---|
40 | protected SurrogateAssistedPopulationBasedAlgorithm Algorithm;
|
---|
41 | [Storable]
|
---|
42 | private List<Tuple<RealVector, double>> Samples;
|
---|
43 | [Storable]
|
---|
44 | protected IRegressionSolution RegressionSolution;
|
---|
45 | protected CancellationToken Cancellation;
|
---|
46 | private IEnumerable<Tuple<RealVector, double>> TruncatedSamples => Samples.Count > Algorithm.MaximalDatasetSize && Algorithm.MaximalDatasetSize > 0 ? Samples.Skip(Samples.Count - Algorithm.MaximalDatasetSize) : Samples;
|
---|
47 | #endregion
|
---|
48 |
|
---|
49 | #region ResultName
|
---|
50 | private const string BestQualityResultName = "Best Quality";
|
---|
51 | private const string BestSolutionResultName = "Best Solution";
|
---|
52 | private const string QualityTableResultName = "Qualities";
|
---|
53 | private const string BestQualityRowName = "Best Quality";
|
---|
54 | private const string WorstQualityRowName = "Worst Quality";
|
---|
55 | private const string CurrentQualityRowName = "Current Quality";
|
---|
56 | private const string MedianQualityRowName = "Median Quality";
|
---|
57 | private const string AverageQualityRowName = "Average Quality";
|
---|
58 | private const string RegressionSolutionResultName = "Model";
|
---|
59 | private const string EvaluatedSoultionsResultName = "EvaluatedSolutions";
|
---|
60 | private const string IterationsResultName = "Iterations";
|
---|
61 | #endregion
|
---|
62 |
|
---|
63 | #region constructors
|
---|
64 | [StorableConstructor]
|
---|
65 | protected StrategyBase(bool deserializing) : base(deserializing) { }
|
---|
66 | protected StrategyBase(StrategyBase original, Cloner cloner) : base(original, cloner) {
|
---|
67 | if (original.Samples != null) Samples = original.Samples.Select(x => new Tuple<RealVector, double>(cloner.Clone(x.Item1), x.Item2)).ToList();
|
---|
68 | RegressionSolution = cloner.Clone(original.RegressionSolution);
|
---|
69 | }
|
---|
70 | protected StrategyBase() { }
|
---|
71 | #endregion
|
---|
72 |
|
---|
73 | public abstract double Evaluate(RealVector r, IRandom random);
|
---|
74 | protected abstract void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, ResultCollection globalResults, IRandom random);
|
---|
75 | protected abstract void ProcessPopulation(Individual[] individuals, double[] qualities, IRandom random);
|
---|
76 | protected abstract void Initialize();
|
---|
77 |
|
---|
78 | public void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
79 | Algorithm.Problem.Analyze(individuals, qualities, results, random);
|
---|
80 | ProcessPopulation(individuals, qualities, random);
|
---|
81 |
|
---|
82 | var globalResults = Algorithm.Results;
|
---|
83 | if (!globalResults.ContainsKey(EvaluatedSoultionsResultName)) globalResults.Add(new Result(EvaluatedSoultionsResultName, new IntValue(Samples.Count)));
|
---|
84 | else ((IntValue)globalResults[EvaluatedSoultionsResultName].Value).Value = Samples.Count;
|
---|
85 | if (!globalResults.ContainsKey(IterationsResultName)) globalResults.Add(new Result(IterationsResultName, new IntValue(0)));
|
---|
86 | else ((IntValue)globalResults[IterationsResultName].Value).Value++;
|
---|
87 |
|
---|
88 | if (Samples.Count != 0) {
|
---|
89 | var min = Samples.Min(x => x.Item2);
|
---|
90 | var max = Samples.Max(x => x.Item2);
|
---|
91 | var bestIdx = Algorithm.Problem.Maximization ? Samples.ArgMax(x => x.Item2) : Samples.ArgMin(x => x.Item2);
|
---|
92 |
|
---|
93 | if (!globalResults.ContainsKey(BestQualityResultName)) globalResults.Add(new Result(BestQualityResultName, new DoubleValue(0.0)));
|
---|
94 | ((DoubleValue)globalResults[BestQualityResultName].Value).Value = Samples[bestIdx].Item2;
|
---|
95 | if (!globalResults.ContainsKey(BestSolutionResultName)) globalResults.Add(new Result(BestSolutionResultName, new RealVector()));
|
---|
96 | globalResults[BestSolutionResultName].Value = Samples[bestIdx].Item1;
|
---|
97 |
|
---|
98 | DataTable table;
|
---|
99 | if (!globalResults.ContainsKey(QualityTableResultName)) {
|
---|
100 | table = new DataTable("Qualites", "Qualites over iteration");
|
---|
101 | globalResults.Add(new Result(QualityTableResultName, table));
|
---|
102 | table.Rows.Add(new DataRow(BestQualityRowName, "Best Quality"));
|
---|
103 | table.Rows.Add(new DataRow(WorstQualityRowName, "Worst Quality"));
|
---|
104 | table.Rows.Add(new DataRow(CurrentQualityRowName, "Current Quality"));
|
---|
105 | table.Rows.Add(new DataRow(MedianQualityRowName, "Median Quality"));
|
---|
106 | table.Rows.Add(new DataRow(AverageQualityRowName, "Average Quality"));
|
---|
107 | }
|
---|
108 | table = (DataTable)globalResults[QualityTableResultName].Value;
|
---|
109 | table.Rows[BestQualityResultName].Values.Add(Algorithm.Problem.Maximization ? max : min);
|
---|
110 | table.Rows[WorstQualityRowName].Values.Add(Algorithm.Problem.Maximization ? min : max);
|
---|
111 | table.Rows[CurrentQualityRowName].Values.Add(Samples[Samples.Count - 1].Item2);
|
---|
112 | table.Rows[AverageQualityRowName].Values.Add(Samples.Average(x => x.Item2));
|
---|
113 | table.Rows[MedianQualityRowName].Values.Add(Samples.Select(x => x.Item2).Median());
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (RegressionSolution != null) {
|
---|
117 | if (!globalResults.ContainsKey(RegressionSolutionResultName))
|
---|
118 | globalResults.Add(new Result(RegressionSolutionResultName, RegressionSolution));
|
---|
119 | else
|
---|
120 | globalResults[RegressionSolutionResultName].Value = RegressionSolution;
|
---|
121 | }
|
---|
122 |
|
---|
123 | Analyze(individuals, qualities, results, globalResults, random);
|
---|
124 | }
|
---|
125 | public void Initialize(SurrogateAssistedPopulationBasedAlgorithm algorithm) {
|
---|
126 | Algorithm = algorithm;
|
---|
127 | Samples = algorithm.InitialSamples?.ToList() ?? new List<Tuple<RealVector, double>>();
|
---|
128 | RegressionSolution = null;
|
---|
129 | Initialize();
|
---|
130 | }
|
---|
131 |
|
---|
132 | #region Helpers for Subclasses
|
---|
133 | protected void BuildRegressionSolution(IRandom random) {
|
---|
134 | RegressionSolution = EgoUtilities.BuildModel(Cancellation, TruncatedSamples, Algorithm.RegressionAlgorithm, random, Algorithm.RemoveDuplicates, RegressionSolution);
|
---|
135 | }
|
---|
136 | protected Tuple<RealVector, double> EvaluateSample(RealVector point, IRandom random) {
|
---|
137 | Cancellation.ThrowIfCancellationRequested();
|
---|
138 | if (Samples.Count >= Algorithm.MaximumEvaluations) { Algorithm.OptimizationAlgorithm.Stop(); return new Tuple<RealVector, double>(point, 0.0); }
|
---|
139 | var p = new Tuple<RealVector, double>(point, Algorithm.Problem.Evaluate(GetIndividual(point), random));
|
---|
140 | Samples.Add(p);
|
---|
141 | return p;
|
---|
142 | }
|
---|
143 | protected Tuple<RealVector, double> EstimateSample(RealVector point, IRandom random) {
|
---|
144 | if (Samples.Count == Algorithm.InitialEvaluations && RegressionSolution == null) BuildRegressionSolution(random);
|
---|
145 | return Samples.Count < Algorithm.InitialEvaluations ? EvaluateSample(point, random) : new Tuple<RealVector, double>(point, RegressionSolution.Model.GetEstimation(point));
|
---|
146 | }
|
---|
147 | #endregion
|
---|
148 |
|
---|
149 | #region Helpers
|
---|
150 | private Individual GetIndividual(RealVector r) {
|
---|
151 | var scope = new Scope();
|
---|
152 | scope.Variables.Add(new Variable(Algorithm.Problem.Encoding.Name, r));
|
---|
153 | return new SingleEncodingIndividual(Algorithm.Problem.Encoding, scope);
|
---|
154 | }
|
---|
155 |
|
---|
156 | public void UpdateCancellation(CancellationToken cancellationToken) {
|
---|
157 | Cancellation = cancellationToken;
|
---|
158 | }
|
---|
159 | #endregion
|
---|
160 | }
|
---|
161 | } |
---|