[8313] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11171] | 3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8313] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Analysis;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Optimization.Operators;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 | using HeuristicLab.PluginInfrastructure;
|
---|
| 34 | using HeuristicLab.Random;
|
---|
| 35 |
|
---|
| 36 | namespace HeuristicLab.Algorithms.RAPGA {
|
---|
| 37 | /// <summary>
|
---|
| 38 | /// A relevant alleles preserving genetic algorithm.
|
---|
| 39 | /// </summary>
|
---|
[8629] | 40 | [Item("RAPGA", "A relevant alleles preserving genetic algorithm (Affenzeller, M. et al. 2007. Self-adaptive population size adjustment for genetic algorithms. Proceedings of Computer Aided Systems Theory: EuroCAST 2007, Lecture Notes in Computer Science, pp 820–828. Springer).")]
|
---|
[8313] | 41 | [Creatable("Algorithms")]
|
---|
| 42 | [StorableClass]
|
---|
| 43 | public sealed class RAPGA : HeuristicOptimizationEngineAlgorithm, IStorableContent {
|
---|
| 44 | public string Filename { get; set; }
|
---|
| 45 |
|
---|
| 46 | #region Problem Properties
|
---|
| 47 | public override Type ProblemType {
|
---|
| 48 | get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
|
---|
| 49 | }
|
---|
| 50 | public new ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
| 51 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
| 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 | }
|
---|
[8330] | 66 | private IValueParameter<IntValue> MinimumPopulationSizeParameter {
|
---|
| 67 | get { return (IValueParameter<IntValue>)Parameters["MinimumPopulationSize"]; }
|
---|
[8349] | 68 | }
|
---|
[8330] | 69 | private IValueParameter<IntValue> MaximumPopulationSizeParameter {
|
---|
[8349] | 70 | get { return (IValueParameter<IntValue>)Parameters["MaximumPopulationSize"]; }
|
---|
[8330] | 71 | }
|
---|
| 72 | private IValueParameter<DoubleValue> ComparisonFactorParameter {
|
---|
| 73 | get { return (IValueParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
|
---|
| 74 | }
|
---|
| 75 | private IValueParameter<IntValue> EffortParameter {
|
---|
| 76 | get { return (IValueParameter<IntValue>)Parameters["Effort"]; }
|
---|
| 77 | }
|
---|
[8377] | 78 | private IValueParameter<IntValue> BatchSizeParameter {
|
---|
| 79 | get { return (IValueParameter<IntValue>)Parameters["BatchSize"]; }
|
---|
| 80 | }
|
---|
[8313] | 81 | public IConstrainedValueParameter<ISelector> SelectorParameter {
|
---|
| 82 | get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
|
---|
| 83 | }
|
---|
| 84 | public IConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
| 85 | get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
| 86 | }
|
---|
| 87 | private ValueParameter<PercentValue> MutationProbabilityParameter {
|
---|
| 88 | get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
| 89 | }
|
---|
| 90 | public IConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
| 91 | get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
| 92 | }
|
---|
| 93 | private ValueParameter<IntValue> ElitesParameter {
|
---|
| 94 | get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
|
---|
| 95 | }
|
---|
[9569] | 96 | private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
|
---|
| 97 | get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
|
---|
| 98 | }
|
---|
[8313] | 99 | private ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
| 100 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
| 101 | }
|
---|
| 102 | private ValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 103 | get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 104 | }
|
---|
[8407] | 105 | public IConstrainedValueParameter<ISingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
|
---|
| 106 | get { return (IConstrainedValueParameter<ISingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
|
---|
[8349] | 107 | }
|
---|
[8313] | 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 PopulationSize {
|
---|
| 120 | get { return PopulationSizeParameter.Value; }
|
---|
| 121 | set { PopulationSizeParameter.Value = value; }
|
---|
| 122 | }
|
---|
[8330] | 123 | public IntValue MinimumPopulationSize {
|
---|
| 124 | get { return MinimumPopulationSizeParameter.Value; }
|
---|
| 125 | set { MinimumPopulationSizeParameter.Value = value; }
|
---|
| 126 | }
|
---|
| 127 | public IntValue MaximumPopulationSize {
|
---|
| 128 | get { return MaximumPopulationSizeParameter.Value; }
|
---|
| 129 | set { MaximumPopulationSizeParameter.Value = value; }
|
---|
| 130 | }
|
---|
| 131 | public DoubleValue ComparisonFactor {
|
---|
| 132 | get { return ComparisonFactorParameter.Value; }
|
---|
| 133 | set { ComparisonFactorParameter.Value = value; }
|
---|
| 134 | }
|
---|
| 135 | public IntValue Effort {
|
---|
| 136 | get { return EffortParameter.Value; }
|
---|
| 137 | set { EffortParameter.Value = value; }
|
---|
| 138 | }
|
---|
[8377] | 139 | public IntValue BatchSize {
|
---|
| 140 | get { return BatchSizeParameter.Value; }
|
---|
| 141 | set { BatchSizeParameter.Value = value; }
|
---|
| 142 | }
|
---|
[8313] | 143 | public ISelector Selector {
|
---|
| 144 | get { return SelectorParameter.Value; }
|
---|
| 145 | set { SelectorParameter.Value = value; }
|
---|
| 146 | }
|
---|
| 147 | public ICrossover Crossover {
|
---|
| 148 | get { return CrossoverParameter.Value; }
|
---|
| 149 | set { CrossoverParameter.Value = value; }
|
---|
| 150 | }
|
---|
| 151 | public PercentValue MutationProbability {
|
---|
| 152 | get { return MutationProbabilityParameter.Value; }
|
---|
| 153 | set { MutationProbabilityParameter.Value = value; }
|
---|
| 154 | }
|
---|
| 155 | public IManipulator Mutator {
|
---|
| 156 | get { return MutatorParameter.Value; }
|
---|
| 157 | set { MutatorParameter.Value = value; }
|
---|
| 158 | }
|
---|
| 159 | public IntValue Elites {
|
---|
| 160 | get { return ElitesParameter.Value; }
|
---|
| 161 | set { ElitesParameter.Value = value; }
|
---|
| 162 | }
|
---|
[9569] | 163 | public bool ReevaluteElites {
|
---|
| 164 | get { return ReevaluateElitesParameter.Value.Value; }
|
---|
| 165 | set { ReevaluateElitesParameter.Value.Value = value; }
|
---|
| 166 | }
|
---|
[8313] | 167 | public MultiAnalyzer Analyzer {
|
---|
| 168 | get { return AnalyzerParameter.Value; }
|
---|
| 169 | set { AnalyzerParameter.Value = value; }
|
---|
| 170 | }
|
---|
| 171 | public IntValue MaximumGenerations {
|
---|
| 172 | get { return MaximumGenerationsParameter.Value; }
|
---|
| 173 | set { MaximumGenerationsParameter.Value = value; }
|
---|
| 174 | }
|
---|
[8407] | 175 | public ISingleObjectiveSolutionSimilarityCalculator SimilarityCalculator {
|
---|
[8349] | 176 | get { return SimilarityCalculatorParameter.Value; }
|
---|
| 177 | set { SimilarityCalculatorParameter.Value = value; }
|
---|
| 178 | }
|
---|
[8313] | 179 | private RandomCreator RandomCreator {
|
---|
| 180 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
| 181 | }
|
---|
| 182 | private SolutionsCreator SolutionsCreator {
|
---|
| 183 | get { return (SolutionsCreator)RandomCreator.Successor; }
|
---|
| 184 | }
|
---|
| 185 | private RAPGAMainLoop RAPGAMainLoop {
|
---|
| 186 | get { return FindMainLoop(SolutionsCreator.Successor); }
|
---|
| 187 | }
|
---|
| 188 | [Storable]
|
---|
| 189 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
[8377] | 190 | [Storable]
|
---|
| 191 | private PopulationSizeAnalyzer populationSizeAnalyzer;
|
---|
[8378] | 192 | [Storable]
|
---|
| 193 | private OffspringSuccessAnalyzer offspringSuccessAnalyzer;
|
---|
[8385] | 194 | [Storable]
|
---|
| 195 | private SelectionPressureAnalyzer selectionPressureAnalyzer;
|
---|
[8313] | 196 | #endregion
|
---|
| 197 |
|
---|
| 198 | [StorableConstructor]
|
---|
| 199 | private RAPGA(bool deserializing) : base(deserializing) { }
|
---|
| 200 | [StorableHook(HookType.AfterDeserialization)]
|
---|
[9569] | 201 | private void AfterDeserialization() {
|
---|
[9592] | 202 | // BackwardsCompatibility3.3
|
---|
[9591] | 203 | #region Backwards compatible code, remove with 3.4
|
---|
[9569] | 204 | if (!Parameters.ContainsKey("ReevaluateElites")) {
|
---|
| 205 | 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 });
|
---|
| 206 | }
|
---|
[9591] | 207 | #endregion
|
---|
[9569] | 208 | Initialize();
|
---|
| 209 | }
|
---|
[8313] | 210 | private RAPGA(RAPGA original, Cloner cloner)
|
---|
| 211 | : base(original, cloner) {
|
---|
| 212 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
[8377] | 213 | populationSizeAnalyzer = cloner.Clone(original.populationSizeAnalyzer);
|
---|
[8378] | 214 | offspringSuccessAnalyzer = cloner.Clone(original.offspringSuccessAnalyzer);
|
---|
[8385] | 215 | selectionPressureAnalyzer = cloner.Clone(original.selectionPressureAnalyzer);
|
---|
[8313] | 216 | Initialize();
|
---|
| 217 | }
|
---|
| 218 | public RAPGA()
|
---|
| 219 | : base() {
|
---|
| 220 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 221 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
[8359] | 222 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
|
---|
[8330] | 223 | Parameters.Add(new ValueParameter<IntValue>("MinimumPopulationSize", "The minimum size of the population of solutions.", new IntValue(2)));
|
---|
[8377] | 224 | Parameters.Add(new ValueParameter<IntValue>("MaximumPopulationSize", "The maximum size of the population of solutions.", new IntValue(300)));
|
---|
[8330] | 225 | Parameters.Add(new ValueParameter<DoubleValue>("ComparisonFactor", "The comparison factor.", new DoubleValue(0.0)));
|
---|
[8377] | 226 | Parameters.Add(new ValueParameter<IntValue>("Effort", "The maximum number of offspring created in each generation.", new IntValue(1000)));
|
---|
| 227 | Parameters.Add(new ValueParameter<IntValue>("BatchSize", "The number of children that should be created during one iteration of the offspring creation process.", new IntValue(10)));
|
---|
[8313] | 228 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
| 229 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
| 230 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
| 231 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 232 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
[9569] | 233 | 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 });
|
---|
[8313] | 234 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
|
---|
| 235 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
|
---|
[8407] | 236 | Parameters.Add(new ConstrainedValueParameter<ISingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions."));
|
---|
[8313] | 237 |
|
---|
| 238 | RandomCreator randomCreator = new RandomCreator();
|
---|
| 239 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
| 240 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
| 241 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
| 242 | RAPGAMainLoop mainLoop = new RAPGAMainLoop();
|
---|
| 243 | OperatorGraph.InitialOperator = randomCreator;
|
---|
| 244 |
|
---|
| 245 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
| 246 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
| 247 | randomCreator.SeedParameter.Value = null;
|
---|
| 248 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
| 249 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
| 250 | randomCreator.Successor = solutionsCreator;
|
---|
| 251 |
|
---|
| 252 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 253 | solutionsCreator.Successor = subScopesCounter;
|
---|
| 254 |
|
---|
| 255 | subScopesCounter.Name = "Initialize EvaluatedSolutions";
|
---|
| 256 | subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
| 257 | subScopesCounter.Successor = resultsCollector;
|
---|
| 258 |
|
---|
| 259 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
| 260 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
| 261 | resultsCollector.Successor = mainLoop;
|
---|
| 262 |
|
---|
| 263 | mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
| 264 | mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
| 265 | mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
[9569] | 266 | mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
|
---|
[8313] | 267 | mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
| 268 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
| 269 | mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
| 270 | mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 271 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 272 | mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
| 273 | mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 274 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
| 275 |
|
---|
| 276 | foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
|
---|
| 277 | SelectorParameter.ValidValues.Add(selector);
|
---|
| 278 | ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
|
---|
| 279 | if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
|
---|
| 280 | ParameterizeSelectors();
|
---|
| 281 |
|
---|
| 282 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
[8377] | 283 | populationSizeAnalyzer = new PopulationSizeAnalyzer();
|
---|
[8378] | 284 | offspringSuccessAnalyzer = new OffspringSuccessAnalyzer();
|
---|
[8385] | 285 | selectionPressureAnalyzer = new SelectionPressureAnalyzer();
|
---|
[8313] | 286 | ParameterizeAnalyzers();
|
---|
| 287 | UpdateAnalyzers();
|
---|
| 288 |
|
---|
| 289 | Initialize();
|
---|
| 290 | }
|
---|
| 291 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 292 | return new RAPGA(this, cloner);
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | public override void Prepare() {
|
---|
[8349] | 296 | if (Problem != null && SimilarityCalculator != null) base.Prepare();
|
---|
[8313] | 297 | }
|
---|
| 298 |
|
---|
| 299 | #region Events
|
---|
| 300 | protected override void OnProblemChanged() {
|
---|
| 301 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 302 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 303 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
| 304 | ParameterizeSolutionsCreator();
|
---|
| 305 | ParameterizeSelectors();
|
---|
| 306 | ParameterizeAnalyzers();
|
---|
| 307 | ParameterizeIterationBasedOperators();
|
---|
| 308 | UpdateCrossovers();
|
---|
| 309 | UpdateMutators();
|
---|
| 310 | UpdateAnalyzers();
|
---|
[8349] | 311 | UpdateSimilarityCalculators();
|
---|
| 312 | ParameterizeRAPGAMainLoop();
|
---|
[8313] | 313 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 314 | base.OnProblemChanged();
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
| 318 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 319 | ParameterizeSolutionsCreator();
|
---|
| 320 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
| 321 | }
|
---|
| 322 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
| 323 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 324 | ParameterizeSolutionsCreator();
|
---|
[8349] | 325 | ParameterizeRAPGAMainLoop();
|
---|
[8313] | 326 | ParameterizeSelectors();
|
---|
| 327 | ParameterizeAnalyzers();
|
---|
| 328 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 329 | base.Problem_EvaluatorChanged(sender, e);
|
---|
| 330 | }
|
---|
| 331 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
[8349] | 332 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
[8313] | 333 | ParameterizeIterationBasedOperators();
|
---|
| 334 | UpdateCrossovers();
|
---|
| 335 | UpdateMutators();
|
---|
| 336 | UpdateAnalyzers();
|
---|
[8349] | 337 | UpdateSimilarityCalculators();
|
---|
| 338 | ParameterizeRAPGAMainLoop();
|
---|
[8313] | 339 | base.Problem_OperatorsChanged(sender, e);
|
---|
| 340 | }
|
---|
[8406] | 341 | private void SimilarityCalculatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 342 | ParameterizeRAPGAMainLoop();
|
---|
| 343 | }
|
---|
| 344 | private void BatchSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[8377] | 345 | BatchSize.ValueChanged += new EventHandler(BatchSize_ValueChanged);
|
---|
[8359] | 346 | ParameterizeSelectors();
|
---|
| 347 | }
|
---|
[8406] | 348 | private void BatchSize_ValueChanged(object sender, EventArgs e) {
|
---|
[8359] | 349 | ParameterizeSelectors();
|
---|
| 350 | }
|
---|
[8313] | 351 | private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 352 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
| 353 | ParameterizeSelectors();
|
---|
| 354 | }
|
---|
| 355 | private void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
| 356 | ParameterizeSelectors();
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 360 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 361 | ParameterizeSelectors();
|
---|
| 362 | }
|
---|
| 363 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
| 364 | ParameterizeSelectors();
|
---|
| 365 | }
|
---|
| 366 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
[8349] | 367 | ParameterizeRAPGAMainLoop();
|
---|
[8313] | 368 | ParameterizeSelectors();
|
---|
| 369 | ParameterizeAnalyzers();
|
---|
[8407] | 370 | ParameterizeSimilarityCalculators();
|
---|
[8313] | 371 | }
|
---|
| 372 | #endregion
|
---|
| 373 |
|
---|
| 374 | #region Helpers
|
---|
| 375 | private void Initialize() {
|
---|
| 376 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
| 377 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 378 | ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
|
---|
| 379 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
[8377] | 380 | BatchSizeParameter.ValueChanged += new EventHandler(BatchSizeParameter_ValueChanged);
|
---|
| 381 | BatchSize.ValueChanged += new EventHandler(BatchSize_ValueChanged);
|
---|
[8406] | 382 | SimilarityCalculatorParameter.ValueChanged += new EventHandler(SimilarityCalculatorParameter_ValueChanged);
|
---|
[8313] | 383 | if (Problem != null) {
|
---|
| 384 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 385 | }
|
---|
| 386 | }
|
---|
| 387 |
|
---|
| 388 | private void ParameterizeSolutionsCreator() {
|
---|
| 389 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 390 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
| 391 | }
|
---|
[8349] | 392 | private void ParameterizeRAPGAMainLoop() {
|
---|
[8313] | 393 | RAPGAMainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 394 | RAPGAMainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 395 | RAPGAMainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[8349] | 396 | foreach (ISimilarityBasedOperator op in RAPGAMainLoop.OperatorGraph.Operators.OfType<ISimilarityBasedOperator>())
|
---|
| 397 | op.SimilarityCalculator = SimilarityCalculator;
|
---|
[8313] | 398 | }
|
---|
| 399 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
| 400 | IStochasticOperator stochasticOp = op as IStochasticOperator;
|
---|
| 401 | if (stochasticOp != null) {
|
---|
| 402 | stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 403 | stochasticOp.RandomParameter.Hidden = true;
|
---|
| 404 | }
|
---|
| 405 | }
|
---|
| 406 | private void ParameterizeSelectors() {
|
---|
| 407 | foreach (ISelector selector in SelectorParameter.ValidValues) {
|
---|
| 408 | selector.CopySelected = new BoolValue(true);
|
---|
[8377] | 409 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * BatchSize.Value);
|
---|
[8313] | 410 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
| 411 | ParameterizeStochasticOperator(selector);
|
---|
| 412 | }
|
---|
| 413 | if (Problem != null) {
|
---|
| 414 | foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
| 415 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 416 | selector.MaximizationParameter.Hidden = true;
|
---|
| 417 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 418 | selector.QualityParameter.Hidden = true;
|
---|
| 419 | }
|
---|
| 420 | }
|
---|
| 421 | }
|
---|
| 422 | private void ParameterizeAnalyzers() {
|
---|
| 423 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 424 | qualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
[8377] | 425 | populationSizeAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 426 | populationSizeAnalyzer.ResultsParameter.Hidden = true;
|
---|
[8378] | 427 | offspringSuccessAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 428 | offspringSuccessAnalyzer.ResultsParameter.Hidden = true;
|
---|
[8385] | 429 | selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 430 | selectionPressureAnalyzer.ResultsParameter.Hidden = true;
|
---|
[8313] | 431 | if (Problem != null) {
|
---|
| 432 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 433 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
| 434 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 435 | qualityAnalyzer.QualityParameter.Depth = 1;
|
---|
| 436 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
| 437 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 438 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
| 439 | }
|
---|
| 440 | }
|
---|
| 441 | private void ParameterizeIterationBasedOperators() {
|
---|
| 442 | if (Problem != null) {
|
---|
| 443 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
| 444 | op.IterationsParameter.ActualName = "Generations";
|
---|
| 445 | op.IterationsParameter.Hidden = true;
|
---|
| 446 | op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
|
---|
| 447 | op.MaximumIterationsParameter.Hidden = true;
|
---|
| 448 | }
|
---|
| 449 | }
|
---|
| 450 | }
|
---|
[8407] | 451 | private void ParameterizeSimilarityCalculators() {
|
---|
| 452 | foreach (ISingleObjectiveSolutionSimilarityCalculator calc in SimilarityCalculatorParameter.ValidValues) {
|
---|
| 453 | calc.QualityVariableName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 454 | }
|
---|
| 455 | }
|
---|
[8313] | 456 | private void UpdateCrossovers() {
|
---|
| 457 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
| 458 | CrossoverParameter.ValidValues.Clear();
|
---|
| 459 | ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
|
---|
| 460 |
|
---|
| 461 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
|
---|
| 462 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
| 463 |
|
---|
| 464 | if (oldCrossover != null) {
|
---|
| 465 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
| 466 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
| 467 | else oldCrossover = null;
|
---|
| 468 | }
|
---|
| 469 | if (oldCrossover == null && defaultCrossover != null)
|
---|
| 470 | CrossoverParameter.Value = defaultCrossover;
|
---|
| 471 | }
|
---|
| 472 | private void UpdateMutators() {
|
---|
| 473 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
| 474 | MutatorParameter.ValidValues.Clear();
|
---|
| 475 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
| 476 | MutatorParameter.ValidValues.Add(mutator);
|
---|
| 477 | if (oldMutator != null) {
|
---|
| 478 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
| 479 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
| 480 | }
|
---|
| 481 | }
|
---|
| 482 | private void UpdateAnalyzers() {
|
---|
| 483 | Analyzer.Operators.Clear();
|
---|
| 484 | if (Problem != null) {
|
---|
| 485 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
| 486 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 487 | param.Depth = 1;
|
---|
| 488 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
| 489 | }
|
---|
| 490 | }
|
---|
| 491 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
[8377] | 492 | Analyzer.Operators.Add(populationSizeAnalyzer, populationSizeAnalyzer.EnabledByDefault);
|
---|
[8378] | 493 | Analyzer.Operators.Add(offspringSuccessAnalyzer, offspringSuccessAnalyzer.EnabledByDefault);
|
---|
[8385] | 494 | Analyzer.Operators.Add(selectionPressureAnalyzer, selectionPressureAnalyzer.EnabledByDefault);
|
---|
[8313] | 495 | }
|
---|
[8349] | 496 | private void UpdateSimilarityCalculators() {
|
---|
[8407] | 497 | ISingleObjectiveSolutionSimilarityCalculator oldSimilarityCalculator = SimilarityCalculatorParameter.Value;
|
---|
[8349] | 498 | SimilarityCalculatorParameter.ValidValues.Clear();
|
---|
[8407] | 499 | ISingleObjectiveSolutionSimilarityCalculator defaultSimilarityCalculator = Problem.Operators.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
|
---|
[8349] | 500 |
|
---|
[8407] | 501 | SimilarityCalculatorParameter.ValidValues.Add(new QualitySimilarityCalculator { QualityVariableName = Problem.Evaluator.QualityParameter.ActualName });
|
---|
[8622] | 502 | SimilarityCalculatorParameter.ValidValues.Add(new NoSimilarityCalculator());
|
---|
[8406] | 503 |
|
---|
[8407] | 504 | foreach (ISingleObjectiveSolutionSimilarityCalculator similarityCalculator in Problem.Operators.OfType<ISingleObjectiveSolutionSimilarityCalculator>())
|
---|
[8349] | 505 | SimilarityCalculatorParameter.ValidValues.Add(similarityCalculator);
|
---|
| 506 |
|
---|
| 507 | if (oldSimilarityCalculator != null) {
|
---|
[8407] | 508 | ISingleObjectiveSolutionSimilarityCalculator similarityCalculator = SimilarityCalculatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSimilarityCalculator.GetType());
|
---|
[8349] | 509 | if (similarityCalculator != null) SimilarityCalculatorParameter.Value = similarityCalculator;
|
---|
| 510 | else oldSimilarityCalculator = null;
|
---|
| 511 | }
|
---|
| 512 | if (oldSimilarityCalculator == null && defaultSimilarityCalculator != null)
|
---|
| 513 | SimilarityCalculatorParameter.Value = defaultSimilarityCalculator;
|
---|
| 514 | }
|
---|
[8313] | 515 | private RAPGAMainLoop FindMainLoop(IOperator start) {
|
---|
| 516 | IOperator mainLoop = start;
|
---|
| 517 | while (mainLoop != null && !(mainLoop is RAPGAMainLoop))
|
---|
| 518 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
| 519 | if (mainLoop == null) return null;
|
---|
| 520 | else return (RAPGAMainLoop)mainLoop;
|
---|
| 521 | }
|
---|
| 522 | #endregion
|
---|
| 523 | }
|
---|
| 524 | }
|
---|