Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/24/16 21:53:06 (9 years ago)
Author:
bburlacu
Message:

#1772: Slight refactor of schema diversification operators to try to fix potential problems with serialization/cloning

Location:
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification
Files:
2 edited

Legend:

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

    r13527 r13565  
    189189      }
    190190
    191       Func<double, double> rule;
    192191      var replacementRule = ReplacementRatioUpdateRuleParameter.Value.Value;
    193 
    194       switch (replacementRule) {
    195         case "f(x) = x": {
    196             rule = x => x;
    197             break;
    198           }
    199         case "f(x) = tanh(x)": {
    200             rule = x => Math.Tanh(x);
    201             break;
    202           }
    203         case "f(x) = tanh(2x)": {
    204             rule = x => Math.Tanh(2 * x);
    205             break;
    206           }
    207         case "f(x) = tanh(3x)": {
    208             rule = x => Math.Tanh(3 * x);
    209             break;
    210           }
    211         case "f(x) = tanh(4x)": {
    212             rule = x => Math.Tanh(4 * x);
    213             break;
    214           }
    215         case "f(x) = 1-sqrt(1-x)": {
    216             rule = x => 1 - Math.Sqrt(1 - x);
    217             break;
    218           }
    219         default:
    220           throw new ArgumentException("Unknown replacement rule");
    221       }
    222192
    223193      var evaluateSchemas = new OperationCollection();
     
    235205      #region create schemas and add subscopes representing the individuals
    236206      foreach (var schema in schemas) {
    237         evaluateSchemas.Add(ExecutionContext.CreateChildOperation(new SchemaEvaluator { Schema = schema, ReplacementRule = rule }, ExecutionContext.Scope));
     207        evaluateSchemas.Add(ExecutionContext.CreateChildOperation(new SchemaEvaluator { Schema = schema, ReplacementRule = replacementRule }, ExecutionContext.Scope));
    238208      }
    239209      #endregion
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tracking/SchemaDiversification/SchemaEvaluator.cs

    r13527 r13565  
    132132    #endregion
    133133
    134     private readonly SymbolicExpressionTreePhenotypicSimilarityCalculator calculator = new SymbolicExpressionTreePhenotypicSimilarityCalculator();
    135     private readonly QueryMatch qm;
    136 
    137     public Func<double, double> ReplacementRule { get; set; }
    138 
    139     private readonly ISymbolicExpressionTreeNodeEqualityComparer comp = new SymbolicExpressionTreeNodeEqualityComparer {
    140       MatchConstantValues = false,
    141       MatchVariableWeights = false,
    142       MatchVariableNames = true
    143     };
     134    private QueryMatch qm;
     135
     136    [Storable]
     137    private SymbolicExpressionTreePhenotypicSimilarityCalculator calculator;
     138
     139    [Storable]
     140    public string ReplacementRule { get; set; }
     141
     142    [Storable]
     143    private ISymbolicExpressionTreeNodeEqualityComparer comparer;
    144144
    145145    [Storable]
     
    153153      if (!Parameters.ContainsKey(StrictSchemaMatchingParameterName))
    154154        Parameters.Add(new LookupParameter<BoolValue>(StrictSchemaMatchingParameterName));
     155
     156      if (calculator == null)
     157        calculator = new SymbolicExpressionTreePhenotypicSimilarityCalculator();
     158
     159      if (comparer == null)
     160        comparer = new SymbolicExpressionTreeNodeEqualityComparer { MatchVariableNames = true, MatchVariableWeights = true, MatchConstantValues = false };
     161
     162      qm = new QueryMatch(comparer) { MatchParents = true };
    155163    }
    156164
    157165    public SchemaEvaluator() {
    158       qm = new QueryMatch(comp) { MatchParents = true };
    159       this.updateQualityOperator = new UpdateQualityOperator();
     166      calculator = new SymbolicExpressionTreePhenotypicSimilarityCalculator();
     167      comparer = new SymbolicExpressionTreeNodeEqualityComparer { MatchVariableNames = true, MatchVariableWeights = true, MatchConstantValues = false };
     168      qm = new QueryMatch(comparer) { MatchParents = true };
     169      updateQualityOperator = new UpdateQualityOperator();
    160170      #region add parameters
    161171      Parameters.Add(new LookupParameter<ISymbolicExpressionTree>(SchemaParameterName, "The current schema to be evaluated"));
     
    186196
    187197    protected SchemaEvaluator(SchemaEvaluator original, Cloner cloner) : base(original, cloner) {
    188       this.comp = new SymbolicExpressionTreeNodeEqualityComparer();
    189       this.qm = new QueryMatch(comp) { MatchParents = original.qm?.MatchParents ?? true };
    190       this.updateQualityOperator = new UpdateQualityOperator();
     198      calculator = cloner.Clone(original.calculator);
     199      comparer = cloner.Clone(original.comparer);
     200      qm = new QueryMatch(comparer) { MatchParents = true };
     201      updateQualityOperator = new UpdateQualityOperator();
    191202      Schema = original.Schema;
    192203    }
     
    199210      var strictSchemaMatching = StrictSchemaMatchingParameter.ActualValue.Value;
    200211      if (strictSchemaMatching) {
    201         comp.MatchVariableWeights = true;
    202         comp.MatchConstantValues = true;
     212        comparer.MatchVariableWeights = true;
     213        comparer.MatchConstantValues = true;
    203214      } else {
    204         comp.MatchVariableWeights = false;
    205         comp.MatchConstantValues = false;
     215        comparer.MatchVariableWeights = false;
     216        comparer.MatchConstantValues = false;
    206217      }
    207218
     
    281292      if (adaptiveReplacementRatio) {
    282293        var r = (double)matchingIndividuals.Count / individuals.Count;
    283         replacementRatio = ReplacementRule(r);
     294        replacementRatio = CalculateReplacementRatio(r);
    284295      } else {
    285296        replacementRatio = ReplacementRatio.Value;
     
    305316    }
    306317
     318    private double CalculateReplacementRatio(double r) {
     319      switch (ReplacementRule) {
     320        case "f(x) = x": {
     321            return r;
     322          }
     323        case "f(x) = tanh(x)": {
     324            return Math.Tanh(r);
     325          }
     326        case "f(x) = tanh(2x)": {
     327            return Math.Tanh(2 * r);
     328          }
     329        case "f(x) = tanh(3x)": {
     330            return Math.Tanh(3 * r);
     331          }
     332        case "f(x) = tanh(4x)": {
     333            return Math.Tanh(4 * r);
     334          }
     335        case "f(x) = 1-sqrt(1-x)": {
     336            return 1 - Math.Sqrt(1 - r);
     337          }
     338        default:
     339          throw new ArgumentException("Unknown replacement rule");
     340      }
     341    }
     342
    307343    public static double CalculateSimilarity(ScopeList individuals, ISolutionSimilarityCalculator calculator, bool parallel = false, int nThreads = -1) {
    308344      double similarity = 0;
Note: See TracChangeset for help on using the changeset viewer.