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