Changeset 3591 for trunk/sources/HeuristicLab.Operators
- Timestamp:
- 05/03/10 14:30:55 (15 years ago)
- 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 31 31 namespace HeuristicLab.Operators { 32 32 /// <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. 34 34 /// </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.")] 36 36 [StorableClass] 37 public abstract class MultiOperator<T> : SingleSuccessorOperatorwhere T : class, IOperator {38 private List<IValueParameter<T>> operatorParameters;39 40 [Storable]41 p rivate 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; } 44 44 } 45 46 public MultiOperator() 45 /// <summary> 46 /// Creates a new instance of CheckedMultiOperator 47 /// </summary> 48 public CheckedMultiOperator() 47 49 : base() { 48 operators = new CheckedItemList<T>(); 49 Initialize(); 50 Operators = new CheckedItemList<T>(); 50 51 } 51 52 [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) { } 123 54 } 124 55 } -
trunk/sources/HeuristicLab.Operators/3.3/HeuristicLab.Operators-3.3.csproj
r3425 r3591 85 85 </ItemGroup> 86 86 <ItemGroup> 87 <Compile Include="CheckedMultiOperator.cs" /> 87 88 <Compile Include="CombinedOperator.cs" /> 88 89 <None Include="HeuristicLabOperatorsPlugin.cs.frame" /> -
trunk/sources/HeuristicLab.Operators/3.3/MultiOperator.cs
r3568 r3591 39 39 40 40 [Storable] 41 private CheckedItemList<T> operators;42 public CheckedItemList<T> Operators {41 private IItemList<T> operators; 42 public IItemList<T> Operators { 43 43 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 } 44 52 } 45 53 46 54 public MultiOperator() 47 55 : base() { 48 operators = new CheckedItemList<T>();56 this.operators = new ItemList<T>(); 49 57 Initialize(); 50 58 } 59 51 60 [StorableConstructor] 52 61 protected MultiOperator(bool deserializing) : base(deserializing) { } … … 65 74 public override IDeepCloneable Clone(Cloner cloner) { 66 75 MultiOperator<T> clone = (MultiOperator<T>)base.Clone(cloner); 67 clone.operators = ( CheckedItemList<T>)cloner.Clone(operators);76 clone.operators = (IItemList<T>)cloner.Clone(operators); 68 77 clone.Initialize(); 69 78 return clone; -
trunk/sources/HeuristicLab.Operators/3.3/StochasticMultiOperator.cs
r3568 r3591 36 36 [Item("StochasticMultiOperator<T>", "Base class for stochastic multi operators.")] 37 37 [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 { 39 39 /// <summary> 40 40 /// Should return true if the StochasticMultiOperator should create a new child operation with the selected successor … … 122 122 throw new InvalidOperationException(Name + ": The list of probabilities has to match the number of operators"); 123 123 } 124 IOperator successor = null; 124 125 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 } 138 138 } 139 139 }
Note: See TracChangeset
for help on using the changeset viewer.