[3356] | 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.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[3376] | 25 | using HeuristicLab.Common;
|
---|
[3356] | 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.PluginInfrastructure;
|
---|
[3368] | 34 | using HeuristicLab.Random;
|
---|
[3356] | 35 | using HeuristicLab.Selection;
|
---|
[3650] | 36 | using HeuristicLab.Analysis;
|
---|
[3356] | 37 |
|
---|
| 38 | namespace HeuristicLab.Algorithms.GeneticAlgorithm {
|
---|
| 39 | /// <summary>
|
---|
| 40 | /// An island genetic algorithm.
|
---|
| 41 | /// </summary>
|
---|
| 42 | [Item("Island Genetic Algorithm", "An island genetic algorithm.")]
|
---|
| 43 | [Creatable("Algorithms")]
|
---|
| 44 | [StorableClass]
|
---|
| 45 | public sealed class IslandGeneticAlgorithm : EngineAlgorithm {
|
---|
| 46 |
|
---|
| 47 | #region Problem Properties
|
---|
| 48 | public override Type ProblemType {
|
---|
| 49 | get { return typeof(ISingleObjectiveProblem); }
|
---|
| 50 | }
|
---|
| 51 | public new ISingleObjectiveProblem Problem {
|
---|
| 52 | get { return (ISingleObjectiveProblem)base.Problem; }
|
---|
| 53 | set { base.Problem = value; }
|
---|
| 54 | }
|
---|
| 55 | #endregion
|
---|
| 56 |
|
---|
| 57 | #region Parameter Properties
|
---|
| 58 | private ValueParameter<IntValue> SeedParameter {
|
---|
| 59 | get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
| 60 | }
|
---|
| 61 | private ValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
| 62 | get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
| 63 | }
|
---|
| 64 | private ValueParameter<IntValue> NumberOfIslandsParameter {
|
---|
| 65 | get { return (ValueParameter<IntValue>)Parameters["NumberOfIslands"]; }
|
---|
| 66 | }
|
---|
| 67 | private ValueParameter<IntValue> MigrationIntervalParameter {
|
---|
| 68 | get { return (ValueParameter<IntValue>)Parameters["MigrationInterval"]; }
|
---|
| 69 | }
|
---|
| 70 | private ValueParameter<PercentValue> MigrationRateParameter {
|
---|
| 71 | get { return (ValueParameter<PercentValue>)Parameters["MigrationRate"]; }
|
---|
| 72 | }
|
---|
| 73 | private ConstrainedValueParameter<IMigrator> MigratorParameter {
|
---|
| 74 | get { return (ConstrainedValueParameter<IMigrator>)Parameters["Migrator"]; }
|
---|
| 75 | }
|
---|
| 76 | private ConstrainedValueParameter<ISelector> EmigrantsSelectorParameter {
|
---|
| 77 | get { return (ConstrainedValueParameter<ISelector>)Parameters["EmigrantsSelector"]; }
|
---|
| 78 | }
|
---|
[3601] | 79 | private ConstrainedValueParameter<IReplacer> ImmigrationReplacerParameter {
|
---|
| 80 | get { return (ConstrainedValueParameter<IReplacer>)Parameters["ImmigrationReplacer"]; }
|
---|
[3356] | 81 | }
|
---|
| 82 | private ValueParameter<IntValue> PopulationSizeParameter {
|
---|
| 83 | get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
| 84 | }
|
---|
[3609] | 85 | private ValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 86 | get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
[3356] | 87 | }
|
---|
| 88 | private ConstrainedValueParameter<ISelector> SelectorParameter {
|
---|
| 89 | get { return (ConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
|
---|
| 90 | }
|
---|
| 91 | private ConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
| 92 | get { return (ConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
| 93 | }
|
---|
| 94 | private ValueParameter<PercentValue> MutationProbabilityParameter {
|
---|
| 95 | get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
| 96 | }
|
---|
| 97 | private OptionalConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
| 98 | get { return (OptionalConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
| 99 | }
|
---|
| 100 | private ValueParameter<IntValue> ElitesParameter {
|
---|
| 101 | get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
|
---|
| 102 | }
|
---|
| 103 | private ValueParameter<BoolValue> ParallelParameter {
|
---|
| 104 | get { return (ValueParameter<BoolValue>)Parameters["Parallel"]; }
|
---|
| 105 | }
|
---|
[3658] | 106 | private ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
| 107 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
[3650] | 108 | }
|
---|
[3658] | 109 | private ValueParameter<MultiAnalyzer> IslandAnalyzerParameter {
|
---|
| 110 | get { return (ValueParameter<MultiAnalyzer>)Parameters["IslandAnalyzer"]; }
|
---|
[3650] | 111 | }
|
---|
[3356] | 112 | #endregion
|
---|
| 113 |
|
---|
| 114 | #region Properties
|
---|
| 115 | public IntValue Seed {
|
---|
| 116 | get { return SeedParameter.Value; }
|
---|
| 117 | set { SeedParameter.Value = value; }
|
---|
| 118 | }
|
---|
| 119 | public BoolValue SetSeedRandomly {
|
---|
| 120 | get { return SetSeedRandomlyParameter.Value; }
|
---|
| 121 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
| 122 | }
|
---|
| 123 | public IntValue NumberOfIslands {
|
---|
| 124 | get { return NumberOfIslandsParameter.Value; }
|
---|
| 125 | set { NumberOfIslandsParameter.Value = value; }
|
---|
| 126 | }
|
---|
| 127 | public IntValue MigrationInterval {
|
---|
| 128 | get { return MigrationIntervalParameter.Value; }
|
---|
| 129 | set { MigrationIntervalParameter.Value = value; }
|
---|
| 130 | }
|
---|
| 131 | public PercentValue MigrationRate {
|
---|
| 132 | get { return MigrationRateParameter.Value; }
|
---|
| 133 | set { MigrationRateParameter.Value = value; }
|
---|
| 134 | }
|
---|
| 135 | public IMigrator Migrator {
|
---|
| 136 | get { return MigratorParameter.Value; }
|
---|
| 137 | set { MigratorParameter.Value = value; }
|
---|
| 138 | }
|
---|
| 139 | public ISelector EmigrantsSelector {
|
---|
| 140 | get { return EmigrantsSelectorParameter.Value; }
|
---|
| 141 | set { EmigrantsSelectorParameter.Value = value; }
|
---|
| 142 | }
|
---|
[3601] | 143 | public IReplacer ImmigrationReplacer {
|
---|
| 144 | get { return ImmigrationReplacerParameter.Value; }
|
---|
| 145 | set { ImmigrationReplacerParameter.Value = value; }
|
---|
[3356] | 146 | }
|
---|
| 147 | public IntValue PopulationSize {
|
---|
| 148 | get { return PopulationSizeParameter.Value; }
|
---|
| 149 | set { PopulationSizeParameter.Value = value; }
|
---|
| 150 | }
|
---|
[3609] | 151 | public IntValue MaximumGenerations {
|
---|
| 152 | get { return MaximumGenerationsParameter.Value; }
|
---|
| 153 | set { MaximumGenerationsParameter.Value = value; }
|
---|
[3356] | 154 | }
|
---|
| 155 | public ISelector Selector {
|
---|
| 156 | get { return SelectorParameter.Value; }
|
---|
| 157 | set { SelectorParameter.Value = value; }
|
---|
| 158 | }
|
---|
| 159 | public ICrossover Crossover {
|
---|
| 160 | get { return CrossoverParameter.Value; }
|
---|
| 161 | set { CrossoverParameter.Value = value; }
|
---|
| 162 | }
|
---|
| 163 | public PercentValue MutationProbability {
|
---|
| 164 | get { return MutationProbabilityParameter.Value; }
|
---|
| 165 | set { MutationProbabilityParameter.Value = value; }
|
---|
| 166 | }
|
---|
| 167 | public IManipulator Mutator {
|
---|
| 168 | get { return MutatorParameter.Value; }
|
---|
| 169 | set { MutatorParameter.Value = value; }
|
---|
| 170 | }
|
---|
| 171 | public IntValue Elites {
|
---|
| 172 | get { return ElitesParameter.Value; }
|
---|
| 173 | set { ElitesParameter.Value = value; }
|
---|
| 174 | }
|
---|
| 175 | public BoolValue Parallel {
|
---|
| 176 | get { return ParallelParameter.Value; }
|
---|
| 177 | set { ParallelParameter.Value = value; }
|
---|
| 178 | }
|
---|
[3658] | 179 | public MultiAnalyzer Analyzer {
|
---|
[3650] | 180 | get { return AnalyzerParameter.Value; }
|
---|
| 181 | set { AnalyzerParameter.Value = value; }
|
---|
| 182 | }
|
---|
[3658] | 183 | public MultiAnalyzer IslandAnalyzer {
|
---|
[3650] | 184 | get { return IslandAnalyzerParameter.Value; }
|
---|
| 185 | set { IslandAnalyzerParameter.Value = value; }
|
---|
| 186 | }
|
---|
[3356] | 187 | private List<ISelector> selectors;
|
---|
| 188 | private IEnumerable<ISelector> Selectors {
|
---|
| 189 | get { return selectors; }
|
---|
| 190 | }
|
---|
| 191 | private List<ISelector> emigrantsSelectors;
|
---|
[3601] | 192 | private List<IReplacer> immigrationReplacers;
|
---|
[3356] | 193 | private List<IMigrator> migrators;
|
---|
[3359] | 194 | private RandomCreator RandomCreator {
|
---|
| 195 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
| 196 | }
|
---|
[3429] | 197 | private UniformSubScopesProcessor IslandProcessor {
|
---|
| 198 | get { return ((RandomCreator.Successor as SubScopesCreator).Successor as UniformSubScopesProcessor); }
|
---|
| 199 | }
|
---|
[3359] | 200 | private SolutionsCreator SolutionsCreator {
|
---|
[3429] | 201 | get { return (SolutionsCreator)IslandProcessor.Operator; }
|
---|
[3359] | 202 | }
|
---|
| 203 | private IslandGeneticAlgorithmMainLoop MainLoop {
|
---|
[3429] | 204 | get { return (IslandGeneticAlgorithmMainLoop)IslandProcessor.Successor; }
|
---|
[3359] | 205 | }
|
---|
[3662] | 206 | private BestAverageWorstQualityAnalyzer islandQualityAnalyzer;
|
---|
[3671] | 207 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
[3356] | 208 | #endregion
|
---|
| 209 |
|
---|
| 210 | [StorableConstructor]
|
---|
| 211 | private IslandGeneticAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
| 212 | public IslandGeneticAlgorithm()
|
---|
| 213 | : base() {
|
---|
| 214 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 215 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
| 216 | Parameters.Add(new ValueParameter<IntValue>("NumberOfIslands", "The number of islands.", new IntValue(5)));
|
---|
| 217 | Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases.", new IntValue(20)));
|
---|
| 218 | Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands.", new PercentValue(0.15)));
|
---|
| 219 | Parameters.Add(new ConstrainedValueParameter<IMigrator>("Migrator", "The migration strategy."));
|
---|
| 220 | Parameters.Add(new ConstrainedValueParameter<ISelector>("EmigrantsSelector", "Selects the individuals that will be migrated."));
|
---|
[3601] | 221 | Parameters.Add(new ConstrainedValueParameter<IReplacer>("ImmigrationReplacer", "Selects the population from the unification of the original population and the immigrants."));
|
---|
[3356] | 222 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
|
---|
[3609] | 223 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
|
---|
[3356] | 224 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
| 225 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
| 226 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
| 227 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 228 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
| 229 | Parameters.Add(new ValueParameter<BoolValue>("Parallel", "True if the islands should be run in parallel (also requires a parallel engine)", new BoolValue(false)));
|
---|
[3658] | 230 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
|
---|
| 231 | Parameters.Add(new ValueParameter<MultiAnalyzer>("IslandAnalyzer", "The operator used to analyze each island.", new MultiAnalyzer()));
|
---|
[3650] | 232 |
|
---|
[3359] | 233 | RandomCreator randomCreator = new RandomCreator();
|
---|
[3356] | 234 | SubScopesCreator populationCreator = new SubScopesCreator();
|
---|
| 235 | UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
|
---|
[3359] | 236 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
| 237 | IslandGeneticAlgorithmMainLoop mainLoop = new IslandGeneticAlgorithmMainLoop();
|
---|
[3356] | 238 | OperatorGraph.InitialOperator = randomCreator;
|
---|
| 239 |
|
---|
| 240 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
| 241 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
| 242 | randomCreator.SeedParameter.Value = null;
|
---|
| 243 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
| 244 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
| 245 | randomCreator.Successor = populationCreator;
|
---|
| 246 |
|
---|
| 247 | populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name;
|
---|
| 248 | populationCreator.Successor = ussp1;
|
---|
| 249 |
|
---|
[3429] | 250 | ussp1.Parallel = null;
|
---|
| 251 | ussp1.ParallelParameter.ActualName = ParallelParameter.Name;
|
---|
[3356] | 252 | ussp1.Operator = solutionsCreator;
|
---|
| 253 | ussp1.Successor = mainLoop;
|
---|
| 254 |
|
---|
| 255 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 256 | solutionsCreator.Successor = null;
|
---|
| 257 |
|
---|
| 258 | mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
|
---|
[3601] | 259 | mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
|
---|
[3609] | 260 | mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
[3356] | 261 | mainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
|
---|
| 262 | mainLoop.MigrationRateParameter.ActualName = MigrationRateParameter.Name;
|
---|
| 263 | mainLoop.MigratorParameter.ActualName = MigratorParameter.Name;
|
---|
| 264 | mainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name;
|
---|
| 265 | mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
| 266 | mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
| 267 | mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
| 268 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
| 269 | mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
| 270 | mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
|
---|
| 271 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
[3650] | 272 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 273 | mainLoop.IslandAnalyzerParameter.ActualName = IslandAnalyzerParameter.Name;
|
---|
[3356] | 274 |
|
---|
| 275 | mainLoop.Successor = null;
|
---|
| 276 |
|
---|
| 277 | Initialize();
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 281 | IslandGeneticAlgorithm clone = (IslandGeneticAlgorithm)base.Clone(cloner);
|
---|
| 282 | clone.Initialize();
|
---|
| 283 | return clone;
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | public override void Prepare() {
|
---|
| 287 | if (Problem != null) base.Prepare();
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | #region Events
|
---|
| 291 | protected override void OnProblemChanged() {
|
---|
| 292 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 293 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 294 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
| 295 | ParameterizeSolutionsCreator();
|
---|
| 296 | ParameterizeMainLoop();
|
---|
| 297 | ParameterizeSelectors();
|
---|
[3650] | 298 | ParameterizeAnalyzers();
|
---|
[3356] | 299 | UpdateCrossovers();
|
---|
| 300 | UpdateMutators();
|
---|
[3650] | 301 | UpdateAnalyzers();
|
---|
[3356] | 302 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 303 | base.OnProblemChanged();
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
| 307 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 308 | ParameterizeSolutionsCreator();
|
---|
| 309 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
| 310 | }
|
---|
| 311 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
| 312 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 313 | ParameterizeSolutionsCreator();
|
---|
| 314 | ParameterizeMainLoop();
|
---|
| 315 | ParameterizeSelectors();
|
---|
[3650] | 316 | ParameterizeAnalyzers();
|
---|
[3356] | 317 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 318 | base.Problem_EvaluatorChanged(sender, e);
|
---|
| 319 | }
|
---|
| 320 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
| 321 | foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
|
---|
| 322 | UpdateCrossovers();
|
---|
| 323 | UpdateMutators();
|
---|
[3650] | 324 | UpdateAnalyzers();
|
---|
[3356] | 325 | base.Problem_OperatorsChanged(sender, e);
|
---|
| 326 | }
|
---|
| 327 | private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 328 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
| 329 | ParameterizeSelectors();
|
---|
| 330 | }
|
---|
| 331 | private void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
| 332 | ParameterizeSelectors();
|
---|
| 333 | }
|
---|
| 334 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 335 | NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 336 | ParameterizeSelectors();
|
---|
| 337 | }
|
---|
| 338 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
| 339 | ParameterizeSelectors();
|
---|
| 340 | }
|
---|
| 341 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 342 | ParameterizeMainLoop();
|
---|
| 343 | ParameterizeSelectors();
|
---|
[3650] | 344 | ParameterizeAnalyzers();
|
---|
[3356] | 345 | }
|
---|
| 346 | private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 347 | MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
|
---|
| 348 | ParameterizeSelectors();
|
---|
| 349 | }
|
---|
| 350 | private void MigrationRate_ValueChanged(object sender, EventArgs e) {
|
---|
| 351 | ParameterizeSelectors();
|
---|
| 352 | }
|
---|
| 353 | #endregion
|
---|
| 354 |
|
---|
| 355 | #region Helpers
|
---|
| 356 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 357 | private void Initialize() {
|
---|
| 358 | InitializeSelectors();
|
---|
[3650] | 359 | InitializeAnalyzers();
|
---|
[3356] | 360 | UpdateSelectors();
|
---|
[3650] | 361 | UpdateAnalyzers();
|
---|
[3356] | 362 | InitializeMigrators();
|
---|
| 363 | UpdateMigrators();
|
---|
| 364 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
| 365 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 366 | MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
|
---|
| 367 | MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
|
---|
| 368 | ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
|
---|
| 369 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
| 370 | if (Problem != null) {
|
---|
| 371 | UpdateCrossovers();
|
---|
| 372 | UpdateMutators();
|
---|
| 373 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 374 | }
|
---|
| 375 | }
|
---|
| 376 | private void ParameterizeSolutionsCreator() {
|
---|
[3359] | 377 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 378 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
[3356] | 379 | }
|
---|
| 380 | private void ParameterizeMainLoop() {
|
---|
[3359] | 381 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 382 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 383 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 384 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[3356] | 385 | }
|
---|
| 386 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
| 387 | if (op is IStochasticOperator)
|
---|
[3359] | 388 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
[3356] | 389 | }
|
---|
| 390 | private void InitializeSelectors() {
|
---|
| 391 | selectors = new List<ISelector>();
|
---|
| 392 | selectors.AddRange(ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name));
|
---|
| 393 | emigrantsSelectors = new List<ISelector>();
|
---|
| 394 | emigrantsSelectors.AddRange(ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name));
|
---|
[3601] | 395 | immigrationReplacers = new List<IReplacer>();
|
---|
| 396 | immigrationReplacers.AddRange(ApplicationManager.Manager.GetInstances<IReplacer>().OrderBy(x => x.Name));
|
---|
[3356] | 397 | ParameterizeSelectors();
|
---|
| 398 | }
|
---|
[3650] | 399 | private void InitializeAnalyzers() {
|
---|
[3662] | 400 | islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
[3671] | 401 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
[3650] | 402 | ParameterizeAnalyzers();
|
---|
| 403 | }
|
---|
[3356] | 404 | private void InitializeMigrators() {
|
---|
| 405 | migrators = new List<IMigrator>();
|
---|
| 406 | migrators.AddRange(ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name));
|
---|
| 407 | UpdateMigrators();
|
---|
| 408 | }
|
---|
| 409 | private void ParameterizeSelectors() {
|
---|
| 410 | foreach (ISelector selector in Selectors) {
|
---|
| 411 | selector.CopySelected = new BoolValue(true);
|
---|
| 412 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
|
---|
| 413 | ParameterizeStochasticOperator(selector);
|
---|
| 414 | }
|
---|
| 415 | foreach (ISelector selector in emigrantsSelectors) {
|
---|
| 416 | selector.CopySelected = new BoolValue(true);
|
---|
| 417 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
|
---|
| 418 | ParameterizeStochasticOperator(selector);
|
---|
| 419 | }
|
---|
[3601] | 420 | foreach (IReplacer replacer in immigrationReplacers) {
|
---|
| 421 | ParameterizeStochasticOperator(replacer);
|
---|
[3356] | 422 | }
|
---|
| 423 | if (Problem != null) {
|
---|
| 424 | foreach (ISingleObjectiveSelector selector in Selectors.OfType<ISingleObjectiveSelector>()) {
|
---|
| 425 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 426 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 427 | }
|
---|
| 428 | foreach (ISingleObjectiveSelector selector in emigrantsSelectors.OfType<ISingleObjectiveSelector>()) {
|
---|
| 429 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 430 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 431 | }
|
---|
[3601] | 432 | foreach (ISingleObjectiveReplacer selector in immigrationReplacers.OfType<ISingleObjectiveReplacer>()) {
|
---|
[3356] | 433 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 434 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 435 | }
|
---|
| 436 | }
|
---|
| 437 | }
|
---|
[3650] | 438 | private void ParameterizeAnalyzers() {
|
---|
| 439 | islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
[3671] | 440 | foreach (IScopeTreeLookupParameter param in ((IAnalyzer)islandQualityAnalyzer).Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 441 | param.Depth = 1;
|
---|
| 442 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 443 | foreach (IScopeTreeLookupParameter param in ((IAnalyzer)qualityAnalyzer).Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 444 | param.Depth = 2;
|
---|
[3650] | 445 | if (Problem != null) {
|
---|
| 446 | islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 447 | islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 448 | islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
[3671] | 449 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 450 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 451 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
[3650] | 452 | }
|
---|
| 453 | }
|
---|
[3356] | 454 | private void UpdateSelectors() {
|
---|
| 455 | ISelector oldSelector = SelectorParameter.Value;
|
---|
| 456 | SelectorParameter.ValidValues.Clear();
|
---|
| 457 | foreach (ISelector selector in Selectors.OrderBy(x => x.Name))
|
---|
| 458 | SelectorParameter.ValidValues.Add(selector);
|
---|
| 459 |
|
---|
| 460 | ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
|
---|
| 461 | if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
|
---|
| 462 |
|
---|
| 463 | if (oldSelector != null) {
|
---|
| 464 | ISelector selector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
|
---|
| 465 | if (selector != null) SelectorParameter.Value = selector;
|
---|
| 466 | }
|
---|
| 467 |
|
---|
| 468 | oldSelector = EmigrantsSelector;
|
---|
| 469 | EmigrantsSelectorParameter.ValidValues.Clear();
|
---|
| 470 | foreach (ISelector selector in emigrantsSelectors)
|
---|
| 471 | EmigrantsSelectorParameter.ValidValues.Add(selector);
|
---|
| 472 | if (oldSelector != null) {
|
---|
| 473 | ISelector selector = EmigrantsSelectorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
|
---|
| 474 | if (selector != null) EmigrantsSelectorParameter.Value = selector;
|
---|
| 475 | }
|
---|
| 476 |
|
---|
[3601] | 477 | IReplacer oldReplacer = ImmigrationReplacerParameter.Value;
|
---|
| 478 | ImmigrationReplacerParameter.ValidValues.Clear();
|
---|
| 479 | foreach (IReplacer replacer in immigrationReplacers)
|
---|
| 480 | ImmigrationReplacerParameter.ValidValues.Add(replacer);
|
---|
| 481 | if (oldReplacer != null) {
|
---|
| 482 | IReplacer replacer = ImmigrationReplacerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSelector.GetType());
|
---|
| 483 | if (replacer != null) ImmigrationReplacerParameter.Value = replacer;
|
---|
[3356] | 484 | }
|
---|
| 485 | }
|
---|
| 486 | private void UpdateMigrators() {
|
---|
| 487 | IMigrator oldMigrator = Migrator;
|
---|
| 488 | MigratorParameter.ValidValues.Clear();
|
---|
| 489 | foreach (IMigrator migrator in migrators)
|
---|
| 490 | MigratorParameter.ValidValues.Add(migrator);
|
---|
| 491 | if (oldMigrator != null) {
|
---|
| 492 | IMigrator migrator = MigratorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMigrator.GetType());
|
---|
| 493 | if (migrator != null) MigratorParameter.Value = migrator;
|
---|
| 494 | } else if (MigratorParameter.ValidValues.Count > 0) MigratorParameter.Value = MigratorParameter.ValidValues.First();
|
---|
| 495 | }
|
---|
| 496 | private void UpdateCrossovers() {
|
---|
| 497 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
| 498 | CrossoverParameter.ValidValues.Clear();
|
---|
| 499 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
|
---|
| 500 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
| 501 | if (oldCrossover != null) {
|
---|
| 502 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
| 503 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
| 504 | }
|
---|
| 505 | }
|
---|
| 506 | private void UpdateMutators() {
|
---|
| 507 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
| 508 | MutatorParameter.ValidValues.Clear();
|
---|
| 509 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
| 510 | MutatorParameter.ValidValues.Add(mutator);
|
---|
| 511 | if (oldMutator != null) {
|
---|
| 512 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
| 513 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
| 514 | }
|
---|
| 515 | }
|
---|
[3650] | 516 | private void UpdateAnalyzers() {
|
---|
| 517 | IslandAnalyzer.Operators.Clear();
|
---|
| 518 | Analyzer.Operators.Clear();
|
---|
| 519 | IslandAnalyzer.Operators.Add(islandQualityAnalyzer);
|
---|
[3671] | 520 | Analyzer.Operators.Add(qualityAnalyzer);
|
---|
[3650] | 521 | if (Problem != null) {
|
---|
[3671] | 522 | /*foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>().OrderBy(x => x.Name)) {
|
---|
| 523 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 524 | param.Depth = 1;
|
---|
| 525 | IslandAnalyzer.Operators.Add(analyzer);
|
---|
| 526 | }*/
|
---|
[3658] | 527 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>().OrderBy(x => x.Name)) {
|
---|
[3671] | 528 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 529 | param.Depth = 2;
|
---|
| 530 | Analyzer.Operators.Add(analyzer);
|
---|
[3650] | 531 | }
|
---|
| 532 | }
|
---|
| 533 | }
|
---|
[3356] | 534 | #endregion
|
---|
| 535 | }
|
---|
| 536 | }
|
---|