source: branches/SAPBA/HeuristicLab.Algorithms.SAPBA/SurrogateAssistedPopulationBasedAlgorithm.cs @ 14893

Last change on this file since 14893 was 14893, checked in by bwerth, 5 years ago

#2780 added intial files for SAPBA

File size: 12.2 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      get { return (SingleObjectiveBasicProblem<IEncoding>)base.Problem; }
48      set { base.Problem = value; }
49    }
50    #endregion
51
52    #region ParameterNames
53    private const string GenerationSizeParameterName = "GenerationSize";
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 OptimizationRestartsParameterName = "Optimization Restarts";
60    private const string RemoveDuplicatesParamterName = "RemoveDuplicates";
61    private const string RegressionAlgorithmParameterName = "RegressionAlgorithm";
62    private const string SeedParameterName = "Seed";
63    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
64    private const string StrategyParameterName = "Strategy";
65    #endregion
66
67    #region ParameterProperties
68    public IFixedValueParameter<IntValue> GenerationSizeParemeter => Parameters[GenerationSizeParameterName] as IFixedValueParameter<IntValue>;
69    public IFixedValueParameter<IntValue> InitialEvaluationsParameter => Parameters[InitialEvaluationsParameterName] as IFixedValueParameter<IntValue>;
70    public IFixedValueParameter<IntValue> MaximalDataSetSizeParameter => Parameters[MaximalDataSetSizeParameterName] as IFixedValueParameter<IntValue>;
71    public IFixedValueParameter<IntValue> MaximumEvaluationsParameter => Parameters[MaximumEvaluationsParameterName] as IFixedValueParameter<IntValue>;
72    public IFixedValueParameter<IntValue> MaximumRuntimeParameter => Parameters[MaximumRuntimeParameterName] as IFixedValueParameter<IntValue>;
73    public IValueParameter<Algorithm> OptimizationAlgorithmParameter => Parameters[OptimizationAlgorithmParameterName] as IValueParameter<Algorithm>;
74    public IFixedValueParameter<IntValue> OptimizationRestartsParemeter => Parameters[OptimizationRestartsParameterName] as IFixedValueParameter<IntValue>;
75    public IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>> RegressionAlgorithmParameter => Parameters[RegressionAlgorithmParameterName] as IValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>;
76    public IFixedValueParameter<BoolValue> RemoveDuplicatesParameter => Parameters[RemoveDuplicatesParamterName] as IFixedValueParameter<BoolValue>;
77    public IFixedValueParameter<IntValue> SeedParameter => Parameters[SeedParameterName] as IFixedValueParameter<IntValue>;
78    public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter => Parameters[SetSeedRandomlyParameterName] as IFixedValueParameter<BoolValue>;
79    public IConstrainedValueParameter<ISurrogateStrategy> StrategyParameter => Parameters[StrategyParameterName] as IConstrainedValueParameter<ISurrogateStrategy>;
80    #endregion
81
82    #region Properties
83    public int GenerationSize => GenerationSizeParemeter.Value.Value;
84    public int InitialEvaluations => InitialEvaluationsParameter.Value.Value;
85    public int MaximalDatasetSize => MaximalDataSetSizeParameter.Value.Value;
86    public int MaximumEvaluations => MaximumEvaluationsParameter.Value.Value;
87    public int MaximumRuntime => MaximumRuntimeParameter.Value.Value;
88    public Algorithm OptimizationAlgorithm => OptimizationAlgorithmParameter.Value;
89    public int OptimizationRestarts => OptimizationRestartsParemeter.Value.Value;
90    public IDataAnalysisAlgorithm<IRegressionProblem> RegressionAlgorithm => RegressionAlgorithmParameter.Value;
91    public bool RemoveDuplicates => RemoveDuplicatesParameter.Value.Value;
92    public int Seed => SeedParameter.Value.Value;
93    public bool SetSeedRandomly => SetSeedRandomlyParameter.Value.Value;
94    public ISurrogateStrategy SurrogateStrategy => StrategyParameter.Value;
95    #endregion
96
97    #region StorableProperties
98    [Storable]
99    private IRandom Random = new MersenneTwister();
100    [Storable]
101    public List<Tuple<RealVector, double>> InitialSamples { get; private set; }
102    [Storable]
103    public SurrogateProblem surrogateProblem;
104    public void SetInitialSamples(RealVector[] solutions, double[] qualities) {
105      InitialSamples = solutions.Zip(qualities, (vector, d) => new Tuple<RealVector, double>(vector, d)).ToList();
106    }
107    #endregion
108
109    #region HLConstructors
110    [StorableConstructor]
111    protected SurrogateAssistedPopulationBasedAlgorithm(bool deserializing) : base(deserializing) { }
112    [StorableHook(HookType.AfterDeserialization)]
113    private void AfterDeseialization() {
114      RegisterEventhandlers();
115    }
116    protected SurrogateAssistedPopulationBasedAlgorithm(SurrogateAssistedPopulationBasedAlgorithm original, Cloner cloner) : base(original, cloner) {
117      Random = cloner.Clone(Random);
118      if (original.InitialSamples != null) InitialSamples = original.InitialSamples.Select(x => new Tuple<RealVector, double>(cloner.Clone(x.Item1), x.Item2)).ToList();
119      RegisterEventhandlers();
120    }
121    public override IDeepCloneable Clone(Cloner cloner) { return new SurrogateAssistedPopulationBasedAlgorithm(this, cloner); }
122    public SurrogateAssistedPopulationBasedAlgorithm() {
123      var cmaes = new CMAEvolutionStrategy.CMAEvolutionStrategy {
124        MaximumGenerations = 300,
125        PopulationSize = 50
126      };
127      var model = new GaussianProcessRegression {
128        Problem = new RegressionProblem()
129      };
130      model.CovarianceFunctionParameter.Value = new CovarianceRationalQuadraticIso();
131
132      cmaes.Problem = new SurrogateProblem();
133      Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluationsParameterName, "", new IntValue(int.MaxValue)));
134      Parameters.Add(new FixedValueParameter<IntValue>(InitialEvaluationsParameterName, "", new IntValue(10)));
135      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)));
136      Parameters.Add(new FixedValueParameter<IntValue>(SeedParameterName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
137      Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyParameterName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
138      Parameters.Add(new ValueParameter<IDataAnalysisAlgorithm<IRegressionProblem>>(RegressionAlgorithmParameterName, "The model used to approximate the problem", model));
139      Parameters.Add(new ValueParameter<Algorithm>(OptimizationAlgorithmParameterName, "The algorithm used to solve the expected improvement subproblem", cmaes));
140      Parameters.Add(new FixedValueParameter<IntValue>(OptimizationRestartsParameterName, "Number of restarts of the SubAlgortihm to avoid local optima", new IntValue(1)));
141      Parameters.Add(new FixedValueParameter<IntValue>(GenerationSizeParameterName, "Number points that are sampled every iteration (stadard EGO: 1)", new IntValue(1)));
142      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)));
143      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"));
144      var strategies = new ItemSet<ISurrogateStrategy> { new GenerationalStrategy() };
145      Parameters.Add(new ConstrainedValueParameter<ISurrogateStrategy>(StrategyParameterName, "The surrogate strategy that dictates how the optimization alg is assisted", strategies));
146      RegisterEventhandlers();
147    }
148    #endregion
149
150    protected override void Initialize(CancellationToken cancellationToken) {
151      base.Initialize(cancellationToken);
152      //encoding
153      var enc = Problem.Encoding as RealVectorEncoding;
154      if (enc == null) throw new ArgumentException("The SAPBA algorithm can only be applied to RealVectorEncodings");
155
156      //random
157      if (SetSeedRandomly) SeedParameter.Value.Value = new System.Random().Next();
158      Random.Reset(Seed);
159
160      //initialize Strategy and Problem
161      SurrogateStrategy.Initialize(this);
162      SurrogateStrategy.UpdateCancellation(cancellationToken);
163      surrogateProblem.SetStrategy(SurrogateStrategy);
164      surrogateProblem.SetProblem(Problem);
165    }
166    protected override void Run(CancellationToken cancellationToken) {
167      SurrogateStrategy.UpdateCancellation(cancellationToken);
168      try { EgoUtilities.SyncRunSubAlgorithm(OptimizationAlgorithm, Random.Next()); }
169      finally { Analyze(); }
170    }
171    private void Analyze() { }
172
173    #region Eventhandling
174    private void RegisterEventhandlers() {
175      DeregisterEventhandlers();
176      OptimizationAlgorithmParameter.ValueChanged += OnOptimizationAlgorithmChanged;
177    }
178    private void DeregisterEventhandlers() {
179      OptimizationAlgorithmParameter.ValueChanged -= OnOptimizationAlgorithmChanged;
180    }
181    private void OnOptimizationAlgorithmChanged(object sender, EventArgs e) {
182      OptimizationAlgorithm.Problem = surrogateProblem;
183    }
184
185    protected override void OnExecutionTimeChanged() {
186      base.OnExecutionTimeChanged();
187      if (CancellationTokenSource == null) return;
188      if (MaximumRuntime == -1) return;
189      if (ExecutionTime.TotalSeconds > MaximumRuntime) CancellationTokenSource.Cancel();
190    }
191    public override void Pause() {
192      base.Pause();
193      if (RegressionAlgorithm.ExecutionState == ExecutionState.Started) RegressionAlgorithm.Pause();
194      if (OptimizationAlgorithm.ExecutionState == ExecutionState.Started) OptimizationAlgorithm.Pause();
195
196    }
197    public override void Stop() {
198      base.Stop();
199      if (RegressionAlgorithm.ExecutionState == ExecutionState.Started) RegressionAlgorithm.Stop();
200      if (OptimizationAlgorithm.ExecutionState == ExecutionState.Started) OptimizationAlgorithm.Stop();
201    }
202    protected override void OnProblemChanged() {
203      base.OnProblemChanged();
204      surrogateProblem.SetProblem(Problem);
205    }
206    #endregion
207
208  }
209}
Note: See TracBrowser for help on using the repository browser.