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 |
|
---|
22 | using HEAL.Attic;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Operators;
|
---|
27 | using HeuristicLab.Optimization;
|
---|
28 | using HeuristicLab.Optimization.Operators;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Selection;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Analysis.FitnessLandscape {
|
---|
33 | /// <summary>
|
---|
34 | /// An operator which represents a local search.
|
---|
35 | /// </summary>
|
---|
36 | [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).")]
|
---|
37 | [StorableType("EA4AD808-4F92-4179-8828-F3D86116FC9F")]
|
---|
38 | public class LocalAnalysisMainLoop : AlgorithmOperator {
|
---|
39 |
|
---|
40 | #region Parameter properties
|
---|
41 | public ValueLookupParameter<IRandom> RandomParameter {
|
---|
42 | get { return (ValueLookupParameter<IRandom>)Parameters["Random"]; }
|
---|
43 | }
|
---|
44 | public ValueLookupParameter<BoolValue> MaximizationParameter {
|
---|
45 | get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
46 | }
|
---|
47 | public ValueLookupParameter<DoubleValue> BestKnownQualityParameter {
|
---|
48 | get { return (ValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
|
---|
49 | }
|
---|
50 | public LookupParameter<DoubleValue> QualityParameter {
|
---|
51 | get { return (LookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
52 | }
|
---|
53 | public ValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
54 | get { return (ValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
55 | }
|
---|
56 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
57 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
58 | }
|
---|
59 | public ValueLookupParameter<IOperator> MutatorParameter {
|
---|
60 | get { return (ValueLookupParameter<IOperator>)Parameters["Mutator"]; }
|
---|
61 | }
|
---|
62 | public ValueLookupParameter<IOperator> SelectorParameter {
|
---|
63 | get { return (ValueLookupParameter<IOperator>)Parameters["Selector"]; }
|
---|
64 | }
|
---|
65 | public ValueLookupParameter<IntValue> SampleSizeParameter {
|
---|
66 | get { return (ValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
|
---|
67 | }
|
---|
68 | public ValueLookupParameter<IOperator> AnalyzerParameter {
|
---|
69 | get { return (ValueLookupParameter<IOperator>)Parameters["Analyzer"]; }
|
---|
70 | }
|
---|
71 | #endregion
|
---|
72 |
|
---|
73 | [StorableConstructor]
|
---|
74 | protected LocalAnalysisMainLoop(StorableConstructorFlag _) : base(_) { }
|
---|
75 | protected LocalAnalysisMainLoop(LocalAnalysisMainLoop original, Cloner cloner) : base(original, cloner) { }
|
---|
76 | public LocalAnalysisMainLoop()
|
---|
77 | : base() {
|
---|
78 | Initialize();
|
---|
79 | }
|
---|
80 |
|
---|
81 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
82 | return new LocalAnalysisMainLoop(this, cloner);
|
---|
83 | }
|
---|
84 |
|
---|
85 | private void Initialize() {
|
---|
86 | #region Create parameters
|
---|
87 | Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
|
---|
88 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
|
---|
89 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
|
---|
90 | Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The best known quality value found so far."));
|
---|
91 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed."));
|
---|
92 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The variable collection where results should be stored."));
|
---|
93 |
|
---|
94 | Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "Mutation Operator."));
|
---|
95 | Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator that selects how to continue."));
|
---|
96 | Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "Number of mutation samples."));
|
---|
97 |
|
---|
98 | Parameters.Add(new ValueLookupParameter<IOperator>("Analyzer", "The operator used to analyze the solution and moves."));
|
---|
99 | #endregion
|
---|
100 |
|
---|
101 | #region Create operators
|
---|
102 | VariableCreator variableCreator = new VariableCreator();
|
---|
103 | Placeholder analyzer1 = new Placeholder();
|
---|
104 | SubScopesCounter evaluatedSolutionsCounter1 = new SubScopesCounter();
|
---|
105 | LeftSelector leftSelector = new LeftSelector();
|
---|
106 | SubScopesProcessor mainProcessor = new SubScopesProcessor();
|
---|
107 | UniformSubScopesProcessor mutationProcessor = new UniformSubScopesProcessor();
|
---|
108 | Placeholder mutator = new Placeholder();
|
---|
109 | Placeholder evaluator = new Placeholder();
|
---|
110 | SubScopesCounter evaluatedSolutionsCounter2 = new SubScopesCounter();
|
---|
111 | Placeholder selector = new Placeholder();
|
---|
112 | RightReducer rightReducer1 = new RightReducer();
|
---|
113 | RightReducer rightReducer2 = new RightReducer();
|
---|
114 | Placeholder analyzer2 = new Placeholder();
|
---|
115 | IntCounter iterationsCounter = new IntCounter();
|
---|
116 | Comparator iterationsComparator = new Comparator();
|
---|
117 | ResultsCollector resultsCollector2 = new ResultsCollector();
|
---|
118 | ConditionalBranch iterationsTermination = new ConditionalBranch();
|
---|
119 |
|
---|
120 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0)));
|
---|
121 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
122 |
|
---|
123 | analyzer1.Name = "Analyzer (placeholder)";
|
---|
124 | analyzer1.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
125 |
|
---|
126 | evaluatedSolutionsCounter1.Name = "EvaluatedSolutions++";
|
---|
127 | evaluatedSolutionsCounter1.AccumulateParameter.Value = new BoolValue(true);
|
---|
128 | evaluatedSolutionsCounter1.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
129 |
|
---|
130 | leftSelector.CopySelected = new BoolValue(true);
|
---|
131 | leftSelector.NumberOfSelectedSubScopesParameter.ActualName = SampleSizeParameter.Name;
|
---|
132 |
|
---|
133 | mainProcessor.Name = "Main Processsor";
|
---|
134 |
|
---|
135 | mutationProcessor.Parallel = new BoolValue(false);
|
---|
136 |
|
---|
137 | mutator.Name = "(Mutator)";
|
---|
138 | mutator.OperatorParameter.ActualName = MutatorParameter.Name;
|
---|
139 |
|
---|
140 | evaluator.Name = "(Evaluator)";
|
---|
141 | evaluator.OperatorParameter.ActualName = "Evaluator";
|
---|
142 |
|
---|
143 | evaluatedSolutionsCounter2.Name = "EvaluatedSolutions++";
|
---|
144 | evaluatedSolutionsCounter2.AccumulateParameter.Value = new BoolValue(true);
|
---|
145 | evaluatedSolutionsCounter2.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
146 |
|
---|
147 | selector.Name = "(Selector)";
|
---|
148 | selector.OperatorParameter.ActualName = SelectorParameter.Name;
|
---|
149 |
|
---|
150 | analyzer2.Name = "(Analyzer)";
|
---|
151 | analyzer2.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
152 |
|
---|
153 | iterationsCounter.Name = "Iterations Counter";
|
---|
154 | iterationsCounter.Increment = new IntValue(1);
|
---|
155 | iterationsCounter.ValueParameter.ActualName = "Iterations";
|
---|
156 |
|
---|
157 | iterationsComparator.Name = "Iterations >= MaximumIterations";
|
---|
158 | iterationsComparator.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
|
---|
159 | iterationsComparator.LeftSideParameter.ActualName = "Iterations";
|
---|
160 | iterationsComparator.RightSideParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
161 | iterationsComparator.ResultParameter.ActualName = "Terminate";
|
---|
162 |
|
---|
163 | resultsCollector2.CopyValue = new BoolValue(true);
|
---|
164 | resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("Iterations"));
|
---|
165 | resultsCollector2.CollectedValues.Add(new LookupParameter<IntValue>("EvaluatedSolutions"));
|
---|
166 | resultsCollector2.ResultsParameter.ActualName = ResultsParameter.Name;
|
---|
167 |
|
---|
168 | iterationsTermination.Name = "Iterations Termination Condition";
|
---|
169 | iterationsTermination.ConditionParameter.ActualName = "Terminate";
|
---|
170 | #endregion
|
---|
171 |
|
---|
172 | #region Create operator graph
|
---|
173 | OperatorGraph.InitialOperator = variableCreator;
|
---|
174 | variableCreator.Successor = evaluatedSolutionsCounter1;
|
---|
175 | evaluatedSolutionsCounter1.Successor = analyzer1;
|
---|
176 | analyzer1.Successor = leftSelector;
|
---|
177 | leftSelector.Successor = mainProcessor;
|
---|
178 | mainProcessor.Operators.Add(new EmptyOperator());
|
---|
179 | mainProcessor.Operators.Add(mutationProcessor);
|
---|
180 | mainProcessor.Successor = rightReducer2;
|
---|
181 | mutationProcessor.Operator = mutator;
|
---|
182 | mutationProcessor.Successor = evaluatedSolutionsCounter2;
|
---|
183 | mutator.Successor = evaluator;
|
---|
184 | evaluator.Successor = null;
|
---|
185 | evaluatedSolutionsCounter2.Successor = selector;
|
---|
186 | selector.Successor = rightReducer1;
|
---|
187 | rightReducer1.Successor = null;
|
---|
188 | rightReducer2.Successor = iterationsCounter;
|
---|
189 | iterationsCounter.Successor = analyzer2;
|
---|
190 | analyzer2.Successor = iterationsComparator;
|
---|
191 | iterationsComparator.Successor = iterationsTermination;
|
---|
192 | iterationsTermination.TrueBranch = resultsCollector2;
|
---|
193 | iterationsTermination.FalseBranch = leftSelector;
|
---|
194 | #endregion
|
---|
195 | }
|
---|
196 | }
|
---|
197 | }
|
---|