[8313] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9539] | 3 | * Copyright (C) 2002-2013 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 | }
|
---|
| 96 | private ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
| 97 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
| 98 | }
|
---|
| 99 | private ValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 100 | get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 101 | }
|
---|
[8407] | 102 | public IConstrainedValueParameter<ISingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
|
---|
| 103 | get { return (IConstrainedValueParameter<ISingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
|
---|
[8349] | 104 | }
|
---|
[8313] | 105 | #endregion
|
---|
| 106 |
|
---|
| 107 | #region Properties
|
---|
| 108 | public IntValue Seed {
|
---|
| 109 | get { return SeedParameter.Value; }
|
---|
| 110 | set { SeedParameter.Value = value; }
|
---|
| 111 | }
|
---|
| 112 | public BoolValue SetSeedRandomly {
|
---|
| 113 | get { return SetSeedRandomlyParameter.Value; }
|
---|
| 114 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
| 115 | }
|
---|
| 116 | public IntValue PopulationSize {
|
---|
| 117 | get { return PopulationSizeParameter.Value; }
|
---|
| 118 | set { PopulationSizeParameter.Value = value; }
|
---|
| 119 | }
|
---|
[8330] | 120 | public IntValue MinimumPopulationSize {
|
---|
| 121 | get { return MinimumPopulationSizeParameter.Value; }
|
---|
| 122 | set { MinimumPopulationSizeParameter.Value = value; }
|
---|
| 123 | }
|
---|
| 124 | public IntValue MaximumPopulationSize {
|
---|
| 125 | get { return MaximumPopulationSizeParameter.Value; }
|
---|
| 126 | set { MaximumPopulationSizeParameter.Value = value; }
|
---|
| 127 | }
|
---|
| 128 | public DoubleValue ComparisonFactor {
|
---|
| 129 | get { return ComparisonFactorParameter.Value; }
|
---|
| 130 | set { ComparisonFactorParameter.Value = value; }
|
---|
| 131 | }
|
---|
| 132 | public IntValue Effort {
|
---|
| 133 | get { return EffortParameter.Value; }
|
---|
| 134 | set { EffortParameter.Value = value; }
|
---|
| 135 | }
|
---|
[8377] | 136 | public IntValue BatchSize {
|
---|
| 137 | get { return BatchSizeParameter.Value; }
|
---|
| 138 | set { BatchSizeParameter.Value = value; }
|
---|
| 139 | }
|
---|
[8313] | 140 | public ISelector Selector {
|
---|
| 141 | get { return SelectorParameter.Value; }
|
---|
| 142 | set { SelectorParameter.Value = value; }
|
---|
| 143 | }
|
---|
| 144 | public ICrossover Crossover {
|
---|
| 145 | get { return CrossoverParameter.Value; }
|
---|
| 146 | set { CrossoverParameter.Value = value; }
|
---|
| 147 | }
|
---|
| 148 | public PercentValue MutationProbability {
|
---|
| 149 | get { return MutationProbabilityParameter.Value; }
|
---|
| 150 | set { MutationProbabilityParameter.Value = value; }
|
---|
| 151 | }
|
---|
| 152 | public IManipulator Mutator {
|
---|
| 153 | get { return MutatorParameter.Value; }
|
---|
| 154 | set { MutatorParameter.Value = value; }
|
---|
| 155 | }
|
---|
| 156 | public IntValue Elites {
|
---|
| 157 | get { return ElitesParameter.Value; }
|
---|
| 158 | set { ElitesParameter.Value = value; }
|
---|
| 159 | }
|
---|
| 160 | public MultiAnalyzer Analyzer {
|
---|
| 161 | get { return AnalyzerParameter.Value; }
|
---|
| 162 | set { AnalyzerParameter.Value = value; }
|
---|
| 163 | }
|
---|
| 164 | public IntValue MaximumGenerations {
|
---|
| 165 | get { return MaximumGenerationsParameter.Value; }
|
---|
| 166 | set { MaximumGenerationsParameter.Value = value; }
|
---|
| 167 | }
|
---|
[8407] | 168 | public ISingleObjectiveSolutionSimilarityCalculator SimilarityCalculator {
|
---|
[8349] | 169 | get { return SimilarityCalculatorParameter.Value; }
|
---|
| 170 | set { SimilarityCalculatorParameter.Value = value; }
|
---|
| 171 | }
|
---|
[8313] | 172 | private RandomCreator RandomCreator {
|
---|
| 173 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
| 174 | }
|
---|
| 175 | private SolutionsCreator SolutionsCreator {
|
---|
| 176 | get { return (SolutionsCreator)RandomCreator.Successor; }
|
---|
| 177 | }
|
---|
| 178 | private RAPGAMainLoop RAPGAMainLoop {
|
---|
| 179 | get { return FindMainLoop(SolutionsCreator.Successor); }
|
---|
| 180 | }
|
---|
| 181 | [Storable]
|
---|
| 182 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
[8377] | 183 | [Storable]
|
---|
| 184 | private PopulationSizeAnalyzer populationSizeAnalyzer;
|
---|
[8378] | 185 | [Storable]
|
---|
| 186 | private OffspringSuccessAnalyzer offspringSuccessAnalyzer;
|
---|
[8385] | 187 | [Storable]
|
---|
| 188 | private SelectionPressureAnalyzer selectionPressureAnalyzer;
|
---|
[8313] | 189 | #endregion
|
---|
| 190 |
|
---|
| 191 | [StorableConstructor]
|
---|
| 192 | private RAPGA(bool deserializing) : base(deserializing) { }
|
---|
| 193 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 194 | private void AfterDeserialization() { Initialize(); }
|
---|
| 195 | private RAPGA(RAPGA original, Cloner cloner)
|
---|
| 196 | : base(original, cloner) {
|
---|
| 197 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
[8377] | 198 | populationSizeAnalyzer = cloner.Clone(original.populationSizeAnalyzer);
|
---|
[8378] | 199 | offspringSuccessAnalyzer = cloner.Clone(original.offspringSuccessAnalyzer);
|
---|
[8385] | 200 | selectionPressureAnalyzer = cloner.Clone(original.selectionPressureAnalyzer);
|
---|
[8313] | 201 | Initialize();
|
---|
| 202 | }
|
---|
| 203 | public RAPGA()
|
---|
| 204 | : base() {
|
---|
| 205 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 206 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
[8359] | 207 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
|
---|
[8330] | 208 | Parameters.Add(new ValueParameter<IntValue>("MinimumPopulationSize", "The minimum size of the population of solutions.", new IntValue(2)));
|
---|
[8377] | 209 | Parameters.Add(new ValueParameter<IntValue>("MaximumPopulationSize", "The maximum size of the population of solutions.", new IntValue(300)));
|
---|
[8330] | 210 | Parameters.Add(new ValueParameter<DoubleValue>("ComparisonFactor", "The comparison factor.", new DoubleValue(0.0)));
|
---|
[8377] | 211 | Parameters.Add(new ValueParameter<IntValue>("Effort", "The maximum number of offspring created in each generation.", new IntValue(1000)));
|
---|
| 212 | 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] | 213 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
| 214 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
| 215 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
| 216 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 217 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
| 218 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
|
---|
| 219 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
|
---|
[8407] | 220 | Parameters.Add(new ConstrainedValueParameter<ISingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions."));
|
---|
[8313] | 221 |
|
---|
| 222 | RandomCreator randomCreator = new RandomCreator();
|
---|
| 223 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
| 224 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
| 225 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
| 226 | RAPGAMainLoop mainLoop = new RAPGAMainLoop();
|
---|
| 227 | OperatorGraph.InitialOperator = randomCreator;
|
---|
| 228 |
|
---|
| 229 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
| 230 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
| 231 | randomCreator.SeedParameter.Value = null;
|
---|
| 232 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
| 233 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
| 234 | randomCreator.Successor = solutionsCreator;
|
---|
| 235 |
|
---|
| 236 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 237 | solutionsCreator.Successor = subScopesCounter;
|
---|
| 238 |
|
---|
| 239 | subScopesCounter.Name = "Initialize EvaluatedSolutions";
|
---|
| 240 | subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
| 241 | subScopesCounter.Successor = resultsCollector;
|
---|
| 242 |
|
---|
| 243 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
| 244 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
| 245 | resultsCollector.Successor = mainLoop;
|
---|
| 246 |
|
---|
| 247 | mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
| 248 | mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
| 249 | mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
| 250 | mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
| 251 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
| 252 | mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
| 253 | mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 254 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 255 | mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
| 256 | mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 257 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
| 258 |
|
---|
| 259 | foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
|
---|
| 260 | SelectorParameter.ValidValues.Add(selector);
|
---|
| 261 | ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
|
---|
| 262 | if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
|
---|
| 263 | ParameterizeSelectors();
|
---|
| 264 |
|
---|
| 265 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
[8377] | 266 | populationSizeAnalyzer = new PopulationSizeAnalyzer();
|
---|
[8378] | 267 | offspringSuccessAnalyzer = new OffspringSuccessAnalyzer();
|
---|
[8385] | 268 | selectionPressureAnalyzer = new SelectionPressureAnalyzer();
|
---|
[8313] | 269 | ParameterizeAnalyzers();
|
---|
| 270 | UpdateAnalyzers();
|
---|
| 271 |
|
---|
| 272 | Initialize();
|
---|
| 273 | }
|
---|
| 274 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 275 | return new RAPGA(this, cloner);
|
---|
| 276 | }
|
---|
| 277 |
|
---|
| 278 | public override void Prepare() {
|
---|
[8349] | 279 | if (Problem != null && SimilarityCalculator != null) base.Prepare();
|
---|
[8313] | 280 | }
|
---|
| 281 |
|
---|
| 282 | #region Events
|
---|
| 283 | protected override void OnProblemChanged() {
|
---|
| 284 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 285 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 286 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
| 287 | ParameterizeSolutionsCreator();
|
---|
| 288 | ParameterizeSelectors();
|
---|
| 289 | ParameterizeAnalyzers();
|
---|
| 290 | ParameterizeIterationBasedOperators();
|
---|
| 291 | UpdateCrossovers();
|
---|
| 292 | UpdateMutators();
|
---|
| 293 | UpdateAnalyzers();
|
---|
[8349] | 294 | UpdateSimilarityCalculators();
|
---|
| 295 | ParameterizeRAPGAMainLoop();
|
---|
[8313] | 296 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 297 | base.OnProblemChanged();
|
---|
| 298 | }
|
---|
| 299 |
|
---|
| 300 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
| 301 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 302 | ParameterizeSolutionsCreator();
|
---|
| 303 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
| 304 | }
|
---|
| 305 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
| 306 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
| 307 | ParameterizeSolutionsCreator();
|
---|
[8349] | 308 | ParameterizeRAPGAMainLoop();
|
---|
[8313] | 309 | ParameterizeSelectors();
|
---|
| 310 | ParameterizeAnalyzers();
|
---|
| 311 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 312 | base.Problem_EvaluatorChanged(sender, e);
|
---|
| 313 | }
|
---|
| 314 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
[8349] | 315 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
[8313] | 316 | ParameterizeIterationBasedOperators();
|
---|
| 317 | UpdateCrossovers();
|
---|
| 318 | UpdateMutators();
|
---|
| 319 | UpdateAnalyzers();
|
---|
[8349] | 320 | UpdateSimilarityCalculators();
|
---|
| 321 | ParameterizeRAPGAMainLoop();
|
---|
[8313] | 322 | base.Problem_OperatorsChanged(sender, e);
|
---|
| 323 | }
|
---|
[8406] | 324 | private void SimilarityCalculatorParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 325 | ParameterizeRAPGAMainLoop();
|
---|
| 326 | }
|
---|
| 327 | private void BatchSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[8377] | 328 | BatchSize.ValueChanged += new EventHandler(BatchSize_ValueChanged);
|
---|
[8359] | 329 | ParameterizeSelectors();
|
---|
| 330 | }
|
---|
[8406] | 331 | private void BatchSize_ValueChanged(object sender, EventArgs e) {
|
---|
[8359] | 332 | ParameterizeSelectors();
|
---|
| 333 | }
|
---|
[8313] | 334 | private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 335 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
| 336 | ParameterizeSelectors();
|
---|
| 337 | }
|
---|
| 338 | private void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
| 339 | ParameterizeSelectors();
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 343 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 344 | ParameterizeSelectors();
|
---|
| 345 | }
|
---|
| 346 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
| 347 | ParameterizeSelectors();
|
---|
| 348 | }
|
---|
| 349 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
[8349] | 350 | ParameterizeRAPGAMainLoop();
|
---|
[8313] | 351 | ParameterizeSelectors();
|
---|
| 352 | ParameterizeAnalyzers();
|
---|
[8407] | 353 | ParameterizeSimilarityCalculators();
|
---|
[8313] | 354 | }
|
---|
| 355 | #endregion
|
---|
| 356 |
|
---|
| 357 | #region Helpers
|
---|
| 358 | private void Initialize() {
|
---|
| 359 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
| 360 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 361 | ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
|
---|
| 362 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
[8377] | 363 | BatchSizeParameter.ValueChanged += new EventHandler(BatchSizeParameter_ValueChanged);
|
---|
| 364 | BatchSize.ValueChanged += new EventHandler(BatchSize_ValueChanged);
|
---|
[8406] | 365 | SimilarityCalculatorParameter.ValueChanged += new EventHandler(SimilarityCalculatorParameter_ValueChanged);
|
---|
[8313] | 366 | if (Problem != null) {
|
---|
| 367 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 368 | }
|
---|
| 369 | }
|
---|
| 370 |
|
---|
| 371 | private void ParameterizeSolutionsCreator() {
|
---|
| 372 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 373 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
| 374 | }
|
---|
[8349] | 375 | private void ParameterizeRAPGAMainLoop() {
|
---|
[8313] | 376 | RAPGAMainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 377 | RAPGAMainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 378 | RAPGAMainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[8349] | 379 | foreach (ISimilarityBasedOperator op in RAPGAMainLoop.OperatorGraph.Operators.OfType<ISimilarityBasedOperator>())
|
---|
| 380 | op.SimilarityCalculator = SimilarityCalculator;
|
---|
[8313] | 381 | }
|
---|
| 382 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
| 383 | IStochasticOperator stochasticOp = op as IStochasticOperator;
|
---|
| 384 | if (stochasticOp != null) {
|
---|
| 385 | stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 386 | stochasticOp.RandomParameter.Hidden = true;
|
---|
| 387 | }
|
---|
| 388 | }
|
---|
| 389 | private void ParameterizeSelectors() {
|
---|
| 390 | foreach (ISelector selector in SelectorParameter.ValidValues) {
|
---|
| 391 | selector.CopySelected = new BoolValue(true);
|
---|
[8377] | 392 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * BatchSize.Value);
|
---|
[8313] | 393 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
| 394 | ParameterizeStochasticOperator(selector);
|
---|
| 395 | }
|
---|
| 396 | if (Problem != null) {
|
---|
| 397 | foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
| 398 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 399 | selector.MaximizationParameter.Hidden = true;
|
---|
| 400 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 401 | selector.QualityParameter.Hidden = true;
|
---|
| 402 | }
|
---|
| 403 | }
|
---|
| 404 | }
|
---|
| 405 | private void ParameterizeAnalyzers() {
|
---|
| 406 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 407 | qualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
[8377] | 408 | populationSizeAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 409 | populationSizeAnalyzer.ResultsParameter.Hidden = true;
|
---|
[8378] | 410 | offspringSuccessAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 411 | offspringSuccessAnalyzer.ResultsParameter.Hidden = true;
|
---|
[8385] | 412 | selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
| 413 | selectionPressureAnalyzer.ResultsParameter.Hidden = true;
|
---|
[8313] | 414 | if (Problem != null) {
|
---|
| 415 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 416 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
| 417 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 418 | qualityAnalyzer.QualityParameter.Depth = 1;
|
---|
| 419 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
| 420 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
| 421 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
| 422 | }
|
---|
| 423 | }
|
---|
| 424 | private void ParameterizeIterationBasedOperators() {
|
---|
| 425 | if (Problem != null) {
|
---|
| 426 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
| 427 | op.IterationsParameter.ActualName = "Generations";
|
---|
| 428 | op.IterationsParameter.Hidden = true;
|
---|
| 429 | op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
|
---|
| 430 | op.MaximumIterationsParameter.Hidden = true;
|
---|
| 431 | }
|
---|
| 432 | }
|
---|
| 433 | }
|
---|
[8407] | 434 | private void ParameterizeSimilarityCalculators() {
|
---|
| 435 | foreach (ISingleObjectiveSolutionSimilarityCalculator calc in SimilarityCalculatorParameter.ValidValues) {
|
---|
| 436 | calc.QualityVariableName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 437 | }
|
---|
| 438 | }
|
---|
[8313] | 439 | private void UpdateCrossovers() {
|
---|
| 440 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
| 441 | CrossoverParameter.ValidValues.Clear();
|
---|
| 442 | ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
|
---|
| 443 |
|
---|
| 444 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
|
---|
| 445 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
| 446 |
|
---|
| 447 | if (oldCrossover != null) {
|
---|
| 448 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
| 449 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
| 450 | else oldCrossover = null;
|
---|
| 451 | }
|
---|
| 452 | if (oldCrossover == null && defaultCrossover != null)
|
---|
| 453 | CrossoverParameter.Value = defaultCrossover;
|
---|
| 454 | }
|
---|
| 455 | private void UpdateMutators() {
|
---|
| 456 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
| 457 | MutatorParameter.ValidValues.Clear();
|
---|
| 458 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
| 459 | MutatorParameter.ValidValues.Add(mutator);
|
---|
| 460 | if (oldMutator != null) {
|
---|
| 461 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
| 462 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
| 463 | }
|
---|
| 464 | }
|
---|
| 465 | private void UpdateAnalyzers() {
|
---|
| 466 | Analyzer.Operators.Clear();
|
---|
| 467 | if (Problem != null) {
|
---|
| 468 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
| 469 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 470 | param.Depth = 1;
|
---|
| 471 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
| 472 | }
|
---|
| 473 | }
|
---|
| 474 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
[8377] | 475 | Analyzer.Operators.Add(populationSizeAnalyzer, populationSizeAnalyzer.EnabledByDefault);
|
---|
[8378] | 476 | Analyzer.Operators.Add(offspringSuccessAnalyzer, offspringSuccessAnalyzer.EnabledByDefault);
|
---|
[8385] | 477 | Analyzer.Operators.Add(selectionPressureAnalyzer, selectionPressureAnalyzer.EnabledByDefault);
|
---|
[8313] | 478 | }
|
---|
[8349] | 479 | private void UpdateSimilarityCalculators() {
|
---|
[8407] | 480 | ISingleObjectiveSolutionSimilarityCalculator oldSimilarityCalculator = SimilarityCalculatorParameter.Value;
|
---|
[8349] | 481 | SimilarityCalculatorParameter.ValidValues.Clear();
|
---|
[8407] | 482 | ISingleObjectiveSolutionSimilarityCalculator defaultSimilarityCalculator = Problem.Operators.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
|
---|
[8349] | 483 |
|
---|
[8407] | 484 | SimilarityCalculatorParameter.ValidValues.Add(new QualitySimilarityCalculator { QualityVariableName = Problem.Evaluator.QualityParameter.ActualName });
|
---|
[8622] | 485 | SimilarityCalculatorParameter.ValidValues.Add(new NoSimilarityCalculator());
|
---|
[8406] | 486 |
|
---|
[8407] | 487 | foreach (ISingleObjectiveSolutionSimilarityCalculator similarityCalculator in Problem.Operators.OfType<ISingleObjectiveSolutionSimilarityCalculator>())
|
---|
[8349] | 488 | SimilarityCalculatorParameter.ValidValues.Add(similarityCalculator);
|
---|
| 489 |
|
---|
| 490 | if (oldSimilarityCalculator != null) {
|
---|
[8407] | 491 | ISingleObjectiveSolutionSimilarityCalculator similarityCalculator = SimilarityCalculatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSimilarityCalculator.GetType());
|
---|
[8349] | 492 | if (similarityCalculator != null) SimilarityCalculatorParameter.Value = similarityCalculator;
|
---|
| 493 | else oldSimilarityCalculator = null;
|
---|
| 494 | }
|
---|
| 495 | if (oldSimilarityCalculator == null && defaultSimilarityCalculator != null)
|
---|
| 496 | SimilarityCalculatorParameter.Value = defaultSimilarityCalculator;
|
---|
| 497 | }
|
---|
[8313] | 498 | private RAPGAMainLoop FindMainLoop(IOperator start) {
|
---|
| 499 | IOperator mainLoop = start;
|
---|
| 500 | while (mainLoop != null && !(mainLoop is RAPGAMainLoop))
|
---|
| 501 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
| 502 | if (mainLoop == null) return null;
|
---|
| 503 | else return (RAPGAMainLoop)mainLoop;
|
---|
| 504 | }
|
---|
| 505 | #endregion
|
---|
| 506 | }
|
---|
| 507 | }
|
---|