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