| 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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 FindMainLoop(SolutionsCreator.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 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
| 183 | UniformSubScopesProcessor strategyVectorProcessor = new UniformSubScopesProcessor();
|
---|
| 184 | Placeholder strategyVectorCreator = new Placeholder();
|
---|
| 185 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
| 186 | EvolutionStrategyMainLoop mainLoop = new EvolutionStrategyMainLoop();
|
---|
| 187 | OperatorGraph.InitialOperator = randomCreator;
|
---|
| 188 |
|
---|
| 189 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
| 190 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
| 191 | randomCreator.SeedParameter.Value = null;
|
---|
| 192 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
| 193 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
| 194 | randomCreator.Successor = solutionsCreator;
|
---|
| 195 |
|
---|
| 196 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 197 | solutionsCreator.Successor = subScopesCounter;
|
---|
| 198 |
|
---|
| 199 | subScopesCounter.Name = "Initialize EvaluatedSolutions";
|
---|
| 200 | subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
| 201 | subScopesCounter.Successor = strategyVectorProcessor;
|
---|
| 202 |
|
---|
| 203 | strategyVectorProcessor.Operator = strategyVectorCreator;
|
---|
| 204 | strategyVectorProcessor.Successor = resultsCollector;
|
---|
| 205 |
|
---|
| 206 | strategyVectorCreator.OperatorParameter.ActualName = "StrategyParameterCreator";
|
---|
| 207 |
|
---|
| 208 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
| 209 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
| 210 | resultsCollector.Successor = mainLoop;
|
---|
| 211 |
|
---|
| 212 | mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 213 | mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 214 | mainLoop.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
|
---|
| 215 | mainLoop.ChildrenParameter.ActualName = ChildrenParameter.Name;
|
---|
| 216 | mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
| 217 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
| 218 | mainLoop.RecombinatorParameter.ActualName = RecombinatorParameter.Name;
|
---|
| 219 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 220 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
| 221 | mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
| 222 |
|
---|
| 223 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
| 224 | ParameterizeAnalyzers();
|
---|
| 225 | UpdateAnalyzers();
|
---|
| 226 |
|
---|
| 227 | Initialize();
|
---|
| 228 | }
|
---|
| 229 | [StorableConstructor]
|
---|
| 230 | private EvolutionStrategy(bool deserializing) : base(deserializing) { }
|
---|
| 231 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 232 | private void AfterDeserialization() {
|
---|
| 233 | Initialize();
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | private EvolutionStrategy(EvolutionStrategy original, Cloner cloner)
|
---|
| 237 | : base(original, cloner) {
|
---|
| 238 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
| 239 | Initialize();
|
---|
| 240 | }
|
---|
| 241 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 242 | return new EvolutionStrategy(this, cloner);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | public override void Prepare() {
|
---|
| 246 | if (Problem != null) base.Prepare();
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | #region Events
|
---|
| 250 | protected override void OnProblemChanged() {
|
---|
| 251 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 252 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 253 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
| 254 | ParameterizeSolutionsCreator();
|
---|
| 255 | ParameterizeMainLoop();
|
---|
| 256 | ParameterizeAnalyzers();
|
---|
| 257 | ParameterizeIterationBasedOperators();
|
---|
| 258 | UpdateRecombinators();
|
---|
| 259 | UpdateMutators();
|
---|
| 260 | UpdateAnalyzers();
|
---|
| 261 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 262 | base.OnProblemChanged();
|
---|
| 263 | }
|
---|
| 264 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
| 265 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 266 | ParameterizeSolutionsCreator();
|
---|
| 267 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
| 268 | }
|
---|
| 269 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
| 270 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 271 | ParameterizeSolutionsCreator();
|
---|
| 272 | ParameterizeMainLoop();
|
---|
| 273 | ParameterizeAnalyzers();
|
---|
| 274 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 275 | base.Problem_EvaluatorChanged(sender, e);
|
---|
| 276 | }
|
---|
| 277 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
| 278 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
| 279 | ParameterizeIterationBasedOperators();
|
---|
| 280 | UpdateRecombinators();
|
---|
| 281 | UpdateMutators();
|
---|
| 282 | UpdateAnalyzers();
|
---|
| 283 | base.Problem_OperatorsChanged(sender, e);
|
---|
| 284 | }
|
---|
| 285 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 286 | ParameterizeMainLoop();
|
---|
| 287 | ParameterizeAnalyzers();
|
---|
| 288 | }
|
---|
| 289 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 290 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 291 | PopulationSize_ValueChanged(null, EventArgs.Empty);
|
---|
| 292 | }
|
---|
| 293 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
| 294 | if (PopulationSize.Value <= 0) PopulationSize.Value = 1;
|
---|
| 295 | if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
|
---|
| 296 | Children.Value = PopulationSize.Value;
|
---|
| 297 | if (PopulationSize.Value < ParentsPerChild.Value)
|
---|
| 298 | ParentsPerChild.Value = PopulationSize.Value;
|
---|
| 299 | }
|
---|
| 300 | private void ParentsPerChildParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 301 | ParentsPerChild.ValueChanged += new EventHandler(ParentsPerChild_ValueChanged);
|
---|
| 302 | ParentsPerChild_ValueChanged(null, EventArgs.Empty);
|
---|
| 303 | }
|
---|
| 304 | private void ParentsPerChild_ValueChanged(object sender, EventArgs e) {
|
---|
| 305 | if (ParentsPerChild.Value < 1 || ParentsPerChild.Value > 1 && RecombinatorParameter.ValidValues.Count == 0)
|
---|
| 306 | ParentsPerChild.Value = 1;
|
---|
| 307 | if (ParentsPerChild.Value > 1 && Recombinator == null) Recombinator = RecombinatorParameter.ValidValues.First();
|
---|
| 308 | if (ParentsPerChild.Value > 1 && ParentsPerChild.Value > PopulationSize.Value)
|
---|
| 309 | PopulationSize.Value = ParentsPerChild.Value;
|
---|
| 310 | }
|
---|
| 311 | private void ChildrenParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 312 | Children.ValueChanged += new EventHandler(Children_ValueChanged);
|
---|
| 313 | Children_ValueChanged(null, EventArgs.Empty);
|
---|
| 314 | }
|
---|
| 315 | private void Children_ValueChanged(object sender, EventArgs e) {
|
---|
| 316 | if (Children.Value <= 0) Children.Value = 1;
|
---|
| 317 | if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
|
---|
| 318 | PopulationSize.Value = Children.Value;
|
---|
| 319 | }
|
---|
| 320 | private void PlusSelectionParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 321 | PlusSelection.ValueChanged += new EventHandler(PlusSelection_ValueChanged);
|
---|
| 322 | PlusSelection_ValueChanged(null, EventArgs.Empty);
|
---|
| 323 | }
|
---|
| 324 | private void PlusSelection_ValueChanged(object sender, EventArgs e) {
|
---|
| 325 | if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
|
---|
| 326 | Children.Value = PopulationSize.Value;
|
---|
| 327 | }
|
---|
| 328 | private void RecombinatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 329 | if (Recombinator == null && ParentsPerChild.Value > 1) ParentsPerChild.Value = 1;
|
---|
| 330 | else if (Recombinator != null && ParentsPerChild.Value == 1) ParentsPerChild.Value = 2;
|
---|
| 331 | if (Recombinator != null && Mutator is ISelfAdaptiveManipulator && StrategyParameterCrossover == null) {
|
---|
| 332 | if (StrategyParameterCrossoverParameter.ValidValues.Count > 0)
|
---|
| 333 | StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
|
---|
| 334 | }
|
---|
| 335 | }
|
---|
| 336 | private void MutatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 337 | if (Mutator is ISelfAdaptiveManipulator) {
|
---|
| 338 | UpdateStrategyParameterOperators();
|
---|
| 339 | } else {
|
---|
| 340 | StrategyParameterCreatorParameter.ValidValues.Clear();
|
---|
| 341 | StrategyParameterCrossoverParameter.ValidValues.Clear();
|
---|
| 342 | StrategyParameterManipulatorParameter.ValidValues.Clear();
|
---|
| 343 | UpdateRecombinators();
|
---|
| 344 | }
|
---|
| 345 | }
|
---|
| 346 | private void StrategyParameterCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 347 | if (Mutator is ISelfAdaptiveManipulator && StrategyParameterCreator == null && StrategyParameterCreatorParameter.ValidValues.Count > 0)
|
---|
| 348 | StrategyParameterCreator = StrategyParameterCreatorParameter.ValidValues.First();
|
---|
| 349 | }
|
---|
| 350 | private void StrategyParameterCrossoverParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 351 | if (Mutator is ISelfAdaptiveManipulator && Recombinator != null && StrategyParameterCrossover == null && StrategyParameterCrossoverParameter.ValidValues.Count > 0)
|
---|
| 352 | StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
|
---|
| 353 | }
|
---|
| 354 | #endregion
|
---|
| 355 |
|
---|
| 356 | #region Helpers
|
---|
| 357 | private void Initialize() {
|
---|
| 358 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
| 359 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 360 | ParentsPerChildParameter.ValueChanged += new EventHandler(ParentsPerChildParameter_ValueChanged);
|
---|
| 361 | ParentsPerChild.ValueChanged += new EventHandler(ParentsPerChild_ValueChanged);
|
---|
| 362 | ChildrenParameter.ValueChanged += new EventHandler(ChildrenParameter_ValueChanged);
|
---|
| 363 | Children.ValueChanged += new EventHandler(Children_ValueChanged);
|
---|
| 364 | PlusSelectionParameter.ValueChanged += new EventHandler(PlusSelectionParameter_ValueChanged);
|
---|
| 365 | PlusSelection.ValueChanged += new EventHandler(PlusSelection_ValueChanged);
|
---|
| 366 | RecombinatorParameter.ValueChanged += new EventHandler(RecombinatorParameter_ValueChanged);
|
---|
| 367 | MutatorParameter.ValueChanged += new EventHandler(MutatorParameter_ValueChanged);
|
---|
| 368 | StrategyParameterCrossoverParameter.ValueChanged += new EventHandler(StrategyParameterCrossoverParameter_ValueChanged);
|
---|
| 369 | StrategyParameterCreatorParameter.ValueChanged += new EventHandler(StrategyParameterCreatorParameter_ValueChanged);
|
---|
| 370 | if (Problem != null)
|
---|
| 371 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 372 | }
|
---|
| 373 | private void ParameterizeSolutionsCreator() {
|
---|
| 374 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 375 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
| 376 | }
|
---|
| 377 | private void ParameterizeMainLoop() {
|
---|
| 378 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 379 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 380 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 381 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 382 | }
|
---|
| 383 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
| 384 | if (op is IStochasticOperator)
|
---|
| 385 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 386 | }
|
---|
| 387 | private void ParameterizeAnalyzers() {
|
---|
| 388 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 389 | if (Problem != null) {
|
---|
| 390 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 391 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 392 | qualityAnalyzer.QualityParameter.Depth = 1;
|
---|
| 393 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 394 | }
|
---|
| 395 | }
|
---|
| 396 | private void ParameterizeIterationBasedOperators() {
|
---|
| 397 | if (Problem != null) {
|
---|
| 398 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
| 399 | op.IterationsParameter.ActualName = "Generations";
|
---|
| 400 | op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
|
---|
| 401 | }
|
---|
| 402 | }
|
---|
| 403 | }
|
---|
| 404 | private void UpdateStrategyParameterOperators() {
|
---|
| 405 | IStrategyParameterCreator oldStrategyCreator = StrategyParameterCreator;
|
---|
| 406 | IStrategyParameterCrossover oldStrategyCrossover = StrategyParameterCrossover;
|
---|
| 407 | IStrategyParameterManipulator oldStrategyManipulator = StrategyParameterManipulator;
|
---|
| 408 | ClearStrategyParameterOperators();
|
---|
| 409 | ISelfAdaptiveManipulator manipulator = (Mutator as ISelfAdaptiveManipulator);
|
---|
| 410 | if (manipulator != null) {
|
---|
| 411 | var operators = Problem.Operators.Where(x => manipulator.StrategyParameterType.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
|
---|
| 412 | foreach (IStrategyParameterCreator strategyCreator in operators.OfType<IStrategyParameterCreator>())
|
---|
| 413 | StrategyParameterCreatorParameter.ValidValues.Add(strategyCreator);
|
---|
| 414 | foreach (IStrategyParameterCrossover strategyRecombinator in operators.OfType<IStrategyParameterCrossover>())
|
---|
| 415 | StrategyParameterCrossoverParameter.ValidValues.Add(strategyRecombinator);
|
---|
| 416 | foreach (IStrategyParameterManipulator strategyManipulator in operators.OfType<IStrategyParameterManipulator>())
|
---|
| 417 | StrategyParameterManipulatorParameter.ValidValues.Add(strategyManipulator);
|
---|
| 418 |
|
---|
| 419 | if (StrategyParameterCrossoverParameter.ValidValues.Count == 0)
|
---|
| 420 | RecombinatorParameter.ValidValues.Clear(); // if there is no strategy parameter crossover, there can be no crossover when the mutation operator needs strategy parameters
|
---|
| 421 |
|
---|
| 422 | if (oldStrategyCreator != null) {
|
---|
| 423 | IStrategyParameterCreator tmp1 = StrategyParameterCreatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyCreator.GetType());
|
---|
| 424 | if (tmp1 != null) StrategyParameterCreator = tmp1;
|
---|
| 425 | } else if (StrategyParameterCreatorParameter.ValidValues.Count > 0) StrategyParameterCreator = StrategyParameterCreatorParameter.ValidValues.First();
|
---|
| 426 | if (oldStrategyCrossover != null) {
|
---|
| 427 | IStrategyParameterCrossover tmp2 = StrategyParameterCrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyCrossover.GetType());
|
---|
| 428 | if (tmp2 != null) StrategyParameterCrossover = tmp2;
|
---|
| 429 | } else if (StrategyParameterCrossoverParameter.ValidValues.Count > 0) StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
|
---|
| 430 | if (oldStrategyManipulator != null) {
|
---|
| 431 | IStrategyParameterManipulator tmp3 = StrategyParameterManipulatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyManipulator.GetType());
|
---|
| 432 | if (tmp3 != null) StrategyParameterManipulator = tmp3;
|
---|
| 433 | } else if (StrategyParameterManipulatorParameter.ValidValues.Count > 0) StrategyParameterManipulator = StrategyParameterManipulatorParameter.ValidValues.First();
|
---|
| 434 | }
|
---|
| 435 | }
|
---|
| 436 | private void ClearStrategyParameterOperators() {
|
---|
| 437 | StrategyParameterCreatorParameter.ValidValues.Clear();
|
---|
| 438 | StrategyParameterCrossoverParameter.ValidValues.Clear();
|
---|
| 439 | StrategyParameterManipulatorParameter.ValidValues.Clear();
|
---|
| 440 | }
|
---|
| 441 | private void UpdateRecombinators() {
|
---|
| 442 | ICrossover oldRecombinator = Recombinator;
|
---|
| 443 | RecombinatorParameter.ValidValues.Clear();
|
---|
| 444 | foreach (ICrossover recombinator in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
|
---|
| 445 | RecombinatorParameter.ValidValues.Add(recombinator);
|
---|
| 446 | }
|
---|
| 447 | if (oldRecombinator != null) {
|
---|
| 448 | ICrossover recombinator = RecombinatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldRecombinator.GetType());
|
---|
| 449 | if (recombinator != null) RecombinatorParameter.Value = recombinator;
|
---|
| 450 | }
|
---|
| 451 | }
|
---|
| 452 | private void UpdateMutators() {
|
---|
| 453 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
| 454 | MutatorParameter.ValidValues.Clear();
|
---|
| 455 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
| 456 | MutatorParameter.ValidValues.Add(mutator);
|
---|
| 457 | if (oldMutator != null) {
|
---|
| 458 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
| 459 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
| 460 | } else if (MutatorParameter.ValidValues.Count > 0 && Problem.Operators.OfType<ISelfAdaptiveManipulator>().Count() > 0) {
|
---|
| 461 | ISelfAdaptiveManipulator mutator = Problem.Operators.OfType<ISelfAdaptiveManipulator>().First();
|
---|
| 462 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
| 463 | }
|
---|
| 464 | }
|
---|
| 465 | private void UpdateAnalyzers() {
|
---|
| 466 | Analyzer.Operators.Clear();
|
---|
| 467 | if (Problem != null) {
|
---|
| 468 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
| 469 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 470 | param.Depth = 1;
|
---|
| 471 | Analyzer.Operators.Add(analyzer);
|
---|
| 472 | }
|
---|
| 473 | }
|
---|
| 474 | Analyzer.Operators.Add(qualityAnalyzer);
|
---|
| 475 | }
|
---|
| 476 | private EvolutionStrategyMainLoop FindMainLoop(IOperator start) {
|
---|
| 477 | IOperator mainLoop = start;
|
---|
| 478 | while (mainLoop != null && !(mainLoop is EvolutionStrategyMainLoop))
|
---|
| 479 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
| 480 | if (mainLoop == null) return null;
|
---|
| 481 | else return (EvolutionStrategyMainLoop)mainLoop;
|
---|
| 482 | }
|
---|
| 483 | #endregion
|
---|
| 484 | }
|
---|
| 485 | }
|
---|