| 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.Analysis;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Optimization.Operators;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 | using HeuristicLab.Random;
|
---|
| 34 |
|
---|
| 35 | namespace HeuristicLab.Algorithms.EvolutionStrategy {
|
---|
| 36 | /// <summary>
|
---|
| 37 | /// A standard genetic algorithm.
|
---|
| 38 | /// </summary>
|
---|
| 39 | [Item("Evolution Strategy", "An evolution strategy.")]
|
---|
| 40 | [Creatable("Algorithms")]
|
---|
| 41 | [StorableClass]
|
---|
| 42 | public sealed class EvolutionStrategy : EngineAlgorithm, IStorableContent {
|
---|
| 43 | public string Filename { get; set; }
|
---|
| 44 |
|
---|
| 45 | #region Problem Properties
|
---|
| 46 | public override Type ProblemType {
|
---|
| 47 | get { return typeof(ISingleObjectiveProblem); }
|
---|
| 48 | }
|
---|
| 49 | public new ISingleObjectiveProblem Problem {
|
---|
| 50 | get { return (ISingleObjectiveProblem)base.Problem; }
|
---|
| 51 | set { base.Problem = value; }
|
---|
| 52 | }
|
---|
| 53 | #endregion
|
---|
| 54 |
|
---|
| 55 | #region Parameter Properties
|
---|
| 56 | private ValueParameter<IntValue> SeedParameter {
|
---|
| 57 | get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
| 58 | }
|
---|
| 59 | private ValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
| 60 | get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
| 61 | }
|
---|
| 62 | private ValueParameter<IntValue> PopulationSizeParameter {
|
---|
| 63 | get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
| 64 | }
|
---|
| 65 | private ValueParameter<IntValue> ParentsPerChildParameter {
|
---|
| 66 | get { return (ValueParameter<IntValue>)Parameters["ParentsPerChild"]; }
|
---|
| 67 | }
|
---|
| 68 | private ValueParameter<IntValue> ChildrenParameter {
|
---|
| 69 | get { return (ValueParameter<IntValue>)Parameters["Children"]; }
|
---|
| 70 | }
|
---|
| 71 | private ValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 72 | get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 73 | }
|
---|
| 74 | private ValueParameter<BoolValue> PlusSelectionParameter {
|
---|
| 75 | get { return (ValueParameter<BoolValue>)Parameters["PlusSelection"]; }
|
---|
| 76 | }
|
---|
| 77 | private ConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
| 78 | get { return (ConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
| 79 | }
|
---|
| 80 | private OptionalConstrainedValueParameter<ICrossover> RecombinatorParameter {
|
---|
| 81 | get { return (OptionalConstrainedValueParameter<ICrossover>)Parameters["Recombinator"]; }
|
---|
| 82 | }
|
---|
| 83 | private ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
| 84 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
| 85 | }
|
---|
| 86 | private OptionalConstrainedValueParameter<IStrategyParameterCreator> StrategyParameterCreatorParameter {
|
---|
| 87 | get { return (OptionalConstrainedValueParameter<IStrategyParameterCreator>)Parameters["StrategyParameterCreator"]; }
|
---|
| 88 | }
|
---|
| 89 | private OptionalConstrainedValueParameter<IStrategyParameterCrossover> StrategyParameterCrossoverParameter {
|
---|
| 90 | get { return (OptionalConstrainedValueParameter<IStrategyParameterCrossover>)Parameters["StrategyParameterCrossover"]; }
|
---|
| 91 | }
|
---|
| 92 | private OptionalConstrainedValueParameter<IStrategyParameterManipulator> StrategyParameterManipulatorParameter {
|
---|
| 93 | get { return (OptionalConstrainedValueParameter<IStrategyParameterManipulator>)Parameters["StrategyParameterManipulator"]; }
|
---|
| 94 | }
|
---|
| 95 | #endregion
|
---|
| 96 |
|
---|
| 97 | #region Properties
|
---|
| 98 | public IntValue Seed {
|
---|
| 99 | get { return SeedParameter.Value; }
|
---|
| 100 | set { SeedParameter.Value = value; }
|
---|
| 101 | }
|
---|
| 102 | public BoolValue SetSeedRandomly {
|
---|
| 103 | get { return SetSeedRandomlyParameter.Value; }
|
---|
| 104 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
| 105 | }
|
---|
| 106 | public IntValue PopulationSize {
|
---|
| 107 | get { return PopulationSizeParameter.Value; }
|
---|
| 108 | set { PopulationSizeParameter.Value = value; }
|
---|
| 109 | }
|
---|
| 110 | public IntValue ParentsPerChild {
|
---|
| 111 | get { return ParentsPerChildParameter.Value; }
|
---|
| 112 | set { ParentsPerChildParameter.Value = value; }
|
---|
| 113 | }
|
---|
| 114 | public IntValue Children {
|
---|
| 115 | get { return ChildrenParameter.Value; }
|
---|
| 116 | set { ChildrenParameter.Value = value; }
|
---|
| 117 | }
|
---|
| 118 | public IntValue MaximumGenerations {
|
---|
| 119 | get { return MaximumGenerationsParameter.Value; }
|
---|
| 120 | set { MaximumGenerationsParameter.Value = value; }
|
---|
| 121 | }
|
---|
| 122 | private BoolValue PlusSelection {
|
---|
| 123 | get { return PlusSelectionParameter.Value; }
|
---|
| 124 | set { PlusSelectionParameter.Value = value; }
|
---|
| 125 | }
|
---|
| 126 | public IManipulator Mutator {
|
---|
| 127 | get { return MutatorParameter.Value; }
|
---|
| 128 | set { MutatorParameter.Value = value; }
|
---|
| 129 | }
|
---|
| 130 | public ICrossover Recombinator {
|
---|
| 131 | get { return RecombinatorParameter.Value; }
|
---|
| 132 | set { RecombinatorParameter.Value = value; }
|
---|
| 133 | }
|
---|
| 134 | public MultiAnalyzer Analyzer {
|
---|
| 135 | get { return AnalyzerParameter.Value; }
|
---|
| 136 | set { AnalyzerParameter.Value = value; }
|
---|
| 137 | }
|
---|
| 138 | public IStrategyParameterCreator StrategyParameterCreator {
|
---|
| 139 | get { return StrategyParameterCreatorParameter.Value; }
|
---|
| 140 | set { StrategyParameterCreatorParameter.Value = value; }
|
---|
| 141 | }
|
---|
| 142 | public IStrategyParameterCrossover StrategyParameterCrossover {
|
---|
| 143 | get { return StrategyParameterCrossoverParameter.Value; }
|
---|
| 144 | set { StrategyParameterCrossoverParameter.Value = value; }
|
---|
| 145 | }
|
---|
| 146 | public IStrategyParameterManipulator StrategyParameterManipulator {
|
---|
| 147 | get { return StrategyParameterManipulatorParameter.Value; }
|
---|
| 148 | set { StrategyParameterManipulatorParameter.Value = value; }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | private RandomCreator RandomCreator {
|
---|
| 152 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
| 153 | }
|
---|
| 154 | private SolutionsCreator SolutionsCreator {
|
---|
| 155 | get { return (SolutionsCreator)RandomCreator.Successor; }
|
---|
| 156 | }
|
---|
| 157 | private EvolutionStrategyMainLoop MainLoop {
|
---|
| 158 | get { return (EvolutionStrategyMainLoop)((UniformSubScopesProcessor)SolutionsCreator.Successor).Successor; }
|
---|
| 159 | }
|
---|
| 160 | [Storable]
|
---|
| 161 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
| 162 | #endregion
|
---|
| 163 |
|
---|
| 164 | public EvolutionStrategy()
|
---|
| 165 | : base() {
|
---|
| 166 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 167 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
| 168 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "µ (mu) - the size of the population.", new IntValue(20)));
|
---|
| 169 | Parameters.Add(new ValueParameter<IntValue>("ParentsPerChild", "ρ (rho) - how many parents should be recombined.", new IntValue(1)));
|
---|
| 170 | Parameters.Add(new ValueParameter<IntValue>("Children", "λ (lambda) - the size of the offspring population.", new IntValue(100)));
|
---|
| 171 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
|
---|
| 172 | Parameters.Add(new ValueParameter<BoolValue>("PlusSelection", "True for plus selection (elitist population), false for comma selection (non-elitist population).", new BoolValue(true)));
|
---|
| 173 | Parameters.Add(new OptionalConstrainedValueParameter<ICrossover>("Recombinator", "The operator used to cross solutions."));
|
---|
| 174 | Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 175 | Parameters.Add(new OptionalConstrainedValueParameter<IStrategyParameterCreator>("StrategyParameterCreator", "The operator that creates the strategy parameters."));
|
---|
| 176 | Parameters.Add(new OptionalConstrainedValueParameter<IStrategyParameterCrossover>("StrategyParameterCrossover", "The operator that recombines the strategy parameters."));
|
---|
| 177 | Parameters.Add(new OptionalConstrainedValueParameter<IStrategyParameterManipulator>("StrategyParameterManipulator", "The operator that manipulates the strategy parameters."));
|
---|
| 178 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
|
---|
| 179 |
|
---|
| 180 | RandomCreator randomCreator = new RandomCreator();
|
---|
| 181 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
| 182 | UniformSubScopesProcessor strategyVectorProcessor = new UniformSubScopesProcessor();
|
---|
| 183 | Placeholder strategyVectorCreator = new Placeholder();
|
---|
| 184 | EvolutionStrategyMainLoop mainLoop = new EvolutionStrategyMainLoop();
|
---|
| 185 | OperatorGraph.InitialOperator = randomCreator;
|
---|
| 186 |
|
---|
| 187 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
| 188 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
| 189 | randomCreator.SeedParameter.Value = null;
|
---|
| 190 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
| 191 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
| 192 | randomCreator.Successor = solutionsCreator;
|
---|
| 193 |
|
---|
| 194 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 195 | solutionsCreator.Successor = strategyVectorProcessor;
|
---|
| 196 |
|
---|
| 197 | strategyVectorProcessor.Operator = strategyVectorCreator;
|
---|
| 198 | strategyVectorProcessor.Successor = mainLoop;
|
---|
| 199 |
|
---|
| 200 | strategyVectorCreator.OperatorParameter.ActualName = "StrategyParameterCreator";
|
---|
| 201 |
|
---|
| 202 | mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 203 | mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 204 | mainLoop.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
|
---|
| 205 | mainLoop.ChildrenParameter.ActualName = ChildrenParameter.Name;
|
---|
| 206 | mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
| 207 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
| 208 | mainLoop.RecombinatorParameter.ActualName = RecombinatorParameter.Name;
|
---|
| 209 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 210 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
| 211 |
|
---|
| 212 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
| 213 | ParameterizeAnalyzers();
|
---|
| 214 | UpdateAnalyzers();
|
---|
| 215 |
|
---|
| 216 | Initialize();
|
---|
| 217 | }
|
---|
| 218 | [StorableConstructor]
|
---|
| 219 | private EvolutionStrategy(bool deserializing) : base(deserializing) { }
|
---|
| 220 |
|
---|
| 221 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 222 | EvolutionStrategy clone = (EvolutionStrategy)base.Clone(cloner);
|
---|
| 223 | clone.qualityAnalyzer = (BestAverageWorstQualityAnalyzer)cloner.Clone(qualityAnalyzer);
|
---|
| 224 | clone.Initialize();
|
---|
| 225 | return clone;
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | public override void Prepare() {
|
---|
| 229 | if (Problem != null) base.Prepare();
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | #region Events
|
---|
| 233 | protected override void OnProblemChanged() {
|
---|
| 234 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 235 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 236 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
| 237 | ParameterizeSolutionsCreator();
|
---|
| 238 | ParameterizeMainLoop();
|
---|
| 239 | ParameterizeAnalyzers();
|
---|
| 240 | ParameterizeIterationBasedOperators();
|
---|
| 241 | UpdateRecombinators();
|
---|
| 242 | UpdateMutators();
|
---|
| 243 | UpdateAnalyzers();
|
---|
| 244 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 245 | base.OnProblemChanged();
|
---|
| 246 | }
|
---|
| 247 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
| 248 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 249 | ParameterizeSolutionsCreator();
|
---|
| 250 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
| 251 | }
|
---|
| 252 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
| 253 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 254 | ParameterizeSolutionsCreator();
|
---|
| 255 | ParameterizeMainLoop();
|
---|
| 256 | ParameterizeAnalyzers();
|
---|
| 257 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 258 | base.Problem_EvaluatorChanged(sender, e);
|
---|
| 259 | }
|
---|
| 260 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
| 261 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
| 262 | ParameterizeIterationBasedOperators();
|
---|
| 263 | UpdateRecombinators();
|
---|
| 264 | UpdateMutators();
|
---|
| 265 | UpdateAnalyzers();
|
---|
| 266 | base.Problem_OperatorsChanged(sender, e);
|
---|
| 267 | }
|
---|
| 268 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 269 | ParameterizeMainLoop();
|
---|
| 270 | ParameterizeAnalyzers();
|
---|
| 271 | }
|
---|
| 272 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 273 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 274 | PopulationSize_ValueChanged(null, EventArgs.Empty);
|
---|
| 275 | }
|
---|
| 276 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
| 277 | if (PopulationSize.Value <= 0) PopulationSize.Value = 1;
|
---|
| 278 | if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
|
---|
| 279 | Children.Value = PopulationSize.Value;
|
---|
| 280 | if (PopulationSize.Value < ParentsPerChild.Value)
|
---|
| 281 | ParentsPerChild.Value = PopulationSize.Value;
|
---|
| 282 | }
|
---|
| 283 | private void ParentsPerChildParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 284 | ParentsPerChild.ValueChanged += new EventHandler(ParentsPerChild_ValueChanged);
|
---|
| 285 | ParentsPerChild_ValueChanged(null, EventArgs.Empty);
|
---|
| 286 | }
|
---|
| 287 | private void ParentsPerChild_ValueChanged(object sender, EventArgs e) {
|
---|
| 288 | if (ParentsPerChild.Value < 1 || ParentsPerChild.Value > 1 && RecombinatorParameter.ValidValues.Count == 0)
|
---|
| 289 | ParentsPerChild.Value = 1;
|
---|
| 290 | if (ParentsPerChild.Value > 1 && Recombinator == null) Recombinator = RecombinatorParameter.ValidValues.First();
|
---|
| 291 | if (ParentsPerChild.Value > 1 && ParentsPerChild.Value > PopulationSize.Value)
|
---|
| 292 | PopulationSize.Value = ParentsPerChild.Value;
|
---|
| 293 | }
|
---|
| 294 | private void ChildrenParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 295 | Children.ValueChanged += new EventHandler(Children_ValueChanged);
|
---|
| 296 | Children_ValueChanged(null, EventArgs.Empty);
|
---|
| 297 | }
|
---|
| 298 | private void Children_ValueChanged(object sender, EventArgs e) {
|
---|
| 299 | if (Children.Value <= 0) Children.Value = 1;
|
---|
| 300 | if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
|
---|
| 301 | PopulationSize.Value = Children.Value;
|
---|
| 302 | }
|
---|
| 303 | private void PlusSelectionParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 304 | PlusSelection.ValueChanged += new EventHandler(PlusSelection_ValueChanged);
|
---|
| 305 | PlusSelection_ValueChanged(null, EventArgs.Empty);
|
---|
| 306 | }
|
---|
| 307 | private void PlusSelection_ValueChanged(object sender, EventArgs e) {
|
---|
| 308 | if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
|
---|
| 309 | Children.Value = PopulationSize.Value;
|
---|
| 310 | }
|
---|
| 311 | private void RecombinatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 312 | if (Recombinator == null && ParentsPerChild.Value > 1) ParentsPerChild.Value = 1;
|
---|
| 313 | else if (Recombinator != null && ParentsPerChild.Value == 1) ParentsPerChild.Value = 2;
|
---|
| 314 | if (Recombinator != null && Mutator is ISelfAdaptiveManipulator && StrategyParameterCrossover == null) {
|
---|
| 315 | if (StrategyParameterCrossoverParameter.ValidValues.Count > 0)
|
---|
| 316 | StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
|
---|
| 317 | }
|
---|
| 318 | }
|
---|
| 319 | private void MutatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 320 | if (Mutator is ISelfAdaptiveManipulator) {
|
---|
| 321 | UpdateStrategyParameterOperators();
|
---|
| 322 | } else {
|
---|
| 323 | StrategyParameterCreatorParameter.ValidValues.Clear();
|
---|
| 324 | StrategyParameterCrossoverParameter.ValidValues.Clear();
|
---|
| 325 | StrategyParameterManipulatorParameter.ValidValues.Clear();
|
---|
| 326 | UpdateRecombinators();
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
| 329 | private void StrategyParameterCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 330 | if (Mutator is ISelfAdaptiveManipulator && StrategyParameterCreator == null && StrategyParameterCreatorParameter.ValidValues.Count > 0)
|
---|
| 331 | StrategyParameterCreator = StrategyParameterCreatorParameter.ValidValues.First();
|
---|
| 332 | }
|
---|
| 333 | private void StrategyParameterCrossoverParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 334 | if (Mutator is ISelfAdaptiveManipulator && Recombinator != null && StrategyParameterCrossover == null && StrategyParameterCrossoverParameter.ValidValues.Count > 0)
|
---|
| 335 | StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
|
---|
| 336 | }
|
---|
| 337 | #endregion
|
---|
| 338 |
|
---|
| 339 | #region Helpers
|
---|
| 340 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 341 | private void Initialize() {
|
---|
| 342 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
| 343 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 344 | ParentsPerChildParameter.ValueChanged += new EventHandler(ParentsPerChildParameter_ValueChanged);
|
---|
| 345 | ParentsPerChild.ValueChanged += new EventHandler(ParentsPerChild_ValueChanged);
|
---|
| 346 | ChildrenParameter.ValueChanged += new EventHandler(ChildrenParameter_ValueChanged);
|
---|
| 347 | Children.ValueChanged += new EventHandler(Children_ValueChanged);
|
---|
| 348 | PlusSelectionParameter.ValueChanged += new EventHandler(PlusSelectionParameter_ValueChanged);
|
---|
| 349 | PlusSelection.ValueChanged += new EventHandler(PlusSelection_ValueChanged);
|
---|
| 350 | RecombinatorParameter.ValueChanged += new EventHandler(RecombinatorParameter_ValueChanged);
|
---|
| 351 | MutatorParameter.ValueChanged += new EventHandler(MutatorParameter_ValueChanged);
|
---|
| 352 | StrategyParameterCrossoverParameter.ValueChanged += new EventHandler(StrategyParameterCrossoverParameter_ValueChanged);
|
---|
| 353 | StrategyParameterCreatorParameter.ValueChanged += new EventHandler(StrategyParameterCreatorParameter_ValueChanged);
|
---|
| 354 | if (Problem != null)
|
---|
| 355 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 356 | }
|
---|
| 357 | private void ParameterizeSolutionsCreator() {
|
---|
| 358 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 359 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
| 360 | }
|
---|
| 361 | private void ParameterizeMainLoop() {
|
---|
| 362 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 363 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 364 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 365 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 366 | }
|
---|
| 367 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
| 368 | if (op is IStochasticOperator)
|
---|
| 369 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 370 | }
|
---|
| 371 | private void ParameterizeAnalyzers() {
|
---|
| 372 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 373 | if (Problem != null) {
|
---|
| 374 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 375 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 376 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 377 | }
|
---|
| 378 | }
|
---|
| 379 | private void ParameterizeIterationBasedOperators() {
|
---|
| 380 | if (Problem != null) {
|
---|
| 381 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
| 382 | op.IterationsParameter.ActualName = "Generations";
|
---|
| 383 | op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
|
---|
| 384 | }
|
---|
| 385 | }
|
---|
| 386 | }
|
---|
| 387 | private void UpdateStrategyParameterOperators() {
|
---|
| 388 | IStrategyParameterCreator oldStrategyCreator = StrategyParameterCreator;
|
---|
| 389 | IStrategyParameterCrossover oldStrategyCrossover = StrategyParameterCrossover;
|
---|
| 390 | IStrategyParameterManipulator oldStrategyManipulator = StrategyParameterManipulator;
|
---|
| 391 | ClearStrategyParameterOperators();
|
---|
| 392 | ISelfAdaptiveManipulator manipulator = (Mutator as ISelfAdaptiveManipulator);
|
---|
| 393 | if (manipulator != null) {
|
---|
| 394 | var operators = Problem.Operators.Where(x => manipulator.StrategyParameterType.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
|
---|
| 395 | foreach (IStrategyParameterCreator strategyCreator in operators.OfType<IStrategyParameterCreator>())
|
---|
| 396 | StrategyParameterCreatorParameter.ValidValues.Add(strategyCreator);
|
---|
| 397 | foreach (IStrategyParameterCrossover strategyRecombinator in operators.OfType<IStrategyParameterCrossover>())
|
---|
| 398 | StrategyParameterCrossoverParameter.ValidValues.Add(strategyRecombinator);
|
---|
| 399 | foreach (IStrategyParameterManipulator strategyManipulator in operators.OfType<IStrategyParameterManipulator>())
|
---|
| 400 | StrategyParameterManipulatorParameter.ValidValues.Add(strategyManipulator);
|
---|
| 401 |
|
---|
| 402 | if (StrategyParameterCrossoverParameter.ValidValues.Count == 0)
|
---|
| 403 | RecombinatorParameter.ValidValues.Clear(); // if there is no strategy parameter crossover, there can be no crossover when the mutation operator needs strategy parameters
|
---|
| 404 |
|
---|
| 405 | if (oldStrategyCreator != null) {
|
---|
| 406 | IStrategyParameterCreator tmp1 = StrategyParameterCreatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyCreator.GetType());
|
---|
| 407 | if (tmp1 != null) StrategyParameterCreator = tmp1;
|
---|
| 408 | } else if (StrategyParameterCreatorParameter.ValidValues.Count > 0) StrategyParameterCreator = StrategyParameterCreatorParameter.ValidValues.First();
|
---|
| 409 | if (oldStrategyCrossover != null) {
|
---|
| 410 | IStrategyParameterCrossover tmp2 = StrategyParameterCrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyCrossover.GetType());
|
---|
| 411 | if (tmp2 != null) StrategyParameterCrossover = tmp2;
|
---|
| 412 | } else if (StrategyParameterCrossoverParameter.ValidValues.Count > 0) StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
|
---|
| 413 | if (oldStrategyManipulator != null) {
|
---|
| 414 | IStrategyParameterManipulator tmp3 = StrategyParameterManipulatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyManipulator.GetType());
|
---|
| 415 | if (tmp3 != null) StrategyParameterManipulator = tmp3;
|
---|
| 416 | } else if (StrategyParameterManipulatorParameter.ValidValues.Count > 0) StrategyParameterManipulator = StrategyParameterManipulatorParameter.ValidValues.First();
|
---|
| 417 | }
|
---|
| 418 | }
|
---|
| 419 | private void ClearStrategyParameterOperators() {
|
---|
| 420 | StrategyParameterCreatorParameter.ValidValues.Clear();
|
---|
| 421 | StrategyParameterCrossoverParameter.ValidValues.Clear();
|
---|
| 422 | StrategyParameterManipulatorParameter.ValidValues.Clear();
|
---|
| 423 | }
|
---|
| 424 | private void UpdateRecombinators() {
|
---|
| 425 | ICrossover oldRecombinator = Recombinator;
|
---|
| 426 | RecombinatorParameter.ValidValues.Clear();
|
---|
| 427 | foreach (ICrossover recombinator in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
|
---|
| 428 | RecombinatorParameter.ValidValues.Add(recombinator);
|
---|
| 429 | }
|
---|
| 430 | if (oldRecombinator != null) {
|
---|
| 431 | ICrossover recombinator = RecombinatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldRecombinator.GetType());
|
---|
| 432 | if (recombinator != null) RecombinatorParameter.Value = recombinator;
|
---|
| 433 | }
|
---|
| 434 | }
|
---|
| 435 | private void UpdateMutators() {
|
---|
| 436 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
| 437 | MutatorParameter.ValidValues.Clear();
|
---|
| 438 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
| 439 | MutatorParameter.ValidValues.Add(mutator);
|
---|
| 440 | if (oldMutator != null) {
|
---|
| 441 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
| 442 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
| 443 | } else if (MutatorParameter.ValidValues.Count > 0 && Problem.Operators.OfType<ISelfAdaptiveManipulator>().Count() > 0) {
|
---|
| 444 | ISelfAdaptiveManipulator mutator = Problem.Operators.OfType<ISelfAdaptiveManipulator>().First();
|
---|
| 445 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
| 446 | }
|
---|
| 447 | }
|
---|
| 448 | private void UpdateAnalyzers() {
|
---|
| 449 | Analyzer.Operators.Clear();
|
---|
| 450 | if (Problem != null) {
|
---|
| 451 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>())
|
---|
| 452 | Analyzer.Operators.Add(analyzer);
|
---|
| 453 | }
|
---|
| 454 | Analyzer.Operators.Add(qualityAnalyzer);
|
---|
| 455 | }
|
---|
| 456 | #endregion
|
---|
| 457 | }
|
---|
| 458 | }
|
---|