Ignore:
Timestamp:
11/19/15 11:42:02 (4 years ago)
Author:
gkronber
Message:

#2472: merged r12921,r12936:12938,r12940,r12947,r13055:13058,r13163,r13267,r13269 from trunk to stable

Location:
stable
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Problems.GeneticProgramming/3.3/Boolean/MultiplexerProblem.cs

    r12938 r13280  
    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
     
    6659      get { return true; }
    6760    }
     61
     62    #region item cloning and persistence
     63    // persistence
     64    [StorableConstructor]
     65    private MultiplexerProblem(bool deserializing) : base(deserializing) { }
     66    [StorableHook(HookType.AfterDeserialization)]
     67    private void AfterDeserialization() {
     68      RegisterEventHandlers();
     69    }
     70
     71    // cloning
     72    private MultiplexerProblem(MultiplexerProblem original, Cloner cloner)
     73      : base(original, cloner) {
     74      RegisterEventHandlers();
     75    }
     76    public override IDeepCloneable Clone(Cloner cloner) {
     77      return new MultiplexerProblem(this, cloner);
     78    }
     79    #endregion
     80
    6881
    6982    public MultiplexerProblem()
     
    98111
    99112      Encoding.Grammar = g;
     113
     114      BestKnownQuality = Math.Pow(2, NumberOfBits); // this is a benchmark problem (the best achievable quality is known for a given number of bits)
    100115    }
    101116
     
    103118    public override double Evaluate(ISymbolicExpressionTree tree, IRandom random) {
    104119      if (NumberOfBits <= 0) throw new NotSupportedException("Number of bits must be larger than zero.");
    105       if (NumberOfBits > 40) throw new NotSupportedException("Mupltiplexer does not support problems with number of bits > 37.");
     120      if (NumberOfBits > 37) throw new NotSupportedException("Multiplexer does not support problems with number of bits > 37.");
    106121      var bs = Enumerable.Range(0, (int)Math.Pow(2, NumberOfBits));
    107122      var addrBits = (int)Math.Log(NumberOfBits, 2); // largest power of two that fits into the number of bits
     
    132147
    133148    private static IEnumerable<bool> InterpretRec(ISymbolicExpressionTreeNode node, IEnumerable<int> bs, byte addrBits) {
    134       Func<ISymbolicExpressionTreeNode, Func<bool, bool>, IEnumerable<bool>> eval1 =
     149      Func<ISymbolicExpressionTreeNode, Func<bool, bool>, IEnumerable<bool>> unaryEval =
    135150        (child, f) => InterpretRec(child, bs, addrBits).Select(f);
    136       Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<bool, bool, bool>, IEnumerable<bool>> eval2 =
     151      Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode, Func<bool, bool, bool>, IEnumerable<bool>> binaryEval =
    137152        (left, right, f) => InterpretRec(left, bs, addrBits).Zip(InterpretRec(right, bs, addrBits), f);
    138153
    139154      switch (node.Symbol.Name) {
    140         case "AND": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y);
    141         case "OR": return eval2(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y);
    142         case "NOT": return eval1(node.GetSubtree(0), (x) => !x);
     155        case "AND": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x & y);
     156        case "OR": return binaryEval(node.GetSubtree(0), node.GetSubtree(1), (x, y) => x | y);
     157        case "NOT": return unaryEval(node.GetSubtree(0), (x) => !x);
    143158        case "IF": return EvalIf(node.GetSubtree(0), node.GetSubtree(1), node.GetSubtree(2), bs, addrBits);
    144159        default: {
     
    176191    }
    177192
    178     #region item cloning and persistence
    179     // persistence
    180     [StorableConstructor]
    181     private MultiplexerProblem(bool deserializing) : base(deserializing) { }
    182 
    183     [StorableHook(HookType.AfterDeserialization)]
    184     private void AfterDeserialization() {
    185       RegisterEventHandlers();
    186     }
    187 
    188     // cloning
    189     private MultiplexerProblem(MultiplexerProblem original, Cloner cloner)
    190       : base(original, cloner) {
    191       RegisterEventHandlers();
    192     }
    193     public override IDeepCloneable Clone(Cloner cloner) {
    194       return new MultiplexerProblem(this, cloner);
    195     }
    196     #endregion
    197 
    198193    #region events
    199 
    200194    private void RegisterEventHandlers() {
    201195      NumberOfBitsParameter.Value.ValueChanged += (sender, args) => UpdateGrammar();
Note: See TracChangeset for help on using the changeset viewer.