[3378] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3378] | 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;
|
---|
[3650] | 24 | using HeuristicLab.Analysis;
|
---|
[4068] | 25 | using HeuristicLab.Common;
|
---|
[3378] | 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
[5356] | 28 | using HeuristicLab.Operators;
|
---|
[3378] | 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Optimization.Operators;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 | using HeuristicLab.PluginInfrastructure;
|
---|
| 34 | using HeuristicLab.Random;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
|
---|
| 37 | /// <summary>
|
---|
| 38 | /// An offspring selection genetic algorithm.
|
---|
| 39 | /// </summary>
|
---|
[13173] | 40 | [Item("Offspring Selection Genetic Algorithm (OSGA)", "An offspring selection genetic algorithm (Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications. CRC Press).")]
|
---|
[12504] | 41 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 120)]
|
---|
[3378] | 42 | [StorableClass]
|
---|
[5809] | 43 | public sealed class OffspringSelectionGeneticAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
|
---|
[4437] | 44 | public string Filename { get; set; }
|
---|
| 45 |
|
---|
[3378] | 46 | #region Problem Properties
|
---|
| 47 | public override Type ProblemType {
|
---|
[5809] | 48 | get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
|
---|
[3378] | 49 | }
|
---|
[5809] | 50 | public new ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
| 51 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
[3378] | 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> PopulationSizeParameter {
|
---|
| 64 | get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
| 65 | }
|
---|
[8121] | 66 | public IConstrainedValueParameter<ISelector> SelectorParameter {
|
---|
| 67 | get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
|
---|
[3378] | 68 | }
|
---|
[8121] | 69 | public IConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
| 70 | get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
[3378] | 71 | }
|
---|
| 72 | private ValueParameter<PercentValue> MutationProbabilityParameter {
|
---|
| 73 | get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
| 74 | }
|
---|
[8121] | 75 | public IConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
| 76 | get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
[3378] | 77 | }
|
---|
| 78 | private ValueParameter<IntValue> ElitesParameter {
|
---|
| 79 | get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
|
---|
| 80 | }
|
---|
[9569] | 81 | private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
|
---|
| 82 | get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
|
---|
| 83 | }
|
---|
[3378] | 84 | private ValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 85 | get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 86 | }
|
---|
[3379] | 87 | private ValueLookupParameter<DoubleValue> SuccessRatioParameter {
|
---|
| 88 | get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
|
---|
| 89 | }
|
---|
| 90 | private ValueLookupParameter<DoubleValue> ComparisonFactorLowerBoundParameter {
|
---|
| 91 | get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorLowerBound"]; }
|
---|
| 92 | }
|
---|
| 93 | private ValueLookupParameter<DoubleValue> ComparisonFactorUpperBoundParameter {
|
---|
| 94 | get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorUpperBound"]; }
|
---|
| 95 | }
|
---|
[8121] | 96 | public IConstrainedValueParameter<IDiscreteDoubleValueModifier> ComparisonFactorModifierParameter {
|
---|
| 97 | get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["ComparisonFactorModifier"]; }
|
---|
[3379] | 98 | }
|
---|
| 99 | private ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
|
---|
| 100 | get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
|
---|
| 101 | }
|
---|
| 102 | private ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
|
---|
| 103 | get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
|
---|
| 104 | }
|
---|
[3510] | 105 | private ValueLookupParameter<IntValue> SelectedParentsParameter {
|
---|
| 106 | get { return (ValueLookupParameter<IntValue>)Parameters["SelectedParents"]; }
|
---|
| 107 | }
|
---|
[3658] | 108 | private ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
| 109 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
[3650] | 110 | }
|
---|
[3891] | 111 | private ValueParameter<IntValue> MaximumEvaluatedSolutionsParameter {
|
---|
| 112 | get { return (ValueParameter<IntValue>)Parameters["MaximumEvaluatedSolutions"]; }
|
---|
| 113 | }
|
---|
[10643] | 114 | private IFixedValueParameter<BoolValue> FillPopulationWithParentsParameter {
|
---|
| 115 | get { return (IFixedValueParameter<BoolValue>)Parameters["FillPopulationWithParents"]; }
|
---|
| 116 | }
|
---|
[3378] | 117 | #endregion
|
---|
| 118 |
|
---|
| 119 | #region Properties
|
---|
| 120 | public IntValue Seed {
|
---|
| 121 | get { return SeedParameter.Value; }
|
---|
| 122 | set { SeedParameter.Value = value; }
|
---|
| 123 | }
|
---|
| 124 | public BoolValue SetSeedRandomly {
|
---|
| 125 | get { return SetSeedRandomlyParameter.Value; }
|
---|
| 126 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
| 127 | }
|
---|
| 128 | public IntValue PopulationSize {
|
---|
| 129 | get { return PopulationSizeParameter.Value; }
|
---|
| 130 | set { PopulationSizeParameter.Value = value; }
|
---|
| 131 | }
|
---|
| 132 | public ISelector Selector {
|
---|
| 133 | get { return SelectorParameter.Value; }
|
---|
| 134 | set { SelectorParameter.Value = value; }
|
---|
| 135 | }
|
---|
| 136 | public ICrossover Crossover {
|
---|
| 137 | get { return CrossoverParameter.Value; }
|
---|
| 138 | set { CrossoverParameter.Value = value; }
|
---|
| 139 | }
|
---|
| 140 | public PercentValue MutationProbability {
|
---|
| 141 | get { return MutationProbabilityParameter.Value; }
|
---|
| 142 | set { MutationProbabilityParameter.Value = value; }
|
---|
| 143 | }
|
---|
| 144 | public IManipulator Mutator {
|
---|
| 145 | get { return MutatorParameter.Value; }
|
---|
| 146 | set { MutatorParameter.Value = value; }
|
---|
| 147 | }
|
---|
| 148 | public IntValue Elites {
|
---|
| 149 | get { return ElitesParameter.Value; }
|
---|
| 150 | set { ElitesParameter.Value = value; }
|
---|
| 151 | }
|
---|
[9569] | 152 | public bool ReevaluteElites {
|
---|
| 153 | get { return ReevaluateElitesParameter.Value.Value; }
|
---|
| 154 | set { ReevaluateElitesParameter.Value.Value = value; }
|
---|
| 155 | }
|
---|
[3378] | 156 | public IntValue MaximumGenerations {
|
---|
| 157 | get { return MaximumGenerationsParameter.Value; }
|
---|
| 158 | set { MaximumGenerationsParameter.Value = value; }
|
---|
| 159 | }
|
---|
[3510] | 160 | public DoubleValue SuccessRatio {
|
---|
[3379] | 161 | get { return SuccessRatioParameter.Value; }
|
---|
| 162 | set { SuccessRatioParameter.Value = value; }
|
---|
| 163 | }
|
---|
[3510] | 164 | public DoubleValue ComparisonFactorLowerBound {
|
---|
[3379] | 165 | get { return ComparisonFactorLowerBoundParameter.Value; }
|
---|
| 166 | set { ComparisonFactorLowerBoundParameter.Value = value; }
|
---|
| 167 | }
|
---|
[3510] | 168 | public DoubleValue ComparisonFactorUpperBound {
|
---|
[3379] | 169 | get { return ComparisonFactorUpperBoundParameter.Value; }
|
---|
| 170 | set { ComparisonFactorUpperBoundParameter.Value = value; }
|
---|
| 171 | }
|
---|
[3510] | 172 | public IDiscreteDoubleValueModifier ComparisonFactorModifier {
|
---|
[3379] | 173 | get { return ComparisonFactorModifierParameter.Value; }
|
---|
| 174 | set { ComparisonFactorModifierParameter.Value = value; }
|
---|
| 175 | }
|
---|
[3510] | 176 | public DoubleValue MaximumSelectionPressure {
|
---|
[3379] | 177 | get { return MaximumSelectionPressureParameter.Value; }
|
---|
| 178 | set { MaximumSelectionPressureParameter.Value = value; }
|
---|
| 179 | }
|
---|
[3510] | 180 | public BoolValue OffspringSelectionBeforeMutation {
|
---|
[3379] | 181 | get { return OffspringSelectionBeforeMutationParameter.Value; }
|
---|
| 182 | set { OffspringSelectionBeforeMutationParameter.Value = value; }
|
---|
| 183 | }
|
---|
[3510] | 184 | public IntValue SelectedParents {
|
---|
| 185 | get { return SelectedParentsParameter.Value; }
|
---|
| 186 | set { SelectedParentsParameter.Value = value; }
|
---|
| 187 | }
|
---|
[3658] | 188 | public MultiAnalyzer Analyzer {
|
---|
[3650] | 189 | get { return AnalyzerParameter.Value; }
|
---|
| 190 | set { AnalyzerParameter.Value = value; }
|
---|
| 191 | }
|
---|
[3891] | 192 | public IntValue MaximumEvaluatedSolutions {
|
---|
| 193 | get { return MaximumEvaluatedSolutionsParameter.Value; }
|
---|
| 194 | set { MaximumEvaluatedSolutionsParameter.Value = value; }
|
---|
| 195 | }
|
---|
[10643] | 196 | public bool FillPopulationWithParents {
|
---|
| 197 | get { return FillPopulationWithParentsParameter.Value.Value; }
|
---|
| 198 | set { FillPopulationWithParentsParameter.Value.Value = value; }
|
---|
| 199 | }
|
---|
[3378] | 200 | private RandomCreator RandomCreator {
|
---|
| 201 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
| 202 | }
|
---|
| 203 | private SolutionsCreator SolutionsCreator {
|
---|
| 204 | get { return (SolutionsCreator)RandomCreator.Successor; }
|
---|
| 205 | }
|
---|
| 206 | private OffspringSelectionGeneticAlgorithmMainLoop MainLoop {
|
---|
[5366] | 207 | get { return FindMainLoop(SolutionsCreator.Successor); }
|
---|
[3378] | 208 | }
|
---|
[3689] | 209 | [Storable]
|
---|
[3672] | 210 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
[3689] | 211 | [Storable]
|
---|
[3672] | 212 | private ValueAnalyzer selectionPressureAnalyzer;
|
---|
[5683] | 213 | [Storable]
|
---|
| 214 | private SuccessfulOffspringAnalyzer successfulOffspringAnalyzer;
|
---|
[3378] | 215 | #endregion
|
---|
| 216 |
|
---|
[3379] | 217 | [StorableConstructor]
|
---|
| 218 | private OffspringSelectionGeneticAlgorithm(bool deserializing) : base(deserializing) { }
|
---|
[4722] | 219 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 220 | private void AfterDeserialization() {
|
---|
[9592] | 221 | // BackwardsCompatibility3.3
|
---|
| 222 | #region Backwards compatible code, remove with 3.4
|
---|
[5683] | 223 | if (successfulOffspringAnalyzer == null)
|
---|
| 224 | successfulOffspringAnalyzer = new SuccessfulOffspringAnalyzer();
|
---|
[9569] | 225 | if (!Parameters.ContainsKey("ReevaluateElites")) {
|
---|
| 226 | 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 });
|
---|
| 227 | }
|
---|
[10643] | 228 | if (!Parameters.ContainsKey("FillPopulationWithParents"))
|
---|
| 229 | Parameters.Add(new FixedValueParameter<BoolValue>("FillPopulationWithParents", "True if the population should be filled with parent individual or false if worse children should be used when the maximum selection pressure is exceeded.", new BoolValue(false)) { Hidden = true });
|
---|
[5683] | 230 | #endregion
|
---|
[7172] | 231 |
|
---|
[4722] | 232 | Initialize();
|
---|
| 233 | }
|
---|
| 234 | private OffspringSelectionGeneticAlgorithm(OffspringSelectionGeneticAlgorithm original, Cloner cloner)
|
---|
| 235 | : base(original, cloner) {
|
---|
| 236 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
| 237 | selectionPressureAnalyzer = cloner.Clone(original.selectionPressureAnalyzer);
|
---|
[5683] | 238 | successfulOffspringAnalyzer = cloner.Clone(original.successfulOffspringAnalyzer);
|
---|
[4722] | 239 | Initialize();
|
---|
| 240 | }
|
---|
| 241 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 242 | return new OffspringSelectionGeneticAlgorithm(this, cloner);
|
---|
| 243 | }
|
---|
[3378] | 244 | public OffspringSelectionGeneticAlgorithm()
|
---|
| 245 | : base() {
|
---|
| 246 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 247 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
| 248 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
|
---|
| 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)));
|
---|
| 252 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 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 });
|
---|
[3378] | 255 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
|
---|
[3379] | 256 | Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved.", new DoubleValue(1)));
|
---|
| 257 | Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorLowerBound", "The lower bound of the comparison factor (start).", new DoubleValue(0)));
|
---|
| 258 | Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorUpperBound", "The upper bound of the comparison factor (end).", new DoubleValue(1)));
|
---|
[3426] | 259 | Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("ComparisonFactorModifier", "The operator used to modify the comparison factor.", new ItemSet<IDiscreteDoubleValueModifier>(new IDiscreteDoubleValueModifier[] { new LinearDiscreteDoubleValueModifier() }), new LinearDiscreteDoubleValueModifier()));
|
---|
[3379] | 260 | Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm.", new DoubleValue(100)));
|
---|
| 261 | Parameters.Add(new ValueLookupParameter<BoolValue>("OffspringSelectionBeforeMutation", "True if the offspring selection step should be applied before mutation, false if it should be applied after mutation.", new BoolValue(false)));
|
---|
[3735] | 262 | Parameters.Add(new ValueLookupParameter<IntValue>("SelectedParents", "How much parents should be selected each time the offspring selection step is performed until the population is filled. This parameter should be about the same or twice the size of PopulationSize for smaller problems, and less for large problems.", new IntValue(200)));
|
---|
[3658] | 263 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
|
---|
[3891] | 264 | Parameters.Add(new ValueParameter<IntValue>("MaximumEvaluatedSolutions", "The maximum number of evaluated solutions (approximately).", new IntValue(int.MaxValue)));
|
---|
[10643] | 265 | Parameters.Add(new FixedValueParameter<BoolValue>("FillPopulationWithParents", "True if the population should be filled with parent individual or false if worse children should be used when the maximum selection pressure is exceeded.", new BoolValue(false)) { Hidden = true });
|
---|
[3891] | 266 |
|
---|
[3378] | 267 | RandomCreator randomCreator = new RandomCreator();
|
---|
| 268 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
[5356] | 269 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
| 270 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
[3378] | 271 | OffspringSelectionGeneticAlgorithmMainLoop mainLoop = new OffspringSelectionGeneticAlgorithmMainLoop();
|
---|
| 272 | OperatorGraph.InitialOperator = randomCreator;
|
---|
| 273 |
|
---|
| 274 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
| 275 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
| 276 | randomCreator.SeedParameter.Value = null;
|
---|
| 277 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
| 278 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
| 279 | randomCreator.Successor = solutionsCreator;
|
---|
| 280 |
|
---|
| 281 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
[5356] | 282 | solutionsCreator.Successor = subScopesCounter;
|
---|
[3378] | 283 |
|
---|
[5356] | 284 | subScopesCounter.Name = "Initialize EvaluatedSolutions";
|
---|
| 285 | subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
| 286 | subScopesCounter.Successor = resultsCollector;
|
---|
| 287 |
|
---|
| 288 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", "", "EvaluatedSolutions"));
|
---|
| 289 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
| 290 | resultsCollector.Successor = mainLoop;
|
---|
| 291 |
|
---|
[3744] | 292 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 293 | mainLoop.ComparisonFactorModifierParameter.ActualName = ComparisonFactorModifierParameter.Name;
|
---|
| 294 | mainLoop.ComparisonFactorParameter.ActualName = "ComparisonFactor";
|
---|
| 295 | mainLoop.ComparisonFactorStartParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
|
---|
[3378] | 296 | mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
| 297 | mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
[9569] | 298 | mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
|
---|
[5356] | 299 | mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
[3378] | 300 | mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
[3744] | 301 | mainLoop.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
|
---|
| 302 | mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
[3378] | 303 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
[3744] | 304 | mainLoop.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
|
---|
[3378] | 305 | mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 306 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
[3744] | 307 | mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
| 308 | mainLoop.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
|
---|
[10643] | 309 | mainLoop.FillPopulationWithParentsParameter.ActualName = FillPopulationWithParentsParameter.Name;
|
---|
[3378] | 310 |
|
---|
[3689] | 311 | foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
|
---|
| 312 | SelectorParameter.ValidValues.Add(selector);
|
---|
| 313 | ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
|
---|
| 314 | if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
|
---|
| 315 | ParameterizeSelectors();
|
---|
| 316 |
|
---|
| 317 | foreach (IDiscreteDoubleValueModifier modifier in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name))
|
---|
| 318 | ComparisonFactorModifierParameter.ValidValues.Add(modifier);
|
---|
| 319 | IDiscreteDoubleValueModifier linearModifier = ComparisonFactorModifierParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("LinearDiscreteDoubleValueModifier"));
|
---|
| 320 | if (linearModifier != null) ComparisonFactorModifierParameter.Value = linearModifier;
|
---|
| 321 | ParameterizeComparisonFactorModifiers();
|
---|
| 322 |
|
---|
| 323 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
| 324 | selectionPressureAnalyzer = new ValueAnalyzer();
|
---|
[5683] | 325 | successfulOffspringAnalyzer = new SuccessfulOffspringAnalyzer();
|
---|
[3689] | 326 | ParameterizeAnalyzers();
|
---|
| 327 | UpdateAnalyzers();
|
---|
| 328 |
|
---|
[3378] | 329 | Initialize();
|
---|
| 330 | }
|
---|
| 331 |
|
---|
| 332 |
|
---|
[4722] | 333 |
|
---|
[3378] | 334 | public override void Prepare() {
|
---|
| 335 | if (Problem != null) base.Prepare();
|
---|
| 336 | }
|
---|
| 337 |
|
---|
| 338 | #region Events
|
---|
| 339 | protected override void OnProblemChanged() {
|
---|
| 340 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 341 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
[7999] | 342 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
[3378] | 343 | ParameterizeSolutionsCreator();
|
---|
[3379] | 344 | ParameterizMainLoop();
|
---|
[3378] | 345 | ParameterizeSelectors();
|
---|
[3650] | 346 | ParameterizeAnalyzers();
|
---|
[3750] | 347 | ParameterizeIterationBasedOperators();
|
---|
[3378] | 348 | UpdateCrossovers();
|
---|
| 349 | UpdateMutators();
|
---|
[3650] | 350 | UpdateAnalyzers();
|
---|
[3378] | 351 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 352 | base.OnProblemChanged();
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
| 356 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 357 | ParameterizeSolutionsCreator();
|
---|
| 358 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
| 359 | }
|
---|
| 360 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
| 361 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 362 | ParameterizeSolutionsCreator();
|
---|
[3379] | 363 | ParameterizMainLoop();
|
---|
[3378] | 364 | ParameterizeSelectors();
|
---|
[3650] | 365 | ParameterizeAnalyzers();
|
---|
[3378] | 366 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 367 | base.Problem_EvaluatorChanged(sender, e);
|
---|
| 368 | }
|
---|
| 369 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
[7999] | 370 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
[3750] | 371 | ParameterizeIterationBasedOperators();
|
---|
[3378] | 372 | UpdateCrossovers();
|
---|
| 373 | UpdateMutators();
|
---|
[3650] | 374 | UpdateAnalyzers();
|
---|
[3378] | 375 | base.Problem_OperatorsChanged(sender, e);
|
---|
| 376 | }
|
---|
| 377 | private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 378 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
| 379 | ParameterizeSelectors();
|
---|
| 380 | }
|
---|
| 381 | private void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
| 382 | ParameterizeSelectors();
|
---|
| 383 | }
|
---|
| 384 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 385 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 386 | ParameterizeSelectors();
|
---|
| 387 | }
|
---|
| 388 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
| 389 | ParameterizeSelectors();
|
---|
| 390 | }
|
---|
| 391 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
[3379] | 392 | ParameterizMainLoop();
|
---|
[3378] | 393 | ParameterizeSelectors();
|
---|
[3650] | 394 | ParameterizeAnalyzers();
|
---|
[3378] | 395 | }
|
---|
| 396 | #endregion
|
---|
| 397 |
|
---|
| 398 | #region Helpers
|
---|
| 399 | private void Initialize() {
|
---|
| 400 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
| 401 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 402 | ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
|
---|
| 403 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
| 404 | if (Problem != null) {
|
---|
| 405 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 406 | }
|
---|
| 407 | }
|
---|
| 408 | private void ParameterizeSolutionsCreator() {
|
---|
| 409 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 410 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
| 411 | }
|
---|
[3379] | 412 | private void ParameterizMainLoop() {
|
---|
[3378] | 413 | MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 414 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 415 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 416 | }
|
---|
| 417 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
| 418 | if (op is IStochasticOperator)
|
---|
| 419 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 420 | }
|
---|
| 421 | private void ParameterizeSelectors() {
|
---|
[3689] | 422 | foreach (ISelector selector in SelectorParameter.ValidValues) {
|
---|
[3378] | 423 | selector.CopySelected = new BoolValue(true);
|
---|
[3510] | 424 | selector.NumberOfSelectedSubScopesParameter.Value = null;
|
---|
| 425 | selector.NumberOfSelectedSubScopesParameter.ActualName = SelectedParentsParameter.Name;
|
---|
[3378] | 426 | ParameterizeStochasticOperator(selector);
|
---|
| 427 | }
|
---|
| 428 | if (Problem != null) {
|
---|
[3689] | 429 | foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
[3378] | 430 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 431 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 432 | }
|
---|
| 433 | }
|
---|
| 434 | }
|
---|
[3650] | 435 | private void ParameterizeAnalyzers() {
|
---|
[3672] | 436 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 437 | selectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
|
---|
| 438 | selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 439 | selectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
|
---|
| 440 | selectionPressureAnalyzer.ValueParameter.Depth = 0;
|
---|
| 441 | selectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
|
---|
[5683] | 442 | successfulOffspringAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 443 | successfulOffspringAnalyzer.GenerationsParameter.ActualName = "Generations";
|
---|
[5725] | 444 | successfulOffspringAnalyzer.SuccessfulOffspringFlagParameter.Value.Value = "SuccessfulOffspring";
|
---|
[5683] | 445 | successfulOffspringAnalyzer.DepthParameter.Value = new IntValue(1);
|
---|
[3650] | 446 | if (Problem != null) {
|
---|
[3672] | 447 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 448 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 449 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
[3650] | 450 | }
|
---|
| 451 | }
|
---|
[3379] | 452 | private void ParameterizeComparisonFactorModifiers() {
|
---|
[3689] | 453 | foreach (IDiscreteDoubleValueModifier modifier in ComparisonFactorModifierParameter.ValidValues) {
|
---|
[3413] | 454 | modifier.IndexParameter.ActualName = "Generations";
|
---|
[3379] | 455 | modifier.EndIndexParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
| 456 | modifier.EndValueParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
|
---|
| 457 | modifier.StartIndexParameter.Value = new IntValue(0);
|
---|
| 458 | modifier.StartValueParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
|
---|
[3413] | 459 | modifier.ValueParameter.ActualName = "ComparisonFactor";
|
---|
[3379] | 460 | }
|
---|
| 461 | }
|
---|
[3750] | 462 | private void ParameterizeIterationBasedOperators() {
|
---|
| 463 | if (Problem != null) {
|
---|
| 464 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
| 465 | op.IterationsParameter.ActualName = "Generations";
|
---|
| 466 | op.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
| 467 | }
|
---|
| 468 | }
|
---|
| 469 | }
|
---|
[3378] | 470 | private void UpdateCrossovers() {
|
---|
| 471 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
| 472 | CrossoverParameter.ValidValues.Clear();
|
---|
[7493] | 473 | ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
|
---|
| 474 |
|
---|
[3378] | 475 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
|
---|
| 476 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
[7493] | 477 |
|
---|
[3378] | 478 | if (oldCrossover != null) {
|
---|
| 479 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
| 480 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
[7509] | 481 | else oldCrossover = null;
|
---|
| 482 | }
|
---|
| 483 | if (oldCrossover == null && defaultCrossover != null)
|
---|
[7493] | 484 | CrossoverParameter.Value = defaultCrossover;
|
---|
[3378] | 485 | }
|
---|
| 486 | private void UpdateMutators() {
|
---|
| 487 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
| 488 | MutatorParameter.ValidValues.Clear();
|
---|
| 489 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
| 490 | MutatorParameter.ValidValues.Add(mutator);
|
---|
| 491 | if (oldMutator != null) {
|
---|
| 492 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
| 493 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
| 494 | }
|
---|
| 495 | }
|
---|
[3650] | 496 | private void UpdateAnalyzers() {
|
---|
| 497 | Analyzer.Operators.Clear();
|
---|
| 498 | if (Problem != null) {
|
---|
[3816] | 499 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
[3672] | 500 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 501 | param.Depth = 1;
|
---|
[7172] | 502 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
[3672] | 503 | }
|
---|
[3650] | 504 | }
|
---|
[7172] | 505 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
| 506 | Analyzer.Operators.Add(selectionPressureAnalyzer, selectionPressureAnalyzer.EnabledByDefault);
|
---|
| 507 | Analyzer.Operators.Add(successfulOffspringAnalyzer, successfulOffspringAnalyzer.EnabledByDefault);
|
---|
[3650] | 508 | }
|
---|
[5366] | 509 | private OffspringSelectionGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
|
---|
| 510 | IOperator mainLoop = start;
|
---|
| 511 | while (mainLoop != null && !(mainLoop is OffspringSelectionGeneticAlgorithmMainLoop))
|
---|
| 512 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
| 513 | if (mainLoop == null) return null;
|
---|
| 514 | else return (OffspringSelectionGeneticAlgorithmMainLoop)mainLoop;
|
---|
| 515 | }
|
---|
[3378] | 516 | #endregion
|
---|
| 517 | }
|
---|
| 518 | }
|
---|