| 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 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 221 | private void AfterDeserialization() {
|
---|
| 222 | Initialize();
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | private EvolutionStrategy(EvolutionStrategy original, Cloner cloner)
|
---|
| 226 | : base(original, cloner) {
|
---|
| 227 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
| 228 | Initialize();
|
---|
| 229 | }
|
---|
| 230 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 231 | return new EvolutionStrategy(this, cloner);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | public override void Prepare() {
|
---|
| 235 | if (Problem != null) base.Prepare();
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | #region Events
|
---|
| 239 | protected override void OnProblemChanged() {
|
---|
| 240 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 241 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 242 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
| 243 | ParameterizeSolutionsCreator();
|
---|
| 244 | ParameterizeMainLoop();
|
---|
| 245 | ParameterizeAnalyzers();
|
---|
| 246 | ParameterizeIterationBasedOperators();
|
---|
| 247 | UpdateRecombinators();
|
---|
| 248 | UpdateMutators();
|
---|
| 249 | UpdateAnalyzers();
|
---|
| 250 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 251 | base.OnProblemChanged();
|
---|
| 252 | }
|
---|
| 253 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
| 254 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 255 | ParameterizeSolutionsCreator();
|
---|
| 256 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
| 257 | }
|
---|
| 258 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
| 259 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 260 | ParameterizeSolutionsCreator();
|
---|
| 261 | ParameterizeMainLoop();
|
---|
| 262 | ParameterizeAnalyzers();
|
---|
| 263 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 264 | base.Problem_EvaluatorChanged(sender, e);
|
---|
| 265 | }
|
---|
| 266 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
| 267 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
| 268 | ParameterizeIterationBasedOperators();
|
---|
| 269 | UpdateRecombinators();
|
---|
| 270 | UpdateMutators();
|
---|
| 271 | UpdateAnalyzers();
|
---|
| 272 | base.Problem_OperatorsChanged(sender, e);
|
---|
| 273 | }
|
---|
| 274 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 275 | ParameterizeMainLoop();
|
---|
| 276 | ParameterizeAnalyzers();
|
---|
| 277 | }
|
---|
| 278 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 279 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 280 | PopulationSize_ValueChanged(null, EventArgs.Empty);
|
---|
| 281 | }
|
---|
| 282 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
| 283 | if (PopulationSize.Value <= 0) PopulationSize.Value = 1;
|
---|
| 284 | if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
|
---|
| 285 | Children.Value = PopulationSize.Value;
|
---|
| 286 | if (PopulationSize.Value < ParentsPerChild.Value)
|
---|
| 287 | ParentsPerChild.Value = PopulationSize.Value;
|
---|
| 288 | }
|
---|
| 289 | private void ParentsPerChildParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 290 | ParentsPerChild.ValueChanged += new EventHandler(ParentsPerChild_ValueChanged);
|
---|
| 291 | ParentsPerChild_ValueChanged(null, EventArgs.Empty);
|
---|
| 292 | }
|
---|
| 293 | private void ParentsPerChild_ValueChanged(object sender, EventArgs e) {
|
---|
| 294 | if (ParentsPerChild.Value < 1 || ParentsPerChild.Value > 1 && RecombinatorParameter.ValidValues.Count == 0)
|
---|
| 295 | ParentsPerChild.Value = 1;
|
---|
| 296 | if (ParentsPerChild.Value > 1 && Recombinator == null) Recombinator = RecombinatorParameter.ValidValues.First();
|
---|
| 297 | if (ParentsPerChild.Value > 1 && ParentsPerChild.Value > PopulationSize.Value)
|
---|
| 298 | PopulationSize.Value = ParentsPerChild.Value;
|
---|
| 299 | }
|
---|
| 300 | private void ChildrenParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 301 | Children.ValueChanged += new EventHandler(Children_ValueChanged);
|
---|
| 302 | Children_ValueChanged(null, EventArgs.Empty);
|
---|
| 303 | }
|
---|
| 304 | private void Children_ValueChanged(object sender, EventArgs e) {
|
---|
| 305 | if (Children.Value <= 0) Children.Value = 1;
|
---|
| 306 | if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
|
---|
| 307 | PopulationSize.Value = Children.Value;
|
---|
| 308 | }
|
---|
| 309 | private void PlusSelectionParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 310 | PlusSelection.ValueChanged += new EventHandler(PlusSelection_ValueChanged);
|
---|
| 311 | PlusSelection_ValueChanged(null, EventArgs.Empty);
|
---|
| 312 | }
|
---|
| 313 | private void PlusSelection_ValueChanged(object sender, EventArgs e) {
|
---|
| 314 | if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
|
---|
| 315 | Children.Value = PopulationSize.Value;
|
---|
| 316 | }
|
---|
| 317 | private void RecombinatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 318 | if (Recombinator == null && ParentsPerChild.Value > 1) ParentsPerChild.Value = 1;
|
---|
| 319 | else if (Recombinator != null && ParentsPerChild.Value == 1) ParentsPerChild.Value = 2;
|
---|
| 320 | if (Recombinator != null && Mutator is ISelfAdaptiveManipulator && StrategyParameterCrossover == null) {
|
---|
| 321 | if (StrategyParameterCrossoverParameter.ValidValues.Count > 0)
|
---|
| 322 | StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
| 325 | private void MutatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 326 | if (Mutator is ISelfAdaptiveManipulator) {
|
---|
| 327 | UpdateStrategyParameterOperators();
|
---|
| 328 | } else {
|
---|
| 329 | StrategyParameterCreatorParameter.ValidValues.Clear();
|
---|
| 330 | StrategyParameterCrossoverParameter.ValidValues.Clear();
|
---|
| 331 | StrategyParameterManipulatorParameter.ValidValues.Clear();
|
---|
| 332 | UpdateRecombinators();
|
---|
| 333 | }
|
---|
| 334 | }
|
---|
| 335 | private void StrategyParameterCreatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 336 | if (Mutator is ISelfAdaptiveManipulator && StrategyParameterCreator == null && StrategyParameterCreatorParameter.ValidValues.Count > 0)
|
---|
| 337 | StrategyParameterCreator = StrategyParameterCreatorParameter.ValidValues.First();
|
---|
| 338 | }
|
---|
| 339 | private void StrategyParameterCrossoverParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 340 | if (Mutator is ISelfAdaptiveManipulator && Recombinator != null && StrategyParameterCrossover == null && StrategyParameterCrossoverParameter.ValidValues.Count > 0)
|
---|
| 341 | StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
|
---|
| 342 | }
|
---|
| 343 | #endregion
|
---|
| 344 |
|
---|
| 345 | #region Helpers
|
---|
| 346 | private void Initialize() {
|
---|
| 347 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
| 348 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 349 | ParentsPerChildParameter.ValueChanged += new EventHandler(ParentsPerChildParameter_ValueChanged);
|
---|
| 350 | ParentsPerChild.ValueChanged += new EventHandler(ParentsPerChild_ValueChanged);
|
---|
| 351 | ChildrenParameter.ValueChanged += new EventHandler(ChildrenParameter_ValueChanged);
|
---|
| 352 | Children.ValueChanged += new EventHandler(Children_ValueChanged);
|
---|
| 353 | PlusSelectionParameter.ValueChanged += new EventHandler(PlusSelectionParameter_ValueChanged);
|
---|
| 354 | PlusSelection.ValueChanged += new EventHandler(PlusSelection_ValueChanged);
|
---|
| 355 | RecombinatorParameter.ValueChanged += new EventHandler(RecombinatorParameter_ValueChanged);
|
---|
| 356 | MutatorParameter.ValueChanged += new EventHandler(MutatorParameter_ValueChanged);
|
---|
| 357 | StrategyParameterCrossoverParameter.ValueChanged += new EventHandler(StrategyParameterCrossoverParameter_ValueChanged);
|
---|
| 358 | StrategyParameterCreatorParameter.ValueChanged += new EventHandler(StrategyParameterCreatorParameter_ValueChanged);
|
---|
| 359 | if (Problem != null)
|
---|
| 360 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 361 | }
|
---|
| 362 | private void ParameterizeSolutionsCreator() {
|
---|
| 363 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 364 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
| 365 | }
|
---|
| 366 | private void ParameterizeMainLoop() {
|
---|
| 367 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 368 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 369 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 370 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 371 | }
|
---|
| 372 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
| 373 | if (op is IStochasticOperator)
|
---|
| 374 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 375 | }
|
---|
| 376 | private void ParameterizeAnalyzers() {
|
---|
| 377 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 378 | if (Problem != null) {
|
---|
| 379 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 380 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 381 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 382 | }
|
---|
| 383 | }
|
---|
| 384 | private void ParameterizeIterationBasedOperators() {
|
---|
| 385 | if (Problem != null) {
|
---|
| 386 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
| 387 | op.IterationsParameter.ActualName = "Generations";
|
---|
| 388 | op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
|
---|
| 389 | }
|
---|
| 390 | }
|
---|
| 391 | }
|
---|
| 392 | private void UpdateStrategyParameterOperators() {
|
---|
| 393 | IStrategyParameterCreator oldStrategyCreator = StrategyParameterCreator;
|
---|
| 394 | IStrategyParameterCrossover oldStrategyCrossover = StrategyParameterCrossover;
|
---|
| 395 | IStrategyParameterManipulator oldStrategyManipulator = StrategyParameterManipulator;
|
---|
| 396 | ClearStrategyParameterOperators();
|
---|
| 397 | ISelfAdaptiveManipulator manipulator = (Mutator as ISelfAdaptiveManipulator);
|
---|
| 398 | if (manipulator != null) {
|
---|
| 399 | var operators = Problem.Operators.Where(x => manipulator.StrategyParameterType.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
|
---|
| 400 | foreach (IStrategyParameterCreator strategyCreator in operators.OfType<IStrategyParameterCreator>())
|
---|
| 401 | StrategyParameterCreatorParameter.ValidValues.Add(strategyCreator);
|
---|
| 402 | foreach (IStrategyParameterCrossover strategyRecombinator in operators.OfType<IStrategyParameterCrossover>())
|
---|
| 403 | StrategyParameterCrossoverParameter.ValidValues.Add(strategyRecombinator);
|
---|
| 404 | foreach (IStrategyParameterManipulator strategyManipulator in operators.OfType<IStrategyParameterManipulator>())
|
---|
| 405 | StrategyParameterManipulatorParameter.ValidValues.Add(strategyManipulator);
|
---|
| 406 |
|
---|
| 407 | if (StrategyParameterCrossoverParameter.ValidValues.Count == 0)
|
---|
| 408 | RecombinatorParameter.ValidValues.Clear(); // if there is no strategy parameter crossover, there can be no crossover when the mutation operator needs strategy parameters
|
---|
| 409 |
|
---|
| 410 | if (oldStrategyCreator != null) {
|
---|
| 411 | IStrategyParameterCreator tmp1 = StrategyParameterCreatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyCreator.GetType());
|
---|
| 412 | if (tmp1 != null) StrategyParameterCreator = tmp1;
|
---|
| 413 | } else if (StrategyParameterCreatorParameter.ValidValues.Count > 0) StrategyParameterCreator = StrategyParameterCreatorParameter.ValidValues.First();
|
---|
| 414 | if (oldStrategyCrossover != null) {
|
---|
| 415 | IStrategyParameterCrossover tmp2 = StrategyParameterCrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyCrossover.GetType());
|
---|
| 416 | if (tmp2 != null) StrategyParameterCrossover = tmp2;
|
---|
| 417 | } else if (StrategyParameterCrossoverParameter.ValidValues.Count > 0) StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
|
---|
| 418 | if (oldStrategyManipulator != null) {
|
---|
| 419 | IStrategyParameterManipulator tmp3 = StrategyParameterManipulatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyManipulator.GetType());
|
---|
| 420 | if (tmp3 != null) StrategyParameterManipulator = tmp3;
|
---|
| 421 | } else if (StrategyParameterManipulatorParameter.ValidValues.Count > 0) StrategyParameterManipulator = StrategyParameterManipulatorParameter.ValidValues.First();
|
---|
| 422 | }
|
---|
| 423 | }
|
---|
| 424 | private void ClearStrategyParameterOperators() {
|
---|
| 425 | StrategyParameterCreatorParameter.ValidValues.Clear();
|
---|
| 426 | StrategyParameterCrossoverParameter.ValidValues.Clear();
|
---|
| 427 | StrategyParameterManipulatorParameter.ValidValues.Clear();
|
---|
| 428 | }
|
---|
| 429 | private void UpdateRecombinators() {
|
---|
| 430 | ICrossover oldRecombinator = Recombinator;
|
---|
| 431 | RecombinatorParameter.ValidValues.Clear();
|
---|
| 432 | foreach (ICrossover recombinator in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
|
---|
| 433 | RecombinatorParameter.ValidValues.Add(recombinator);
|
---|
| 434 | }
|
---|
| 435 | if (oldRecombinator != null) {
|
---|
| 436 | ICrossover recombinator = RecombinatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldRecombinator.GetType());
|
---|
| 437 | if (recombinator != null) RecombinatorParameter.Value = recombinator;
|
---|
| 438 | }
|
---|
| 439 | }
|
---|
| 440 | private void UpdateMutators() {
|
---|
| 441 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
| 442 | MutatorParameter.ValidValues.Clear();
|
---|
| 443 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
| 444 | MutatorParameter.ValidValues.Add(mutator);
|
---|
| 445 | if (oldMutator != null) {
|
---|
| 446 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
| 447 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
| 448 | } else if (MutatorParameter.ValidValues.Count > 0 && Problem.Operators.OfType<ISelfAdaptiveManipulator>().Count() > 0) {
|
---|
| 449 | ISelfAdaptiveManipulator mutator = Problem.Operators.OfType<ISelfAdaptiveManipulator>().First();
|
---|
| 450 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
| 451 | }
|
---|
| 452 | }
|
---|
| 453 | private void UpdateAnalyzers() {
|
---|
| 454 | Analyzer.Operators.Clear();
|
---|
| 455 | if (Problem != null) {
|
---|
| 456 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>())
|
---|
| 457 | Analyzer.Operators.Add(analyzer);
|
---|
| 458 | }
|
---|
| 459 | Analyzer.Operators.Add(qualityAnalyzer);
|
---|
| 460 | }
|
---|
| 461 | #endregion
|
---|
| 462 | }
|
---|
| 463 | }
|
---|