Free cookie consent management tool by TermsFeed Policy Generator

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/Boolean
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • 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();
Note: See TracChangeset for help on using the changeset viewer.