[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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 |
|
---|
[2865] | 22 | using System;
|
---|
| 23 | using System.Linq;
|
---|
[17097] | 24 | using HEAL.Attic;
|
---|
[4068] | 25 | using HeuristicLab.Analysis;
|
---|
[3376] | 26 | using HeuristicLab.Common;
|
---|
[2] | 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Data;
|
---|
[5346] | 29 | using HeuristicLab.Operators;
|
---|
[2851] | 30 | using HeuristicLab.Optimization;
|
---|
[3021] | 31 | using HeuristicLab.Optimization.Operators;
|
---|
[2852] | 32 | using HeuristicLab.Parameters;
|
---|
[2882] | 33 | using HeuristicLab.PluginInfrastructure;
|
---|
[3368] | 34 | using HeuristicLab.Random;
|
---|
[2] | 35 |
|
---|
[3196] | 36 | namespace HeuristicLab.Algorithms.GeneticAlgorithm {
|
---|
[1153] | 37 | /// <summary>
|
---|
[3198] | 38 | /// A genetic algorithm.
|
---|
[1153] | 39 | /// </summary>
|
---|
[13295] | 40 | [Item("Genetic Algorithm (GA)", "A genetic algorithm.")]
|
---|
| 41 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 100)]
|
---|
[17097] | 42 | [StorableType("B63D21BD-D6AE-474B-A319-AC92CCB30AF6")]
|
---|
[5809] | 43 | public sealed class GeneticAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
|
---|
[4437] | 44 | public string Filename { get; set; }
|
---|
| 45 |
|
---|
[2986] | 46 | #region Problem Properties
|
---|
| 47 | public override Type ProblemType {
|
---|
[5809] | 48 | get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
|
---|
[2986] | 49 | }
|
---|
[5809] | 50 | public new ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
| 51 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
[2986] | 52 | set { base.Problem = value; }
|
---|
| 53 | }
|
---|
| 54 | #endregion
|
---|
[2852] | 55 |
|
---|
[2986] | 56 | #region Parameter Properties
|
---|
[3048] | 57 | private ValueParameter<IntValue> SeedParameter {
|
---|
| 58 | get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
|
---|
[2986] | 59 | }
|
---|
[3048] | 60 | private ValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
| 61 | get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
|
---|
[2986] | 62 | }
|
---|
[3048] | 63 | private ValueParameter<IntValue> PopulationSizeParameter {
|
---|
| 64 | get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
[2986] | 65 | }
|
---|
[8121] | 66 | public IConstrainedValueParameter<ISelector> SelectorParameter {
|
---|
| 67 | get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
|
---|
[2986] | 68 | }
|
---|
[8121] | 69 | public IConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
| 70 | get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
[2986] | 71 | }
|
---|
[3095] | 72 | private ValueParameter<PercentValue> MutationProbabilityParameter {
|
---|
| 73 | get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
[2986] | 74 | }
|
---|
[8121] | 75 | public IConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
| 76 | get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
[2986] | 77 | }
|
---|
[3048] | 78 | private ValueParameter<IntValue> ElitesParameter {
|
---|
| 79 | get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
|
---|
[2986] | 80 | }
|
---|
[9673] | 81 | private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
|
---|
| 82 | get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
|
---|
| 83 | }
|
---|
[3658] | 84 | private ValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
| 85 | get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
|
---|
[3616] | 86 | }
|
---|
[3048] | 87 | private ValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 88 | get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
[2986] | 89 | }
|
---|
| 90 | #endregion
|
---|
[2865] | 91 |
|
---|
[2986] | 92 | #region Properties
|
---|
[3048] | 93 | public IntValue Seed {
|
---|
[2986] | 94 | get { return SeedParameter.Value; }
|
---|
| 95 | set { SeedParameter.Value = value; }
|
---|
| 96 | }
|
---|
[3048] | 97 | public BoolValue SetSeedRandomly {
|
---|
[2986] | 98 | get { return SetSeedRandomlyParameter.Value; }
|
---|
| 99 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
| 100 | }
|
---|
[3048] | 101 | public IntValue PopulationSize {
|
---|
[2986] | 102 | get { return PopulationSizeParameter.Value; }
|
---|
| 103 | set { PopulationSizeParameter.Value = value; }
|
---|
| 104 | }
|
---|
| 105 | public ISelector Selector {
|
---|
| 106 | get { return SelectorParameter.Value; }
|
---|
| 107 | set { SelectorParameter.Value = value; }
|
---|
| 108 | }
|
---|
| 109 | public ICrossover Crossover {
|
---|
| 110 | get { return CrossoverParameter.Value; }
|
---|
| 111 | set { CrossoverParameter.Value = value; }
|
---|
| 112 | }
|
---|
[3095] | 113 | public PercentValue MutationProbability {
|
---|
[2986] | 114 | get { return MutationProbabilityParameter.Value; }
|
---|
| 115 | set { MutationProbabilityParameter.Value = value; }
|
---|
| 116 | }
|
---|
| 117 | public IManipulator Mutator {
|
---|
| 118 | get { return MutatorParameter.Value; }
|
---|
| 119 | set { MutatorParameter.Value = value; }
|
---|
| 120 | }
|
---|
[3048] | 121 | public IntValue Elites {
|
---|
[2986] | 122 | get { return ElitesParameter.Value; }
|
---|
| 123 | set { ElitesParameter.Value = value; }
|
---|
| 124 | }
|
---|
[9673] | 125 | public bool ReevaluteElites {
|
---|
| 126 | get { return ReevaluateElitesParameter.Value.Value; }
|
---|
| 127 | set { ReevaluateElitesParameter.Value.Value = value; }
|
---|
| 128 | }
|
---|
[3658] | 129 | public MultiAnalyzer Analyzer {
|
---|
[3616] | 130 | get { return AnalyzerParameter.Value; }
|
---|
| 131 | set { AnalyzerParameter.Value = value; }
|
---|
| 132 | }
|
---|
[3048] | 133 | public IntValue MaximumGenerations {
|
---|
[2986] | 134 | get { return MaximumGenerationsParameter.Value; }
|
---|
| 135 | set { MaximumGenerationsParameter.Value = value; }
|
---|
| 136 | }
|
---|
| 137 | private RandomCreator RandomCreator {
|
---|
| 138 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
| 139 | }
|
---|
[3023] | 140 | private SolutionsCreator SolutionsCreator {
|
---|
| 141 | get { return (SolutionsCreator)RandomCreator.Successor; }
|
---|
[2986] | 142 | }
|
---|
[3198] | 143 | private GeneticAlgorithmMainLoop GeneticAlgorithmMainLoop {
|
---|
[5366] | 144 | get { return FindMainLoop(SolutionsCreator.Successor); }
|
---|
[2986] | 145 | }
|
---|
[3680] | 146 | [Storable]
|
---|
[3662] | 147 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
[2986] | 148 | #endregion
|
---|
[2852] | 149 |
|
---|
[3198] | 150 | public GeneticAlgorithm()
|
---|
[2986] | 151 | : base() {
|
---|
[3048] | 152 | Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
| 153 | Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
| 154 | Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
|
---|
[2986] | 155 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
| 156 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
[3095] | 157 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
[15238] | 158 | Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
[3048] | 159 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
[9673] | 160 | 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 });
|
---|
[3658] | 161 | Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
|
---|
[3048] | 162 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
|
---|
[2] | 163 |
|
---|
[2986] | 164 | RandomCreator randomCreator = new RandomCreator();
|
---|
[3023] | 165 | SolutionsCreator solutionsCreator = new SolutionsCreator();
|
---|
[5352] | 166 | SubScopesCounter subScopesCounter = new SubScopesCounter();
|
---|
[5356] | 167 | ResultsCollector resultsCollector = new ResultsCollector();
|
---|
| 168 | GeneticAlgorithmMainLoop mainLoop = new GeneticAlgorithmMainLoop();
|
---|
[2986] | 169 | OperatorGraph.InitialOperator = randomCreator;
|
---|
[2882] | 170 |
|
---|
[2986] | 171 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
| 172 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
| 173 | randomCreator.SeedParameter.Value = null;
|
---|
| 174 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
| 175 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
[3023] | 176 | randomCreator.Successor = solutionsCreator;
|
---|
[2] | 177 |
|
---|
[3023] | 178 | solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
[5352] | 179 | solutionsCreator.Successor = subScopesCounter;
|
---|
[2] | 180 |
|
---|
[5352] | 181 | subScopesCounter.Name = "Initialize EvaluatedSolutions";
|
---|
| 182 | subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
[5356] | 183 | subScopesCounter.Successor = resultsCollector;
|
---|
[5346] | 184 |
|
---|
[5356] | 185 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
| 186 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
| 187 | resultsCollector.Successor = mainLoop;
|
---|
[2] | 188 |
|
---|
[5356] | 189 | mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
|
---|
| 190 | mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
|
---|
| 191 | mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
|
---|
[9673] | 192 | mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
|
---|
[5356] | 193 | mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
| 194 | mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
|
---|
| 195 | mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
|
---|
| 196 | mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 197 | mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
|
---|
| 198 | mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
| 199 | mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 200 | mainLoop.ResultsParameter.ActualName = "Results";
|
---|
| 201 |
|
---|
[3680] | 202 | foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
|
---|
| 203 | SelectorParameter.ValidValues.Add(selector);
|
---|
| 204 | ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
|
---|
| 205 | if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
|
---|
| 206 | ParameterizeSelectors();
|
---|
| 207 |
|
---|
| 208 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
| 209 | ParameterizeAnalyzers();
|
---|
| 210 | UpdateAnalyzers();
|
---|
| 211 |
|
---|
[3280] | 212 | Initialize();
|
---|
[2986] | 213 | }
|
---|
| 214 | [StorableConstructor]
|
---|
[17097] | 215 | private GeneticAlgorithm(StorableConstructorFlag _) : base(_) { }
|
---|
[4722] | 216 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 217 | private void AfterDeserialization() {
|
---|
[9673] | 218 | // BackwardsCompatibility3.3
|
---|
| 219 | #region Backwards compatible code, remove with 3.4
|
---|
| 220 | if (!Parameters.ContainsKey("ReevaluateElites")) {
|
---|
| 221 | 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 });
|
---|
| 222 | }
|
---|
[15238] | 223 | var optionalMutatorParameter = MutatorParameter as OptionalConstrainedValueParameter<IManipulator>;
|
---|
| 224 | if (optionalMutatorParameter != null) {
|
---|
| 225 | Parameters.Remove(optionalMutatorParameter);
|
---|
| 226 | Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 227 | foreach (var m in optionalMutatorParameter.ValidValues)
|
---|
| 228 | MutatorParameter.ValidValues.Add(m);
|
---|
| 229 | if (optionalMutatorParameter.Value == null) MutationProbability.Value = 0; // to guarantee that the old configuration results in the same behavior
|
---|
| 230 | else Mutator = optionalMutatorParameter.Value;
|
---|
| 231 | optionalMutatorParameter.ValidValues.Clear(); // to avoid dangling references to the old parameter its valid values are cleared
|
---|
| 232 | }
|
---|
[9673] | 233 | #endregion
|
---|
| 234 |
|
---|
[4722] | 235 | Initialize();
|
---|
| 236 | }
|
---|
[2] | 237 |
|
---|
[9673] | 238 |
|
---|
| 239 |
|
---|
[4722] | 240 | private GeneticAlgorithm(GeneticAlgorithm original, Cloner cloner)
|
---|
| 241 | : base(original, cloner) {
|
---|
| 242 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
| 243 | Initialize();
|
---|
| 244 | }
|
---|
[2986] | 245 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 246 | return new GeneticAlgorithm(this, cloner);
|
---|
[2986] | 247 | }
|
---|
[2882] | 248 |
|
---|
[3275] | 249 | public override void Prepare() {
|
---|
| 250 | if (Problem != null) base.Prepare();
|
---|
[3188] | 251 | }
|
---|
| 252 |
|
---|
[2986] | 253 | #region Events
|
---|
| 254 | protected override void OnProblemChanged() {
|
---|
| 255 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
| 256 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
[7999] | 257 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
[3023] | 258 | ParameterizeSolutionsCreator();
|
---|
[3198] | 259 | ParameterizeGeneticAlgorithmMainLoop();
|
---|
[2986] | 260 | ParameterizeSelectors();
|
---|
[3616] | 261 | ParameterizeAnalyzers();
|
---|
[3750] | 262 | ParameterizeIterationBasedOperators();
|
---|
[2986] | 263 | UpdateCrossovers();
|
---|
| 264 | UpdateMutators();
|
---|
[3616] | 265 | UpdateAnalyzers();
|
---|
[2986] | 266 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 267 | base.OnProblemChanged();
|
---|
| 268 | }
|
---|
[3139] | 269 |
|
---|
[2986] | 270 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
| 271 | ParameterizeStochasticOperator(Problem.SolutionCreator);
|
---|
[3023] | 272 | ParameterizeSolutionsCreator();
|
---|
[2986] | 273 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
| 274 | }
|
---|
| 275 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
| 276 | ParameterizeStochasticOperator(Problem.Evaluator);
|
---|
[3023] | 277 | ParameterizeSolutionsCreator();
|
---|
[3198] | 278 | ParameterizeGeneticAlgorithmMainLoop();
|
---|
[2986] | 279 | ParameterizeSelectors();
|
---|
[3616] | 280 | ParameterizeAnalyzers();
|
---|
[2986] | 281 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
| 282 | base.Problem_EvaluatorChanged(sender, e);
|
---|
| 283 | }
|
---|
| 284 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
[8351] | 285 | foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
|
---|
[3750] | 286 | ParameterizeIterationBasedOperators();
|
---|
[2986] | 287 | UpdateCrossovers();
|
---|
| 288 | UpdateMutators();
|
---|
[3616] | 289 | UpdateAnalyzers();
|
---|
[2986] | 290 | base.Problem_OperatorsChanged(sender, e);
|
---|
| 291 | }
|
---|
| 292 | private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 293 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
| 294 | ParameterizeSelectors();
|
---|
| 295 | }
|
---|
| 296 | private void Elites_ValueChanged(object sender, EventArgs e) {
|
---|
| 297 | ParameterizeSelectors();
|
---|
| 298 | }
|
---|
[5206] | 299 |
|
---|
[2986] | 300 | private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
|
---|
| 301 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 302 | ParameterizeSelectors();
|
---|
| 303 | }
|
---|
| 304 | private void PopulationSize_ValueChanged(object sender, EventArgs e) {
|
---|
| 305 | ParameterizeSelectors();
|
---|
| 306 | }
|
---|
| 307 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
[3198] | 308 | ParameterizeGeneticAlgorithmMainLoop();
|
---|
[2986] | 309 | ParameterizeSelectors();
|
---|
[3616] | 310 | ParameterizeAnalyzers();
|
---|
[2986] | 311 | }
|
---|
| 312 | #endregion
|
---|
[2852] | 313 |
|
---|
[2986] | 314 | #region Helpers
|
---|
[3280] | 315 | private void Initialize() {
|
---|
[2986] | 316 | PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
|
---|
| 317 | PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
|
---|
| 318 | ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
|
---|
| 319 | Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
|
---|
[3139] | 320 | if (Problem != null) {
|
---|
[2986] | 321 | Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
|
---|
[3139] | 322 | }
|
---|
[2986] | 323 | }
|
---|
[2852] | 324 |
|
---|
[3023] | 325 | private void ParameterizeSolutionsCreator() {
|
---|
| 326 | SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 327 | SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
[2986] | 328 | }
|
---|
[3198] | 329 | private void ParameterizeGeneticAlgorithmMainLoop() {
|
---|
| 330 | GeneticAlgorithmMainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 331 | GeneticAlgorithmMainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 332 | GeneticAlgorithmMainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[2986] | 333 | }
|
---|
| 334 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
[6051] | 335 | IStochasticOperator stochasticOp = op as IStochasticOperator;
|
---|
| 336 | if (stochasticOp != null) {
|
---|
| 337 | stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
| 338 | stochasticOp.RandomParameter.Hidden = true;
|
---|
| 339 | }
|
---|
[2986] | 340 | }
|
---|
| 341 | private void ParameterizeSelectors() {
|
---|
[3680] | 342 | foreach (ISelector selector in SelectorParameter.ValidValues) {
|
---|
[3048] | 343 | selector.CopySelected = new BoolValue(true);
|
---|
| 344 | selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
|
---|
[6051] | 345 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
[2986] | 346 | ParameterizeStochasticOperator(selector);
|
---|
| 347 | }
|
---|
| 348 | if (Problem != null) {
|
---|
[3680] | 349 | foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
[2986] | 350 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
[6051] | 351 | selector.MaximizationParameter.Hidden = true;
|
---|
[2986] | 352 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[6051] | 353 | selector.QualityParameter.Hidden = true;
|
---|
[2986] | 354 | }
|
---|
| 355 | }
|
---|
| 356 | }
|
---|
[3616] | 357 | private void ParameterizeAnalyzers() {
|
---|
| 358 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
[6051] | 359 | qualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
[3616] | 360 | if (Problem != null) {
|
---|
| 361 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
[6051] | 362 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
[3616] | 363 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[3662] | 364 | qualityAnalyzer.QualityParameter.Depth = 1;
|
---|
[6051] | 365 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
[3616] | 366 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
[6051] | 367 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
[3616] | 368 | }
|
---|
| 369 | }
|
---|
[3750] | 370 | private void ParameterizeIterationBasedOperators() {
|
---|
| 371 | if (Problem != null) {
|
---|
| 372 | foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
| 373 | op.IterationsParameter.ActualName = "Generations";
|
---|
[6051] | 374 | op.IterationsParameter.Hidden = true;
|
---|
[3750] | 375 | op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
|
---|
[6051] | 376 | op.MaximumIterationsParameter.Hidden = true;
|
---|
[3750] | 377 | }
|
---|
| 378 | }
|
---|
| 379 | }
|
---|
[2986] | 380 | private void UpdateCrossovers() {
|
---|
[3303] | 381 | ICrossover oldCrossover = CrossoverParameter.Value;
|
---|
| 382 | CrossoverParameter.ValidValues.Clear();
|
---|
[7493] | 383 | ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
|
---|
| 384 |
|
---|
[3303] | 385 | foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
|
---|
| 386 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
[7493] | 387 |
|
---|
[3303] | 388 | if (oldCrossover != null) {
|
---|
| 389 | ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
|
---|
| 390 | if (crossover != null) CrossoverParameter.Value = crossover;
|
---|
[7509] | 391 | else oldCrossover = null;
|
---|
| 392 | }
|
---|
| 393 | if (oldCrossover == null && defaultCrossover != null)
|
---|
[7493] | 394 | CrossoverParameter.Value = defaultCrossover;
|
---|
[2986] | 395 | }
|
---|
| 396 | private void UpdateMutators() {
|
---|
[3303] | 397 | IManipulator oldMutator = MutatorParameter.Value;
|
---|
| 398 | MutatorParameter.ValidValues.Clear();
|
---|
[15238] | 399 | IManipulator defaultMutator = Problem.Operators.OfType<IManipulator>().FirstOrDefault();
|
---|
| 400 |
|
---|
[3303] | 401 | foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
|
---|
| 402 | MutatorParameter.ValidValues.Add(mutator);
|
---|
[15238] | 403 |
|
---|
[3303] | 404 | if (oldMutator != null) {
|
---|
| 405 | IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
|
---|
| 406 | if (mutator != null) MutatorParameter.Value = mutator;
|
---|
[15238] | 407 | else oldMutator = null;
|
---|
[3076] | 408 | }
|
---|
[15238] | 409 |
|
---|
| 410 | if (oldMutator == null && defaultMutator != null)
|
---|
| 411 | MutatorParameter.Value = defaultMutator;
|
---|
[2986] | 412 | }
|
---|
[3616] | 413 | private void UpdateAnalyzers() {
|
---|
| 414 | Analyzer.Operators.Clear();
|
---|
| 415 | if (Problem != null) {
|
---|
[3816] | 416 | foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
[3663] | 417 | foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
| 418 | param.Depth = 1;
|
---|
[7172] | 419 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
[3662] | 420 | }
|
---|
[3616] | 421 | }
|
---|
[7172] | 422 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
[3616] | 423 | }
|
---|
[5366] | 424 | private GeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
|
---|
| 425 | IOperator mainLoop = start;
|
---|
| 426 | while (mainLoop != null && !(mainLoop is GeneticAlgorithmMainLoop))
|
---|
| 427 | mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
|
---|
| 428 | if (mainLoop == null) return null;
|
---|
| 429 | else return (GeneticAlgorithmMainLoop)mainLoop;
|
---|
| 430 | }
|
---|
[2986] | 431 | #endregion
|
---|
[2] | 432 | }
|
---|
| 433 | }
|
---|