Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/Algorithms/LocalAnalysisMainLoop.cs @ 16573

Last change on this file since 16573 was 16573, checked in by gkronber, 5 years ago

#2520: changed HeuristicLab.FLA addon to compile with new HL.Persistence

File size: 11.8 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Optimization.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Selection;
31using HEAL.Attic;
32
33namespace HeuristicLab.Analysis.FitnessLandscape {
34  /// <summary>
35  /// An operator which represents a local search.
36  /// </summary>
37  [Item("LocalAnalysisMainLoop", "An operator which represents the main loop of a best improvement local search (if only a single move is generated in each iteration it is a first improvement local search).")]
38  [StorableType("D1B3518C-6A0A-4123-893E-0D4C89DA056C")]
39  public class LocalAnalysisMainLoop : AlgorithmOperator {
40
41    #region Parameter properties
42    public ValueLookupParameter<IRandom> RandomParameter {
43      get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
44    }
45    public ValueLookupParameter<BoolValue> MaximizationParameter {
46      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
47    }
48    public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
49      get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
50    }
51    public LookupParameter<DoubleValue> QualityParameter {
52      get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
53    }
54    public ValueLookupParameter<IntValue> MaximumIterationsParameter {
55      get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
56    }
57    public ValueLookupParameter<ResultCollection> ResultsParameter {
58      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
59    }
60    public ValueLookupParameter<IOperator> MutatorParameter {
61      get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
62    }
63    public ValueLookupParameter<IOperator> SelectorParameter {
64      get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
65    }
66    public ValueLookupParameter<IntValue> SampleSizeParameter {
67      get { return (ValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
68    }
69    public ValueLookupParameter<IOperator> AnalyzerParameter {
70      get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
71    }
72    #endregion
73
74    [StorableConstructor]
75    protected LocalAnalysisMainLoop(StorableConstructorFlag _) : base(_) { }
76    protected LocalAnalysisMainLoop(LocalAnalysisMainLoop original, Cloner cloner) : base(original, cloner) { }
77    public LocalAnalysisMainLoop()
78      : base() {
79      Initialize();
80    }
81
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new LocalAnalysisMainLoop(this, cloner);
84    }
85
86    private void Initialize() {
87      #region Create parameters
88      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
89      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
90      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
91      Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
92      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
93      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The variable collection where results should be stored."));
94
95      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "Mutation Operator."));
96      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator that selects how to continue."));
97      Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "Number of mutation samples."));
98
99      Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution and moves."));
100      #endregion
101
102      #region Create operators
103      VariableCreator variableCreator = new VariableCreator();
104      SubScopesProcessor subScopesProcessor0 = new SubScopesProcessor();
105      Assigner bestQualityInitializer = new Assigner();
106      Placeholder analyzer1 = new Placeholder();
107      ResultsCollector resultsCollector1 = new ResultsCollector();
108      LeftSelector leftSelector = new LeftSelector();
109      SubScopesProcessor mainProcessor = new SubScopesProcessor();
110      UniformSubScopesProcessor mutationProcessor = new UniformSubScopesProcessor();
111      Placeholder mutator = new Placeholder();
112      Placeholder evaluator = new Placeholder();
113      IntCounter evaluatedMovesCounter = new IntCounter();
114      Placeholder selector = new Placeholder();
115      SubScopesProcessor moveMakingProcessor = new SubScopesProcessor();
116      UniformSubScopesProcessor selectedMoveMakingProcessor = new UniformSubScopesProcessor();
117      QualityComparator qualityComparator = new QualityComparator();
118      ConditionalBranch improvesQualityBranch = new ConditionalBranch();
119      Assigner bestQualityUpdater = new Assigner();
120      Placeholder analyzer2 = new Placeholder();
121      RightReducer rightReducer = new RightReducer();
122      RightReducer rightReducer2 = new RightReducer();
123      IntCounter iterationsCounter = new IntCounter();
124      Comparator iterationsComparator = new Comparator();
125      ResultsCollector resultsCollector2 = new ResultsCollector();
126      ConditionalBranch iterationsTermination = new ConditionalBranch();
127
128      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
129      variableCreator.CollectedValues.Add(new ValueParameter<DoubleValue>("BestQuality", new DoubleValue(0)));
130      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedMoves", new IntValue(0)));
131
132      bestQualityInitializer.Name = "Initialize BestQuality";
133      bestQualityInitializer.LeftSideParameter.ActualName = "BestQuality";
134      bestQualityInitializer.RightSideParameter.ActualName = QualityParameter.ActualName;
135
136      analyzer1.Name = "Analyzer (placeholder)";
137      analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
138
139      resultsCollector1.CopyValue = new BoolValue(false);
140      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
141      resultsCollector1.CollectedValues.Add(new LookupParameter<DoubleValue>("Best Quality", null, "BestQuality"));
142      resultsCollector1.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
143      resultsCollector1.ResultsParameter.ActualName = ResultsParameter.Name;
144
145      leftSelector.CopySelected = new BoolValue(true);
146      leftSelector.NumberOfSelectedSubScopesParameter.ActualName = SampleSizeParameter.Name;
147
148      mutationProcessor.Parallel = new BoolValue(true);
149
150      mutator.Name = "(Mutator)";
151      mutator.OperatorParameter.ActualName = MutatorParameter.Name;
152
153      evaluator.Name = "(Evaluator)";
154      evaluator.OperatorParameter.ActualName = "Evaluator";
155
156      evaluatedMovesCounter.Name = "EvaluatedMoves++";
157      evaluatedMovesCounter.ValueParameter.ActualName = "EvaluatedMoves";
158      evaluatedMovesCounter.Increment = new IntValue(1);
159
160      selector.Name = "Selector (placeholder)";
161      selector.OperatorParameter.ActualName = SelectorParameter.Name;
162
163      qualityComparator.LeftSideParameter.ActualName = QualityParameter.Name;
164      qualityComparator.RightSideParameter.ActualName = "BestQuality";
165      qualityComparator.ResultParameter.ActualName = "IsBetter";
166
167      improvesQualityBranch.ConditionParameter.ActualName = "IsBetter";
168
169      bestQualityUpdater.Name = "Update BestQuality";
170      bestQualityUpdater.LeftSideParameter.ActualName = "BestQuality";
171      bestQualityUpdater.RightSideParameter.ActualName = QualityParameter.Name;
172
173      analyzer2.Name = "Analyzer (placeholder)";
174      analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
175
176      iterationsCounter.Name = "Iterations Counter";
177      iterationsCounter.Increment = new IntValue(1);
178      iterationsCounter.ValueParameter.ActualName = "Iterations";
179
180      iterationsComparator.Name = "Iterations >= MaximumIterations";
181      iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
182      iterationsComparator.LeftSideParameter.ActualName = "Iterations";
183      iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
184      iterationsComparator.ResultParameter.ActualName = "Terminate";
185
186      resultsCollector2.CopyValue = new BoolValue(false);
187      resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Moves", null, "EvaluatedMoves"));
188      resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
189
190      iterationsTermination.Name = "Iterations Termination Condition";
191      iterationsTermination.ConditionParameter.ActualName = "Terminate";
192      #endregion
193
194      #region Create operator graph
195      OperatorGraph.InitialOperator = variableCreator;
196      variableCreator.Successor = subScopesProcessor0;
197      subScopesProcessor0.Operators.Add(bestQualityInitializer);
198      subScopesProcessor0.Successor = resultsCollector1;
199      bestQualityInitializer.Successor = analyzer1;
200      analyzer1.Successor = null;
201      resultsCollector1.Successor = leftSelector;
202      leftSelector.Successor = mainProcessor;
203      mainProcessor.Operators.Add(new EmptyOperator());
204      mainProcessor.Operators.Add(mutationProcessor);
205      mainProcessor.Successor = rightReducer2;
206      mutationProcessor.Operator = mutator;
207      mutationProcessor.Successor = selector;
208      mutator.Successor = evaluator;
209      evaluator.Successor = evaluatedMovesCounter;
210      evaluatedMovesCounter.Successor = null;
211      selector.Successor = moveMakingProcessor;
212      moveMakingProcessor.Operators.Add(new EmptyOperator());
213      moveMakingProcessor.Operators.Add(selectedMoveMakingProcessor);
214      moveMakingProcessor.Successor = rightReducer;
215      selectedMoveMakingProcessor.Operator = qualityComparator;
216      selectedMoveMakingProcessor.Successor = null;
217      qualityComparator.Successor = improvesQualityBranch;
218      improvesQualityBranch.TrueBranch = bestQualityUpdater;
219      improvesQualityBranch.FalseBranch = null;
220      improvesQualityBranch.Successor = analyzer2;
221      bestQualityUpdater.Successor = null;
222      analyzer2.Successor = null;
223      rightReducer.Successor = null;
224      rightReducer2.Successor = iterationsCounter;
225      iterationsCounter.Successor = iterationsComparator;
226      iterationsComparator.Successor = resultsCollector2;
227      resultsCollector2.Successor = iterationsTermination;
228      iterationsTermination.TrueBranch = null;
229      iterationsTermination.FalseBranch = leftSelector;
230      #endregion
231    }
232
233    public override IOperation Apply() {
234      return base.Apply();
235    }
236  }
237}
Note: See TracBrowser for help on using the repository browser.