Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17076 for trunk


Ignore:
Timestamp:
07/05/19 10:26:41 (5 years ago)
Author:
gkronber
Message:

#2950: made some small changes while reviewing.

Location:
trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDiversityPreservingCrossover.cs

    r16980 r17076  
    4242    private const string StrictHashingParameterName = "StrictHashing";
    4343
    44     private static readonly Func<byte[], ulong> hashFunction = HashUtil.JSHash;
     44    private static readonly Func<byte[], ulong> hashFunction = HashUtil.DJBHash;
    4545
    4646    #region Parameter Properties
     
    8888    }
    8989
    90     public SymbolicDataAnalysisExpressionDiversityPreservingCrossover() {
    91       name = "DiversityCrossover";
     90    public SymbolicDataAnalysisExpressionDiversityPreservingCrossover() : base() {
    9291      Parameters.Add(new ValueLookupParameter<PercentValue>(InternalCrossoverPointProbabilityParameterName, "The probability to select an internal crossover point (instead of a leaf node).", new PercentValue(0.9)));
    9392      Parameters.Add(new ValueLookupParameter<BoolValue>(WindowingParameterName, "Use proportional sampling with windowing for cutpoint selection.", new BoolValue(false)));
     
    9695    }
    9796
    98     private SymbolicDataAnalysisExpressionDiversityPreservingCrossover(SymbolicDataAnalysisExpressionDiversityPreservingCrossover<T> original, Cloner cloner) : base(original, cloner) {
    99     }
     97    private SymbolicDataAnalysisExpressionDiversityPreservingCrossover(SymbolicDataAnalysisExpressionDiversityPreservingCrossover<T> original, Cloner cloner) : base(original, cloner) { }
    10098
    10199    public override IDeepCloneable Clone(Cloner cloner) {
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/HashExtensions.cs

    r16983 r17076  
    2525namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
    2626  public static class SymbolicExpressionHashExtensions {
     27    /// <summary>
     28    /// Holds data that is necessary to handle tree nodes in hashing / simplification.
     29    /// </summary>
     30    /// <typeparam name="T">The tree node type</typeparam>
    2731    public sealed class HashNode<T> : IComparable<HashNode<T>>, IEquatable<HashNode<T>> where T : class {
    2832      public T Data;
     
    3842      public SimplifyAction Simplify;
    3943
    40       //public IComparer<T> Comparer;
    41 
    4244      public bool IsLeaf => Arity == 0;
    43 
    44       //public HashNode(IComparer<T> comparer) {
    45       //  Comparer = comparer;
    46       //}
    47 
    48       //public HashNode() { }
    4945
    5046      public int CompareTo(HashNode<T> other) {
     
    170166
    171167    /// <summary>
    172     /// Get a function node's child indicest
     168    /// Get a function node's child indices
    173169    /// </summary>
    174170    /// <typeparam name="T">The data type encapsulated by a hash node</typeparam>
    175     /// <param name="nodes">An array of hash nodes with up-to-date node sizes</param>
     171    /// <param name="nodes">An array of hash nodes with up-to-date node sizes (see UpdateNodeSizes)</param>
    176172    /// <param name="i">The index in the array of hash nodes of the node whose children we want to iterate</param>
    177173    /// <returns>An array containing child indices</returns>
     
    188184    }
    189185
     186    /// <summary>
     187    /// Determines size of each branch and sets the results for each node.
     188    /// </summary>
     189    /// <typeparam name="T">The data type encapsulated by a hash node</typeparam>
     190    /// <param name="nodes">An array of hash nodes in postfix order.</param>
     191    /// <returns>The array with updated node sizes. The array is not copied.</returns>
    190192    public static HashNode<T>[] UpdateNodeSizes<T>(this HashNode<T>[] nodes) where T : class {
    191193      for (int i = 0; i < nodes.Length; ++i) {
     
    196198        }
    197199        node.Size = node.Arity;
    198 
     200        // visit all children and sum up their size (assumes postfix order).
    199201        for (int j = i - 1, k = 0; k < node.Arity; j -= 1 + nodes[j].Size, ++k) {
    200202          node.Size += nodes[j].Size;
     
    204206    }
    205207
     208    // disables duplicate branches and removes the disabled nodes
    206209    public static HashNode<T>[] Reduce<T>(this HashNode<T>[] nodes) where T : class {
    207210      int count = 0;
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/SymbolicExpressionTreeHash.cs

    r16983 r17076  
    4848      ulong hashFunction(byte[] input) => HashUtil.DJBHash(input);
    4949
    50       var hashNodes = simplify ? node.MakeNodes(strict).Simplify(hashFunction) : node.MakeNodes(strict).Sort(hashFunction);
     50      var hashNodes = simplify ? node.MakeNodes(strict).Simplify(hashFunction) : node.MakeNodes(strict).Sort(hashFunction); // simplify sorts implicitly
    5151      var hashes = new ulong[hashNodes.Length];
    5252      for (int i = 0; i < hashes.Length; ++i) {
     
    210210      var treeNodes = nodes.Select(x => x.Data.Symbol.CreateTreeNode()).ToArray();
    211211
     212      // construct tree top down (assumes postfix order for nodes)
    212213      for (int i = nodes.Length - 1; i >= 0; --i) {
    213214        var node = nodes[i];
     
    244245    // (in other words simplification should be applied in a bottom-up fashion)
    245246    public static ISymbolicExpressionTree Simplify(ISymbolicExpressionTree tree) {
    246       ulong hashFunction(byte[] bytes) => HashUtil.JSHash(bytes);
    247       var root = tree.Root.GetSubtree(0).GetSubtree(0);
     247      ulong hashFunction(byte[] bytes) => HashUtil.DJBHash(bytes);
     248      var root = tree.ActualRoot();
    248249      var nodes = root.MakeNodes();
    249250      var simplified = nodes.Simplify(hashFunction);
     
    369370        nodes[i].Enabled = false;
    370371      } else if ((parentSymbol is Exponential && childSymbol is Logarithm) || (parentSymbol is Logarithm && childSymbol is Exponential)) {
     372        // exp(log(x)) == x only for positive x. We consider this as equivalent for hashing anyway.
    371373        child.Enabled = parent.Enabled = false;
    372374      }
  • trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Selectors/DiversitySelector.cs

    r16839 r17076  
    6666    }
    6767
    68     public bool StrictSimilarity { get { return StrictSimilarityParameter.Value.Value; } }
    69 
    70     public double SimilarityWeight { get { return SimilarityWeightParameter.Value.Value; } }
     68    public bool StrictSimilarity {
     69      get { return StrictSimilarityParameter.Value.Value; }
     70      set { StrictSimilarityParameter.Value.Value = value; }
     71    }
     72
     73    public double SimilarityWeight {
     74      get { return SimilarityWeightParameter.Value.Value; }
     75      set { SimilarityWeightParameter.Value.Value = value; }
     76    }
    7177
    7278    public DiversitySelector() : base() {
     
    7480      Parameters.Add(new FixedValueParameter<DoubleValue>(SimilarityWeightParameterName, "Weight of the diversity term.", new DoubleValue(1)));
    7581      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees that should be analyzed."));
    76       Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(DiversityParameterName));
    7782      Parameters.Add(new ValueParameter<ISingleObjectiveSelector>(SelectorParameterName, "The inner selection operator to select the parents.", new TournamentSelector()));
     83      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(DiversityParameterName, "The diversity value calcuated by the operator (output). The inner selector uses this value."));
    7884
    7985      RegisterParameterEventHandlers();
     
    8187
    8288    [StorableConstructor]
    83     private DiversitySelector(StorableConstructorFlag deserializing) : base(deserializing) { }
     89    private DiversitySelector(StorableConstructorFlag _) : base(_) { }
    8490
    8591    private DiversitySelector(DiversitySelector original, Cloner cloner) : base(original, cloner) { }
     
    9197    [StorableHook(HookType.AfterDeserialization)]
    9298    private void AfterDeserialization() {
    93       RegisterParameterEventHandlers();
    94 
    9599      if (!Parameters.ContainsKey(DiversityParameterName)) {
    96100        Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(DiversityParameterName));
    97101      }
     102
     103      RegisterParameterEventHandlers();
    98104    }
    99105
     
    103109      CopySelectedParameter.ValueChanged += CopySelectedParameter_ValueChanged;
    104110      CopySelected.ValueChanged += CopySelected_ValueChanged;
    105     }
     111
     112      MaximizationParameter.NameChanged += MaximizationParameter_NameChanged;
     113      QualityParameter.NameChanged += QualityParameter_NameChanged;
     114      RandomParameter.NameChanged += RandomParameter_NameChanged;
     115    }
     116
     117    private void RandomParameter_NameChanged(object sender, EventArgs e) { ParameterizeSelector(Selector); }
     118    private void QualityParameter_NameChanged(object sender, EventArgs e) { ParameterizeSelector(Selector); }
     119    private void MaximizationParameter_NameChanged(object sender, EventArgs e) { ParameterizeSelector(Selector); }
    106120
    107121    private void CopySelectedParameter_ValueChanged(object sender, EventArgs e) {
     
    127141      if (w.IsAlmost(0)) {
    128142        ApplyInnerSelector();
    129         return CurrentScope.SubScopes[1].SubScopes.ToArray();
     143        return CurrentScope.SubScopes[1].SubScopes.ToArray();  // return selected individuals (selectors create two sub-scopes with remaining and selected)
    130144      }
    131145
Note: See TracChangeset for help on using the changeset viewer.