- Timestamp:
- 04/19/10 23:34:34 (15 years ago)
- Location:
- trunk/sources
- Files:
-
- 6 added
- 1 deleted
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.BinaryVectorEncoding/3.3/HeuristicLab.Encodings.BinaryVectorEncoding-3.3.csproj
r3418 r3425 86 86 <ItemGroup> 87 87 <Compile Include="Creators\RandomBinaryVectorCreator.cs" /> 88 <Compile Include="Crossovers\MultiBinaryVectorCrossover.cs" /> 88 89 <Compile Include="Crossovers\UniformCrossover.cs" /> 89 90 <Compile Include="Crossovers\SinglePointCrossover.cs" /> -
trunk/sources/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/HeuristicLab.Encodings.IntegerVectorEncoding-3.3.csproj
r3384 r3425 83 83 <SubType>Code</SubType> 84 84 </Compile> 85 <Compile Include="Crossovers\MultiIntegerVectorCrossover.cs" /> 85 86 <Compile Include="Crossovers\SinglePointCrossover.cs"> 86 87 <SubType>Code</SubType> -
trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Crossovers/MultiPermutationCrossover.cs
r3419 r3425 20 20 #endregion 21 21 22 using System.Linq; 22 23 using HeuristicLab.Collections; 23 24 using HeuristicLab.Core; … … 27 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 28 29 29 namespace HeuristicLab.Encodings.PermutationEncoding .Crossovers{30 namespace HeuristicLab.Encodings.PermutationEncoding { 30 31 [Item("MultiPermutationCrossover", "Randomly selects and applies one of its crossovers every time it is called.")] 31 32 [StorableClass] 32 public class MultiPermutationCrossover : MultiCrossover<IPermutationCrossover>, IPermutationCrossover {33 public class MultiPermutationCrossover : StochasticMultiOperator<IPermutationCrossover>, IPermutationCrossover { 33 34 public override bool CanChangeName { 34 35 get { return false; } 35 36 } 37 protected override bool CreateChildOperation { 38 get { return true; } 39 } 40 public override bool AutomaticTypeDiscovery { 41 get { return true; } 42 } 43 36 44 public ILookupParameter<ItemArray<Permutation>> ParentsParameter { 37 45 get { return (ILookupParameter<ItemArray<Permutation>>)Parameters["Parents"]; } … … 58 66 private void Initialize() { 59 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(); 60 73 } 61 74 … … 65 78 66 79 private void ParameterizeCrossovers() { 67 foreach (IPermutationCrossover crossover in Operators ) {80 foreach (IPermutationCrossover crossover in Operators.OfType<IPermutationCrossover>()) { 68 81 crossover.ChildParameter.ActualName = ChildParameter.Name; 69 82 crossover.ParentsParameter.ActualName = ParentsParameter.Name; 70 83 } 71 foreach (IStochasticOperator crossover in Operators ) {84 foreach (IStochasticOperator crossover in Operators.OfType<IStochasticOperator>()) { 72 85 crossover.RandomParameter.ActualName = RandomParameter.Name; 73 86 } -
trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/HeuristicLab.Encodings.PermutationEncoding-3.3.csproj
r3420 r3425 103 103 <Compile Include="Manipulators\InsertionManipulator.cs" /> 104 104 <Compile Include="Manipulators\InversionManipulator.cs" /> 105 <Compile Include="Manipulators\MultiPermutationManipulator.cs" /> 105 106 <Compile Include="Manipulators\ScrambleManipulator.cs" /> 106 107 <Compile Include="Manipulators\Swap2Manipulator.cs" /> -
trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/HeuristicLab.Encodings.RealVectorEncoding-3.3.csproj
r3418 r3425 95 95 <Compile Include="Crossovers\HeuristicCrossover.cs" /> 96 96 <Compile Include="Crossovers\LocalCrossover.cs" /> 97 <Compile Include="Crossovers\MultiRealVectorCrossover.cs" /> 97 98 <Compile Include="Crossovers\RandomConvexCrossover.cs" /> 98 99 <Compile Include="Crossovers\SimulatedBinaryCrossover.cs" /> … … 111 112 <Compile Include="Interfaces\IRealVectorMoveGenerator.cs" /> 112 113 <Compile Include="Interfaces\IRealVectorMoveOperator.cs" /> 114 <Compile Include="Manipulators\MultiRealVectorManipulator.cs" /> 113 115 <Compile Include="Manipulators\NormalAllPositionsManipulator.cs" /> 114 116 <Compile Include="Moves\AdditiveMoveTabuChecker.cs" /> -
trunk/sources/HeuristicLab.Operators/3.3/HeuristicLab.Operators-3.3.csproj
r3418 r3425 91 91 <Compile Include="Assigner.cs" /> 92 92 <Compile Include="AlgorithmOperator.cs" /> 93 <Compile Include="StochasticMultiOperator.cs" /> 93 94 <Compile Include="MultiOperator.cs" /> 94 95 <Compile Include="Operator.cs" /> -
trunk/sources/HeuristicLab.Operators/3.3/StochasticMultiBranch.cs
r3418 r3425 20 20 #endregion 21 21 22 using System;23 using System.Collections.Generic;24 using System.Collections.ObjectModel;25 using System.Linq;26 using HeuristicLab.Collections;27 using HeuristicLab.Common;28 22 using HeuristicLab.Core; 29 using HeuristicLab.Data;30 using HeuristicLab.Parameters;31 23 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 32 24 … … 37 29 [Item("StochasticMultiBranch", "Branch of operators that have different probabilities to get executed.")] 38 30 [StorableClass] 39 public class StochasticMultiBranch : MultiOperator<IOperator> { 40 public ValueLookupParameter<DoubleArray> ProbabilitiesParameter { 41 get { return (ValueLookupParameter<DoubleArray>)Parameters["Probabilities"]; } 42 } 43 public ILookupParameter<IRandom> RandomParameter { 44 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 45 } 46 47 public DoubleArray Probabilities { 48 get { return ProbabilitiesParameter.Value; } 49 set { ProbabilitiesParameter.Value = value; } 50 } 51 52 [StorableConstructor] 53 public StochasticMultiBranch(bool deserializing) : base() { } 54 /// <summary> 55 /// Initializes a new instance of <see cref="StochasticMultiBranch"/> with two parameters 56 /// (<c>Probabilities</c> and <c>Random</c>). 57 /// </summary> 58 public StochasticMultiBranch() 59 : base() { 60 Parameters.Add(new ValueLookupParameter<DoubleArray>("Probabilities", "The array of relative probabilities for each operator.", new DoubleArray())); 61 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 62 Initialize(); 63 } 64 65 [StorableHook(HookType.AfterDeserialization)] 66 private void Initialize() { 67 Operators.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(Operators_ItemsAdded); 68 Operators.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(Operators_ItemsRemoved); 69 Operators.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<IOperator>>(Operators_ItemsMoved); 70 } 71 72 void Operators_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) { 73 if (Probabilities != null) { 74 DoubleArray oldProb = (DoubleArray)Probabilities.Clone(); 75 foreach (IndexedItem<IOperator> old in e.OldItems) { 76 foreach (IndexedItem<IOperator> item in e.Items) { 77 if (old.Value == item.Value && item.Index < Probabilities.Length && old.Index < oldProb.Length) 78 Probabilities[item.Index] = oldProb[old.Index]; 79 } 80 } 81 } 82 } 83 84 void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) { 85 if (Probabilities != null && Probabilities.Length > Operators.Count) { 86 List<double> probs = new List<double>(Probabilities.Cast<DoubleValue>().Select(x => x.Value)); 87 var sorted = e.Items.OrderByDescending(x => x.Index); 88 foreach (IndexedItem<IOperator> item in sorted) 89 if (probs.Count > item.Index) probs.RemoveAt(item.Index); 90 Probabilities = new DoubleArray(probs.ToArray()); 91 } 92 } 93 94 private void Operators_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IndexedItem<IOperator>> e) { 95 if (Probabilities != null && Probabilities.Length < Operators.Count) { 96 DoubleArray probs = new DoubleArray(Operators.Count); 97 double avg = 0; 98 if (Probabilities.Length > 0) { 99 for (int i = 0; i < Probabilities.Length; i++) 100 avg += Probabilities[i]; 101 avg /= (double)Probabilities.Length; 102 } else avg = 1; 103 104 var added = e.Items.OrderBy(x => x.Index).ToList(); 105 int insertCount = 0; 106 for (int i = 0; i < Operators.Count; i++) { 107 if (insertCount < added.Count && i == added[insertCount].Index) { 108 probs[i] = avg; 109 insertCount++; 110 } else if (i - insertCount < Probabilities.Length) { 111 probs[i] = Probabilities[i - insertCount]; 112 } else probs[i] = avg; 113 } 114 Probabilities = probs; 115 } 116 } 117 118 /// <summary> 119 /// Applies an operator of the branches to the current scope with a 120 /// specific probability. 121 /// </summary> 122 /// <exception cref="InvalidOperationException">Thrown when the list of probabilites does not 123 /// match the number of operators.</exception> 124 /// <returns>A new operation with the operator that was selected followed by the current operator's successor.</returns> 125 public override IOperation Apply() { 126 IRandom random = RandomParameter.ActualValue; 127 DoubleArray probabilities = ProbabilitiesParameter.ActualValue; 128 if(probabilities.Length != Operators.Count) { 129 throw new InvalidOperationException("StochasticMultiBranch: The list of probabilities has to match the number of operators"); 130 } 131 double sum = 0; 132 for (int i = 0; i < Operators.Count; i++) { 133 sum += probabilities[i]; 134 } 135 double r = random.NextDouble() * sum; 136 sum = 0; 137 IOperator successor = null; 138 for(int i = 0; i < Operators.Count; i++) { 139 sum += probabilities[i]; 140 if(sum > r) { 141 successor = Operators[i]; 142 break; 143 } 144 } 145 OperationCollection next = new OperationCollection(base.Apply()); 146 if (successor != null) { 147 next.Insert(0, ExecutionContext.CreateOperation(successor)); 148 } 149 return next; 31 public class StochasticMultiBranch : StochasticMultiOperator<IOperator> { 32 protected override bool CreateChildOperation { 33 get { return false; } 150 34 } 151 35 } -
trunk/sources/HeuristicLab.Optimization.Operators/3.3/HeuristicLab.Optimization.Operators-3.3.csproj
r3418 r3425 93 93 <Compile Include="QualityComparator.cs" /> 94 94 <Compile Include="SolutionsCreator.cs" /> 95 <Compile Include="MultiCrossover.cs" />96 95 <Compile Include="TabuMaker.cs" /> 97 96 <Compile Include="UnidirectionalRingMigrator.cs" />
Note: See TracChangeset
for help on using the changeset viewer.