[3356] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16565] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3356] | 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;
|
---|
[4068] | 24 | using HeuristicLab.Analysis;
|
---|
[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;
|
---|
[16565] | 32 | using HEAL.Attic;
|
---|
[3356] | 33 | using HeuristicLab.PluginInfrastructure;
|
---|
[3368] | 34 | using HeuristicLab.Random;
|
---|
[3356] | 35 |
|
---|
| 36 | namespace HeuristicLab.Algorithms.GeneticAlgorithm {
|
---|
| 37 | /// <summary>
|
---|
| 38 | /// An island genetic algorithm.
|
---|
| 39 | /// </summary>
|
---|
[13173] | 40 | [Item("Island Genetic Algorithm (Island-GA)", "An island genetic algorithm.")]
|
---|
| 41 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 110)]
|
---|
[16565] | 42 | [StorableType("C36FD509-4EF2-4BA7-9483-8CFCEF7EDA91")]
|
---|
[5809] | 43 | public sealed class IslandGeneticAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
|
---|
[4437] | 44 | public string Filename { get; set; }
|
---|
[3356] | 45 |
|
---|
| 46 | #region Problem Properties
|
---|
| 47 | public override Type ProblemType {
|
---|
[5809] | 48 | get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
|
---|
[3356] | 49 | }
|
---|
[5809] | 50 | public new ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
| 51 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
[3356] | 52 | set { base.Problem = value; }
|
---|
| 53 | }
|
---|
| 54 | #endregion
|
---|
| 55 |
|
---|
| 56 | #region Parameter Properties
|
---|
| 57 | private ValueParameter<IntValue> SeedParameter {
|
---|
| 58 | get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
| 59 | }
|
---|
| 60 | private ValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
| 61 | get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
| 62 | }
|
---|
| 63 | private ValueParameter<IntValue> NumberOfIslandsParameter {
|
---|
| 64 | get { return (ValueParameter<IntValue>)Parameters["NumberOfIslands"]; }
|
---|
| 65 | }
|
---|
| 66 | private ValueParameter<IntValue> MigrationIntervalParameter {
|
---|
| 67 | get { return (ValueParameter<IntValue>)Parameters["MigrationInterval"]; }
|
---|
| 68 | }
|
---|
| 69 | private ValueParameter<PercentValue> MigrationRateParameter {
|
---|
| 70 | get { return (ValueParameter<PercentValue>)Parameters["MigrationRate"]; }
|
---|
| 71 | }
|
---|
[8121] | 72 | public IConstrainedValueParameter<IMigrator> MigratorParameter {
|
---|
| 73 | get { return (IConstrainedValueParameter<IMigrator>)Parameters["Migrator"]; }
|
---|
[3356] | 74 | }
|
---|
[8121] | 75 | public IConstrainedValueParameter<ISelector> EmigrantsSelectorParameter {
|
---|
| 76 | get { return (IConstrainedValueParameter<ISelector>)Parameters["EmigrantsSelector"]; }
|
---|
[3356] | 77 | }
|
---|
[8121] | 78 | public IConstrainedValueParameter<IReplacer> ImmigrationReplacerParameter {
|
---|
| 79 | get { return (IConstrainedValueParameter<IReplacer>)Parameters["ImmigrationReplacer"]; }
|
---|
[3356] | 80 | }
|
---|
| 81 | private ValueParameter<IntValue> PopulationSizeParameter {
|
---|
| 82 | get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
| 83 | }
|
---|
[3609] | 84 | private ValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 85 | get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
[3356] | 86 | }
|
---|
[8121] | 87 | public IConstrainedValueParameter<ISelector> SelectorParameter {
|
---|
| 88 | get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
|
---|
[3356] | 89 | }
|
---|
[8121] | 90 | public IConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
| 91 | get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
[3356] | 92 | }
|
---|
| 93 | private ValueParameter<PercentValue> MutationProbabilityParameter {
|
---|
| 94 | get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
| 95 | }
|
---|
[8121] | 96 | public IConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
| 97 | get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
[3356] | 98 | }
|
---|
| 99 | private ValueParameter<IntValue> ElitesParameter {
|
---|
| 100 | get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
|
---|
| 101 | }
|
---|
[9569] | 102 | private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
|
---|
| 103 | get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
|
---|
| 104 | }
|
---|
[3658] | 105 | private ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
| 106 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
[3650] | 107 | }
|
---|
[3658] | 108 | private ValueParameter<MultiAnalyzer> IslandAnalyzerParameter {
|
---|
| 109 | get { return (ValueParameter<MultiAnalyzer>)Parameters["IslandAnalyzer"]; }
|
---|
[3650] | 110 | }
|
---|
[3356] | 111 | #endregion
|
---|
| 112 |
|
---|
| 113 | #region Properties
|
---|
| 114 | public IntValue Seed {
|
---|
| 115 | get { return SeedParameter.Value; }
|
---|
| 116 | set { SeedParameter.Value = value; }
|
---|
| 117 | }
|
---|
| 118 | public BoolValue SetSeedRandomly {
|
---|
| 119 | get { return SetSeedRandomlyParameter.Value; }
|
---|
| 120 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
| 121 | }
|
---|
| 122 | public IntValue NumberOfIslands {
|
---|
| 123 | get { return NumberOfIslandsParameter.Value; }
|
---|
| 124 | set { NumberOfIslandsParameter.Value = value; }
|
---|
| 125 | }
|
---|
| 126 | public IntValue MigrationInterval {
|
---|
| 127 | get { return MigrationIntervalParameter.Value; }
|
---|
| 128 | set { MigrationIntervalParameter.Value = value; }
|
---|
| 129 | }
|
---|
| 130 | public PercentValue MigrationRate {
|
---|
| 131 | get { return MigrationRateParameter.Value; }
|
---|
| 132 | set { MigrationRateParameter.Value = value; }
|
---|
| 133 | }
|
---|
| 134 | public IMigrator Migrator {
|
---|
| 135 | get { return MigratorParameter.Value; }
|
---|
| 136 | set { MigratorParameter.Value = value; }
|
---|
| 137 | }
|
---|
| 138 | public ISelector EmigrantsSelector {
|
---|
| 139 | get { return EmigrantsSelectorParameter.Value; }
|
---|
| 140 | set { EmigrantsSelectorParameter.Value = value; }
|
---|
| 141 | }
|
---|
[3601] | 142 | public IReplacer ImmigrationReplacer {
|
---|
| 143 | get { return ImmigrationReplacerParameter.Value; }
|
---|
| 144 | set { ImmigrationReplacerParameter.Value = value; }
|
---|
[3356] | 145 | }
|
---|
| 146 | public IntValue PopulationSize {
|
---|
| 147 | get { return PopulationSizeParameter.Value; }
|
---|
| 148 | set { PopulationSizeParameter.Value = value; }
|
---|
| 149 | }
|
---|
[3609] | 150 | public IntValue MaximumGenerations {
|
---|
| 151 | get { return MaximumGenerationsParameter.Value; }
|
---|
| 152 | set { MaximumGenerationsParameter.Value = value; }
|
---|
[3356] | 153 | }
|
---|
| 154 | public ISelector Selector {
|
---|
| 155 | get { return SelectorParameter.Value; }
|
---|
| 156 | set { SelectorParameter.Value = value; }
|
---|
| 157 | }
|
---|
| 158 | public ICrossover Crossover {
|
---|
| 159 | get { return CrossoverParameter.Value; }
|
---|
| 160 | set { CrossoverParameter.Value = value; }
|
---|
| 161 | }
|
---|
| 162 | public PercentValue MutationProbability {
|
---|
| 163 | get { return MutationProbabilityParameter.Value; }
|
---|
| 164 | set { MutationProbabilityParameter.Value = value; }
|
---|
| 165 | }
|
---|
| 166 | public IManipulator Mutator {
|
---|
| 167 | get { return MutatorParameter.Value; }
|
---|
| 168 | set { MutatorParameter.Value = value; }
|
---|
| 169 | }
|
---|
| 170 | public IntValue Elites {
|
---|
| 171 | get { return ElitesParameter.Value; }
|
---|
| 172 | set { ElitesParameter.Value = value; }
|
---|
| 173 | }
|
---|
[9569] | 174 | public bool ReevaluteElites {
|
---|
| 175 | get { return ReevaluateElitesParameter.Value.Value; }
|
---|
| 176 | set { ReevaluateElitesParameter.Value.Value = value; }
|
---|
| 177 | }
|
---|
[3658] | 178 | public MultiAnalyzer Analyzer {
|
---|
[3650] | 179 | get { return AnalyzerParameter.Value; }
|
---|
| 180 | set { AnalyzerParameter.Value = value; }
|
---|
| 181 | }
|
---|
[3658] | 182 | public MultiAnalyzer IslandAnalyzer {
|
---|
[3650] | 183 | get { return IslandAnalyzerParameter.Value; }
|
---|
| 184 | set { IslandAnalyzerParameter.Value = value; }
|
---|
| 185 | }
|
---|
[3359] | 186 | private RandomCreator RandomCreator {
|
---|
| 187 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
| 188 | }
|
---|
[3429] | 189 | private UniformSubScopesProcessor IslandProcessor {
|
---|
[9039] | 190 | get { return OperatorGraph.Iterate().OfType<UniformSubScopesProcessor>().First(x => x.Operator is SolutionsCreator); }
|
---|
[3429] | 191 | }
|
---|
[3359] | 192 | private SolutionsCreator SolutionsCreator {
|
---|
[3429] | 193 | get { return (SolutionsCreator)IslandProcessor.Operator; }
|
---|
[3359] | 194 | }
|
---|
| 195 | private IslandGeneticAlgorithmMainLoop MainLoop {
|
---|
[5366] | 196 | get { return FindMainLoop(IslandProcessor.Successor); }
|
---|
[3359] | 197 | }
|
---|
[3689] | 198 | [Storable]
|
---|
[3662] | 199 | private BestAverageWorstQualityAnalyzer islandQualityAnalyzer;
|
---|
[3689] | 200 | [Storable]
|
---|
[3671] | 201 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
[3356] | 202 | #endregion
|
---|
| 203 |
|
---|
| 204 | [StorableConstructor]
|
---|
[16565] | 205 | private IslandGeneticAlgorithm(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 206 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 207 | private void AfterDeserialization() {
|
---|
[9592] | 208 | // BackwardsCompatibility3.3
|
---|
[9591] | 209 | #region Backwards compatible code, remove with 3.4
|
---|
[9569] | 210 | if (!Parameters.ContainsKey("ReevaluateElites")) {
|
---|
| 211 | Parameters.Add(new FixedValueParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)", (BoolValue)new BoolValue(false).AsReadOnly()) { Hidden = true });
|
---|
| 212 | }
|
---|
[15049] | 213 | var optionalMutatorParameter = MutatorParameter as OptionalConstrainedValueParameter<IManipulator>;
|
---|
| 214 | if (optionalMutatorParameter != null) {
|
---|
| 215 | Parameters.Remove(optionalMutatorParameter);
|
---|
| 216 | Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 217 | foreach (var m in optionalMutatorParameter.ValidValues)
|
---|
| 218 | MutatorParameter.ValidValues.Add(m);
|
---|
| 219 | if (optionalMutatorParameter.Value == null) MutationProbability.Value = 0; // to guarantee that the old configuration results in the same behavior
|
---|
| 220 | else Mutator = optionalMutatorParameter.Value;
|
---|
| 221 | optionalMutatorParameter.ValidValues.Clear(); // to avoid dangling references to the old parameter its valid values are cleared
|
---|
| 222 | }
|
---|
[9591] | 223 | #endregion
|
---|
| 224 |
|
---|
[4722] | 225 | Initialize();
|
---|
| 226 | }
|
---|
| 227 | private IslandGeneticAlgorithm(IslandGeneticAlgorithm original, Cloner cloner)
|
---|
| 228 | : base(original, cloner) {
|
---|
| 229 | islandQualityAnalyzer = cloner.Clone(original.islandQualityAnalyzer);
|
---|
| 230 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
| 231 | Initialize();
|
---|
| 232 | }
|
---|
| 233 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 234 | return new IslandGeneticAlgorithm(this, cloner);
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[3356] | 237 | public IslandGeneticAlgorithm()
|
---|
| 238 | : base() {
|
---|
| 239 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 240 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
| 241 | Parameters.Add(new ValueParameter<IntValue>("NumberOfIslands", "The number of islands.", new IntValue(5)));
|
---|
| 242 | Parameters.Add(new ValueParameter<IntValue>("MigrationInterval", "The number of generations that should pass between migration phases.", new IntValue(20)));
|
---|
| 243 | Parameters.Add(new ValueParameter<PercentValue>("MigrationRate", "The proportion of individuals that should migrate between the islands.", new PercentValue(0.15)));
|
---|
| 244 | Parameters.Add(new ConstrainedValueParameter<IMigrator>("Migrator", "The migration strategy."));
|
---|
| 245 | Parameters.Add(new ConstrainedValueParameter<ISelector>("EmigrantsSelector", "Selects the individuals that will be migrated."));
|
---|
[3601] | 246 | Parameters.Add(new ConstrainedValueParameter<IReplacer>("ImmigrationReplacer", "Selects the population from the unification of the original population and the immigrants."));
|
---|
[3356] | 247 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
|
---|
[3609] | 248 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
|
---|
[3356] | 249 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
| 250 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
| 251 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
[15049] | 252 | Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
[3356] | 253 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
[9569] | 254 | Parameters.Add(new FixedValueParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)", new BoolValue(false)) { Hidden = true });
|
---|
[3658] | 255 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
|
---|
| 256 | Parameters.Add(new ValueParameter<MultiAnalyzer>("IslandAnalyzer", "The operator used to analyze each island.", new MultiAnalyzer()));
|
---|
[4068] | 257 |
|
---|
[3359] | 258 | RandomCreator randomCreator = new RandomCreator();
|
---|
[9039] | 259 | UniformSubScopesProcessor ussp0 = new UniformSubScopesProcessor();
|
---|
| 260 | LocalRandomCreator localRandomCreator = new LocalRandomCreator();
|
---|
| 261 | RandomCreator globalRandomResetter = new RandomCreator();
|
---|
[3356] | 262 | SubScopesCreator populationCreator = new SubScopesCreator();
|
---|
| 263 | UniformSubScopesProcessor ussp1 = new UniformSubScopesProcessor();
|
---|
[3359] | 264 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
[5351] | 265 | VariableCreator variableCreator = new VariableCreator();
|
---|
| 266 | UniformSubScopesProcessor ussp2 = new UniformSubScopesProcessor();
|
---|
| 267 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
[5356] | 268 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
[3359] | 269 | IslandGeneticAlgorithmMainLoop mainLoop = new IslandGeneticAlgorithmMainLoop();
|
---|
[3356] | 270 | OperatorGraph.InitialOperator = randomCreator;
|
---|
| 271 |
|
---|
[7395] | 272 | randomCreator.RandomParameter.ActualName = "GlobalRandom";
|
---|
[3356] | 273 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
| 274 | randomCreator.SeedParameter.Value = null;
|
---|
| 275 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
| 276 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
| 277 | randomCreator.Successor = populationCreator;
|
---|
| 278 |
|
---|
| 279 | populationCreator.NumberOfSubScopesParameter.ActualName = NumberOfIslandsParameter.Name;
|
---|
[9039] | 280 | populationCreator.Successor = ussp0;
|
---|
[3356] | 281 |
|
---|
[9039] | 282 | ussp0.Operator = localRandomCreator;
|
---|
| 283 | ussp0.Successor = globalRandomResetter;
|
---|
| 284 |
|
---|
| 285 | // BackwardsCompatibility3.3
|
---|
| 286 | // the global random is resetted to ensure the same algorithm results
|
---|
[9076] | 287 | #region Backwards compatible code, remove global random resetter with 3.4 and rewire the operator graph
|
---|
[9039] | 288 | globalRandomResetter.RandomParameter.ActualName = "GlobalRandom";
|
---|
| 289 | globalRandomResetter.SeedParameter.ActualName = SeedParameter.Name;
|
---|
| 290 | globalRandomResetter.SeedParameter.Value = null;
|
---|
| 291 | globalRandomResetter.SetSeedRandomlyParameter.Value = new BoolValue(false);
|
---|
| 292 | globalRandomResetter.Successor = ussp1;
|
---|
| 293 | #endregion
|
---|
| 294 |
|
---|
[3356] | 295 | ussp1.Operator = solutionsCreator;
|
---|
[5351] | 296 | ussp1.Successor = variableCreator;
|
---|
[3356] | 297 |
|
---|
| 298 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
[7395] | 299 | //don't create solutions in parallel because the hive engine would distribute these tasks
|
---|
| 300 | solutionsCreator.ParallelParameter.Value = new BoolValue(false);
|
---|
[3356] | 301 | solutionsCreator.Successor = null;
|
---|
| 302 |
|
---|
[5351] | 303 | variableCreator.Name = "Initialize EvaluatedSolutions";
|
---|
| 304 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue()));
|
---|
| 305 | variableCreator.Successor = ussp2;
|
---|
| 306 |
|
---|
| 307 | ussp2.Operator = subScopesCounter;
|
---|
[5356] | 308 | ussp2.Successor = resultsCollector;
|
---|
[5351] | 309 |
|
---|
| 310 | subScopesCounter.Name = "Count EvaluatedSolutions";
|
---|
| 311 | subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
| 312 | subScopesCounter.Successor = null;
|
---|
| 313 |
|
---|
[5356] | 314 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
| 315 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
| 316 | resultsCollector.Successor = mainLoop;
|
---|
| 317 |
|
---|
[3356] | 318 | mainLoop.EmigrantsSelectorParameter.ActualName = EmigrantsSelectorParameter.Name;
|
---|
[3601] | 319 | mainLoop.ImmigrationReplacerParameter.ActualName = ImmigrationReplacerParameter.Name;
|
---|
[3609] | 320 | mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
[3356] | 321 | mainLoop.MigrationIntervalParameter.ActualName = MigrationIntervalParameter.Name;
|
---|
| 322 | mainLoop.MigrationRateParameter.ActualName = MigrationRateParameter.Name;
|
---|
| 323 | mainLoop.MigratorParameter.ActualName = MigratorParameter.Name;
|
---|
| 324 | mainLoop.NumberOfIslandsParameter.ActualName = NumberOfIslandsParameter.Name;
|
---|
| 325 | mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
| 326 | mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
| 327 | mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
[9569] | 328 | mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
|
---|
[3356] | 329 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
| 330 | mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
| 331 | mainLoop.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
|
---|
| 332 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
[3650] | 333 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 334 | mainLoop.IslandAnalyzerParameter.ActualName = IslandAnalyzerParameter.Name;
|
---|
[5351] | 335 | mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
[3356] | 336 | mainLoop.Successor = null;
|
---|
| 337 |
|
---|
[3689] | 338 | foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
|
---|
| 339 | SelectorParameter.ValidValues.Add(selector);
|
---|
| 340 | ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
|
---|
| 341 | if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
|
---|
| 342 |
|
---|
| 343 | foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
|
---|
| 344 | EmigrantsSelectorParameter.ValidValues.Add(selector);
|
---|
| 345 |
|
---|
| 346 | foreach (IReplacer replacer in ApplicationManager.Manager.GetInstances<IReplacer>().OrderBy(x => x.Name))
|
---|
| 347 | ImmigrationReplacerParameter.ValidValues.Add(replacer);
|
---|
| 348 |
|
---|
| 349 | ParameterizeSelectors();
|
---|
| 350 |
|
---|
[13094] | 351 | foreach (IMigrator migrator in ApplicationManager.Manager.GetInstances<IMigrator>().OrderBy(x => x.Name)) {
|
---|
| 352 | // BackwardsCompatibility3.3
|
---|
| 353 | // Set the migration direction to counterclockwise
|
---|
| 354 | var unidirectionalRing = migrator as UnidirectionalRingMigrator;
|
---|
[13109] | 355 | if (unidirectionalRing != null) unidirectionalRing.ClockwiseMigrationParameter.Value = new BoolValue(false);
|
---|
[3689] | 356 | MigratorParameter.ValidValues.Add(migrator);
|
---|
[13094] | 357 | }
|
---|
[3689] | 358 |
|
---|
| 359 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
| 360 | islandQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
| 361 | ParameterizeAnalyzers();
|
---|
| 362 | UpdateAnalyzers();
|
---|
| 363 |
|
---|
[3356] | 364 | Initialize();
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | public override void Prepare() {
|
---|
| 368 | if (Problem != null) base.Prepare();
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | #region Events
|
---|
| 372 | protected override void OnProblemChanged() {
|
---|
| 373 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
[9039] | 374 | ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
|
---|
[7999] | 375 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
[3356] | 376 | ParameterizeSolutionsCreator();
|
---|
| 377 | ParameterizeMainLoop();
|
---|
| 378 | ParameterizeSelectors();
|
---|
[3650] | 379 | ParameterizeAnalyzers();
|
---|
[3750] | 380 | ParameterizeIterationBasedOperators();
|
---|
[3356] | 381 | UpdateCrossovers();
|
---|
| 382 | UpdateMutators();
|
---|
[3650] | 383 | UpdateAnalyzers();
|
---|
[3356] | 384 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 385 | base.OnProblemChanged();
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
| 389 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 390 | ParameterizeSolutionsCreator();
|
---|
| 391 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
| 392 | }
|
---|
| 393 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
[9039] | 394 | ParameterizeStochasticOperatorForIsland(Problem.Evaluator);
|
---|
[3356] | 395 | ParameterizeSolutionsCreator();
|
---|
| 396 | ParameterizeMainLoop();
|
---|
| 397 | ParameterizeSelectors();
|
---|
[3650] | 398 | ParameterizeAnalyzers();
|
---|
[3356] | 399 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 400 | base.Problem_EvaluatorChanged(sender, e);
|
---|
| 401 | }
|
---|
| 402 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
[7999] | 403 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
[3750] | 404 | ParameterizeIterationBasedOperators();
|
---|
[3356] | 405 | UpdateCrossovers();
|
---|
| 406 | UpdateMutators();
|
---|
[3650] | 407 | UpdateAnalyzers();
|
---|
[3356] | 408 | base.Problem_OperatorsChanged(sender, e);
|
---|
| 409 | }
|
---|
| 410 | private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 411 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
| 412 | ParameterizeSelectors();
|
---|
| 413 | }
|
---|
| 414 | private void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
| 415 | ParameterizeSelectors();
|
---|
| 416 | }
|
---|
| 417 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 418 | NumberOfIslands.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 419 | ParameterizeSelectors();
|
---|
| 420 | }
|
---|
| 421 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
| 422 | ParameterizeSelectors();
|
---|
| 423 | }
|
---|
| 424 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 425 | ParameterizeMainLoop();
|
---|
| 426 | ParameterizeSelectors();
|
---|
[3650] | 427 | ParameterizeAnalyzers();
|
---|
[3356] | 428 | }
|
---|
| 429 | private void MigrationRateParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 430 | MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
|
---|
| 431 | ParameterizeSelectors();
|
---|
| 432 | }
|
---|
| 433 | private void MigrationRate_ValueChanged(object sender, EventArgs e) {
|
---|
| 434 | ParameterizeSelectors();
|
---|
| 435 | }
|
---|
| 436 | #endregion
|
---|
| 437 |
|
---|
| 438 | #region Helpers
|
---|
| 439 | private void Initialize() {
|
---|
| 440 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
| 441 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 442 | MigrationRateParameter.ValueChanged += new EventHandler(MigrationRateParameter_ValueChanged);
|
---|
| 443 | MigrationRate.ValueChanged += new EventHandler(MigrationRate_ValueChanged);
|
---|
| 444 | ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
|
---|
| 445 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
| 446 | if (Problem != null) {
|
---|
| 447 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 448 | }
|
---|
| 449 | }
|
---|
| 450 | private void ParameterizeSolutionsCreator() {
|
---|
[3359] | 451 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 452 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
[3356] | 453 | }
|
---|
| 454 | private void ParameterizeMainLoop() {
|
---|
[3359] | 455 | MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 456 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 457 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 458 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[3356] | 459 | }
|
---|
| 460 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
[6051] | 461 | IStochasticOperator stochasticOp = op as IStochasticOperator;
|
---|
| 462 | if (stochasticOp != null) {
|
---|
| 463 | stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 464 | stochasticOp.RandomParameter.Hidden = true;
|
---|
| 465 | }
|
---|
[3356] | 466 | }
|
---|
[7395] | 467 | private void ParameterizeStochasticOperatorForIsland(IOperator op) {
|
---|
| 468 | IStochasticOperator stochasticOp = op as IStochasticOperator;
|
---|
| 469 | if (stochasticOp != null) {
|
---|
| 470 | stochasticOp.RandomParameter.ActualName = "LocalRandom";
|
---|
| 471 | stochasticOp.RandomParameter.Hidden = true;
|
---|
| 472 | }
|
---|
| 473 | }
|
---|
[3356] | 474 | private void ParameterizeSelectors() {
|
---|
[3689] | 475 | foreach (ISelector selector in SelectorParameter.ValidValues) {
|
---|
[3356] | 476 | selector.CopySelected = new BoolValue(true);
|
---|
| 477 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
|
---|
[6051] | 478 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
[7395] | 479 | ParameterizeStochasticOperatorForIsland(selector);
|
---|
[3356] | 480 | }
|
---|
[3689] | 481 | foreach (ISelector selector in EmigrantsSelectorParameter.ValidValues) {
|
---|
[3356] | 482 | selector.CopySelected = new BoolValue(true);
|
---|
| 483 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue((int)Math.Ceiling(PopulationSize.Value * MigrationRate.Value));
|
---|
[6051] | 484 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
[3356] | 485 | ParameterizeStochasticOperator(selector);
|
---|
| 486 | }
|
---|
[3689] | 487 | foreach (IReplacer replacer in ImmigrationReplacerParameter.ValidValues) {
|
---|
[3601] | 488 | ParameterizeStochasticOperator(replacer);
|
---|
[3356] | 489 | }
|
---|
| 490 | if (Problem != null) {
|
---|
[3689] | 491 | foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
[3356] | 492 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
[6051] | 493 | selector.MaximizationParameter.Hidden = true;
|
---|
[3356] | 494 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[6051] | 495 | selector.QualityParameter.Hidden = true;
|
---|
[3356] | 496 | }
|
---|
[3689] | 497 | foreach (ISingleObjectiveSelector selector in EmigrantsSelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
[3356] | 498 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
[6051] | 499 | selector.MaximizationParameter.Hidden = true;
|
---|
[3356] | 500 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[6051] | 501 | selector.QualityParameter.Hidden = true;
|
---|
[3356] | 502 | }
|
---|
[3689] | 503 | foreach (ISingleObjectiveReplacer selector in ImmigrationReplacerParameter.ValidValues.OfType<ISingleObjectiveReplacer>()) {
|
---|
[3356] | 504 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
[6051] | 505 | selector.MaximizationParameter.Hidden = true;
|
---|
[3356] | 506 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[6051] | 507 | selector.QualityParameter.Hidden = true;
|
---|
[3356] | 508 | }
|
---|
| 509 | }
|
---|
| 510 | }
|
---|
[3650] | 511 | private void ParameterizeAnalyzers() {
|
---|
| 512 | islandQualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
[6051] | 513 | islandQualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
[3673] | 514 | islandQualityAnalyzer.QualityParameter.Depth = 1;
|
---|
[3671] | 515 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
[6051] | 516 | qualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
[3673] | 517 | qualityAnalyzer.QualityParameter.Depth = 2;
|
---|
| 518 |
|
---|
[3650] | 519 | if (Problem != null) {
|
---|
| 520 | islandQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
[6051] | 521 | islandQualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
[3650] | 522 | islandQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[6051] | 523 | islandQualityAnalyzer.QualityParameter.Hidden = true;
|
---|
[3650] | 524 | islandQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
[6051] | 525 | islandQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
[3671] | 526 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
[6051] | 527 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
[3671] | 528 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[6051] | 529 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
[3671] | 530 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
[6051] | 531 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
[3650] | 532 | }
|
---|
| 533 | }
|
---|
[3750] | 534 | private void ParameterizeIterationBasedOperators() {
|
---|
| 535 | if (Problem != null) {
|
---|
| 536 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
| 537 | op.IterationsParameter.ActualName = "Generations";
|
---|
[6051] | 538 | op.IterationsParameter.Hidden = true;
|
---|
[3750] | 539 | op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
|
---|
[6051] | 540 | op.MaximumIterationsParameter.Hidden = true;
|
---|
[3750] | 541 | }
|
---|
| 542 | }
|
---|
| 543 | }
|
---|
[3356] | 544 | private void UpdateCrossovers() {
|
---|
| 545 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
[7511] | 546 | ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
|
---|
[3356] | 547 | CrossoverParameter.ValidValues.Clear();
|
---|
[7524] | 548 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
|
---|
| 549 | ParameterizeStochasticOperatorForIsland(crossover);
|
---|
[3356] | 550 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
[7524] | 551 | }
|
---|
[3356] | 552 | if (oldCrossover != null) {
|
---|
| 553 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
| 554 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
[7511] | 555 | else oldCrossover = null;
|
---|
[3356] | 556 | }
|
---|
[7511] | 557 | if (oldCrossover == null && defaultCrossover != null)
|
---|
| 558 | CrossoverParameter.Value = defaultCrossover;
|
---|
[3356] | 559 | }
|
---|
| 560 | private void UpdateMutators() {
|
---|
| 561 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
| 562 | MutatorParameter.ValidValues.Clear();
|
---|
[15049] | 563 | IManipulator defaultMutator = Problem.Operators.OfType<IManipulator>().FirstOrDefault();
|
---|
| 564 |
|
---|
[7395] | 565 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name)) {
|
---|
| 566 | ParameterizeStochasticOperatorForIsland(mutator);
|
---|
[3356] | 567 | MutatorParameter.ValidValues.Add(mutator);
|
---|
[7395] | 568 | }
|
---|
[3356] | 569 | if (oldMutator != null) {
|
---|
| 570 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
| 571 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
[15049] | 572 | else oldMutator = null;
|
---|
[3356] | 573 | }
|
---|
[15049] | 574 |
|
---|
| 575 | if (oldMutator == null && defaultMutator != null)
|
---|
| 576 | MutatorParameter.Value = defaultMutator;
|
---|
[3356] | 577 | }
|
---|
[3650] | 578 | private void UpdateAnalyzers() {
|
---|
| 579 | IslandAnalyzer.Operators.Clear();
|
---|
| 580 | Analyzer.Operators.Clear();
|
---|
[7172] | 581 | IslandAnalyzer.Operators.Add(islandQualityAnalyzer, islandQualityAnalyzer.EnabledByDefault);
|
---|
[3650] | 582 | if (Problem != null) {
|
---|
[3816] | 583 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
[3671] | 584 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 585 | param.Depth = 2;
|
---|
[7172] | 586 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
[3650] | 587 | }
|
---|
| 588 | }
|
---|
[7172] | 589 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
[3650] | 590 | }
|
---|
[5366] | 591 | private IslandGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
|
---|
| 592 | IOperator mainLoop = start;
|
---|
| 593 | while (mainLoop != null && !(mainLoop is IslandGeneticAlgorithmMainLoop))
|
---|
| 594 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
| 595 | if (mainLoop == null) return null;
|
---|
| 596 | else return (IslandGeneticAlgorithmMainLoop)mainLoop;
|
---|
| 597 | }
|
---|
[3356] | 598 | #endregion
|
---|
| 599 | }
|
---|
| 600 | }
|
---|