Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/05/15 16:20:55 (8 years ago)
Author:
pfleck
Message:

#2269

  • Implemented full wiring of ALPS.
  • Created new AlpsGeneticAlgorithmMainOperator instead of using a modified GeneticAlgorithmMainLoop because of wiring issues.
  • Separated LayerCreator into generic LastScopeCloner and ResultsHistoryWiper.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/AlpsGeneticAlgorithmMainOperator.cs

    r13113 r13124  
    2020#endregion
    2121
    22 using System.Linq;
    23 using HeuristicLab.Algorithms.GeneticAlgorithm;
     22using HeuristicLab.Common;
     23using HeuristicLab.Core;
    2424using HeuristicLab.Data;
    2525using HeuristicLab.Operators;
    26 using HeuristicLab.Optimization;
    2726using HeuristicLab.Optimization.Operators;
    2827using HeuristicLab.Parameters;
     28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2929using HeuristicLab.Selection;
    3030
    3131namespace HeuristicLab.Algorithms.ALPS {
    32   public static class AlpsGeneticAlgorithmMainOperator {
    33     public static GeneticAlgorithmMainLoop Create() {
    34       var mainLoop = new GeneticAlgorithmMainLoop();
    35 
    36       var selector = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Selector");
    37       var crossover = mainLoop.OperatorGraph.Iterate().OfType<Placeholder>().First(o => o.OperatorParameter.ActualName == "Crossover");
    38       var subScopesCounter = mainLoop.OperatorGraph.Iterate().OfType<SubScopesCounter>().First();
    39       var elitesMerger = mainLoop.OperatorGraph.Iterate().OfType<MergingReducer>().First();
    40 
    41       // Operator starts with calculating number of selected scopes base on plus/comma-selection replacement scheme
    42       var numberOfSelectedSubScopesCalculator = new ExpressionCalculator() { Name = "NumberOfSelectedSubScopes = 2 * (PopulationSize - (PlusSelection ? 0 : Elites))" };
     32  [Item("AlpsGeneticAlgorithmMainOperator", "An operator that represents the core of an ALPS genetic algorithm.")]
     33  [StorableClass]
     34  public sealed class AlpsGeneticAlgorithmMainOperator : AlgorithmOperator {
     35    #region Parameter properties
     36    public IValueLookupParameter<IRandom> RandomParameter {
     37      get { return (IValueLookupParameter<IRandom>)Parameters["Random"]; }
     38    }
     39
     40    public IValueLookupParameter<IOperator> EvaluatorParameter {
     41      get { return (IValueLookupParameter<IOperator>)Parameters["Evaluator"]; }
     42    }
     43    public IValueLookupParameter<IntValue> EvaluatedSolutionsParameter {
     44      get { return (IValueLookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
     45    }
     46    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
     47      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
     48    }
     49    public IValueLookupParameter<BoolValue> MaximizationParameter {
     50      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
     51    }
     52
     53    public IValueLookupParameter<IntValue> PopulationSizeParameter {
     54      get { return (IValueLookupParameter<IntValue>)Parameters["PopulationSize"]; }
     55    }
     56    public IValueLookupParameter<IOperator> SelectorParameter {
     57      get { return (IValueLookupParameter<IOperator>)Parameters["Selector"]; }
     58    }
     59    public IValueLookupParameter<IOperator> CrossoverParameter {
     60      get { return (IValueLookupParameter<IOperator>)Parameters["Crossover"]; }
     61    }
     62    public IValueLookupParameter<IOperator> MutatorParameter {
     63      get { return (IValueLookupParameter<IOperator>)Parameters["Mutator"]; }
     64    }
     65    public IValueLookupParameter<PercentValue> MutationProbabilityParameter {
     66      get { return (IValueLookupParameter<PercentValue>)Parameters["MutationProbability"]; }
     67    }
     68    public IValueLookupParameter<IntValue> ElitesParameter {
     69      get { return (IValueLookupParameter<IntValue>)Parameters["Elites"]; }
     70    }
     71    public IValueLookupParameter<BoolValue> ReevaluateElitesParameter {
     72      get { return (IValueLookupParameter<BoolValue>)Parameters["ReevaluateElites"]; }
     73    }
     74    public IValueLookupParameter<BoolValue> PlusSelectionParameter {
     75      get { return (IValueLookupParameter<BoolValue>)Parameters["PlusSelection"]; }
     76    }
     77
     78    public IScopeTreeLookupParameter<DoubleValue> AgeParameter {
     79      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Age"]; }
     80    }
     81    public IValueLookupParameter<DoubleValue> AgeInheritanceParameter {
     82      get { return (IValueLookupParameter<DoubleValue>)Parameters["AgeInheritance"]; }
     83    }
     84    public IValueLookupParameter<DoubleValue> AgeIncrementParameter {
     85      get { return (IValueLookupParameter<DoubleValue>)Parameters["AgeIncrement"]; }
     86    }
     87    #endregion
     88
     89    [StorableConstructor]
     90    private AlpsGeneticAlgorithmMainOperator(bool deserializing) : base(deserializing) { }
     91    private AlpsGeneticAlgorithmMainOperator(AlpsGeneticAlgorithmMainOperator original, Cloner cloner)
     92      : base(original, cloner) {
     93    }
     94    public override IDeepCloneable Clone(Cloner cloner) {
     95      return new AlpsGeneticAlgorithmMainOperator(this, cloner);
     96    }
     97    public AlpsGeneticAlgorithmMainOperator()
     98      : base() {
     99      Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator."));
     100
     101      Parameters.Add(new ValueLookupParameter<IOperator>("Evaluator", "The operator used to evaluate solutions. This operator is executed in parallel, if an engine is used which supports parallelization."));
     102      Parameters.Add(new ValueLookupParameter<IntValue>("EvaluatedSolutions", "The number of times solutions have been evaluated."));
     103      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution."));
     104      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false."));
     105
     106      Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population."));
     107      Parameters.Add(new ValueLookupParameter<IOperator>("Selector", "The operator used to select solutions for reproduction."));
     108      Parameters.Add(new ValueLookupParameter<IOperator>("Crossover", "The operator used to cross solutions."));
     109      Parameters.Add(new ValueLookupParameter<IOperator>("Mutator", "The operator used to mutate solutions."));
     110      Parameters.Add(new ValueLookupParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution."));
     111      Parameters.Add(new ValueLookupParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation."));
     112      Parameters.Add(new ValueLookupParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)"));
     113      Parameters.Add(new ValueLookupParameter<BoolValue>("PlusSelection", "Include the parents in the selection of the invividuals for the next generation."));
     114
     115      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Age", "The age of individuals."));
     116      Parameters.Add(new ValueLookupParameter<DoubleValue>("AgeInheritance", "A weight that determines the age of a child after crossover based on the older (1.0) and younger (0.0) parent."));
     117      Parameters.Add(new ValueLookupParameter<DoubleValue>("AgeIncrement", "The value the age the individuals is incremented if they survives a generation."));
     118
     119
     120      var numberOfSelectedParentsCalculator = new ExpressionCalculator() { Name = "NumberOfSelectedParents = 2 * (PopulationSize - (PlusSelection ? 0 : Elites))" };
     121      var selector = new Placeholder() { Name = "Selector (Placeholder)" };
     122      var subScopesProcessor1 = new SubScopesProcessor();
     123      var childrenCreator = new ChildrenCreator();
     124      var uniformSubScopesProcessor1 = new UniformSubScopesProcessor();
     125      var crossover = new Placeholder() { Name = "Crossover (Placeholder)" };
     126      var stochasticBranch = new StochasticBranch() { Name = "MutationProbability" };
     127      var mutator = new Placeholder() { Name = "Mutator (Placeholder)" };
     128      var ageCalculator = new WeightingReducer() { Name = "Calculate Age" };
     129      var subScopesRemover = new SubScopesRemover();
     130      var uniformSubScopesProcessor2 = new UniformSubScopesProcessor();
     131      var evaluator = new Placeholder() { Name = "Evaluator (Placeholder)" };
     132      var subScopesCounter = new SubScopesCounter() { Name = "Increment EvaluatedSolutions" };
    43133      var replacementBranch = new ConditionalBranch() { Name = "PlusSelection?" };
    44 
    45       // Set new initial operator
    46       mainLoop.OperatorGraph.InitialOperator = numberOfSelectedSubScopesCalculator;
    47 
    48       numberOfSelectedSubScopesCalculator.CollectedValues.Add(new LookupParameter<IntValue>("PopulationSize"));
    49       numberOfSelectedSubScopesCalculator.CollectedValues.Add(new LookupParameter<IntValue>("Elites"));
    50       numberOfSelectedSubScopesCalculator.CollectedValues.Add(new LookupParameter<BoolValue>("PlusSelection"));
    51       numberOfSelectedSubScopesCalculator.ExpressionResultParameter.ActualName = "NumberOfSelectedSubScopes";
    52       numberOfSelectedSubScopesCalculator.ExpressionParameter.Value = new StringValue("PopulationSize 0 Elites PlusSelection if - 2 * toint");
    53       numberOfSelectedSubScopesCalculator.Successor = selector;
    54 
    55       // Use Elitism or Plus-Selection as replacement strategy
    56       var selectedProcessor = (SubScopesProcessor)selector.Successor;
    57       var elitismReplacement = selectedProcessor.Successor;
    58       selectedProcessor.Successor = replacementBranch;
    59       replacementBranch.ConditionParameter.ActualName = "PlusSelection";
    60       replacementBranch.FalseBranch = elitismReplacement;
    61 
    62       // Plus selection replacement
    63134      var replacementMergingReducer = new MergingReducer();
    64135      var replacementBestSelector = new BestSelector();
    65136      var replacementRightReducer = new RightReducer();
     137      var subScopesProcessor2 = new SubScopesProcessor();
     138      var bestSelector = new BestSelector();
     139      var rightReducer = new RightReducer();
     140      var mergingReducer = new MergingReducer();
     141      var reevaluateElitesBranch = new ConditionalBranch() { Name = "Reevaluate elites ?" };
     142      var incrementAgeProcessor = new UniformSubScopesProcessor();
     143      var ageIncrementor = new DoubleCounter() { Name = "Increment Age" };
     144
     145      OperatorGraph.InitialOperator = numberOfSelectedParentsCalculator;
     146
     147      numberOfSelectedParentsCalculator.CollectedValues.Add(new LookupParameter<IntValue>(PopulationSizeParameter.Name));
     148      numberOfSelectedParentsCalculator.CollectedValues.Add(new LookupParameter<IntValue>(ElitesParameter.Name));
     149      numberOfSelectedParentsCalculator.CollectedValues.Add(new LookupParameter<BoolValue>(PlusSelectionParameter.Name));
     150      numberOfSelectedParentsCalculator.ExpressionResultParameter.ActualName = "NumberOfSelectedSubScopes";
     151      numberOfSelectedParentsCalculator.ExpressionParameter.Value = new StringValue("PopulationSize 0 Elites PlusSelection if - 2 * toint");
     152      numberOfSelectedParentsCalculator.Successor = selector;
     153
     154      selector.OperatorParameter.ActualName = SelectorParameter.Name;
     155      selector.Successor = subScopesProcessor1;
     156
     157      subScopesProcessor1.Operators.Add(new EmptyOperator());
     158      subScopesProcessor1.Operators.Add(childrenCreator);
     159      subScopesProcessor1.Successor = replacementBranch;
     160
     161      childrenCreator.ParentsPerChild = new IntValue(2);
     162      childrenCreator.Successor = uniformSubScopesProcessor1;
     163
     164      uniformSubScopesProcessor1.Operator = crossover;
     165      uniformSubScopesProcessor1.Successor = uniformSubScopesProcessor2;
     166
     167      crossover.OperatorParameter.ActualName = CrossoverParameter.Name;
     168      crossover.Successor = stochasticBranch;
     169
     170      stochasticBranch.ProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
     171      stochasticBranch.RandomParameter.ActualName = RandomParameter.Name;
     172      stochasticBranch.FirstBranch = mutator;
     173      stochasticBranch.SecondBranch = null;
     174      stochasticBranch.Successor = ageCalculator;
     175
     176      mutator.OperatorParameter.ActualName = MutatorParameter.Name;
     177      mutator.Successor = null;
     178
     179      ageCalculator.ParameterToReduce.ActualName = AgeParameter.Name;
     180      ageCalculator.TargetParameter.ActualName = AgeParameter.Name;
     181      ageCalculator.WeightParameter.ActualName = AgeInheritanceParameter.Name;
     182      ageCalculator.Successor = subScopesRemover;
     183
     184      subScopesRemover.RemoveAllSubScopes = true;
     185      subScopesRemover.Successor = null;
     186
     187      uniformSubScopesProcessor2.Parallel.Value = true;
     188      uniformSubScopesProcessor2.Operator = evaluator;
     189      uniformSubScopesProcessor2.Successor = subScopesCounter;
     190
     191      evaluator.OperatorParameter.ActualName = EvaluatorParameter.Name;
     192      evaluator.Successor = null;
     193
     194      subScopesCounter.ValueParameter.ActualName = EvaluatedSolutionsParameter.Name;
     195      subScopesCounter.AccumulateParameter.Value = new BoolValue(false);
     196      subScopesCounter.Successor = null;
     197
     198      replacementBranch.ConditionParameter.ActualName = PlusSelectionParameter.Name;
    66199      replacementBranch.TrueBranch = replacementMergingReducer;
     200      replacementBranch.FalseBranch = subScopesProcessor2;
     201      replacementBranch.Successor = incrementAgeProcessor;
    67202
    68203      replacementMergingReducer.Successor = replacementBestSelector;
    69204
    70       replacementBestSelector.NumberOfSelectedSubScopesParameter.ActualName = "PopulationSize";
     205      replacementBestSelector.NumberOfSelectedSubScopesParameter.ActualName = PopulationSizeParameter.Name;
    71206      replacementBestSelector.CopySelected = new BoolValue(false);
    72207      replacementBestSelector.Successor = replacementRightReducer;
    73208
    74       replacementRightReducer.Successor = null;
    75 
    76       // Increment ages of all individuals after replacement
    77       var incrementAgeProcessor = new UniformSubScopesProcessor();
    78       var ageIncrementor = new DoubleCounter() { Name = "Increment Age" };
    79       replacementBranch.Successor = incrementAgeProcessor;
     209      replacementRightReducer.Successor = reevaluateElitesBranch;
     210
     211      subScopesProcessor2.Operators.Add(bestSelector);
     212      subScopesProcessor2.Operators.Add(new EmptyOperator());
     213      subScopesProcessor2.Successor = mergingReducer;
     214
     215      bestSelector.CopySelected = new BoolValue(false);
     216      bestSelector.MaximizationParameter.ActualName = MaximizationParameter.Name;
     217      bestSelector.NumberOfSelectedSubScopesParameter.ActualName = ElitesParameter.Name;
     218      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
     219      bestSelector.Successor = rightReducer;
     220
     221      rightReducer.Successor = reevaluateElitesBranch;
     222
     223      mergingReducer.Successor = null;
     224
     225      reevaluateElitesBranch.ConditionParameter.ActualName = ReevaluateElitesParameter.Name;
     226      reevaluateElitesBranch.TrueBranch = uniformSubScopesProcessor2;
     227      reevaluateElitesBranch.FalseBranch = null;
     228      reevaluateElitesBranch.Successor = null;
     229
     230
    80231      incrementAgeProcessor.Operator = ageIncrementor;
    81232      incrementAgeProcessor.Successor = null;
    82       ageIncrementor.ValueParameter.ActualName = "Age";
    83       ageIncrementor.Increment = new DoubleValue(1.0);
    84 
    85       // Insert AgeCalculator between crossover and its successor
    86       var crossoverSuccessor = crossover.Successor;
    87       var ageCalculator = new WeightingReducer() { Name = "Calculate Age" };
    88       crossover.Successor = ageCalculator;
    89 
    90       ageCalculator.ParameterToReduce.ActualName = "Age";
    91       ageCalculator.TargetParameter.ActualName = "Age";
    92       ageCalculator.WeightParameter.ActualName = "AgeInheritance";
    93       ageCalculator.Successor = crossoverSuccessor;
    94 
    95       // When counting the evaluated solutions, write in LayerEvaluatedSolutions
    96       subScopesCounter.ValueParameter.ActualName = "LayerEvaluatedSolutions";
    97       subScopesCounter.AccumulateParameter.Value = new BoolValue(false);
    98 
    99       // Instead of generational loop after merging of elites, stop
    100       elitesMerger.Successor = null;
    101 
    102       // Parameterize
    103       foreach (var stochasticOperator in mainLoop.OperatorGraph.Iterate().OfType<IStochasticOperator>())
    104         stochasticOperator.RandomParameter.ActualName = "LocalRandom";
    105       foreach (var stochasticBranch in mainLoop.OperatorGraph.Iterate().OfType<StochasticBranch>())
    106         stochasticBranch.RandomParameter.ActualName = "LocalRandom";
    107 
    108       // Remove unused operators
    109       var usedOperators = mainLoop.OperatorGraph.Iterate();
    110       var unusedOperators = mainLoop.OperatorGraph.Operators.Except(usedOperators);
    111       foreach (var op in unusedOperators.ToList())
    112         mainLoop.OperatorGraph.Operators.Remove(op);
    113 
    114       return mainLoop;
     233
     234      ageIncrementor.ValueParameter.ActualName = AgeParameter.Name;
     235      ageIncrementor.IncrementParameter.Value = null;
     236      ageIncrementor.IncrementParameter.ActualName = AgeIncrementParameter.Name;
     237      ageIncrementor.Successor = null;
    115238    }
    116239  }
Note: See TracChangeset for help on using the changeset viewer.