Free cookie consent management tool by TermsFeed Policy Generator

Changeset 13267 for trunk/sources


Ignore:
Timestamp:
11/18/15 22:29:17 (9 years ago)
Author:
mkommend
Message:

#2472: Minor code improvements in HL.Problems.GP (typos, code organization, code unification).

Location:
trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/BasicSymbolicRegression/Problem.cs

    r12937 r13267  
    102102      return InterpretRec(tree.Root.GetSubtree(0).GetSubtree(0), dataset, rows);
    103103    }
    104 
    105 
     104                                                                                                       
    106105    private IEnumerable<double> InterpretRec(ISymbolicExpressionTreeNode node, IDataset dataset, IEnumerable<int> rows) {
    107       var eval = CreateEvalClosure(dataset, rows);
     106      Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<double, double, double>, IEnumerable<double>> binaryEval =
     107        (left, right, f) => InterpretRec(left, dataset,rows).Zip(InterpretRec(right, dataset, rows), f);
    108108
    109109      switch (node.Symbol.Name) {
    110         case "+": return eval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x + y);
    111         case "*": return eval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x * y);
    112         case "-": return eval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x - y);
    113         case "%": return eval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => y.IsAlmost(0.0) ? 0.0 : x / y); // protected division
     110        case "+": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x + y);
     111        case "*": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x * y);
     112        case "-": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x - y);
     113        case "%": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => y.IsAlmost(0.0) ? 0.0 : x / y); // protected division
    114114        default: {
    115115            double erc;
     
    124124    }
    125125
    126     private Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<double, double, double>, IEnumerable<double>> CreateEvalClosure(IDataset dataset, IEnumerable<int> rows) {
    127       // capture dataset and rows in scope
    128       return (a, b, f) => {
    129         var leftResult = InterpretRec(a, dataset, rows);
    130         var rightResult = InterpretRec(b, dataset, rows);
    131         return leftResult.Zip(rightResult, f);
    132       };
    133     }
    134 
    135126    #region item cloning and persistence
    136127    // persistence
  • trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/Boolean/EvenParityProblem.cs

    r13163 r13267  
    3838
    3939    #region parameter names
    40 
    4140    private const string NumberOfBitsParameterName = "NumberOfBits";
    42 
    4341    #endregion
    4442
     
    4745      get { return (IFixedValueParameter<IntValue>)Parameters[NumberOfBitsParameterName]; }
    4846    }
    49 
    5047    #endregion
    5148
    5249    #region Properties
    53 
    5450    public int NumberOfBits {
    5551      get { return NumberOfBitsParameter.Value.Value; }
    5652      set { NumberOfBitsParameter.Value.Value = value; }
    5753    }
    58 
    59 
    6054    #endregion
    6155
     
    110104    }
    111105
    112 
    113106    private static IEnumerable<bool> InterpretRec(ISymbolicExpressionTreeNode node, IEnumerable<int> bs) {
    114       Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<bool, bool, bool>, IEnumerable<bool>> eval2 =
     107      Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<bool, bool, bool>, IEnumerable<bool>> binaryEval =
    115108        (left, right, f) => InterpretRec(left, bs).Zip(InterpretRec(right, bs), f);
    116109
    117110      switch (node.Symbol.Name) {
    118         case "AND": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y);
    119         case "OR": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y);
    120         case "NAND": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x & y));
    121         case "NOR": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x | y));
     111        case "AND":  return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y);
     112        case "OR":   return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y);
     113        case "NAND": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x & y));
     114        case "NOR":  return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => !(x | y));
    122115        default: {
    123116            byte bitPos;
     
    154147
    155148    #region events
    156 
    157149    private void RegisterEventHandlers() {
    158150      NumberOfBitsParameter.Value.ValueChanged += (sender, args) => UpdateGrammar();
  • trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/Boolean/MultiplexerProblem.cs

    r13238 r13267  
    4040
    4141    #region parameter names
    42 
    4342    private const string NumberOfBitsParameterName = "NumberOfBits";
    44 
    4543    #endregion
    4644
    4745    #region Parameter Properties
    48 
    4946    public IFixedValueParameter<IntValue> NumberOfBitsParameter {
    5047      get { return (IFixedValueParameter<IntValue>)Parameters[NumberOfBitsParameterName]; }
    5148    }
    52 
    5349    #endregion
    5450
    5551    #region Properties
    56 
    5752    public int NumberOfBits {
    5853      get { return NumberOfBitsParameter.Value.Value; }
    5954      set { NumberOfBitsParameter.Value.Value = value; }
    6055    }
    61 
    62 
    6356    #endregion
    6457
     
    10598    public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) {
    10699      if (NumberOfBits <= 0) throw new NotSupportedException("Number of bits must be larger than zero.");
    107       if (NumberOfBits > 37) throw new NotSupportedException("Mupltiplexer does not support problems with number of bits > 37.");
     100      if (NumberOfBits > 37) throw new NotSupportedException("Multiplexer does not support problems with number of bits > 37.");
    108101      var bs = Enumerable.Range(0, (int)Math.Pow(2, NumberOfBits));
    109102      var addrBits = (int)Math.Log(NumberOfBits, 2); // largest power of two that fits into the number of bits
     
    134127
    135128    private static IEnumerable<bool> InterpretRec(ISymbolicExpressionTreeNode node, IEnumerable<int> bs, byte addrBits) {
    136       Func<ISymbolicExpressionTreeNode, Func<bool, bool>, IEnumerable<bool>> eval1 =
     129      Func<ISymbolicExpressionTreeNode, Func<bool, bool>, IEnumerable<bool>> unaryEval =
    137130        (child, f) => InterpretRec(child, bs, addrBits).Select(f);
    138       Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<bool, bool, bool>, IEnumerable<bool>> eval2 =
     131      Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<bool, bool, bool>, IEnumerable<bool>> binaryEval =
    139132        (left, right, f) => InterpretRec(left, bs, addrBits).Zip(InterpretRec(right, bs, addrBits), f);
    140133
    141134      switch (node.Symbol.Name) {
    142         case "AND": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y);
    143         case "OR": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y);
    144         case "NOT": return eval1(node.GetSubtree(0), (x) => !x);
    145         case "IF": return EvalIf(node.GetSubtree(0), node.GetSubtree(1), node.GetSubtree(2), bs, addrBits);
     135        case "AND": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y);
     136        case "OR":  return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y);
     137        case "NOT": return unaryEval(node.GetSubtree(0), (x) => !x);
     138        case "IF":  return EvalIf(node.GetSubtree(0), node.GetSubtree(1), node.GetSubtree(2), bs, addrBits);
    146139        default: {
    147140            if (node.Symbol.Name[0] == 'a') {
     
    199192
    200193    #region events
    201 
    202194    private void RegisterEventHandlers() {
    203195      NumberOfBitsParameter.Value.ValueChanged += (sender, args) => UpdateGrammar();
  • trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/LawnMower/Interpreter.cs

    r12911 r13267  
    2626
    2727namespace HeuristicLab.Problems.GeneticProgramming.LawnMower {
    28   public class Interpreter {
     28  public static class Interpreter {
    2929    private enum Heading {
    3030      South,
     
    4545    }
    4646
    47 
    4847    public static bool[,] EvaluateLawnMowerProgram(int length, int width, ISymbolicExpressionTree tree) {
    49 
    5048      bool[,] lawn = new bool[length, width];
    5149      var mowerState = new MowerState();
     
    5654      return lawn;
    5755    }
    58 
    5956
    6057    private static Tuple<int, int> EvaluateLawnMowerProgram(ISymbolicExpressionTreeNode node, MowerState mowerState, bool[,] lawn, IEnumerable<ISymbolicExpressionTreeNode> adfs) {
  • trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/LawnMower/Problem.cs

    r12911 r13267  
    5151
    5252    [StorableConstructor]
    53     protected Problem(bool deserializing)
    54       : base(deserializing) {
     53    protected Problem(bool deserializing) : base(deserializing) { }
     54    [StorableHook(HookType.AfterDeserialization)]
     55    private void AfterDeserialization() { }
     56
     57    protected Problem(Problem original, Cloner cloner) : base(original, cloner) { }
     58    public override IDeepCloneable Clone(Cloner cloner) {
     59      return new Problem(this, cloner);
    5560    }
    56     protected Problem(Problem original, Cloner cloner)
    57       : base(original, cloner) {
    58     }
     61
    5962    public Problem()
    6063      : base() {
     
    8790    }
    8891
    89 
    90     [StorableHook(HookType.AfterDeserialization)]
    91     private void AfterDeserialization() { }
    92 
    9392    public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) {
    9493      var length = LawnLengthParameter.Value.Value;
     
    105104      return numberOfMowedCells;
    106105    }
    107 
    108     public override IDeepCloneable Clone(Cloner cloner) {
    109       return new Problem(this, cloner);
    110     }
    111106  }
    112107}
  • trunk/sources/HeuristicLab.Problems.GeneticProgramming/3.3/LawnMower/Solution.cs

    r12911 r13267  
    3939    [StorableConstructor]
    4040    private Solution(bool deserializing) : base(deserializing) { }
     41    [StorableHook(HookType.AfterDeserialization)]
     42    private void AfterDeserialization() { }
     43
    4144    private Solution(Solution original, Cloner cloner)
    4245      : base(original, cloner) {
     
    4548      this.Tree = cloner.Clone(original.Tree);
    4649      this.Quality = original.Quality;
     50    }
     51    public override IDeepCloneable Clone(Cloner cloner) {
     52      return new Solution(this, cloner);
    4753    }
    4854
     
    5460      this.Quality = quality;
    5561    }
    56     [StorableHook(HookType.AfterDeserialization)]
    57     private void AfterDeserialization() {
    58     }
    59     public override IDeepCloneable Clone(Cloner cloner) {
    60       return new Solution(this, cloner);
    61     }
    6262  }
    6363}
Note: See TracChangeset for help on using the changeset viewer.