Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/20/10 16:35:42 (15 years ago)
Author:
abeham
Message:

Updated StochasticMultiOperator and Multicrossover/Multimutation #976

Location:
trunk/sources/HeuristicLab.Operators/3.3
Files:
2 edited

Legend:

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

    r3407 r3445  
    8686    #region Events
    8787    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);
     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);
    9393    }
    9494    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);
     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);
    100100    }
    101     private void operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
     101    protected virtual void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    102102      UpdateOperatorParameters();
    103103    }
    104     private void operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
     104    protected virtual void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    105105      UpdateOperatorParameters();
    106106    }
    107     private void operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
     107    protected virtual void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    108108      foreach (IndexedItem<T> item in e.Items)
    109109        operatorParameters[item.Index].Value = item.Value;
    110110    }
    111     private void operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
     111    protected virtual void Operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    112112      foreach (IndexedItem<T> item in e.Items)
    113113        operatorParameters[item.Index].Value = item.Value;
    114114    }
    115     private void operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
     115    protected virtual void Operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    116116      UpdateOperatorParameters();
    117117    }
  • trunk/sources/HeuristicLab.Operators/3.3/StochasticMultiOperator.cs

    r3425 r3445  
    3838  public abstract class StochasticMultiOperator<T> : MultiOperator<T> where T : class, IOperator {
    3939    /// <summary>
    40     /// Returns false by default. If this is overriden to return true, there will be an automatic type discovery
    41     /// of all instantiable types of T (except the own type) and instances will be added to the Operators list.
    42     /// </summary>
    43     public virtual bool AutomaticTypeDiscovery { get { return false; } }
    44     /// <summary>
    4540    /// Should return true if the StochasticMultiOperator should create a new child operation with the selected successor
    4641    /// or if it should create a new operation. If you need to shield the parameters of the successor you should return true here.
     
    7065      Parameters.Add(new ValueLookupParameter<DoubleArray>("Probabilities", "The array of relative probabilities for each operator.", new DoubleArray()));
    7166      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
    72       Initialize();
    7367    }
    7468
    75     [StorableHook(HookType.AfterDeserialization)]
    76     private void Initialize() {
    77       Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsAdded);
    78       Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsRemoved);
    79       Operators.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<T>>(Operators_ItemsMoved);
    80       if (AutomaticTypeDiscovery) {
    81         IEnumerable<Type> types = ApplicationManager.Manager.GetTypes(typeof(T), true);
    82         foreach (Type type in types.OrderBy(x => x.FullName)) {
    83           if (type != this.GetType())
    84             Operators.Add((T)Activator.CreateInstance(type));
    85         }
    86       }
    87     }
    88 
    89     void Operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
    90       if (Probabilities != null) {
    91         DoubleArray oldProb = (DoubleArray)Probabilities.Clone();
    92         foreach (IndexedItem<T> old in e.OldItems) {
    93           foreach (IndexedItem<T> item in e.Items) {
    94             if (old.Value == item.Value && item.Index < Probabilities.Length && old.Index < oldProb.Length)
    95               Probabilities[item.Index] = oldProb[old.Index];
    96           }
    97         }
    98       }
    99     }
    100 
    101     void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
     69    protected override void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
     70      base.Operators_ItemsRemoved(sender, e);
    10271      if (Probabilities != null && Probabilities.Length > Operators.Count) {
    10372        List<double> probs = new List<double>(Probabilities.Cast<double>());
     
    10978    }
    11079
    111     private void Operators_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
     80    protected override void Operators_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
     81      base.Operators_ItemsAdded(sender, e);
    11282      if (Probabilities != null && Probabilities.Length < Operators.Count) {
    11383        DoubleArray probs = new DoubleArray(Operators.Count);
    11484        double avg = 0;
    11585        if (Probabilities.Length > 0) {
    116           for (int i = 0; i < Probabilities.Length; i++)
    117             avg += Probabilities[i];
    118           avg /= (double)Probabilities.Length;
     86          int zeros = 0;
     87          for (int i = 0; i < Probabilities.Length; i++) {
     88            if (Probabilities[i] == 0) zeros++;
     89            else avg += Probabilities[i];
     90          }
     91          if (Probabilities.Length - zeros > 0)
     92            avg /= (double)(Probabilities.Length - zeros);
     93          else avg = 1;
    11994        } else avg = 1;
    12095
Note: See TracChangeset for help on using the changeset viewer.