Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/10/10 03:39:02 (14 years ago)
Author:
swagner
Message:

Operator architecture refactoring (#95)

  • worked on parameters and operators
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Operators/3.3/StochasticBranch.cs

    r1530 r2773  
    2323using System.Collections.Generic;
    2424using System.Text;
     25using System.Xml;
    2526using HeuristicLab.Core;
    2627using HeuristicLab.Data;
     28using HeuristicLab.Parameters;
     29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2730
    2831namespace HeuristicLab.Operators {
    2932  /// <summary>
    30   /// Branch of (one or) two operators that have different probabilities to get executed.
     33  /// A branch of two operators which are executed with a specified probability.
    3134  /// </summary>
    32   public class StochasticBranch : OperatorBase {
    33     /// <inheritdoc select="summary"/>
    34     public override string Description {
    35       get { return @"TODO\r\nOperator description still missing ..."; }
     35  [Item("StochasticBranch", "A branch of two operators which are executed with a specified probability.")]
     36  [Creatable("Test")]
     37  [EmptyStorableClass]
     38  public class StochasticBranch : SingleSuccessorOperator {
     39    public LookupParameter<IRandom> RandomParameter {
     40      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
     41    }
     42    public ValueLookupParameter<DoubleData> ProbabilityParameter {
     43      get { return (ValueLookupParameter<DoubleData>)Parameters["Probability"]; }
     44    }
     45    protected OperatorParameter FirstBranchParameter {
     46      get { return (OperatorParameter)Parameters["FirstBranch"]; }
     47    }
     48    protected OperatorParameter SecondBranchParameter {
     49      get { return (OperatorParameter)Parameters["SecondBranch"]; }
     50    }
     51    public IOperator FirstBranch {
     52      get { return FirstBranchParameter.Value; }
     53      set { FirstBranchParameter.Value = value; }
     54    }
     55    public IOperator SecondBranch {
     56      get { return SecondBranchParameter.Value; }
     57      set { SecondBranchParameter.Value = value; }
    3658    }
    3759
    38     /// <summary>
    39     /// Initializes a new instance of <see cref="StochasticBranch"/> with two variable infos
    40     /// (<c>Random</c> and <c>Probability</c>).
    41     /// </summary>
    4260    public StochasticBranch()
    4361      : base() {
    44       AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(IRandom), VariableKind.In));
    45       AddVariableInfo(new VariableInfo("Probability", "Probability to choose first branch", typeof(DoubleData), VariableKind.In));
     62      Parameters.Add(new LookupParameter<IRandom>("Random", "A pseudo random number generator."));
     63      Parameters.Add(new ValueLookupParameter<DoubleData>("Probability", "The probability to execute the first branch."));
     64      Parameters.Add(new OperatorParameter("FirstBranch", "The operator which is executed with the given probability."));
     65      Parameters.Add(new OperatorParameter("SecondBranch", "The operator which is executed if the first branch is not executed."));
    4666    }
    4767
    48     /// <summary>
    49     /// Applies the operator of branch one with a specific probability on the given
    50     /// <paramref name="scope"/>, or - if existent - with another probability operator of branch two. 
    51     /// </summary>
    52     /// <param name="scope">The scope to apply the operators on.</param>
    53     /// <returns>A new <see cref="AtomicOperation"/> with either operator 1 or operator 2 applied
    54     /// to the given <paramref name="scope"/> or <c>null</c>.</returns>
    55     public override IOperation Apply(IScope scope) {
    56       IRandom random = GetVariableValue<IRandom>("Random", scope, true);
    57       DoubleData probability = GetVariableValue<DoubleData>("Probability", scope, true);
    58 
    59       bool result = random.NextDouble() < probability.Data;
    60       if ((result) && (SubOperators.Count > 0) && (SubOperators[0] != null))
    61         return new AtomicOperation(SubOperators[0], scope);
    62       else if ((!result) && (SubOperators.Count > 1) && (SubOperators[1] != null))
    63         return new AtomicOperation(SubOperators[1], scope);
    64       return null;
     68    public override IExecutionSequence Apply() {
     69      ExecutionContextCollection next = new ExecutionContextCollection(base.Apply());
     70      if (RandomParameter.ActualValue.NextDouble() < ProbabilityParameter.ActualValue.Value) {
     71        if (FirstBranch != null) next.Insert(0, new ExecutionContext(ExecutionContext.Parent, FirstBranch, ExecutionContext.Scope));
     72      } else {
     73        if (SecondBranch != null) next.Insert(0, new ExecutionContext(ExecutionContext.Parent, SecondBranch, ExecutionContext.Scope));
     74      }
     75      return next;
    6576    }
    6677  }
Note: See TracChangeset for help on using the changeset viewer.