Ignore:
Timestamp:
01/11/16 21:29:25 (5 years ago)
Author:
bburlacu
Message:

#1772: Update diversification operators (add extra evaluations to the evaluation counter, add the option for strict schema matching, add additional replacement ratio update rules.

File:
1 edited

Legend:

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

    r13480 r13496  
    5151    private const string UseAdaptiveReplacementRatioParameterName = "UseAdaptiveReplacementRatio";
    5252    private const string ReplacementRatioUpdateRuleParameterName = "ReplacementRatioUpdateRule";
     53    private const string StrictSchemaMatchingParameterName = "StrictSchemaMatching";
    5354    #endregion
    5455
     
    6061    public IFixedValueParameter<BoolValue> UseAdaptiveReplacementRatioParameter {
    6162      get { return (IFixedValueParameter<BoolValue>)Parameters[UseAdaptiveReplacementRatioParameterName]; }
     63    }
     64    public IFixedValueParameter<BoolValue> StrictSchemaMatchingParameter {
     65      get { return (IFixedValueParameter<BoolValue>)Parameters[StrictSchemaMatchingParameterName]; }
    6266    }
    6367    public IFixedValueParameter<BoolValue> ExclusiveMatchingParameter {
     
    104108    public bool ExecuteInParallel { get { return ExecuteInParallelParameter.Value.Value; } }
    105109    public double PercentageOfPopulation { get { return PercentageOfPopulationParameter.Value.Value; } }
     110    public bool StrictSchemaMatching { get { return StrictSchemaMatchingParameter.Value.Value; } }
    106111    #endregion
    107112
    108113    private UpdateEstimatedValuesOperator updateEstimatedValuesOperator;
    109114    private DiversificationStatisticsOperator diversificationStatisticsOperator;
     115
     116    public override void ClearState() {
     117      NumberOfChangedTreesParameter.Value.Value = 0;
     118      NumberOfChangedTreesParameter.Value.Value = 0;
     119      AverageSchemaLengthParameter.Value.Value = 0;
     120      base.ClearState();
     121    }
    110122
    111123    public SchemaCreator() {
     
    121133      Parameters.Add(new FixedValueParameter<BoolValue>(ScaleEstimatedValuesParameterName, new BoolValue(true)));
    122134      Parameters.Add(new FixedValueParameter<BoolValue>(ExclusiveMatchingParameterName, new BoolValue(false)));
     135      Parameters.Add(new FixedValueParameter<BoolValue>(StrictSchemaMatchingParameterName, new BoolValue(false)));
    123136      Parameters.Add(new ValueParameter<IntValue>(NumberOfChangedTreesParameterName, new IntValue(0)));
    124137      Parameters.Add(new ValueParameter<IntValue>(NumberOfSchemasParameterName, new IntValue(0)));
     
    126139      Parameters.Add(new FixedValueParameter<BoolValue>(UseAdaptiveReplacementRatioParameterName, new BoolValue(true)));
    127140
    128       var replacementRatioUpdateRules = new ItemSet<StringValue>(new[] { new StringValue("f(x) = x"), new StringValue("f(x) = tanh(x)"), new StringValue("f(x) = tanh(2x)"), new StringValue("f(x) = tanh(3x)") });
     141      var replacementRatioUpdateRules = new ItemSet<StringValue>(new[] {
     142        new StringValue("f(x) = x"),
     143        new StringValue("f(x) = tanh(x)"),
     144        new StringValue("f(x) = tanh(2x)"),
     145        new StringValue("f(x) = tanh(3x)"),
     146        new StringValue("f(x) = tanh(4x)"),
     147        new StringValue("f(x) = 1-sqrt(1-x)")
     148      });
    129149      Parameters.Add(new ConstrainedValueParameter<StringValue>(ReplacementRatioUpdateRuleParameterName, replacementRatioUpdateRules));
    130150      #endregion
     
    146166    protected SchemaCreator(bool deserializing) : base(deserializing) { }
    147167
     168
     169    [StorableHook(HookType.AfterDeserialization)]
     170    private void AfterDeserialization() {
     171      if (!Parameters.ContainsKey(StrictSchemaMatchingParameterName))
     172        Parameters.Add(new FixedValueParameter<BoolValue>(StrictSchemaMatchingParameterName, new BoolValue(false)));
     173    }
     174
    148175    public override IOperation Apply() {
    149176      // apply only when at least one generation has passed
     
    153180
    154181      var n = (int)Math.Round(ExecutionContext.Scope.SubScopes.Count * PercentageOfPopulation);
    155       var scopes = new ScopeList(ExecutionContext.Scope.SubScopes.Take(n));
    156182
    157183      var updateEstimatedValues = new OperationCollection { Parallel = true };
     
    159185        updateEstimatedValuesOperator = new UpdateEstimatedValuesOperator();
    160186
    161       foreach (var s in scopes) {
    162         if (!s.Variables.ContainsKey("EstimatedValues"))
    163           s.Variables.Add(new Core.Variable("EstimatedValues"));
     187      foreach (var s in ExecutionContext.Scope.SubScopes.Where(s => !s.Variables.ContainsKey("EstimatedValues"))) {
    164188        updateEstimatedValues.Add(ExecutionContext.CreateChildOperation(updateEstimatedValuesOperator, s));
    165189      }
     
    185209            break;
    186210          }
     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          }
    187219        default:
    188220          throw new ArgumentException("Unknown replacement rule");
     
    192224
    193225      // for now, only consider crossover offspring
     226      var scopes = new ScopeList(ExecutionContext.Scope.SubScopes.OrderByDescending(x => ((DoubleValue)x.Variables["Quality"].Value).Value).Take(n));
    194227      var vertices = from s in scopes
    195228                     let t = (ISymbolicExpressionTree)s.Variables["SymbolicExpressionTree"].Value
     
    222255    public static IEnumerable<ISymbolicExpressionTree> GenerateSchemas(IEnumerable<IGenealogyGraphNode<ISymbolicExpressionTree>> vertices, int minimumSchemaLength) {
    223256      var anySubtreeSymbol = new AnySubtreeSymbol();
    224       //      var anyNodeSymbol = new AnyNodeSymbol();
     257      //            var anyNodeSymbol = new AnyNodeSymbol();
    225258      var groups = vertices.GroupBy(x => x.Parents.First()).OrderByDescending(g => g.Count()).ToList();
    226259      var hash = new HashSet<string>();
     
    246279            continue;
    247280
    248           //          var replacement = anySubtreeSymbol.CreateTreeNode();
    249           //          ReplaceSubtree(node, replacement, false);
    250           var replacement = new AnyNodeSymbol(node.Symbol.MinimumArity, node.Symbol.MinimumArity).CreateTreeNode();
    251           ReplaceSubtree(node, replacement, true);
     281          var replacement = anySubtreeSymbol.CreateTreeNode();
     282          ReplaceSubtree(node, replacement, false);
     283          //          var replacement = new AnyNodeSymbol(node.Symbol.MinimumArity, node.Symbol.MinimumArity).CreateTreeNode();
     284          //          ReplaceSubtree(node, replacement, true);
    252285          replaced = true;
    253286        }
Note: See TracChangeset for help on using the changeset viewer.