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 HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Data;
|
---|
25 | using HeuristicLab.Operators;
|
---|
26 | using HeuristicLab.Optimization;
|
---|
27 | using HeuristicLab.Optimization.Operators;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Random;
|
---|
31 | using System;
|
---|
32 | using System.Linq;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Analysis.FitnessLandscape {
|
---|
35 | [Item("Local Analysis", "A local analysis algorithm.")]
|
---|
36 | [StorableClass]
|
---|
37 | public abstract class LocalAnalysis<T> : HeuristicOptimizationEngineAlgorithm, IStorableContent where T : class, IOperator, new() {
|
---|
38 | public string Filename { get; set; }
|
---|
39 |
|
---|
40 | #region Problem Properties
|
---|
41 | public override Type ProblemType {
|
---|
42 | get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
|
---|
43 | }
|
---|
44 | public new ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
45 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
46 | set { base.Problem = value; }
|
---|
47 | }
|
---|
48 | #endregion
|
---|
49 |
|
---|
50 | #region Parameter Properties
|
---|
51 | public IFixedValueParameter<IntValue> SeedParameter {
|
---|
52 | get { return (IFixedValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
53 | }
|
---|
54 | public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
55 | get { return (IFixedValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
56 | }
|
---|
57 | public IConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
58 | get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
59 | }
|
---|
60 | public IFixedValueParameter<IntValue> MaximumIterationsParameter {
|
---|
61 | get { return (IFixedValueParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
62 | }
|
---|
63 | public IValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
64 | get { return (IValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
65 | }
|
---|
66 | public IFixedValueParameter<T> SelectorParameter {
|
---|
67 | get { return (IFixedValueParameter<T>)Parameters["Selector"]; }
|
---|
68 | }
|
---|
69 | #endregion
|
---|
70 |
|
---|
71 | #region Properties
|
---|
72 | protected RandomCreator RandomCreator {
|
---|
73 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
74 | }
|
---|
75 | protected VariableCreator VariableCreator {
|
---|
76 | get { return (VariableCreator)RandomCreator.Successor; }
|
---|
77 | }
|
---|
78 | protected SolutionsCreator SolutionsCreator {
|
---|
79 | get { return (SolutionsCreator)VariableCreator.Successor; }
|
---|
80 | }
|
---|
81 | protected LocalAnalysisMainLoop MainLoop {
|
---|
82 | get { return (LocalAnalysisMainLoop)SolutionsCreator.Successor; }
|
---|
83 | }
|
---|
84 |
|
---|
85 | [Storable]
|
---|
86 | private QualityTrailMultiAnalyzer qualityTrailAnalyzer;
|
---|
87 | #endregion
|
---|
88 |
|
---|
89 | [StorableConstructor]
|
---|
90 | protected LocalAnalysis(bool deserializing) : base(deserializing) { }
|
---|
91 | protected LocalAnalysis(LocalAnalysis<T> original, Cloner cloner)
|
---|
92 | : base(original, cloner) {
|
---|
93 | qualityTrailAnalyzer = cloner.Clone(original.qualityTrailAnalyzer);
|
---|
94 | RegisterEventHandlers();
|
---|
95 | }
|
---|
96 | protected LocalAnalysis(T selector)
|
---|
97 | : base() {
|
---|
98 | Parameters.Add(new FixedValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
99 | Parameters.Add(new FixedValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
100 | Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "Mutation operator."));
|
---|
101 | Parameters.Add(new FixedValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(10000)));
|
---|
102 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the solution and moves.", new MultiAnalyzer()));
|
---|
103 | Parameters.Add(new FixedValueParameter<T>("Selector", "Selection operator.", selector));
|
---|
104 |
|
---|
105 | RandomCreator randomCreator = new RandomCreator();
|
---|
106 | VariableCreator variableCreator = new VariableCreator();
|
---|
107 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
108 | LocalAnalysisMainLoop laMainLoop = new LocalAnalysisMainLoop();
|
---|
109 | OperatorGraph.InitialOperator = randomCreator;
|
---|
110 |
|
---|
111 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
112 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
113 | randomCreator.SeedParameter.Value = null;
|
---|
114 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
115 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
116 | randomCreator.Successor = variableCreator;
|
---|
117 |
|
---|
118 | variableCreator.Successor = solutionsCreator;
|
---|
119 |
|
---|
120 | solutionsCreator.NumberOfSolutions = new IntValue(1);
|
---|
121 | solutionsCreator.Successor = laMainLoop;
|
---|
122 |
|
---|
123 | laMainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
124 | laMainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
125 | laMainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
126 | laMainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
127 | laMainLoop.ResultsParameter.ActualName = "Results";
|
---|
128 | laMainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
129 |
|
---|
130 | qualityTrailAnalyzer = new QualityTrailMultiAnalyzer();
|
---|
131 | qualityTrailAnalyzer.UpdateIntervalParameter.Value = null;
|
---|
132 | qualityTrailAnalyzer.UpdateIntervalParameter.ActualName = "MaximumIterations";
|
---|
133 | AnalyzerParameter.Value.Operators.Add(qualityTrailAnalyzer, true);
|
---|
134 |
|
---|
135 | RegisterEventHandlers();
|
---|
136 | }
|
---|
137 |
|
---|
138 | public override void Prepare() {
|
---|
139 | if (Problem != null) base.Prepare();
|
---|
140 | }
|
---|
141 |
|
---|
142 | #region Events
|
---|
143 | protected override void OnProblemChanged() {
|
---|
144 | base.OnProblemChanged();
|
---|
145 | UpdateMutators();
|
---|
146 | Parameterize();
|
---|
147 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
148 | }
|
---|
149 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
150 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
151 | Parameterize();
|
---|
152 | }
|
---|
153 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
154 | base.Problem_EvaluatorChanged(sender, e);
|
---|
155 | Parameterize();
|
---|
156 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
157 | }
|
---|
158 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
159 | UpdateMutators();
|
---|
160 | Parameterize();
|
---|
161 | base.Problem_OperatorsChanged(sender, e);
|
---|
162 | }
|
---|
163 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
164 | Parameterize();
|
---|
165 | }
|
---|
166 | #endregion
|
---|
167 |
|
---|
168 | #region Helpers
|
---|
169 | private void RegisterEventHandlers() {
|
---|
170 | if (Problem != null) {
|
---|
171 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | private void UpdateMutators() {
|
---|
176 | var selected = MutatorParameter.Value;
|
---|
177 | MutatorParameter.ValidValues.Clear();
|
---|
178 | foreach (var m in Problem.Operators.OfType<IManipulator>()) {
|
---|
179 | MutatorParameter.ValidValues.Add(m);
|
---|
180 | if (selected != null && selected.GetType() == m.GetType()) MutatorParameter.Value = m;
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | protected virtual void Parameterize() {
|
---|
185 | if (Problem == null) return;
|
---|
186 |
|
---|
187 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
188 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
189 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
190 | foreach (var sOp in Problem.Operators.OfType<IStochasticOperator>()) {
|
---|
191 | sOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
192 | }
|
---|
193 | foreach (var iOp in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
194 | iOp.IterationsParameter.ActualName = "Iterations";
|
---|
195 | iOp.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
196 | }
|
---|
197 | var sEval = Problem.Evaluator as IStochasticOperator;
|
---|
198 | if (sEval != null) sEval.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
199 | var sCrea = Problem.SolutionCreator as IStochasticOperator;
|
---|
200 | if (sCrea != null) sCrea.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
201 | var sSel = SelectorParameter.Value as IStochasticOperator;
|
---|
202 | if (sSel != null) sSel.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
203 | var sel = SelectorParameter.Value as ISelector;
|
---|
204 | if (sel != null) {
|
---|
205 | sel.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
|
---|
206 | sel.CopySelected = new BoolValue(false);
|
---|
207 | var sos = sel as ISingleObjectiveSelector;
|
---|
208 | if (sos != null) {
|
---|
209 | sos.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
210 | sos.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
211 | }
|
---|
212 | }
|
---|
213 | }
|
---|
214 | #endregion
|
---|
215 | }
|
---|
216 | }
|
---|