[8313] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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;
|
---|
| 396 | }
|
---|
| 397 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
| 398 | IStochasticOperator stochasticOp = op as IStochasticOperator;
|
---|
| 399 | if (stochasticOp != null) {
|
---|
| 400 | stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 401 | stochasticOp.RandomParameter.Hidden = true;
|
---|
| 402 | }
|
---|
| 403 | }
|
---|
| 404 | private void ParameterizeSelectors() {
|
---|
| 405 | foreach (ISelector selector in SelectorParameter.ValidValues) {
|
---|
| 406 | selector.CopySelected = new BoolValue(true);
|
---|
[8377] | 407 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * BatchSize.Value);
|
---|
[8313] | 408 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
| 409 | ParameterizeStochasticOperator(selector);
|
---|
| 410 | }
|
---|
| 411 | if (Problem != null) {
|
---|
| 412 | foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
| 413 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 414 | selector.MaximizationParameter.Hidden = true;
|
---|
| 415 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 416 | selector.QualityParameter.Hidden = true;
|
---|
| 417 | }
|
---|
| 418 | }
|
---|
| 419 | }
|
---|
| 420 | private void ParameterizeAnalyzers() {
|
---|
| 421 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 422 | qualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
[8377] | 423 | populationSizeAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 424 | populationSizeAnalyzer.ResultsParameter.Hidden = true;
|
---|
[8378] | 425 | offspringSuccessAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 426 | offspringSuccessAnalyzer.ResultsParameter.Hidden = true;
|
---|
[8385] | 427 | selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 428 | selectionPressureAnalyzer.ResultsParameter.Hidden = true;
|
---|
[8313] | 429 | if (Problem != null) {
|
---|
| 430 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 431 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
| 432 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 433 | qualityAnalyzer.QualityParameter.Depth = 1;
|
---|
| 434 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
| 435 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 436 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
| 437 | }
|
---|
| 438 | }
|
---|
| 439 | private void ParameterizeIterationBasedOperators() {
|
---|
| 440 | if (Problem != null) {
|
---|
| 441 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
| 442 | op.IterationsParameter.ActualName = "Generations";
|
---|
| 443 | op.IterationsParameter.Hidden = true;
|
---|
| 444 | op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
|
---|
| 445 | op.MaximumIterationsParameter.Hidden = true;
|
---|
| 446 | }
|
---|
| 447 | }
|
---|
| 448 | }
|
---|
[8407] | 449 | private void ParameterizeSimilarityCalculators() {
|
---|
| 450 | foreach (ISingleObjectiveSolutionSimilarityCalculator calc in SimilarityCalculatorParameter.ValidValues) {
|
---|
| 451 | calc.QualityVariableName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 452 | }
|
---|
| 453 | }
|
---|
[8313] | 454 | private void UpdateCrossovers() {
|
---|
| 455 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
| 456 | CrossoverParameter.ValidValues.Clear();
|
---|
| 457 | ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
|
---|
| 458 |
|
---|
| 459 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
|
---|
| 460 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
| 461 |
|
---|
| 462 | if (oldCrossover != null) {
|
---|
| 463 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
| 464 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
| 465 | else oldCrossover = null;
|
---|
| 466 | }
|
---|
| 467 | if (oldCrossover == null && defaultCrossover != null)
|
---|
| 468 | CrossoverParameter.Value = defaultCrossover;
|
---|
| 469 | }
|
---|
| 470 | private void UpdateMutators() {
|
---|
| 471 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
| 472 | MutatorParameter.ValidValues.Clear();
|
---|
| 473 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
| 474 | MutatorParameter.ValidValues.Add(mutator);
|
---|
| 475 | if (oldMutator != null) {
|
---|
| 476 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
| 477 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
| 478 | }
|
---|
| 479 | }
|
---|
| 480 | private void UpdateAnalyzers() {
|
---|
| 481 | Analyzer.Operators.Clear();
|
---|
| 482 | if (Problem != null) {
|
---|
| 483 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
| 484 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 485 | param.Depth = 1;
|
---|
| 486 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
| 487 | }
|
---|
| 488 | }
|
---|
| 489 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
[8377] | 490 | Analyzer.Operators.Add(populationSizeAnalyzer, populationSizeAnalyzer.EnabledByDefault);
|
---|
[8378] | 491 | Analyzer.Operators.Add(offspringSuccessAnalyzer, offspringSuccessAnalyzer.EnabledByDefault);
|
---|
[8385] | 492 | Analyzer.Operators.Add(selectionPressureAnalyzer, selectionPressureAnalyzer.EnabledByDefault);
|
---|
[8313] | 493 | }
|
---|
[8349] | 494 | private void UpdateSimilarityCalculators() {
|
---|
[8407] | 495 | ISingleObjectiveSolutionSimilarityCalculator oldSimilarityCalculator = SimilarityCalculatorParameter.Value;
|
---|
[8349] | 496 | SimilarityCalculatorParameter.ValidValues.Clear();
|
---|
[8407] | 497 | ISingleObjectiveSolutionSimilarityCalculator defaultSimilarityCalculator = Problem.Operators.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
|
---|
[8349] | 498 |
|
---|
[8407] | 499 | SimilarityCalculatorParameter.ValidValues.Add(new QualitySimilarityCalculator { QualityVariableName = Problem.Evaluator.QualityParameter.ActualName });
|
---|
[8622] | 500 | SimilarityCalculatorParameter.ValidValues.Add(new NoSimilarityCalculator());
|
---|
[8406] | 501 |
|
---|
[8407] | 502 | foreach (ISingleObjectiveSolutionSimilarityCalculator similarityCalculator in Problem.Operators.OfType<ISingleObjectiveSolutionSimilarityCalculator>())
|
---|
[8349] | 503 | SimilarityCalculatorParameter.ValidValues.Add(similarityCalculator);
|
---|
| 504 |
|
---|
| 505 | if (oldSimilarityCalculator != null) {
|
---|
[8407] | 506 | ISingleObjectiveSolutionSimilarityCalculator similarityCalculator = SimilarityCalculatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSimilarityCalculator.GetType());
|
---|
[8349] | 507 | if (similarityCalculator != null) SimilarityCalculatorParameter.Value = similarityCalculator;
|
---|
| 508 | else oldSimilarityCalculator = null;
|
---|
| 509 | }
|
---|
| 510 | if (oldSimilarityCalculator == null && defaultSimilarityCalculator != null)
|
---|
| 511 | SimilarityCalculatorParameter.Value = defaultSimilarityCalculator;
|
---|
| 512 | }
|
---|
[8313] | 513 | private RAPGAMainLoop FindMainLoop(IOperator start) {
|
---|
| 514 | IOperator mainLoop = start;
|
---|
| 515 | while (mainLoop != null && !(mainLoop is RAPGAMainLoop))
|
---|
| 516 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
| 517 | if (mainLoop == null) return null;
|
---|
| 518 | else return (RAPGAMainLoop)mainLoop;
|
---|
| 519 | }
|
---|
| 520 | #endregion
|
---|
| 521 | }
|
---|
| 522 | }
|
---|