[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 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
[13583] | 25 | using HeuristicLab.Operators;
|
---|
[7128] | 26 | using HeuristicLab.Optimization;
|
---|
| 27 | using HeuristicLab.Optimization.Operators;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.Random;
|
---|
[13583] | 31 | using System;
|
---|
| 32 | using System.Linq;
|
---|
[7128] | 33 |
|
---|
| 34 | namespace HeuristicLab.Analysis.FitnessLandscape {
|
---|
| 35 | [Item("Local Analysis", "A local analysis algorithm.")]
|
---|
| 36 | [StorableClass]
|
---|
[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 | }
|
---|
[13583] | 63 | public IValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
| 64 | get { return (IValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
[7128] | 65 | }
|
---|
[13583] | 66 | public IFixedValueParameter<T> SelectorParameter {
|
---|
| 67 | get { return (IFixedValueParameter<T>)Parameters["Selector"]; }
|
---|
[7128] | 68 | }
|
---|
| 69 | #endregion
|
---|
| 70 |
|
---|
| 71 | #region Properties
|
---|
[13583] | 72 | protected RandomCreator RandomCreator {
|
---|
[7128] | 73 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
| 74 | }
|
---|
[13583] | 75 | protected VariableCreator VariableCreator {
|
---|
| 76 | get { return (VariableCreator)RandomCreator.Successor; }
|
---|
[7128] | 77 | }
|
---|
[13583] | 78 | protected SolutionsCreator SolutionsCreator {
|
---|
| 79 | get { return (SolutionsCreator)VariableCreator.Successor; }
|
---|
| 80 | }
|
---|
| 81 | protected LocalAnalysisMainLoop MainLoop {
|
---|
[7128] | 82 | get { return (LocalAnalysisMainLoop)SolutionsCreator.Successor; }
|
---|
| 83 | }
|
---|
[13583] | 84 |
|
---|
[7128] | 85 | [Storable]
|
---|
| 86 | private QualityTrailMultiAnalyzer qualityTrailAnalyzer;
|
---|
| 87 | #endregion
|
---|
| 88 |
|
---|
| 89 | [StorableConstructor]
|
---|
[13583] | 90 | protected LocalAnalysis(bool deserializing) : base(deserializing) { }
|
---|
| 91 | protected LocalAnalysis(LocalAnalysis<T> original, Cloner cloner)
|
---|
[7128] | 92 | : base(original, cloner) {
|
---|
[13583] | 93 | qualityTrailAnalyzer = cloner.Clone(original.qualityTrailAnalyzer);
|
---|
| 94 | RegisterEventHandlers();
|
---|
[7128] | 95 | }
|
---|
[13583] | 96 | protected LocalAnalysis(T selector)
|
---|
[7128] | 97 | : base() {
|
---|
[13583] | 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)));
|
---|
[7128] | 100 | Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "Mutation operator."));
|
---|
[13583] | 101 | Parameters.Add(new FixedValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(10000)));
|
---|
[7128] | 102 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the solution and moves.", new MultiAnalyzer()));
|
---|
[13583] | 103 | Parameters.Add(new FixedValueParameter<T>("Selector", "Selection operator.", selector));
|
---|
[7128] | 104 |
|
---|
| 105 | RandomCreator randomCreator = new RandomCreator();
|
---|
[13583] | 106 | VariableCreator variableCreator = new VariableCreator();
|
---|
[7128] | 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;
|
---|
[13583] | 116 | randomCreator.Successor = variableCreator;
|
---|
[7128] | 117 |
|
---|
[13583] | 118 | variableCreator.Successor = solutionsCreator;
|
---|
| 119 |
|
---|
[7128] | 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();
|
---|
[13583] | 131 | qualityTrailAnalyzer.UpdateIntervalParameter.Value = null;
|
---|
| 132 | qualityTrailAnalyzer.UpdateIntervalParameter.ActualName = "MaximumIterations";
|
---|
| 133 | AnalyzerParameter.Value.Operators.Add(qualityTrailAnalyzer, true);
|
---|
[7128] | 134 |
|
---|
[13583] | 135 | RegisterEventHandlers();
|
---|
[7128] | 136 | }
|
---|
| 137 |
|
---|
| 138 | public override void Prepare() {
|
---|
[13583] | 139 | if (Problem != null) base.Prepare();
|
---|
[7128] | 140 | }
|
---|
| 141 |
|
---|
| 142 | #region Events
|
---|
| 143 | protected override void OnProblemChanged() {
|
---|
[13583] | 144 | base.OnProblemChanged();
|
---|
[7128] | 145 | UpdateMutators();
|
---|
[13583] | 146 | Parameterize();
|
---|
| 147 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
[7128] | 148 | }
|
---|
| 149 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
| 150 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
[13583] | 151 | Parameterize();
|
---|
[7128] | 152 | }
|
---|
| 153 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
| 154 | base.Problem_EvaluatorChanged(sender, e);
|
---|
[13583] | 155 | Parameterize();
|
---|
| 156 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
[7128] | 157 | }
|
---|
| 158 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
| 159 | UpdateMutators();
|
---|
[13583] | 160 | Parameterize();
|
---|
[7128] | 161 | base.Problem_OperatorsChanged(sender, e);
|
---|
| 162 | }
|
---|
| 163 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
[13583] | 164 | Parameterize();
|
---|
[7128] | 165 | }
|
---|
| 166 | #endregion
|
---|
| 167 |
|
---|
| 168 | #region Helpers
|
---|
[13583] | 169 | private void RegisterEventHandlers() {
|
---|
[7128] | 170 | if (Problem != null) {
|
---|
[13583] | 171 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
[7128] | 172 | }
|
---|
| 173 | }
|
---|
[13583] | 174 |
|
---|
[7128] | 175 | private void UpdateMutators() {
|
---|
[13583] | 176 | var selected = MutatorParameter.Value;
|
---|
[7128] | 177 | MutatorParameter.ValidValues.Clear();
|
---|
| 178 | foreach (var m in Problem.Operators.OfType<IManipulator>()) {
|
---|
| 179 | MutatorParameter.ValidValues.Add(m);
|
---|
[13583] | 180 | if (selected != null && selected.GetType() == m.GetType()) MutatorParameter.Value = m;
|
---|
[7128] | 181 | }
|
---|
| 182 | }
|
---|
[13583] | 183 |
|
---|
| 184 | protected virtual void Parameterize() {
|
---|
| 185 | if (Problem == null) return;
|
---|
| 186 |
|
---|
[7128] | 187 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 188 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 189 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[13583] | 190 | foreach (var sOp in Problem.Operators.OfType<IStochasticOperator>()) {
|
---|
| 191 | sOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
[7128] | 192 | }
|
---|
[13583] | 193 | foreach (var iOp in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
| 194 | iOp.IterationsParameter.ActualName = "Iterations";
|
---|
| 195 | iOp.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name;
|
---|
[7128] | 196 | }
|
---|
[13583] | 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;
|
---|
[7128] | 211 | }
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | #endregion
|
---|
| 215 | }
|
---|
| 216 | }
|
---|