[7128] | 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 |
|
---|
[16958] | 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HEAL.Attic;
|
---|
[7128] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
[13583] | 28 | using HeuristicLab.Operators;
|
---|
[7128] | 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Optimization.Operators;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Random;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Analysis.FitnessLandscape {
|
---|
| 35 | [Item("Local Analysis", "A local analysis algorithm.")]
|
---|
[16958] | 36 | [StorableType("2D91C41A-81F8-4EDC-8FD9-F9AE8F4BDA1D")]
|
---|
[13583] | 37 | public abstract class LocalAnalysis<T> : HeuristicOptimizationEngineAlgorithm, IStorableContent where T : class, IOperator, new() {
|
---|
[7128] | 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
|
---|
[13583] | 51 | public IFixedValueParameter<IntValue> SeedParameter {
|
---|
| 52 | get { return (IFixedValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
[7128] | 53 | }
|
---|
[13583] | 54 | public IFixedValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
| 55 | get { return (IFixedValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
[7128] | 56 | }
|
---|
[8172] | 57 | public IConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
| 58 | get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
[7128] | 59 | }
|
---|
[13583] | 60 | public IFixedValueParameter<IntValue> MaximumIterationsParameter {
|
---|
| 61 | get { return (IFixedValueParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
[7128] | 62 | }
|
---|
[13920] | 63 | public IFixedValueParameter<IntValue> RepetitionsParameter {
|
---|
| 64 | get { return (IFixedValueParameter<IntValue>)Parameters["Repetitions"]; }
|
---|
| 65 | }
|
---|
[13583] | 66 | public IValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
| 67 | get { return (IValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
[7128] | 68 | }
|
---|
[13583] | 69 | public IFixedValueParameter<T> SelectorParameter {
|
---|
| 70 | get { return (IFixedValueParameter<T>)Parameters["Selector"]; }
|
---|
[7128] | 71 | }
|
---|
| 72 | #endregion
|
---|
| 73 |
|
---|
| 74 | #region Properties
|
---|
[13920] | 75 | protected RandomCreator GlobalRandomCreator {
|
---|
[7128] | 76 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
| 77 | }
|
---|
[13920] | 78 | protected UniformSubScopesProcessor FirstUniformSubScopesProcessor {
|
---|
| 79 | get { return (UniformSubScopesProcessor)((SubScopesCreator)GlobalRandomCreator.Successor).Successor; }
|
---|
| 80 | }
|
---|
| 81 | protected LocalRandomCreator IteratedRandomCreator {
|
---|
| 82 | get { return (LocalRandomCreator)FirstUniformSubScopesProcessor.Operator; }
|
---|
| 83 | }
|
---|
[13583] | 84 | protected VariableCreator VariableCreator {
|
---|
[13920] | 85 | get { return (VariableCreator)IteratedRandomCreator.Successor; }
|
---|
[7128] | 86 | }
|
---|
[13920] | 87 | protected UniformSubScopesProcessor SecondUniformSubScopesProcessor {
|
---|
| 88 | get { return (UniformSubScopesProcessor)FirstUniformSubScopesProcessor.Successor; }
|
---|
| 89 | }
|
---|
[13583] | 90 | protected SolutionsCreator SolutionsCreator {
|
---|
[13920] | 91 | get { return (SolutionsCreator)SecondUniformSubScopesProcessor.Operator; }
|
---|
[13583] | 92 | }
|
---|
| 93 | protected LocalAnalysisMainLoop MainLoop {
|
---|
[7128] | 94 | get { return (LocalAnalysisMainLoop)SolutionsCreator.Successor; }
|
---|
| 95 | }
|
---|
[13583] | 96 |
|
---|
[7128] | 97 | [Storable]
|
---|
| 98 | private QualityTrailMultiAnalyzer qualityTrailAnalyzer;
|
---|
| 99 | #endregion
|
---|
| 100 |
|
---|
| 101 | [StorableConstructor]
|
---|
[16958] | 102 | protected LocalAnalysis(StorableConstructorFlag _) : base(_) { }
|
---|
[13583] | 103 | protected LocalAnalysis(LocalAnalysis<T> original, Cloner cloner)
|
---|
[7128] | 104 | : base(original, cloner) {
|
---|
[13583] | 105 | qualityTrailAnalyzer = cloner.Clone(original.qualityTrailAnalyzer);
|
---|
| 106 | RegisterEventHandlers();
|
---|
[7128] | 107 | }
|
---|
[13583] | 108 | protected LocalAnalysis(T selector)
|
---|
[7128] | 109 | : base() {
|
---|
[13583] | 110 | Parameters.Add(new FixedValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 111 | Parameters.Add(new FixedValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
[7128] | 112 | Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "Mutation operator."));
|
---|
[13920] | 113 | Parameters.Add(new FixedValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(100)));
|
---|
| 114 | Parameters.Add(new FixedValueParameter<IntValue>("Repetitions", "The number of repetitions that should be performed.", new IntValue(10)));
|
---|
[7128] | 115 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the solution and moves.", new MultiAnalyzer()));
|
---|
[13583] | 116 | Parameters.Add(new FixedValueParameter<T>("Selector", "Selection operator.", selector));
|
---|
[7128] | 117 |
|
---|
| 118 | RandomCreator randomCreator = new RandomCreator();
|
---|
[13920] | 119 | SubScopesCreator ssc = new SubScopesCreator();
|
---|
| 120 | UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
|
---|
| 121 | UniformSubScopesProcessor ussp2 = new UniformSubScopesProcessor();
|
---|
| 122 | LocalRandomCreator lrc = new LocalRandomCreator();
|
---|
[13583] | 123 | VariableCreator variableCreator = new VariableCreator();
|
---|
[7128] | 124 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
| 125 | LocalAnalysisMainLoop laMainLoop = new LocalAnalysisMainLoop();
|
---|
[13920] | 126 | ResultsCollector collector = new ResultsCollector();
|
---|
| 127 |
|
---|
[7128] | 128 | OperatorGraph.InitialOperator = randomCreator;
|
---|
| 129 |
|
---|
| 130 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
| 131 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
| 132 | randomCreator.SeedParameter.Value = null;
|
---|
| 133 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
| 134 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
[13920] | 135 | randomCreator.Successor = ssc;
|
---|
[7128] | 136 |
|
---|
[13920] | 137 | ssc.NumberOfSubScopesParameter.Value = null;
|
---|
| 138 | ssc.NumberOfSubScopesParameter.ActualName = RepetitionsParameter.Name;
|
---|
| 139 | ssc.Successor = ussp1;
|
---|
[13583] | 140 |
|
---|
[13920] | 141 | ussp1.Depth = new IntValue(1);
|
---|
| 142 | ussp1.Parallel = new BoolValue(false);
|
---|
| 143 | ussp1.Operator = lrc;
|
---|
| 144 | ussp1.Successor = ussp2;
|
---|
| 145 |
|
---|
| 146 | lrc.GlobalRandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
|
---|
| 147 | lrc.LocalRandomParameter.ActualName = "IteratedRandom";
|
---|
| 148 | lrc.Successor = variableCreator;
|
---|
| 149 |
|
---|
| 150 | var iterResults = new ValueParameter<ResultCollection>("IteratedResults", new ResultCollection());
|
---|
| 151 | variableCreator.CollectedValues.Add(iterResults);
|
---|
| 152 | variableCreator.Successor = null;
|
---|
| 153 |
|
---|
| 154 | ussp2.Depth = new IntValue(1);
|
---|
| 155 | ussp2.Parallel = new BoolValue(true);
|
---|
| 156 | ussp2.Operator = solutionsCreator;
|
---|
| 157 | ussp2.Successor = collector;
|
---|
| 158 |
|
---|
[7128] | 159 | solutionsCreator.NumberOfSolutions = new IntValue(1);
|
---|
| 160 | solutionsCreator.Successor = laMainLoop;
|
---|
| 161 |
|
---|
| 162 | laMainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
| 163 | laMainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
| 164 | laMainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
[13920] | 165 | laMainLoop.RandomParameter.ActualName = lrc.LocalRandomParameter.ActualName;
|
---|
| 166 | laMainLoop.ResultsParameter.ActualName = iterResults.Name;
|
---|
[7128] | 167 | laMainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 168 |
|
---|
| 169 | qualityTrailAnalyzer = new QualityTrailMultiAnalyzer();
|
---|
[13583] | 170 | qualityTrailAnalyzer.UpdateIntervalParameter.Value = null;
|
---|
[13920] | 171 | qualityTrailAnalyzer.UpdateIntervalParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
[13583] | 172 | AnalyzerParameter.Value.Operators.Add(qualityTrailAnalyzer, true);
|
---|
[7128] | 173 |
|
---|
[13920] | 174 | collector.CollectedValues.Add(new ScopeTreeLookupParameter<ResultCollection>("IteratedResults", 1));
|
---|
| 175 | collector.ResultsParameter.ActualName = "Results";
|
---|
| 176 | collector.CopyValue = new BoolValue(false);
|
---|
| 177 | collector.Successor = null;
|
---|
| 178 |
|
---|
[13583] | 179 | RegisterEventHandlers();
|
---|
[7128] | 180 | }
|
---|
| 181 |
|
---|
| 182 | public override void Prepare() {
|
---|
[13583] | 183 | if (Problem != null) base.Prepare();
|
---|
[7128] | 184 | }
|
---|
| 185 |
|
---|
[13920] | 186 | protected override void OnStopped() {
|
---|
| 187 | var iteratedResults = Results.SingleOrDefault(x => x.Name == "IteratedResults");
|
---|
| 188 | if (iteratedResults != null) {
|
---|
| 189 | var rc = iteratedResults.Value as ItemArray<ResultCollection>;
|
---|
| 190 | if (rc != null) {
|
---|
| 191 | var results = rc.FirstOrDefault();
|
---|
| 192 | if (results != null) {
|
---|
| 193 | var toAvg = results.Select(x => x.Name).ToDictionary(x => x, x => 0.0);
|
---|
| 194 | foreach (var r in rc) {
|
---|
| 195 | foreach (var v in r) {
|
---|
| 196 | if (!toAvg.ContainsKey(v.Name)) continue;
|
---|
| 197 | var value = v.Value as IntValue;
|
---|
| 198 | if (value != null) toAvg[v.Name] += value.Value;
|
---|
| 199 | else {
|
---|
| 200 | var doubleValue = v.Value as DoubleValue;
|
---|
| 201 | if (doubleValue != null) toAvg[v.Name] += doubleValue.Value;
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | foreach (var r in toAvg.Keys.OrderBy(x => x)) {
|
---|
| 207 | Results.Add(new Result(r, new DoubleValue(toAvg[r] / RepetitionsParameter.Value.Value)));
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | base.OnStopped();
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[7128] | 215 | #region Events
|
---|
| 216 | protected override void OnProblemChanged() {
|
---|
[13583] | 217 | base.OnProblemChanged();
|
---|
[7128] | 218 | UpdateMutators();
|
---|
[13583] | 219 | Parameterize();
|
---|
| 220 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
[7128] | 221 | }
|
---|
| 222 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
| 223 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
[13583] | 224 | Parameterize();
|
---|
[7128] | 225 | }
|
---|
| 226 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
| 227 | base.Problem_EvaluatorChanged(sender, e);
|
---|
[13583] | 228 | Parameterize();
|
---|
| 229 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
[7128] | 230 | }
|
---|
| 231 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
| 232 | UpdateMutators();
|
---|
[13583] | 233 | Parameterize();
|
---|
[7128] | 234 | base.Problem_OperatorsChanged(sender, e);
|
---|
| 235 | }
|
---|
| 236 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
[13583] | 237 | Parameterize();
|
---|
[7128] | 238 | }
|
---|
| 239 | #endregion
|
---|
| 240 |
|
---|
| 241 | #region Helpers
|
---|
[13583] | 242 | private void RegisterEventHandlers() {
|
---|
[7128] | 243 | if (Problem != null) {
|
---|
[13583] | 244 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
[7128] | 245 | }
|
---|
| 246 | }
|
---|
[13583] | 247 |
|
---|
[7128] | 248 | private void UpdateMutators() {
|
---|
[13583] | 249 | var selected = MutatorParameter.Value;
|
---|
[7128] | 250 | MutatorParameter.ValidValues.Clear();
|
---|
| 251 | foreach (var m in Problem.Operators.OfType<IManipulator>()) {
|
---|
| 252 | MutatorParameter.ValidValues.Add(m);
|
---|
[13583] | 253 | if (selected != null && selected.GetType() == m.GetType()) MutatorParameter.Value = m;
|
---|
[7128] | 254 | }
|
---|
| 255 | }
|
---|
[13583] | 256 |
|
---|
| 257 | protected virtual void Parameterize() {
|
---|
| 258 | if (Problem == null) return;
|
---|
| 259 |
|
---|
[7128] | 260 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 261 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 262 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[13583] | 263 | foreach (var sOp in Problem.Operators.OfType<IStochasticOperator>()) {
|
---|
[13920] | 264 | sOp.RandomParameter.ActualName = IteratedRandomCreator.LocalRandomParameter.ActualName;
|
---|
[7128] | 265 | }
|
---|
[13583] | 266 | foreach (var iOp in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
| 267 | iOp.IterationsParameter.ActualName = "Iterations";
|
---|
| 268 | iOp.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
[7128] | 269 | }
|
---|
[13583] | 270 | var sEval = Problem.Evaluator as IStochasticOperator;
|
---|
[13920] | 271 | if (sEval != null) sEval.RandomParameter.ActualName = IteratedRandomCreator.LocalRandomParameter.ActualName;
|
---|
[13583] | 272 | var sCrea = Problem.SolutionCreator as IStochasticOperator;
|
---|
[13920] | 273 | if (sCrea != null) sCrea.RandomParameter.ActualName = IteratedRandomCreator.LocalRandomParameter.ActualName;
|
---|
[13583] | 274 | var sSel = SelectorParameter.Value as IStochasticOperator;
|
---|
[13920] | 275 | if (sSel != null) sSel.RandomParameter.ActualName = IteratedRandomCreator.LocalRandomParameter.ActualName;
|
---|
[13583] | 276 | var sel = SelectorParameter.Value as ISelector;
|
---|
| 277 | if (sel != null) {
|
---|
| 278 | sel.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
|
---|
| 279 | sel.CopySelected = new BoolValue(false);
|
---|
| 280 | var sos = sel as ISingleObjectiveSelector;
|
---|
| 281 | if (sos != null) {
|
---|
| 282 | sos.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 283 | sos.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[7128] | 284 | }
|
---|
| 285 | }
|
---|
[13920] | 286 |
|
---|
| 287 | foreach (var op in Problem.Operators.OfType<IParameterizedItem>()) {
|
---|
| 288 | var resultsParam = op.Parameters.SingleOrDefault(x => x.Name == "Results");
|
---|
| 289 | var lookupParam = resultsParam as ILookupParameter<ResultCollection>;
|
---|
| 290 | if (lookupParam == null) continue;
|
---|
| 291 | lookupParam.ActualName = "IteratedResults";
|
---|
| 292 | }
|
---|
[7128] | 293 | }
|
---|
| 294 | #endregion
|
---|
| 295 | }
|
---|
| 296 | }
|
---|