Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/Algorithms/RepeatedLocalAnalysis.cs @ 13777

Last change on this file since 13777 was 8172, checked in by gkronber, 12 years ago

#1797: adapted return type of ConstrainedValueParameter properties in FLA branch.

File size: 14.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Linq;
24using HeuristicLab.Analysis.FitnessLandscape.Algorithms;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Random;
35
36namespace HeuristicLab.Analysis.FitnessLandscape {
37  [Item("Repeated Local Analysis", "A local analysis algorithm restarted several times.")]
38  [Creatable("Algorithms")]
39  [StorableClass]
40  public sealed class RepeatedLocalAnalysis : HeuristicOptimizationEngineAlgorithm, IStorableContent {
41    public string Filename { get; set; }
42
43    #region Problem Properties
44    public override Type ProblemType {
45      get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
46    }
47    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
48      get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
49      set { base.Problem = value; }
50    }
51    #endregion
52
53    #region Parameter Properties
54    private ValueParameter<IntValue> SeedParameter {
55      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
56    }
57    private ValueParameter<BoolValue> SetSeedRandomlyParameter {
58      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
59    }
60    public IConstrainedValueParameter<IManipulator> MutatorParameter {
61      get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
62    }
63    public IConstrainedValueParameter<ISelector> SelectorParameter {
64      get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
65    }
66    private ValueParameter<IntValue> MaximumIterationsParameter {
67      get { return (ValueParameter<IntValue>)Parameters["MaximumIterations"]; }
68    }
69    private ValueParameter<IntValue> SampleSizeParameter {
70      get { return (ValueParameter<IntValue>)Parameters["SampleSize"]; }
71    }
72    private ValueParameter<MultiAnalyzer> AnalyzerParameter {
73      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
74    }
75    public ValueLookupParameter<IntValue> RepeatsParameter {
76      get { return (ValueLookupParameter<IntValue>)Parameters["Repeats"]; }
77    }
78    public ValueParameter<RepeatMultiAnalyzer> RepeatAnalyzerParameter {
79      get { return (ValueParameter<RepeatMultiAnalyzer>)Parameters["RepeatAnalyzer"]; }
80    }
81    #endregion
82
83    #region Properties
84    private RandomCreator RandomCreator {
85      get { return (RandomCreator)OperatorGraph.InitialOperator; }
86    }
87    private SolutionsCreator SolutionsCreator {
88      get { return (SolutionsCreator)RandomCreator.Successor; }
89    }
90    private ScopeDuplicator ScopeDuplicator {
91      get { return (ScopeDuplicator)SolutionsCreator.Successor; }
92    }
93    private UniformSubScopesProcessor UniformSubscopesProcessor {
94      get { return (UniformSubScopesProcessor)ScopeDuplicator.Successor; }
95    }
96    private LocalAnalysisMainLoop MainLoop {
97      get { return (LocalAnalysisMainLoop)UniformSubscopesProcessor.Operator; }
98    }
99
100    [Storable]
101    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
102    [Storable]
103    private QualityTrailMultiAnalyzer qualityTrailAnalyzer;
104    [Storable]
105    private RepeatMultiAnalyzer repeatMultiAnalyzer;
106    #endregion
107
108    [StorableConstructor]
109    private RepeatedLocalAnalysis(bool deserializing) : base(deserializing) { }
110    private RepeatedLocalAnalysis(RepeatedLocalAnalysis original, Cloner cloner)
111      : base(original, cloner) {
112      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
113      qualityTrailAnalyzer = cloner.Clone(original.qualityTrailAnalyzer);
114      repeatMultiAnalyzer = cloner.Clone(original.repeatMultiAnalyzer);
115      Initialize();
116    }
117    public RepeatedLocalAnalysis()
118      : base() {
119      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
120      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
121      Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "Mutation operator."));
122      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "Selection operator."));
123      Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(10000)));
124      Parameters.Add(new ValueParameter<IntValue>("SampleSize", "Number of moves that MultiMoveGenerators should create. This is ignored for Exhaustive- and SingleMoveGenerators.", new IntValue(1)));
125      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the solution and moves.", new MultiAnalyzer()));
126      Parameters.Add(new ValueLookupParameter<IntValue>("Repeats", "The number of runs of the local analysis from the same starting point.", new IntValue(1)));
127      Parameters.Add(new ValueParameter<RepeatMultiAnalyzer>("RepeatAnalyzer", "Consolidating analyzer for repeats.", new RepeatMultiAnalyzer()));
128
129      RandomCreator randomCreator = new RandomCreator();
130      SolutionsCreator solutionsCreator = new SolutionsCreator();
131      ScopeDuplicator scopeDulicator = new ScopeDuplicator();
132      UniformSubScopesProcessor uniformSubScopesProcessor = new UniformSubScopesProcessor();
133      LocalAnalysisMainLoop laMainLoop = new LocalAnalysisMainLoop();
134      SubScopesRemover subscopesRemover = new SubScopesRemover();
135
136      repeatMultiAnalyzer = RepeatAnalyzerParameter.Value;
137
138      OperatorGraph.InitialOperator = randomCreator;
139
140      randomCreator.RandomParameter.ActualName = "Random";
141      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
142      randomCreator.SeedParameter.Value = null;
143      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
144      randomCreator.SetSeedRandomlyParameter.Value = null;
145      randomCreator.Successor = solutionsCreator;
146
147      solutionsCreator.NumberOfSolutions = new IntValue(1);
148      solutionsCreator.Successor = scopeDulicator;
149
150      scopeDulicator.NrOfDuplicatesParameter.ActualName = RepeatsParameter.Name;
151      scopeDulicator.Successor = uniformSubScopesProcessor;
152
153      uniformSubScopesProcessor.Operator = laMainLoop;
154      uniformSubScopesProcessor.Parallel = new BoolValue(true);
155      uniformSubScopesProcessor.Successor = repeatMultiAnalyzer;
156
157      repeatMultiAnalyzer.Successor = subscopesRemover;
158
159      laMainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
160      laMainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
161      laMainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
162      laMainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
163      laMainLoop.ResultsParameter.ActualName = "Results";
164      laMainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
165
166      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
167      qualityTrailAnalyzer = new QualityTrailMultiAnalyzer();
168      ParameterizeAnalyzers();
169      UpdateAnalyzers();
170
171      Initialize();
172    }
173
174    public override IDeepCloneable Clone(Cloner cloner) {
175      return new RepeatedLocalAnalysis(this, cloner);
176    }
177
178    public override void Prepare() {
179      if (Problem != null)
180        base.Prepare();
181    }
182
183    #region Events
184    protected override void OnProblemChanged() {
185      ParameterizeStochasticOperator(Problem.SolutionCreator);
186      ParameterizeStochasticOperator(Problem.Evaluator);
187      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
188      ParameterizeSolutionsCreator();
189      ParameterizeMainLoop();
190      UpdateMutators();
191      UpdateSelectors();
192      UpdateAnalyzers();
193      ParameterizeSelector();
194      ParameterizeAnalyzers();
195      ParameterizeIterationBasedOperators();
196      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
197      base.OnProblemChanged();
198    }
199    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
200      ParameterizeStochasticOperator(Problem.SolutionCreator);
201      ParameterizeSolutionsCreator();
202      base.Problem_SolutionCreatorChanged(sender, e);
203    }
204    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
205      ParameterizeStochasticOperator(Problem.Evaluator);
206      ParameterizeSolutionsCreator();
207      ParameterizeMainLoop();
208      ParameterizeSelector();
209      ParameterizeAnalyzers();
210      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
211      base.Problem_EvaluatorChanged(sender, e);
212    }
213    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
214      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
215      UpdateMutators();
216      UpdateSelectors();
217      UpdateAnalyzers();
218      ParameterizeMainLoop();
219      ParameterizeSelector();
220      ParameterizeAnalyzers();
221      ParameterizeIterationBasedOperators();
222      base.Problem_OperatorsChanged(sender, e);
223    }
224    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
225      ParameterizeMainLoop();
226      ParameterizeSelector();
227    }
228    private void MutatorParameter_ValueChanged(object sender, EventArgs e) {
229      ParameterizeMainLoop();
230      ParameterizeSelector();
231      ParameterizeAnalyzers();
232    }
233    #endregion
234
235    #region Helpers
236
237    [StorableHook(HookType.AfterDeserialization)]
238    private void AfterDeserializationHook() {
239      if (RepeatAnalyzerParameter.Value.Successor == null)
240        RepeatAnalyzerParameter.Value.Successor = new SubScopesRemover();
241      Initialize();
242    }
243
244    private void Initialize() {
245      if (Problem != null) {
246        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
247      }
248      MutatorParameter.ValueChanged += MutatorParameter_ValueChanged;
249    }
250    private void UpdateSelectors() {
251      SelectorParameter.ValidValues.Clear();
252      foreach (var s in ApplicationManager.Manager.GetInstances<ISelector>())
253        SelectorParameter.ValidValues.Add(s);
254    }
255    private void UpdateMutators() {
256      MutatorParameter.ValidValues.Clear();
257      foreach (var m in Problem.Operators.OfType<IManipulator>()) {
258        MutatorParameter.ValidValues.Add(m);
259      }
260    }
261    private void UpdateAnalyzers() {
262      AnalyzerParameter.Value.Operators.Clear();
263      if (Problem != null) {
264        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
265          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
266            param.Depth = 0;
267          AnalyzerParameter.Value.Operators.Add(analyzer);
268        }
269      }
270      AnalyzerParameter.Value.Operators.Add(qualityAnalyzer);
271      AnalyzerParameter.Value.Operators.Add(qualityTrailAnalyzer);
272    }
273    private void ParameterizeSolutionsCreator() {
274      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
275      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
276    }
277    private void ParameterizeMainLoop() {
278      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
279      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
280      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
281    }
282    private void ParameterizeStochasticOperator(IOperator op) {
283      if (op is IStochasticOperator)
284        ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
285    }
286    private void ParameterizeSelector() {
287      if (Problem != null) {
288        foreach (var op in SelectorParameter.ValidValues) {
289          op.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
290          op.CopySelected = new BoolValue(false);
291          ISingleObjectiveSelector sos = op as ISingleObjectiveSelector;
292          if (sos != null) {
293            sos.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
294            sos.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
295          }
296          IStochasticOperator so = op as IStochasticOperator;
297          if (so != null) {
298            so.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
299          }
300        }
301      }
302    }
303    private void ParameterizeAnalyzers() {
304      qualityAnalyzer.ResultsParameter.ActualName = "Results";
305      if (Problem != null) {
306        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
307        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
308      }
309    }
310    private void ParameterizeIterationBasedOperators() {
311      if (Problem != null) {
312        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
313          op.IterationsParameter.ActualName = "Iterations";
314          op.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
315        }
316      }
317    }
318    #endregion
319  }
320}
Note: See TracBrowser for help on using the repository browser.