Changeset 3445 for trunk/sources/HeuristicLab.Operators
- Timestamp:
- 04/20/10 16:35:42 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Operators/3.3
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Operators/3.3/MultiOperator.cs
r3407 r3445 86 86 #region Events 87 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);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 93 } 94 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);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 100 } 101 pr ivate void operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {101 protected virtual void Operators_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) { 102 102 UpdateOperatorParameters(); 103 103 } 104 pr ivate void operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {104 protected virtual void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) { 105 105 UpdateOperatorParameters(); 106 106 } 107 pr ivate void operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {107 protected virtual void Operators_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) { 108 108 foreach (IndexedItem<T> item in e.Items) 109 109 operatorParameters[item.Index].Value = item.Value; 110 110 } 111 pr ivate void operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {111 protected virtual void Operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) { 112 112 foreach (IndexedItem<T> item in e.Items) 113 113 operatorParameters[item.Index].Value = item.Value; 114 114 } 115 pr ivate void operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {115 protected virtual void Operators_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) { 116 116 UpdateOperatorParameters(); 117 117 } -
trunk/sources/HeuristicLab.Operators/3.3/StochasticMultiOperator.cs
r3425 r3445 38 38 public abstract class StochasticMultiOperator<T> : MultiOperator<T> where T : class, IOperator { 39 39 /// <summary> 40 /// Returns false by default. If this is overriden to return true, there will be an automatic type discovery41 /// 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>45 40 /// Should return true if the StochasticMultiOperator should create a new child operation with the selected successor 46 41 /// or if it should create a new operation. If you need to shield the parameters of the successor you should return true here. … … 70 65 Parameters.Add(new ValueLookupParameter<DoubleArray>("Probabilities", "The array of relative probabilities for each operator.", new DoubleArray())); 71 66 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 72 Initialize();73 67 } 74 68 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); 102 71 if (Probabilities != null && Probabilities.Length > Operators.Count) { 103 72 List<double> probs = new List<double>(Probabilities.Cast<double>()); … … 109 78 } 110 79 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); 112 82 if (Probabilities != null && Probabilities.Length < Operators.Count) { 113 83 DoubleArray probs = new DoubleArray(Operators.Count); 114 84 double avg = 0; 115 85 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; 119 94 } else avg = 1; 120 95
Note: See TracChangeset
for help on using the changeset viewer.