Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/17/15 20:13:57 (9 years ago)
Author:
bburlacu
Message:

#1772: Schema diversification strategy: implement adaptive replacement ratio and added number of evaluated solutions per generation to the diversification statistics operator

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/SchemaEvaluator.cs

    r12988 r13480  
    2121
    2222using System;
     23using System.Collections.Generic;
    2324using System.Linq;
    2425using System.Threading.Tasks;
     
    2829using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2930using HeuristicLab.EvolutionTracking;
     31using HeuristicLab.Optimization;
    3032using HeuristicLab.Parameters;
    3133using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    4951    private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
    5052    private const string MutatorParameterName = "Mutator";
     53    private const string CrossoverParameterName = "Crossover";
    5154    private const string RandomReplacementParameterName = "RandomReplacement";
    5255    private const string NumberOfChangedTreesParameterName = "NumberOfChangedTrees";
     
    5457    private const string MaxDegreeOfParalellismParameterName = "MaxDegreeOfParallelism";
    5558    private const string ExclusiveMatchingParameterName = "ExclusiveMatching";
     59    private const string UseAdaptiveReplacementRatioParameterName = "UseAdaptiveReplacementRatio";
    5660    #endregion
    5761
    5862    #region parameters
     63    public ILookupParameter<BoolValue> UseAdaptiveReplacementRatioParameter {
     64      get { return (ILookupParameter<BoolValue>)Parameters[UseAdaptiveReplacementRatioParameterName]; }
     65    }
    5966    public ILookupParameter<BoolValue> ExclusiveMatchingParameter {
    6067      get { return (ILookupParameter<BoolValue>)Parameters[ExclusiveMatchingParameterName]; }
     
    8693    public ILookupParameter<ISymbolicExpressionTreeManipulator> MutatorParameter {
    8794      get { return (ILookupParameter<ISymbolicExpressionTreeManipulator>)Parameters[MutatorParameterName]; }
     95    }
     96    public ILookupParameter<ISymbolicExpressionTreeCrossover> CrossoverParameter {
     97      get { return (ILookupParameter<ISymbolicExpressionTreeCrossover>)Parameters[CrossoverParameterName]; }
    8898    }
    8999    public ILookupParameter<IRandom> RandomParameter {
     
    120130    private readonly SymbolicExpressionTreePhenotypicSimilarityCalculator calculator = new SymbolicExpressionTreePhenotypicSimilarityCalculator();
    121131    private readonly QueryMatch qm;
     132
     133    public Func<double, double> ReplacementRule { get; set; }
    122134
    123135    private readonly ISymbolicExpressionTreeNodeEqualityComparer comp = new SymbolicExpressionTreeNodeEqualityComparer {
     
    148160      Parameters.Add(new LookupParameter<BoolValue>(ApplyLinearScalingParameterName));
    149161      Parameters.Add(new LookupParameter<ISymbolicExpressionTreeManipulator>(MutatorParameterName));
     162      Parameters.Add(new LookupParameter<ISymbolicExpressionTreeCrossover>(CrossoverParameterName));
    150163      Parameters.Add(new LookupParameter<BoolValue>(RandomReplacementParameterName));
    151164      Parameters.Add(new LookupParameter<IntValue>(NumberOfChangedTreesParameterName));
     
    153166      Parameters.Add(new LookupParameter<IntValue>(MaxDegreeOfParalellismParameterName));
    154167      Parameters.Add(new LookupParameter<BoolValue>(ExclusiveMatchingParameterName));
     168      Parameters.Add(new LookupParameter<BoolValue>(UseAdaptiveReplacementRatioParameterName));
    155169      #endregion
    156170    }
     
    175189    public override IOperation Apply() {
    176190      var individuals = ExecutionContext.Scope.SubScopes; // the scopes represent the individuals
     191      var trees = individuals.Select(x => (ISymbolicExpressionTree)x.Variables["SymbolicExpressionTree"].Value).ToList();
     192      var qualities = individuals.Select(x => (DoubleValue)x.Variables["Quality"].Value).ToList();
    177193
    178194      var random = RandomParameter.ActualValue;
     
    185201      // first apply the length and root equality checks in order to filter the individuals
    186202      var exclusiveMatching = ExclusiveMatchingParameter.ActualValue.Value;
    187       var filtered = exclusiveMatching ? (from ind in individuals
    188                                           where !ind.Variables.ContainsKey("AlreadyMatched")
    189                                           let t = (ISymbolicExpressionTree)ind.Variables["SymbolicExpressionTree"].Value
    190                                           where t.Length >= s.Length && qm.Comparer.Equals(t.Root.GetSubtree(0).GetSubtree(0), sRoot)
    191                                           select ind).ToList()
    192                                        : (from ind in individuals
    193                                           let t = (ISymbolicExpressionTree)ind.Variables["SymbolicExpressionTree"].Value
    194                                           where t.Length >= s.Length && qm.Comparer.Equals(t.Root.GetSubtree(0).GetSubtree(0), sRoot)
    195                                           select ind).ToList();
     203      var filtered = new List<int>();
     204      for (int i = 0; i < individuals.Count; ++i) {
     205        if (exclusiveMatching && individuals[i].Variables.ContainsKey("AlreadyMatched")) continue;
     206        var t = trees[i];
     207        var tRoot = t.Root.GetSubtree(0).GetSubtree(0);
     208        if (t.Length < s.Length || !qm.Comparer.Equals(tRoot, sRoot)) continue;
     209        filtered.Add(i);
     210      }
    196211
    197212      // if we don't have enough filtered individuals, then we are done
     213      // if the schema exceeds the minimum frequency, it gets processed further
    198214      if (filtered.Count < countThreshold) {
    199215        return base.Apply();
     
    211227        Parallel.For(0, filtered.Count, new ParallelOptions { MaxDegreeOfParallelism = maxDegreeOfParallelism },
    212228          i => {
    213             var t = (ISymbolicExpressionTree)filtered[i].Variables["SymbolicExpressionTree"].Value;
     229            var index = filtered[i];
     230            var t = trees[index];
    214231            var tNodes = QueryMatch.InitializePostOrder(t.Root.GetSubtree(0).GetSubtree(0));
    215232            if (qm.Match(tNodes, sNodes)) {
     
    218235          });
    219236
    220         for (int i = 0; i < matching.Length; ++i) {
    221           if (matching[i])
    222             matchingIndividuals.Add(filtered[i]);
    223         }
     237        matchingIndividuals.AddRange(filtered.Where((x, i) => matching[i]).Select(x => individuals[x]));
    224238      } else {
    225239        for (int i = 0; i < filtered.Count; ++i) {
     
    228242            break;
    229243
    230           var ind = filtered[i];
    231           var t = (ISymbolicExpressionTree)ind.Variables["SymbolicExpressionTree"].Value;
    232           var tNodes = QueryMatch.InitializePostOrder(t.Root.GetSubtree(0).GetSubtree(0));
     244          var index = filtered[i];
     245          var tRoot = trees[index].Root.GetSubtree(0).GetSubtree(0);
     246          var tNodes = QueryMatch.InitializePostOrder(tRoot);
    233247          if (qm.Match(tNodes, sNodes))
    234             matchingIndividuals.Add(ind);
     248            matchingIndividuals.Add(individuals[index]);
    235249        }
    236250      }
    237251
    238       // additional condition: the average schema quality should be equal or greater than the population average quality
    239252      if (matchingIndividuals.Count < countThreshold) {
    240253        return base.Apply();
    241254      }
    242255
    243       var similarity = CalculatePhenotypicSimilarity(matchingIndividuals, calculator, executeInParallel, maxDegreeOfParallelism);
     256      var similarity = CalculateSimilarity(matchingIndividuals, calculator, executeInParallel, maxDegreeOfParallelism);
    244257      if (similarity < MinimumPhenotypicSimilarity.Value) {
    245258        return base.Apply();
    246259      }
    247260
    248       int n = (int)Math.Floor(matchingIndividuals.Count * ReplacementRatio.Value);
     261      double replacementRatio;
     262      var adaptiveReplacementRatio = UseAdaptiveReplacementRatioParameter.ActualValue.Value;
     263
     264      if (adaptiveReplacementRatio) {
     265        var r = (double)matchingIndividuals.Count / individuals.Count;
     266        replacementRatio = ReplacementRule(r);
     267      } else {
     268        replacementRatio = ReplacementRatio.Value;
     269      }
     270
     271      int n = (int)Math.Floor(matchingIndividuals.Count * replacementRatio);
    249272      var individualsToReplace = RandomReplacement.Value ? matchingIndividuals.SampleRandomWithoutRepetition(random, n).ToList()
    250273                                                         : matchingIndividuals.OrderBy(x => (DoubleValue)x.Variables["Quality"].Value).Take(n).ToList();
     
    265288    }
    266289
    267     private static double CalculatePhenotypicSimilarity(ScopeList individuals, SymbolicExpressionTreePhenotypicSimilarityCalculator calculator, bool parallel = false, int nThreads = -1) {
     290    public static double CalculateSimilarity(ScopeList individuals, ISolutionSimilarityCalculator calculator, bool parallel = false, int nThreads = -1) {
    268291      double similarity = 0;
    269292      int count = individuals.Count;
     293      if (count < 2) return double.NaN;
    270294      if (parallel) {
    271295        var parallelOptions = new ParallelOptions { MaxDegreeOfParallelism = nThreads };
Note: See TracChangeset for help on using the changeset viewer.