Changeset 3445


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

Updated StochasticMultiOperator and Multicrossover/Multimutation #976

Location:
trunk/sources
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/Crossovers/MultiBinaryVectorCrossover.cs

    r3425 r3445  
    3131  [Item("MultiBinaryVectorCrossover", "Randomly selects and applies one of its crossovers every time it is called.")]
    3232  [StorableClass]
    33   public class MultiBinaryVectorCrossover : StochasticMultiOperator<IBinaryVectorCrossover>, IBinaryVectorCrossover {
     33  public class MultiBinaryVectorCrossover : StochasticMultiOperator<IBinaryVectorCrossover>, IBinaryVectorCrossover, IStochasticOperator {
    3434    public override bool CanChangeName {
    3535      get { return false; }
    3636    }
    3737    protected override bool CreateChildOperation {
    38       get { return true; }
    39     }
    40     public override bool AutomaticTypeDiscovery {
    4138      get { return true; }
    4239    }
     
    5855      Parameters.Add(new LookupParameter<BinaryVector>("Child", "The child binary vector resulting from the crossover."));
    5956      ChildParameter.ActualName = "BinaryVector";
     57    }
    6058
    61       Initialize();
     59    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IBinaryVectorCrossover>> e) {
     60      base.Operators_ItemsReplaced(sender, e);
    6261      ParameterizeCrossovers();
    6362    }
    6463
    65     [StorableHook(HookType.AfterDeserialization)]
    66     private void Initialize() {
    67       Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IBinaryVectorCrossover>>(Operators_ItemsAdded);
    68       Operators.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IBinaryVectorCrossover>>(Operators_ItemsReplaced);
    69     }
    70 
    71     private void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IBinaryVectorCrossover>> e) {
    72       ParameterizeCrossovers();
    73     }
    74 
    75     private void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IBinaryVectorCrossover>> e) {
     64    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IBinaryVectorCrossover>> e) {
     65      base.Operators_ItemsAdded(sender, e);
    7666      ParameterizeCrossovers();
    7767    }
  • trunk/sources/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Crossovers/MultiIntegerVectorCrossover.cs

    r3425 r3445  
    3131  [Item("MultiIntegerVectorCrossover", "Randomly selects and applies one of its crossovers every time it is called.")]
    3232  [StorableClass]
    33   public class MultiIntegerVectorCrossover : StochasticMultiOperator<IIntegerVectorCrossover>, IIntegerVectorCrossover {
     33  public class MultiIntegerVectorCrossover : StochasticMultiOperator<IIntegerVectorCrossover>, IIntegerVectorCrossover, IStochasticOperator {
    3434    public override bool CanChangeName {
    3535      get { return false; }
    3636    }
    3737    protected override bool CreateChildOperation {
    38       get { return true; }
    39     }
    40     public override bool AutomaticTypeDiscovery {
    4138      get { return true; }
    4239    }
     
    5855      Parameters.Add(new LookupParameter<IntegerVector>("Child", "The child integer vector resulting from the crossover."));
    5956      ChildParameter.ActualName = "IntegerVector";
     57    }
    6058
    61       Initialize();
     59    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IIntegerVectorCrossover>> e) {
     60      base.Operators_ItemsReplaced(sender, e);
    6261      ParameterizeCrossovers();
    6362    }
    6463
    65     [StorableHook(HookType.AfterDeserialization)]
    66     private void Initialize() {
    67       Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IIntegerVectorCrossover>>(Operators_ItemsAdded);
    68       Operators.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IIntegerVectorCrossover>>(Operators_ItemsReplaced);
    69     }
    70 
    71     private void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IIntegerVectorCrossover>> e) {
    72       ParameterizeCrossovers();
    73     }
    74 
    75     private void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IIntegerVectorCrossover>> e) {
     64    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IIntegerVectorCrossover>> e) {
     65      base.Operators_ItemsAdded(sender, e);
    7666      ParameterizeCrossovers();
    7767    }
  • trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Crossovers/MultiPermutationCrossover.cs

    r3425 r3445  
    3131  [Item("MultiPermutationCrossover", "Randomly selects and applies one of its crossovers every time it is called.")]
    3232  [StorableClass]
    33   public class MultiPermutationCrossover : StochasticMultiOperator<IPermutationCrossover>, IPermutationCrossover {
     33  public class MultiPermutationCrossover : StochasticMultiOperator<IPermutationCrossover>, IPermutationCrossover, IStochasticOperator {
    3434    public override bool CanChangeName {
    3535      get { return false; }
    3636    }
    3737    protected override bool CreateChildOperation {
    38       get { return true; }
    39     }
    40     public override bool AutomaticTypeDiscovery {
    4138      get { return true; }
    4239    }
     
    5855      Parameters.Add(new LookupParameter<Permutation>("Child", "The child permutation resulting from the crossover."));
    5956      ChildParameter.ActualName = "Permutation";
     57    }
    6058
    61       Initialize();
     59    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IPermutationCrossover>> e) {
     60      base.Operators_ItemsReplaced(sender, e);
    6261      ParameterizeCrossovers();
    6362    }
    6463
    65     [StorableHook(HookType.AfterDeserialization)]
    66     private void Initialize() {
    67       Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IPermutationCrossover>>(Operators_ItemsAdded);
    68       Operators.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IPermutationCrossover>>(Operators_ItemsReplaced);
    69     }
    70 
    71     private void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IPermutationCrossover>> e) {
    72       ParameterizeCrossovers();
    73     }
    74 
    75     private void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IPermutationCrossover>> e) {
     64    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IPermutationCrossover>> e) {
     65      base.Operators_ItemsAdded(sender, e);
    7666      ParameterizeCrossovers();
    7767    }
  • trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Manipulators/MultiPermutationManipulator.cs

    r3425 r3445  
    3131  [Item("MultiPermutationManipulator", "Randomly selects and applies one of its manipulators every time it is called.")]
    3232  [StorableClass]
    33   public class MultiPermutationManipulator : StochasticMultiOperator<IPermutationManipulator>, IPermutationManipulator {
     33  public class MultiPermutationManipulator : StochasticMultiOperator<IPermutationManipulator>, IPermutationManipulator, IStochasticOperator {
    3434    public override bool CanChangeName {
    3535      get { return false; }
    3636    }
    3737    protected override bool CreateChildOperation {
    38       get { return true; }
    39     }
    40     public override bool AutomaticTypeDiscovery {
    4138      get { return true; }
    4239    }
     
    5148      : base() {
    5249      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The permutation that is being manipulating."));
     50    }
    5351
    54       Initialize();
     52    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IPermutationManipulator>> e) {
     53      base.Operators_ItemsReplaced(sender, e);
    5554      ParameterizeManipulators();
    5655    }
    5756
    58     [StorableHook(HookType.AfterDeserialization)]
    59     private void Initialize() {
    60       Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IPermutationManipulator>>(Operators_ItemsAdded);
    61       Operators.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IPermutationManipulator>>(Operators_ItemsReplaced);
    62     }
    63 
    64     private void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IPermutationManipulator>> e) {
    65       ParameterizeManipulators();
    66     }
    67 
    68     private void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IPermutationManipulator>> e) {
     57    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IPermutationManipulator>> e) {
     58      base.Operators_ItemsAdded(sender, e);
    6959      ParameterizeManipulators();
    7060    }
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Crossovers/MultiRealVectorCrossover.cs

    r3425 r3445  
    3232  [Item("MultiRealVectorCrossover", "Randomly selects and applies one of its crossovers every time it is called.")]
    3333  [StorableClass]
    34   public class MultiRealVectorCrossover : StochasticMultiOperator<IRealVectorCrossover>, IRealVectorCrossover {
     34  public class MultiRealVectorCrossover : StochasticMultiOperator<IRealVectorCrossover>, IRealVectorCrossover, IStochasticOperator {
    3535    public override bool CanChangeName {
    3636      get { return false; }
    3737    }
    3838    protected override bool CreateChildOperation {
    39       get { return true; }
    40     }
    41     public override bool AutomaticTypeDiscovery {
    4239      get { return true; }
    4340    }
     
    6259      ChildParameter.ActualName = "RealVector";
    6360      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the vector."));
     61    }
    6462
    65       Initialize();
     63    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IRealVectorCrossover>> e) {
     64      base.Operators_ItemsReplaced(sender, e);
    6665      ParameterizeCrossovers();
    6766    }
    6867
    69     [StorableHook(HookType.AfterDeserialization)]
    70     private void Initialize() {
    71       Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IRealVectorCrossover>>(Operators_ItemsAdded);
    72       Operators.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IRealVectorCrossover>>(Operators_ItemsReplaced);
    73     }
    74 
    75     private void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IRealVectorCrossover>> e) {
    76       ParameterizeCrossovers();
    77     }
    78 
    79     private void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IRealVectorCrossover>> e) {
     68    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IRealVectorCrossover>> e) {
     69      base.Operators_ItemsAdded(sender, e);
    8070      ParameterizeCrossovers();
    8171    }
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Manipulators/MultiRealVectorManipulator.cs

    r3425 r3445  
    3232  [Item("MultiRealVectorManipulator", "Randomly selects and applies one of its manipulators every time it is called.")]
    3333  [StorableClass]
    34   public class MultiRealVectorManipulator : StochasticMultiOperator<IRealVectorManipulator>, IRealVectorManipulator {
     34  public class MultiRealVectorManipulator : StochasticMultiOperator<IRealVectorManipulator>, IRealVectorManipulator, IStochasticOperator {
    3535    public override bool CanChangeName {
    3636      get { return false; }
    3737    }
    3838    protected override bool CreateChildOperation {
    39       get { return true; }
    40     }
    41     public override bool AutomaticTypeDiscovery {
    4239      get { return true; }
    4340    }
     
    5653      Parameters.Add(new LookupParameter<RealVector>("RealVector", "The real vector that is being manipulating."));
    5754      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "The lower and upper bounds for each dimension of the vector."));
     55    }
    5856
    59       Initialize();
     57    protected override void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IRealVectorManipulator>> e) {
     58      base.Operators_ItemsReplaced(sender, e);
    6059      ParameterizeManipulators();
    6160    }
    6261
    63     [StorableHook(HookType.AfterDeserialization)]
    64     private void Initialize() {
    65       Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IRealVectorManipulator>>(Operators_ItemsAdded);
    66       Operators.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IRealVectorManipulator>>(Operators_ItemsReplaced);
    67     }
    68 
    69     private void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IRealVectorManipulator>> e) {
    70       ParameterizeManipulators();
    71     }
    72 
    73     private void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IRealVectorManipulator>> e) {
     62    protected override void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IRealVectorManipulator>> e) {
     63      base.Operators_ItemsAdded(sender, e);
    7464      ParameterizeManipulators();
    7565    }
  • 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.