Free cookie consent management tool by TermsFeed Policy Generator

Changeset 3425 for trunk


Ignore:
Timestamp:
04/19/10 23:34:34 (15 years ago)
Author:
abeham
Message:

Changed MultiCrossover to StochasticMultiOperator
Derived StochasticMultiBranch from StochasticMultiOperator
Derived encoding specific mutation/crossover operators from StochasticMultiOperator
#976

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  
    8686  <ItemGroup>
    8787    <Compile Include="Creators\RandomBinaryVectorCreator.cs" />
     88    <Compile Include="Crossovers\MultiBinaryVectorCrossover.cs" />
    8889    <Compile Include="Crossovers\UniformCrossover.cs" />
    8990    <Compile Include="Crossovers\SinglePointCrossover.cs" />
  • trunk/sources/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/HeuristicLab.Encodings.IntegerVectorEncoding-3.3.csproj

    r3384 r3425  
    8383      <SubType>Code</SubType>
    8484    </Compile>
     85    <Compile Include="Crossovers\MultiIntegerVectorCrossover.cs" />
    8586    <Compile Include="Crossovers\SinglePointCrossover.cs">
    8687      <SubType>Code</SubType>
  • trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Crossovers/MultiPermutationCrossover.cs

    r3419 r3425  
    2020#endregion
    2121
     22using System.Linq;
    2223using HeuristicLab.Collections;
    2324using HeuristicLab.Core;
     
    2728using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    2829
    29 namespace HeuristicLab.Encodings.PermutationEncoding.Crossovers {
     30namespace HeuristicLab.Encodings.PermutationEncoding {
    3031  [Item("MultiPermutationCrossover", "Randomly selects and applies one of its crossovers every time it is called.")]
    3132  [StorableClass]
    32   public class MultiPermutationCrossover : MultiCrossover<IPermutationCrossover>, IPermutationCrossover {
     33  public class MultiPermutationCrossover : StochasticMultiOperator<IPermutationCrossover>, IPermutationCrossover {
    3334    public override bool CanChangeName {
    3435      get { return false; }
    3536    }
     37    protected override bool CreateChildOperation {
     38      get { return true; }
     39    }
     40    public override bool AutomaticTypeDiscovery {
     41      get { return true; }
     42    }
     43
    3644    public ILookupParameter<ItemArray<Permutation>> ParentsParameter {
    3745      get { return (ILookupParameter<ItemArray<Permutation>>)Parameters["Parents"]; }
     
    5866    private void Initialize() {
    5967      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();
    6073    }
    6174
     
    6578
    6679    private void ParameterizeCrossovers() {
    67       foreach (IPermutationCrossover crossover in Operators) {
     80      foreach (IPermutationCrossover crossover in Operators.OfType<IPermutationCrossover>()) {
    6881        crossover.ChildParameter.ActualName = ChildParameter.Name;
    6982        crossover.ParentsParameter.ActualName = ParentsParameter.Name;
    7083      }
    71       foreach (IStochasticOperator crossover in Operators) {
     84      foreach (IStochasticOperator crossover in Operators.OfType<IStochasticOperator>()) {
    7285        crossover.RandomParameter.ActualName = RandomParameter.Name;
    7386      }
  • trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/HeuristicLab.Encodings.PermutationEncoding-3.3.csproj

    r3420 r3425  
    103103    <Compile Include="Manipulators\InsertionManipulator.cs" />
    104104    <Compile Include="Manipulators\InversionManipulator.cs" />
     105    <Compile Include="Manipulators\MultiPermutationManipulator.cs" />
    105106    <Compile Include="Manipulators\ScrambleManipulator.cs" />
    106107    <Compile Include="Manipulators\Swap2Manipulator.cs" />
  • trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/HeuristicLab.Encodings.RealVectorEncoding-3.3.csproj

    r3418 r3425  
    9595    <Compile Include="Crossovers\HeuristicCrossover.cs" />
    9696    <Compile Include="Crossovers\LocalCrossover.cs" />
     97    <Compile Include="Crossovers\MultiRealVectorCrossover.cs" />
    9798    <Compile Include="Crossovers\RandomConvexCrossover.cs" />
    9899    <Compile Include="Crossovers\SimulatedBinaryCrossover.cs" />
     
    111112    <Compile Include="Interfaces\IRealVectorMoveGenerator.cs" />
    112113    <Compile Include="Interfaces\IRealVectorMoveOperator.cs" />
     114    <Compile Include="Manipulators\MultiRealVectorManipulator.cs" />
    113115    <Compile Include="Manipulators\NormalAllPositionsManipulator.cs" />
    114116    <Compile Include="Moves\AdditiveMoveTabuChecker.cs" />
  • trunk/sources/HeuristicLab.Operators/3.3/HeuristicLab.Operators-3.3.csproj

    r3418 r3425  
    9191    <Compile Include="Assigner.cs" />
    9292    <Compile Include="AlgorithmOperator.cs" />
     93    <Compile Include="StochasticMultiOperator.cs" />
    9394    <Compile Include="MultiOperator.cs" />
    9495    <Compile Include="Operator.cs" />
  • trunk/sources/HeuristicLab.Operators/3.3/StochasticMultiBranch.cs

    r3418 r3425  
    2020#endregion
    2121
    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;
    2822using HeuristicLab.Core;
    29 using HeuristicLab.Data;
    30 using HeuristicLab.Parameters;
    3123using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3224
     
    3729  [Item("StochasticMultiBranch", "Branch of operators that have different probabilities to get executed.")]
    3830  [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; }
    15034    }
    15135  }
  • trunk/sources/HeuristicLab.Optimization.Operators/3.3/HeuristicLab.Optimization.Operators-3.3.csproj

    r3418 r3425  
    9393    <Compile Include="QualityComparator.cs" />
    9494    <Compile Include="SolutionsCreator.cs" />
    95     <Compile Include="MultiCrossover.cs" />
    9695    <Compile Include="TabuMaker.cs" />
    9796    <Compile Include="UnidirectionalRingMigrator.cs" />
Note: See TracChangeset for help on using the changeset viewer.