Changeset 7106


Ignore:
Timestamp:
11/30/11 17:23:19 (8 years ago)
Author:
bburlacu
Message:

#1683: Implemented the SemanticSimilarityCrossover.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionSemanticSimilarityCrossover.cs

    r7089 r7106  
    2424using System.Linq;
    2525using System.Text;
     26using HeuristicLab.Parameters;
    2627using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2728using HeuristicLab.Common;
     
    3435  [Item("SemanticSimilarityCrossover", "An operator which performs subtree swapping based on semantic similarity.")]
    3536  public sealed class SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
     37    private const string SemanticSimilarityLowerBoundParameterName = "SemanticSimilarityLowerBound";
     38    private const string SemanticSimilarityUpperBoundParameterName = "SemanticSimilarityUpperBound";
     39
     40    #region Parameter properties
     41    public IValueParameter<DoubleValue> SemanticSimilarityLowerBoundParameter {
     42      get { return (IValueParameter<DoubleValue>)Parameters[SemanticSimilarityLowerBoundParameterName]; }
     43    }
     44    public IValueParameter<DoubleValue> SemanticSimilarityUpperBoundParameter {
     45      get { return (IValueParameter<DoubleValue>)Parameters[SemanticSimilarityUpperBoundParameterName]; }
     46    }
     47    #endregion
     48
     49    #region Properties
     50    public DoubleValue SemanticSimilarityLowerBound {
     51      get { return SemanticSimilarityLowerBoundParameter.Value; }
     52    }
     53    public DoubleValue SemanticSimilarityUpperBound {
     54      get { return SemanticSimilarityUpperBoundParameter.Value; }
     55    }
     56    #endregion
     57
    3658    [StorableConstructor]
    3759    private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(bool deserializing) : base(deserializing) { }
    3860    private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> original, Cloner cloner) : base(original, cloner) { }
    39     public SymbolicDataAnalysisExpressionSemanticSimilarityCrossover() : base() { }
     61    public SymbolicDataAnalysisExpressionSemanticSimilarityCrossover()
     62      : base() {
     63      Parameters.Add(new ValueLookupParameter<DoubleValue>(SemanticSimilarityLowerBoundParameterName, "The lower bound of the semantic similarity interval."));
     64      Parameters.Add(new ValueLookupParameter<DoubleValue>(SemanticSimilarityUpperBoundParameterName, "The lower bound of the semantic similarity interval."));
     65
     66      // Set some decent default values for the lower and upper bound parameters
     67      SemanticSimilarityLowerBoundParameter.Value = new DoubleValue(0.0001);
     68      SemanticSimilarityUpperBoundParameter.Value = new DoubleValue(10);
     69    }
    4070    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T>(this, cloner); }
    4171
     
    4474      IEnumerable<int> rows = GenerateRowsToEvaluate();
    4575      T problemData = ProblemDataParameter.ActualValue;
    46       return Cross(random, parent0, parent1, interpreter, problemData, rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value);
     76      return Cross(random, parent0, parent1, interpreter, problemData, rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value, SemanticSimilarityLowerBoundParameter.Value.Value, SemanticSimilarityUpperBoundParameter.Value.Value);
    4777    }
    4878    /// <summary>
     
    5181    /// </summary>
    5282    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
    53                                                 ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) {
    54       List<CutPoint> crossoverPoints0 = new List<CutPoint>();
     83                                                ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength, double lower, double upper) {
     84      var crossoverPoints0 = new List<CutPoint>();
    5585      parent0.Root.ForEachNodePostfix((n) => {
    5686        if (n.Subtrees.Any() && n != parent0.Root)
     
    5888            crossoverPoints0.Add(new CutPoint(n, child));
    5989      });
    60       CutPoint crossoverPoint0 = crossoverPoints0[random.Next(crossoverPoints0.Count)];
     90      var crossoverPoint0 = crossoverPoints0[random.Next(crossoverPoints0.Count)];
    6191      int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child);
    6292      int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength();
    6393
    64       List<ISymbolicExpressionTreeNode> allowedBranches = new List<ISymbolicExpressionTreeNode>();
     94      var allowedBranches = new List<ISymbolicExpressionTreeNode>();
    6595      parent1.Root.ForEachNodePostfix((n) => {
    6696        if (n.Subtrees.Any() && n != parent1.Root)
     
    83113      var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol);
    84114      IEnumerable<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows);
    85 
     115      crossoverPoint0.Child.Parent = crossoverPoint0.Parent; // restore parent
    86116      ISymbolicExpressionTreeNode selectedBranch = null;
    87117
    88118      // pick the first node that fulfills the semantic similarity conditions
    89119      foreach (var node in allowedBranches) {
    90         var tree1 = CreateTreeFromNode(random, node, startSymbol, rootSymbol);
     120        var parent = node.Parent;
     121        var tree1 = CreateTreeFromNode(random, node, startSymbol, rootSymbol); // this will affect node.Parent
    91122        IEnumerable<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows);
     123        node.Parent = parent; // restore parent
    92124
    93         //double ssd = SSD(estimatedValues0, estimatedValues1);
    94125        OnlineCalculatorError errorState;
    95126        double ssd = OnlineMeanAbsoluteErrorCalculator.Calculate(estimatedValues0, estimatedValues1, out errorState);
    96127
    97         // TODO: introduce parameters alpha and beta for establishing the interval boundaries. the best values need to be determined experimentally
    98         if (0.0001 < ssd && ssd < 10) {
     128        if (lower < ssd && ssd < upper) {
    99129          selectedBranch = node;
    100130          break;
    101131        }
    102132      }
     133
     134      if (selectedBranch == null)
     135        return parent0;
    103136
    104137      // perform the actual swap
Note: See TracChangeset for help on using the changeset viewer.