Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/Algorithms/LocalAnalysis.cs @ 14649

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

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

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