[11567] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12018] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[11567] | 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;
|
---|
[11578] | 23 | using System.Linq;
|
---|
[11567] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
[11568] | 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
[11567] | 28 | using HeuristicLab.Optimization;
|
---|
[11578] | 29 | using HeuristicLab.Optimization.Operators;
|
---|
[11568] | 30 | using HeuristicLab.Parameters;
|
---|
[11567] | 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[11578] | 32 | using HeuristicLab.PluginInfrastructure;
|
---|
| 33 | using HeuristicLab.Random;
|
---|
| 34 | using HeuristicLab.Selection;
|
---|
[11567] | 35 |
|
---|
| 36 | namespace HeuristicLab.Algorithms.ALPS {
|
---|
| 37 | [Item("ALPS Genetic Algorithm", "A genetic algorithm with an age-layered population structure.")]
|
---|
| 38 | [Creatable("Algorithms")]
|
---|
| 39 | [StorableClass]
|
---|
[12119] | 40 | public sealed class AlpsGeneticAlgorithm : Alps {
|
---|
[11567] | 41 | #region Parameter Properties
|
---|
[12035] | 42 | private IValueParameter<IntArray> PopulationSizeParameter {
|
---|
| 43 | get { return (IValueParameter<IntArray>)Parameters["PopulationSize"]; }
|
---|
[11568] | 44 | }
|
---|
| 45 | private IValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
| 46 | get { return (IValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
|
---|
| 47 | }
|
---|
| 48 | public IConstrainedValueParameter<ISelector> SelectorParameter {
|
---|
| 49 | get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
|
---|
| 50 | }
|
---|
| 51 | public IConstrainedValueParameter<ICrossover> CrossoverParameter {
|
---|
| 52 | get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
|
---|
| 53 | }
|
---|
| 54 | private IValueParameter<PercentValue> MutationProbabilityParameter {
|
---|
| 55 | get { return (IValueParameter<PercentValue>)Parameters["MutationProbability"]; }
|
---|
| 56 | }
|
---|
| 57 | public IConstrainedValueParameter<IManipulator> MutatorParameter {
|
---|
| 58 | get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
|
---|
| 59 | }
|
---|
| 60 | private IValueParameter<IntValue> ElitesParameter {
|
---|
| 61 | get { return (IValueParameter<IntValue>)Parameters["Elites"]; }
|
---|
| 62 | }
|
---|
| 63 | private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
|
---|
| 64 | get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
|
---|
| 65 | }
|
---|
[12186] | 66 | private IValueParameter<BoolValue> PlusSelectionParameter {
|
---|
| 67 | get { return (IValueParameter<BoolValue>)Parameters["PlusSelection"]; }
|
---|
| 68 | }
|
---|
[11567] | 69 | #endregion
|
---|
| 70 |
|
---|
| 71 | #region Properties
|
---|
[12035] | 72 | public IntArray PopulationSize {
|
---|
[11568] | 73 | get { return PopulationSizeParameter.Value; }
|
---|
| 74 | set { PopulationSizeParameter.Value = value; }
|
---|
| 75 | }
|
---|
| 76 | public IntValue MaximumGenerations {
|
---|
| 77 | get { return MaximumGenerationsParameter.Value; }
|
---|
| 78 | set { MaximumGenerationsParameter.Value = value; }
|
---|
| 79 | }
|
---|
[12119] | 80 |
|
---|
[11568] | 81 | public ISelector Selector {
|
---|
| 82 | get { return SelectorParameter.Value; }
|
---|
| 83 | set { SelectorParameter.Value = value; }
|
---|
| 84 | }
|
---|
| 85 | public ICrossover Crossover {
|
---|
| 86 | get { return CrossoverParameter.Value; }
|
---|
| 87 | set { CrossoverParameter.Value = value; }
|
---|
| 88 | }
|
---|
| 89 | public PercentValue MutationProbability {
|
---|
| 90 | get { return MutationProbabilityParameter.Value; }
|
---|
| 91 | set { MutationProbabilityParameter.Value = value; }
|
---|
| 92 | }
|
---|
| 93 | public IManipulator Mutator {
|
---|
| 94 | get { return MutatorParameter.Value; }
|
---|
| 95 | set { MutatorParameter.Value = value; }
|
---|
| 96 | }
|
---|
| 97 | public IntValue Elites {
|
---|
| 98 | get { return ElitesParameter.Value; }
|
---|
| 99 | set { ElitesParameter.Value = value; }
|
---|
| 100 | }
|
---|
| 101 | public bool ReevaluteElites {
|
---|
| 102 | get { return ReevaluateElitesParameter.Value.Value; }
|
---|
| 103 | set { ReevaluateElitesParameter.Value.Value = value; }
|
---|
| 104 | }
|
---|
[12186] | 105 | public bool PlusSelection {
|
---|
| 106 | get { return PlusSelectionParameter.Value.Value; }
|
---|
| 107 | set { PlusSelectionParameter.Value.Value = value; }
|
---|
| 108 | }
|
---|
[11578] | 109 |
|
---|
[11580] | 110 | private AlpsGeneticAlgorithmMainLoop MainLoop {
|
---|
| 111 | get { return OperatorGraph.Iterate().OfType<AlpsGeneticAlgorithmMainLoop>().First(); }
|
---|
| 112 | }
|
---|
[11676] | 113 | #endregion
|
---|
| 114 |
|
---|
[11567] | 115 | [StorableConstructor]
|
---|
| 116 | private AlpsGeneticAlgorithm(bool deserializing)
|
---|
| 117 | : base(deserializing) { }
|
---|
| 118 | private AlpsGeneticAlgorithm(AlpsGeneticAlgorithm original, Cloner cloner)
|
---|
| 119 | : base(original, cloner) {
|
---|
[11578] | 120 | Initialize();
|
---|
[11567] | 121 | }
|
---|
| 122 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 123 | return new AlpsGeneticAlgorithm(this, cloner);
|
---|
| 124 | }
|
---|
| 125 | public AlpsGeneticAlgorithm()
|
---|
| 126 | : base() {
|
---|
[12048] | 127 | Parameters.Add(new ValueParameter<IntArray>("PopulationSize", "The size of the population of solutions each layer.", new IntArray(new[] { 100 })));
|
---|
[11568] | 128 | Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
|
---|
| 129 | Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
|
---|
| 130 | Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
|
---|
| 131 | Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
|
---|
| 132 | Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
|
---|
| 133 | Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
|
---|
| 134 | 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 });
|
---|
[12186] | 135 | Parameters.Add(new ValueParameter<BoolValue>("PlusSelection", "Include the parents in the selection of the invividuals for the next generation.", new BoolValue(false)) { Hidden = true });
|
---|
[11578] | 136 |
|
---|
| 137 | var globalRandomCreator = new RandomCreator();
|
---|
| 138 | var layer0Creator = new SubScopesCreator() { Name = "Create Layer Zero" };
|
---|
[12035] | 139 | var layer0Processor = new LayerUniformSubScopesProcessor();
|
---|
[11578] | 140 | var localRandomCreator = new LocalRandomCreator();
|
---|
[11585] | 141 | var layerVariableCreator = new VariableCreator();
|
---|
[11578] | 142 | var layerSolutionsCreator = new SolutionsCreator();
|
---|
| 143 | var initializeAgeProcessor = new UniformSubScopesProcessor();
|
---|
| 144 | var initializeAge = new VariableCreator() { Name = "Initialize Age" };
|
---|
[11676] | 145 | var initializeLayerPopulationSize = new SubScopesCounter() { Name = "Init LayerPopulationCounter" };
|
---|
| 146 | var initializeLocalEvaluatedSolutions = new Assigner() { Name = "Initialize LayerEvaluatedSolutions" };
|
---|
[11578] | 147 | var initializeGlobalEvaluatedSolutions = new DataReducer() { Name = "Initialize EvaluatedSolutions" };
|
---|
| 148 | var resultsCollector = new ResultsCollector();
|
---|
[11580] | 149 | var mainLoop = new AlpsGeneticAlgorithmMainLoop();
|
---|
| 150 |
|
---|
[11578] | 151 | OperatorGraph.InitialOperator = globalRandomCreator;
|
---|
| 152 |
|
---|
| 153 | globalRandomCreator.RandomParameter.ActualName = "GlobalRandom";
|
---|
| 154 | globalRandomCreator.SeedParameter.Value = null;
|
---|
| 155 | globalRandomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
| 156 | globalRandomCreator.Successor = layer0Creator;
|
---|
| 157 |
|
---|
| 158 | layer0Creator.NumberOfSubScopesParameter.Value = new IntValue(1);
|
---|
| 159 | layer0Creator.Successor = layer0Processor;
|
---|
| 160 |
|
---|
[12035] | 161 | layer0Processor.Operator = localRandomCreator;
|
---|
[11578] | 162 | layer0Processor.Successor = initializeGlobalEvaluatedSolutions;
|
---|
| 163 |
|
---|
[11585] | 164 | localRandomCreator.Successor = layerVariableCreator;
|
---|
[11578] | 165 |
|
---|
[11585] | 166 | layerVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Layer", new IntValue(0)));
|
---|
| 167 | layerVariableCreator.Successor = layerSolutionsCreator;
|
---|
| 168 |
|
---|
[11578] | 169 | layerSolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
| 170 | layerSolutionsCreator.Successor = initializeAgeProcessor;
|
---|
| 171 |
|
---|
| 172 | initializeAgeProcessor.Operator = initializeAge;
|
---|
[11676] | 173 | initializeAgeProcessor.Successor = initializeLayerPopulationSize;
|
---|
[11578] | 174 |
|
---|
[11676] | 175 | initializeLayerPopulationSize.ValueParameter.ActualName = "LayerPopulationSize";
|
---|
| 176 | initializeLayerPopulationSize.Successor = initializeLocalEvaluatedSolutions;
|
---|
| 177 |
|
---|
[11578] | 178 | initializeAge.CollectedValues.Add(new ValueParameter<IntValue>("Age", new IntValue(0)));
|
---|
| 179 | initializeAge.Successor = null;
|
---|
| 180 |
|
---|
[11676] | 181 | initializeLocalEvaluatedSolutions.LeftSideParameter.ActualName = "LayerEvaluatedSolutions";
|
---|
| 182 | initializeLocalEvaluatedSolutions.RightSideParameter.ActualName = "LayerPopulationSize";
|
---|
[11578] | 183 | initializeLocalEvaluatedSolutions.Successor = null;
|
---|
| 184 |
|
---|
| 185 | initializeGlobalEvaluatedSolutions.ReductionOperation.Value.Value = ReductionOperations.Sum;
|
---|
| 186 | initializeGlobalEvaluatedSolutions.TargetOperation.Value.Value = ReductionOperations.Assign;
|
---|
[11590] | 187 | initializeGlobalEvaluatedSolutions.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
|
---|
[11578] | 188 | initializeGlobalEvaluatedSolutions.TargetParameter.ActualName = "EvaluatedSolutions";
|
---|
| 189 | initializeGlobalEvaluatedSolutions.Successor = resultsCollector;
|
---|
| 190 |
|
---|
[11609] | 191 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
|
---|
[11578] | 192 | resultsCollector.Successor = mainLoop;
|
---|
| 193 |
|
---|
| 194 | foreach (var selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(s => !(s is IMultiObjectiveSelector)).OrderBy(s => Name))
|
---|
| 195 | SelectorParameter.ValidValues.Add(selector);
|
---|
[12119] | 196 | var tournamentSelector = SelectorParameter.ValidValues.OfType<TournamentSelector>().FirstOrDefault();
|
---|
| 197 | if (tournamentSelector != null) {
|
---|
[12260] | 198 | tournamentSelector.GroupSizeParameter.Value = new IntValue(4);
|
---|
[12119] | 199 | SelectorParameter.Value = tournamentSelector;
|
---|
| 200 | }
|
---|
[11578] | 201 |
|
---|
| 202 | ParameterizeSelectors();
|
---|
| 203 | Initialize();
|
---|
[11567] | 204 | }
|
---|
[11578] | 205 |
|
---|
| 206 | #region Events
|
---|
| 207 | protected override void OnProblemChanged() {
|
---|
[12119] | 208 | base.OnProblemChanged();
|
---|
[11578] | 209 | ParameterizeSolutionsCreator();
|
---|
| 210 | ParameterizeMainLoop();
|
---|
| 211 | ParameterizeSelectors();
|
---|
[12120] | 212 | ParameterizeIterationBasedOperators();
|
---|
[11578] | 213 | UpdateCrossovers();
|
---|
| 214 | UpdateMutators();
|
---|
| 215 | }
|
---|
| 216 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
[12119] | 217 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
[11578] | 218 | ParameterizeSolutionsCreator();
|
---|
| 219 | }
|
---|
| 220 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
[12119] | 221 | base.Problem_EvaluatorChanged(sender, e);
|
---|
[11578] | 222 | ParameterizeSolutionsCreator();
|
---|
| 223 | ParameterizeMainLoop();
|
---|
| 224 | ParameterizeSelectors();
|
---|
| 225 | }
|
---|
| 226 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
[12119] | 227 | base.Problem_OperatorsChanged(sender, e);
|
---|
[12120] | 228 | ParameterizeIterationBasedOperators();
|
---|
[11578] | 229 | UpdateCrossovers();
|
---|
| 230 | UpdateMutators();
|
---|
| 231 | }
|
---|
[12119] | 232 | protected override void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
| 233 | base.Evaluator_QualityParameter_ActualNameChanged(sender, e);
|
---|
[11578] | 234 | ParameterizeMainLoop();
|
---|
| 235 | ParameterizeSelectors();
|
---|
| 236 | }
|
---|
| 237 | #endregion
|
---|
| 238 |
|
---|
| 239 | #region Parameterization
|
---|
| 240 | private void Initialize() {
|
---|
| 241 | }
|
---|
| 242 | private void ParameterizeSolutionsCreator() {
|
---|
[11590] | 243 | MainLoop.LayerUpdator.SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 244 | MainLoop.LayerUpdator.SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
[11578] | 245 | }
|
---|
| 246 | private void ParameterizeMainLoop() {
|
---|
[12045] | 247 | MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 248 | MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[11583] | 249 | MainLoop.MainOperator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
| 250 | MainLoop.MainOperator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 251 | MainLoop.MainOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
[11609] | 252 | MainLoop.LayerUpdator.SolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
|
---|
[11578] | 253 | }
|
---|
| 254 | private void ParameterizeSelectors() {
|
---|
| 255 | foreach (var selector in SelectorParameter.ValidValues) {
|
---|
| 256 | selector.CopySelected = new BoolValue(true);
|
---|
[12035] | 257 | // Explicit setting of NumberOfSelectedSubScopesParameter is not required anymore because the NumberOfSelectedSubScopesCalculator calculates it itself
|
---|
| 258 | //selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize - Elites.Value));
|
---|
[11578] | 259 | selector.NumberOfSelectedSubScopesParameter.Hidden = true;
|
---|
| 260 | ParameterizeStochasticOperatorForLayer(selector);
|
---|
| 261 | }
|
---|
| 262 | if (Problem != null) {
|
---|
| 263 | foreach (var selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
|
---|
| 264 | selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
| 265 | selector.MaximizationParameter.Hidden = true;
|
---|
| 266 | selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
| 267 | selector.QualityParameter.Hidden = true;
|
---|
| 268 | }
|
---|
| 269 | }
|
---|
| 270 | }
|
---|
[12120] | 271 | private void ParameterizeIterationBasedOperators() {
|
---|
| 272 | if (Problem != null) {
|
---|
| 273 | foreach (var @operator in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
| 274 | @operator.IterationsParameter.ActualName = "Generations";
|
---|
| 275 | @operator.IterationsParameter.Hidden = true;
|
---|
| 276 | @operator.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
| 277 | @operator.MaximumIterationsParameter.Hidden = true;
|
---|
| 278 | }
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
[12271] | 281 |
|
---|
| 282 | protected override ReductionOperations GetAgeInheritanceReduction(AgeInheritance ageInheritance) {
|
---|
| 283 | switch (ageInheritance) {
|
---|
| 284 | case ALPS.AgeInheritance.Older: return ReductionOperations.Max;
|
---|
| 285 | case ALPS.AgeInheritance.Agerage: return ReductionOperations.Avg;
|
---|
| 286 | case ALPS.AgeInheritance.Younger: return ReductionOperations.Min;
|
---|
| 287 | default: throw new NotSupportedException("AgeInheritance " + ageInheritance + " is not supported.");
|
---|
| 288 | }
|
---|
| 289 | }
|
---|
[11578] | 290 | #endregion
|
---|
| 291 |
|
---|
| 292 | #region Updates
|
---|
| 293 | private void UpdateCrossovers() {
|
---|
| 294 | var oldCrossover = CrossoverParameter.Value;
|
---|
| 295 | var defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
|
---|
| 296 | CrossoverParameter.ValidValues.Clear();
|
---|
| 297 | foreach (var crossover in Problem.Operators.OfType<ICrossover>().OrderBy(c => c.Name)) {
|
---|
| 298 | ParameterizeStochasticOperatorForLayer(crossover);
|
---|
| 299 | CrossoverParameter.ValidValues.Add(crossover);
|
---|
| 300 | }
|
---|
| 301 | if (oldCrossover != null) {
|
---|
| 302 | var crossover = CrossoverParameter.ValidValues.FirstOrDefault(c => c.GetType() == oldCrossover.GetType());
|
---|
[11678] | 303 | if (crossover != null)
|
---|
| 304 | CrossoverParameter.Value = crossover;
|
---|
| 305 | else
|
---|
| 306 | oldCrossover = null;
|
---|
[11578] | 307 | }
|
---|
| 308 | if (oldCrossover == null && defaultCrossover != null)
|
---|
| 309 | CrossoverParameter.Value = defaultCrossover;
|
---|
| 310 | }
|
---|
| 311 | private void UpdateMutators() {
|
---|
| 312 | var oldMutator = MutatorParameter.Value;
|
---|
| 313 | MutatorParameter.ValidValues.Clear();
|
---|
| 314 | foreach (var mutator in Problem.Operators.OfType<IManipulator>().OrderBy(m => m.Name)) {
|
---|
| 315 | ParameterizeStochasticOperatorForLayer(mutator);
|
---|
| 316 | MutatorParameter.ValidValues.Add(mutator);
|
---|
| 317 | }
|
---|
| 318 | if (oldMutator != null) {
|
---|
| 319 | var mutator = MutatorParameter.ValidValues.FirstOrDefault(m => m.GetType() == oldMutator.GetType());
|
---|
[11678] | 320 | if (mutator != null)
|
---|
| 321 | MutatorParameter.Value = mutator;
|
---|
[11578] | 322 | }
|
---|
| 323 | }
|
---|
| 324 | #endregion
|
---|
[11567] | 325 | }
|
---|
| 326 | } |
---|