Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2780_SAPBA/HeuristicLab.Algorithms.SAPBA/SurrogateAssistedPopulationBasedAlgorithm.cs @ 16559

Last change on this file since 16559 was 14894, checked in by bwerth, 8 years ago

#2780 worked on SAPBA; added IndividualStrategy

File size: 11.4 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 System.Threading;
26using HeuristicLab.Algorithms.DataAnalysis;
27using HeuristicLab.Algorithms.SAPBA.Strategies;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Encodings.RealVectorEncoding;
32using HeuristicLab.Optimization;
33using HeuristicLab.Parameters;
34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35using HeuristicLab.Problems.DataAnalysis;
36using HeuristicLab.Random;
37
38namespace HeuristicLab.Algorithms.SAPBA {
39  [StorableClass]
40  [Creatable(CreatableAttribute.Categories.Algorithms, Priority = 95)]
41  [Item("SurrogateAssistedPopulationBasedAlgorithm", "")]
42  public class SurrogateAssistedPopulationBasedAlgorithm : BasicAlgorithm, ISurrogateAlgorithm<RealVector> {
43    #region Basic-Alg-Essentials
44    public override bool SupportsPause => true;
45    public override Type ProblemType => typeof(SingleObjectiveBasicProblem<IEncoding>);
46    public new SingleObjectiveBasicProblem<IEncoding> Problem
47    {
48      get { return (SingleObjectiveBasicProblem<IEncoding>)base.Problem; }
49      set { base.Problem = value; }
50    }
51    #endregion
52
53    #region ParameterNames
54    private const string InitialEvaluationsParameterName = "Initial Evaluations";
55    private const string MaximalDataSetSizeParameterName = "Maximal Dataset Size";
56    private const string MaximumEvaluationsParameterName = "Maximum Evaluations";
57    private const string MaximumRuntimeParameterName = "Maximum Runtime";
58    private const string OptimizationAlgorithmParameterName = "Optimization Algorithm";
59    private const string RemoveDuplicatesParamterName = "RemoveDuplicates";
60    private const string RegressionAlgorithmParameterName = "RegressionAlgorithm";
61    private const string SeedParameterName = "Seed";
62    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
63    private const string StrategyParameterName = "Strategy";
64    #endregion
65
66    #region ParameterProperties
67    public IFixedValueParameter<IntValue> InitialEvaluationsParameter => Parameters[InitialEvaluationsParameterName] as IFixedValueParameter<IntValue>;
68    public IFixedValueParameter<IntValue> MaximalDataSetSizeParameter => Parameters[MaximalDataSetSizeParameterName] as IFixedValueParameter<IntValue>;
69    public IFixedValueParameter<IntValue> MaximumEvaluationsParameter => Parameters[MaximumEvaluationsParameterName] as IFixedValueParameter<IntValue>;
70    public IFixedValueParameter<IntValue> MaximumRuntimeParameter => Parameters[MaximumRuntimeParameterName] as IFixedValueParameter<IntValue>;
71    public IValueParameter<Algorithm> OptimizationAlgorithmParameter => Parameters[OptimizationAlgorithmParameterName] as IValueParameter<Algorithm>;
72    public IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>> RegressionAlgorithmParameter => Parameters[RegressionAlgorithmParameterName] as IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>;
73    public IFixedValueParameter<BoolValue> RemoveDuplicatesParameter => Parameters[RemoveDuplicatesParamterName] as IFixedValueParameter<BoolValue>;
74    public IFixedValueParameter<IntValue> SeedParameter => Parameters[SeedParameterName] as IFixedValueParameter<IntValue>;
75    public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter => Parameters[SetSeedRandomlyParameterName] as IFixedValueParameter<BoolValue>;
76    public IConstrainedValueParameter<ISurrogateStrategy> StrategyParameter => Parameters[StrategyParameterName] as IConstrainedValueParameter<ISurrogateStrategy>;
77    #endregion
78
79    #region Properties
80    public int InitialEvaluations => InitialEvaluationsParameter.Value.Value;
81    public int MaximalDatasetSize => MaximalDataSetSizeParameter.Value.Value;
82    public int MaximumEvaluations => MaximumEvaluationsParameter.Value.Value;
83    public int MaximumRuntime => MaximumRuntimeParameter.Value.Value;
84    public Algorithm OptimizationAlgorithm => OptimizationAlgorithmParameter.Value;
85    public IDataAnalysisAlgorithm<IRegressionProblem> RegressionAlgorithm => RegressionAlgorithmParameter.Value;
86    public bool RemoveDuplicates => RemoveDuplicatesParameter.Value.Value;
87    public int Seed => SeedParameter.Value.Value;
88    public bool SetSeedRandomly => SetSeedRandomlyParameter.Value.Value;
89    public ISurrogateStrategy SurrogateStrategy => StrategyParameter.Value;
90    #endregion
91
92    #region StorableProperties
93    [Storable]
94    private IRandom Random = new MersenneTwister();
95    [Storable]
96    public List<Tuple<RealVector, double>> InitialSamples { get; private set; }
97    [Storable]
98    public SurrogateProblem surrogateProblem;
99    public void SetInitialSamples(RealVector[] solutions, double[] qualities) {
100      InitialSamples = solutions.Zip(qualities, (vector, d) => new Tuple<RealVector, double>(vector, d)).ToList();
101    }
102    #endregion
103
104    #region HLConstructors
105    [StorableConstructor]
106    protected SurrogateAssistedPopulationBasedAlgorithm(bool deserializing) : base(deserializing) { }
107    [StorableHook(HookType.AfterDeserialization)]
108    private void AfterDeseialization() {
109      RegisterEventhandlers();
110    }
111    protected SurrogateAssistedPopulationBasedAlgorithm(SurrogateAssistedPopulationBasedAlgorithm original, Cloner cloner) : base(original, cloner) {
112      Random = cloner.Clone(Random);
113      if (original.InitialSamples != null) InitialSamples = original.InitialSamples.Select(x => new Tuple<RealVector, double>(cloner.Clone(x.Item1), x.Item2)).ToList();
114      RegisterEventhandlers();
115    }
116    public override IDeepCloneable Clone(Cloner cloner) { return new SurrogateAssistedPopulationBasedAlgorithm(this, cloner); }
117    public SurrogateAssistedPopulationBasedAlgorithm() {
118      surrogateProblem = new SurrogateProblem();
119      var geneticAlgorithm = new GeneticAlgorithm.GeneticAlgorithm {
120        PopulationSize = { Value = 50 },
121        Problem = surrogateProblem
122      };
123      var model = new GaussianProcessRegression {
124        Problem = new RegressionProblem()
125      };
126      model.CovarianceFunctionParameter.Value = new CovarianceRationalQuadraticIso();
127
128      Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluationsParameterName, "", new IntValue(int.MaxValue)));
129      Parameters.Add(new FixedValueParameter<IntValue>(InitialEvaluationsParameterName, "", new IntValue(10)));
130      Parameters.Add(new FixedValueParameter<IntValue>(MaximumRuntimeParameterName, "The maximum runtime in seconds after which the algorithm stops. Use -1 to specify no limit for the runtime", new IntValue(-1)));
131      Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
132      Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
133      Parameters.Add(new ValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>(RegressionAlgorithmParameterName, "The model used to approximate the problem", model));
134      Parameters.Add(new ValueParameter<Algorithm>(OptimizationAlgorithmParameterName, "The algorithm used to solve the expected improvement subproblem", geneticAlgorithm));
135      Parameters.Add(new FixedValueParameter<IntValue>(MaximalDataSetSizeParameterName, "The maximum number of sample points used to generate the model. Set 0 or less to use always all samples ", new IntValue(-1)));
136      Parameters.Add(new FixedValueParameter<BoolValue>(RemoveDuplicatesParamterName, "Wether duplicate samples should be replaced by a single sample with an averaged quality. This GREATLY decreases the chance of ill conditioned models (unbuildable models) but is not theoretically sound as the model ignores the increasing certainty in this region"));
137      var strategies = new ItemSet<ISurrogateStrategy> { new GenerationalStrategy(), new IndividualStrategy() };
138      Parameters.Add(new ConstrainedValueParameter<ISurrogateStrategy>(StrategyParameterName, "The surrogate strategy that dictates how the optimization alg is assisted", strategies, strategies.First()));
139      RegisterEventhandlers();
140    }
141    #endregion
142
143    protected override void Initialize(CancellationToken cancellationToken) {
144      base.Initialize(cancellationToken);
145      //encoding
146      var enc = Problem.Encoding as RealVectorEncoding;
147      if (enc == null) throw new ArgumentException("The SAPBA algorithm can only be applied to RealVectorEncodings");
148
149      //random
150      if (SetSeedRandomly) SeedParameter.Value.Value = new System.Random().Next();
151      Random.Reset(Seed);
152
153      //initialize Strategy and Problem
154      SurrogateStrategy.Initialize(this);
155      SurrogateStrategy.UpdateCancellation(cancellationToken);
156      surrogateProblem.SetStrategy(SurrogateStrategy);
157      surrogateProblem.SetProblem(Problem);
158    }
159    protected override void Run(CancellationToken cancellationToken) {
160      SurrogateStrategy.UpdateCancellation(cancellationToken);
161      try { EgoUtilities.SyncRunSubAlgorithm(OptimizationAlgorithm, Random.Next()); }
162      finally { Analyze(); }
163    }
164    private void Analyze() { }
165
166    #region Eventhandling
167    private void RegisterEventhandlers() {
168      DeregisterEventhandlers();
169      OptimizationAlgorithmParameter.ValueChanged += OnOptimizationAlgorithmChanged;
170    }
171    private void DeregisterEventhandlers() {
172      OptimizationAlgorithmParameter.ValueChanged -= OnOptimizationAlgorithmChanged;
173    }
174    private void OnOptimizationAlgorithmChanged(object sender, EventArgs e) {
175      OptimizationAlgorithm.Problem = surrogateProblem;
176    }
177
178    protected override void OnExecutionTimeChanged() {
179      base.OnExecutionTimeChanged();
180      if (CancellationTokenSource == null) return;
181      if (MaximumRuntime == -1) return;
182      if (ExecutionTime.TotalSeconds > MaximumRuntime) CancellationTokenSource.Cancel();
183    }
184    public override void Pause() {
185      base.Pause();
186      if (RegressionAlgorithm.ExecutionState == ExecutionState.Started) RegressionAlgorithm.Pause();
187      if (OptimizationAlgorithm.ExecutionState == ExecutionState.Started) OptimizationAlgorithm.Pause();
188
189    }
190    public override void Stop() {
191      base.Stop();
192      if (RegressionAlgorithm.ExecutionState == ExecutionState.Started) RegressionAlgorithm.Stop();
193      if (OptimizationAlgorithm.ExecutionState == ExecutionState.Started) OptimizationAlgorithm.Stop();
194    }
195    protected override void OnProblemChanged() {
196      base.OnProblemChanged();
197      surrogateProblem.SetProblem(Problem);
198    }
199    #endregion
200
201  }
202}
Note: See TracBrowser for help on using the repository browser.