Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/03/10 14:30:55 (15 years ago)
Author:
gkronber
Message:

Separated MultiOperator into two classes MultiOperator and CheckedMultiOperator. #992 (CheckedItemList and CheckedItemCollection is necessary)

Location:
trunk/sources/HeuristicLab.Operators/3.3
Files:
3 edited
1 copied

Legend:

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

    r3588 r3591  
    3131namespace HeuristicLab.Operators {
    3232  /// <summary>
    33   /// A base class for operators which apply arbitrary many other operators of a specific type.
     33  /// A base class for operators which apply arbitrary many other operators of a specific type, operators can be checked/unchecked.
    3434  /// </summary>
    35   [Item("MultiOperator<T>", "A base class for operators which apply arbitrary many other operators of a specific type.")]
     35  [Item("CheckedMultiOperator<T>", "A base class for operators which apply arbitrary many other operators of a specific type.")]
    3636  [StorableClass]
    37   public abstract class MultiOperator<T> : SingleSuccessorOperator where T : class, IOperator {
    38     private List<IValueParameter<T>> operatorParameters;
    39 
    40     [Storable]
    41     private CheckedItemList<T> operators;
    42     public CheckedItemList<T> Operators {
    43       get { return operators; }
     37  public abstract class CheckedMultiOperator<T> : MultiOperator<T> where T : class, IOperator {
     38    /// <summary>
     39    /// Gets the operators of the checked multi operator
     40    /// </summary>
     41    public new ICheckedItemList<T> Operators {
     42      get { return (ICheckedItemList<T>)base.Operators; }
     43      protected set { base.Operators = value; }
    4444    }
    45 
    46     public MultiOperator()
     45    /// <summary>
     46    /// Creates a new instance of CheckedMultiOperator
     47    /// </summary>
     48    public CheckedMultiOperator()
    4749      : base() {
    48       operators = new CheckedItemList<T>();
    49       Initialize();
     50      Operators = new CheckedItemList<T>();
    5051    }
    5152    [StorableConstructor]
    52     protected MultiOperator(bool deserializing) : base(deserializing) { }
    53 
    54     [StorableHook(HookType.AfterDeserialization)]
    55     private void Initialize() {
    56       if (operators != null) RegisterOperatorsEvents();
    57       operatorParameters = new List<IValueParameter<T>>();
    58       for (int i = 0; i < Operators.Count; i++) {
    59         IValueParameter<T> opParam = (IValueParameter<T>)Parameters[i.ToString()];
    60         operatorParameters.Add(opParam);
    61         opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
    62       }
    63     }
    64 
    65     public override IDeepCloneable Clone(Cloner cloner) {
    66       MultiOperator<T> clone = (MultiOperator<T>)base.Clone(cloner);
    67       clone.operators = (CheckedItemList<T>)cloner.Clone(operators);
    68       clone.Initialize();
    69       return clone;
    70     }
    71 
    72     private void UpdateOperatorParameters() {
    73       foreach (IValueParameter<T> opParam in operatorParameters) {
    74         opParam.ValueChanged -= new EventHandler(opParam_ValueChanged);
    75         Parameters.Remove(opParam.Name);
    76       }
    77       operatorParameters.Clear();
    78       for (int i = 0; i < Operators.Count; i++) {
    79         IValueParameter<T> opParam = new ValueParameter<T>(i.ToString(), string.Empty, Operators[i]);
    80         opParam.ValueChanged += new EventHandler(opParam_ValueChanged);
    81         Parameters.Add(opParam);
    82         operatorParameters.Add(opParam);
    83       }
    84     }
    85 
    86     #region Events
    87     private void RegisterOperatorsEvents() {
    88       operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsAdded);
    89       operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsRemoved);
    90       operators.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsReplaced);
    91       operators.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsMoved);
    92       operators.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_CollectionReset);
    93     }
    94     private void DeregisterOperatorsEvents() {
    95       operators.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsAdded);
    96       operators.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsRemoved);
    97       operators.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsReplaced);
    98       operators.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsMoved);
    99       operators.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_CollectionReset);
    100     }
    101     protected virtual void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    102       UpdateOperatorParameters();
    103     }
    104     protected virtual void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    105       UpdateOperatorParameters();
    106     }
    107     protected virtual void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    108       foreach (IndexedItem<T> item in e.Items)
    109         operatorParameters[item.Index].Value = item.Value;
    110     }
    111     protected virtual void Operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    112       foreach (IndexedItem<T> item in e.Items)
    113         operatorParameters[item.Index].Value = item.Value;
    114     }
    115     protected virtual void Operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    116       UpdateOperatorParameters();
    117     }
    118     private void opParam_ValueChanged(object sender, EventArgs e) {
    119       IValueParameter<T> opParam = (IValueParameter<T>)sender;
    120       operators[operatorParameters.IndexOf(opParam)] = opParam.Value;
    121     }
    122     #endregion
     53    protected CheckedMultiOperator(bool deserializing) : base(deserializing) { }
    12354  }
    12455}
  • trunk/sources/HeuristicLab.Operators/3.3/HeuristicLab.Operators-3.3.csproj

    r3425 r3591  
    8585  </ItemGroup>
    8686  <ItemGroup>
     87    <Compile Include="CheckedMultiOperator.cs" />
    8788    <Compile Include="CombinedOperator.cs" />
    8889    <None Include="HeuristicLabOperatorsPlugin.cs.frame" />
  • trunk/sources/HeuristicLab.Operators/3.3/MultiOperator.cs

    r3568 r3591  
    3939
    4040    [Storable]
    41     private CheckedItemList<T> operators;
    42     public CheckedItemList<T> Operators {
     41    private IItemList<T> operators;
     42    public IItemList<T> Operators {
    4343      get { return operators; }
     44      protected set {
     45        if (operators != value) {
     46          if (value == null) throw new ArgumentException();
     47          DeregisterOperatorsEvents();
     48          operators = value;
     49          RegisterOperatorsEvents();
     50        }
     51      }
    4452    }
    4553
    4654    public MultiOperator()
    4755      : base() {
    48       operators = new CheckedItemList<T>();
     56      this.operators = new ItemList<T>();
    4957      Initialize();
    5058    }
     59
    5160    [StorableConstructor]
    5261    protected MultiOperator(bool deserializing) : base(deserializing) { }
     
    6574    public override IDeepCloneable Clone(Cloner cloner) {
    6675      MultiOperator<T> clone = (MultiOperator<T>)base.Clone(cloner);
    67       clone.operators = (CheckedItemList<T>)cloner.Clone(operators);
     76      clone.operators = (IItemList<T>)cloner.Clone(operators);
    6877      clone.Initialize();
    6978      return clone;
  • trunk/sources/HeuristicLab.Operators/3.3/StochasticMultiOperator.cs

    r3568 r3591  
    3636  [Item("StochasticMultiOperator<T>", "Base class for stochastic multi operators.")]
    3737  [StorableClass]
    38   public abstract class StochasticMultiOperator<T> : MultiOperator<T> where T : class, IOperator {
     38  public abstract class StochasticMultiOperator<T> : CheckedMultiOperator<T> where T : class, IOperator {
    3939    /// <summary>
    4040    /// Should return true if the StochasticMultiOperator should create a new child operation with the selected successor
     
    122122        throw new InvalidOperationException(Name + ": The list of probabilities has to match the number of operators");
    123123      }
     124      IOperator successor = null;
    124125      var checkedOperators = Operators.CheckedItems;
    125       if (checkedOperators.Count() == 0) {
    126         throw new InvalidOperationException(Name + ": At least one operator must be checked.");
    127       }
    128       double sum = (from indexedItem in checkedOperators select probabilities[indexedItem.Index]).Sum();
    129       if (sum == 0) throw new InvalidOperationException(Name + ": All selected operators have zero probability.");
    130       double r = random.NextDouble() * sum;
    131       sum = 0;
    132       IOperator successor = null;
    133       foreach (var indexedItem in checkedOperators) {
    134         sum += probabilities[indexedItem.Index];
    135         if (sum > r) {
    136           successor = indexedItem.Value;
    137           break;
     126      if (checkedOperators.Count() > 0) {
     127        // select a random operator from the checked operators
     128        double sum = (from indexedItem in checkedOperators select probabilities[indexedItem.Index]).Sum();
     129        if (sum == 0) throw new InvalidOperationException(Name + ": All selected operators have zero probability.");
     130        double r = random.NextDouble() * sum;
     131        sum = 0;
     132        foreach (var indexedItem in checkedOperators) {
     133          sum += probabilities[indexedItem.Index];
     134          if (sum > r) {
     135            successor = indexedItem.Value;
     136            break;
     137          }
    138138        }
    139139      }
Note: See TracChangeset for help on using the changeset viewer.