Changeset 3593 for trunk/sources/HeuristicLab.Operators
- Timestamp:
- 05/03/10 17:27:21 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Operators/3.3
- Files:
-
- 1 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Operators/3.3/HeuristicLab.Operators-3.3.csproj
r3591 r3593 92 92 <Compile Include="Assigner.cs" /> 93 93 <Compile Include="AlgorithmOperator.cs" /> 94 <Compile Include="StochasticMultiOperator.cs" />95 94 <Compile Include="MultiOperator.cs" /> 96 95 <Compile Include="Operator.cs" /> … … 99 98 <Compile Include="ScopeCleaner.cs" /> 100 99 <Compile Include="StochasticBranch.cs" /> 101 <Compile Include="StochasticMultiBranch.cs" /> 100 <Compile Include="StochasticMultiBranch.cs"> 101 <SubType>Code</SubType> 102 </Compile> 102 103 <Compile Include="SubScopesCreator.cs" /> 103 104 <Compile Include="SubScopesMixer.cs" /> -
trunk/sources/HeuristicLab.Operators/3.3/StochasticMultiBranch.cs
r3425 r3593 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 25 using HeuristicLab.Collections; 22 26 using HeuristicLab.Core; 27 using HeuristicLab.Data; 28 using HeuristicLab.Parameters; 23 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.PluginInfrastructure; 24 31 25 32 namespace HeuristicLab.Operators { 26 33 /// <summary> 27 /// Branch of operators that have different probabilities to get executed.34 /// Selects one of its branches (if there are any) given a list of relative probabilities. 28 35 /// </summary> 29 [Item("StochasticMultiBranch ", "Branch of operators that have different probabilities to get executed.")]36 [Item("StochasticMultiBranch<T>", "Selects one of its branches (if there are any) given a list of relative probabilities.")] 30 37 [StorableClass] 31 public class StochasticMultiBranch : StochasticMultiOperator<IOperator> { 32 protected override bool CreateChildOperation { 38 public class StochasticMultiBranch<T> : CheckedMultiOperator<T> where T : class, IOperator { 39 /// <summary> 40 /// Should return true if the StochasticMultiOperator should create a new child operation with the selected successor 41 /// or if it should create a new operation. If you need to shield the parameters of the successor you should return true here. 42 /// </summary> 43 protected virtual bool CreateChildOperation { 33 44 get { return false; } 45 } 46 47 public ValueLookupParameter<DoubleArray> ProbabilitiesParameter { 48 get { return (ValueLookupParameter<DoubleArray>)Parameters["Probabilities"]; } 49 } 50 public ILookupParameter<IRandom> RandomParameter { 51 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 52 } 53 54 public DoubleArray Probabilities { 55 get { return ProbabilitiesParameter.Value; } 56 set { ProbabilitiesParameter.Value = value; } 57 } 58 59 [StorableConstructor] 60 protected StochasticMultiBranch(bool deserializing) : base(deserializing) { } 61 /// <summary> 62 /// Initializes a new instance of <see cref="StochasticMultiOperator"/> with two parameters 63 /// (<c>Probabilities</c> and <c>Random</c>). 64 /// </summary> 65 public StochasticMultiBranch() 66 : base() { 67 Parameters.Add(new ValueLookupParameter<DoubleArray>("Probabilities", "The array of relative probabilities for each operator.", new DoubleArray())); 68 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 69 } 70 71 protected override void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) { 72 base.Operators_ItemsRemoved(sender, e); 73 if (Probabilities != null && Probabilities.Length > Operators.Count) { 74 List<double> probs = new List<double>(Probabilities.Cast<double>()); 75 var sorted = e.Items.OrderByDescending(x => x.Index); 76 foreach (IndexedItem<T> item in sorted) 77 if (probs.Count > item.Index) probs.RemoveAt(item.Index); 78 Probabilities = new DoubleArray(probs.ToArray()); 79 } 80 } 81 82 protected override void Operators_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IndexedItem<T>> e) { 83 base.Operators_ItemsAdded(sender, e); 84 if (Probabilities != null && Probabilities.Length < Operators.Count) { 85 double avg = (Probabilities.Where(x => x > 0).Count() > 0) ? (Probabilities.Where(x => x > 0).Average()) : (1); 86 // add the average of all probabilities in the respective places (the new operators) 87 var added = e.Items.OrderBy(x => x.Index).ToList(); 88 int insertCount = 0; 89 DoubleArray probs = new DoubleArray(Operators.Count); 90 for (int i = 0; i < Operators.Count; i++) { 91 if (insertCount < added.Count && i == added[insertCount].Index) { 92 probs[i] = avg; 93 insertCount++; 94 } else if (i - insertCount < Probabilities.Length) { 95 probs[i] = Probabilities[i - insertCount]; 96 } else probs[i] = avg; 97 } 98 Probabilities = probs; 99 } 100 } 101 102 /// <summary> 103 /// Applies an operator of the branches to the current scope with a 104 /// specific probability. 105 /// </summary> 106 /// <exception cref="InvalidOperationException">Thrown when the list of probabilites does not 107 /// match the number of operators, the list of selected operators is empty, 108 /// or all selected operators have zero probabitlity.</exception> 109 /// <returns>A new operation with the operator that was selected followed by the current operator's successor.</returns> 110 public override IOperation Apply() { 111 IRandom random = RandomParameter.ActualValue; 112 DoubleArray probabilities = ProbabilitiesParameter.ActualValue; 113 if (probabilities.Length != Operators.Count) { 114 throw new InvalidOperationException(Name + ": The list of probabilities has to match the number of operators"); 115 } 116 IOperator successor = null; 117 var checkedOperators = Operators.CheckedItems; 118 if (checkedOperators.Count() > 0) { 119 // select a random operator from the checked operators 120 double sum = (from indexedItem in checkedOperators select probabilities[indexedItem.Index]).Sum(); 121 if (sum == 0) throw new InvalidOperationException(Name + ": All selected operators have zero probability."); 122 double r = random.NextDouble() * sum; 123 sum = 0; 124 foreach (var indexedItem in checkedOperators) { 125 sum += probabilities[indexedItem.Index]; 126 if (sum > r) { 127 successor = indexedItem.Value; 128 break; 129 } 130 } 131 } 132 OperationCollection next = new OperationCollection(base.Apply()); 133 if (successor != null) { 134 if (CreateChildOperation) 135 next.Insert(0, ExecutionContext.CreateChildOperation(successor)); 136 else next.Insert(0, ExecutionContext.CreateOperation(successor)); 137 } 138 return next; 34 139 } 35 140 }
Note: See TracChangeset
for help on using the changeset viewer.