- Timestamp:
- 04/04/15 23:58:27 (10 years ago)
- Location:
- branches/LinearLinkage/HeuristicLab.Encodings.LinearLinkageEncoding/3.3
- Files:
-
- 4 added
- 2 deleted
- 2 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/LinearLinkage/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/HeuristicLab.Encodings.LinearLinkageEncoding-3.3.csproj
r12285 r12286 106 106 <Compile Include="Crossovers\GroupCrossover.cs" /> 107 107 <Compile Include="Crossovers\LowestIndexMaxCrossover.cs" /> 108 <Compile Include="Crossovers\MultiLLECrossover.cs" /> 108 109 <Compile Include="Interfaces\ILinearLinkageCreator.cs" /> 109 110 <Compile Include="LinearLinkage.cs" /> … … 112 113 <Compile Include="LinearLinkageManipulator.cs" /> 113 114 <Compile Include="LinearLinkageCrossover.cs" /> 114 <Compile Include="ShakingOperators\SplitGroupShakingOperator.cs" /> 115 <Compile Include="ShakingOperators\SwapNShakingOperator.cs" /> 115 <Compile Include="Manipulators\MoveItemManipulator.cs" /> 116 <Compile Include="Manipulators\MergeGroupManipulator.cs" /> 117 <Compile Include="Manipulators\MultiLLEManipulator.cs" /> 118 <Compile Include="ShakingOperators\LLEShakingOperator.cs" /> 116 119 <None Include="HeuristicLab.snk" /> 117 120 <None Include="Plugin.cs.frame" /> … … 123 126 <Compile Include="Interfaces\ILinearLinkageShakingOperator.cs" /> 124 127 <Compile Include="Manipulators\SplitGroupManipulator.cs" /> 125 <Compile Include="Manipulators\Swap NManipulator.cs" />128 <Compile Include="Manipulators\SwapItemManipulator.cs" /> 126 129 <Compile Include="Plugin.cs" /> 127 130 <None Include="Properties\AssemblyInfo.cs.frame" /> -
branches/LinearLinkage/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/Manipulators/SplitGroupManipulator.cs
r12285 r12286 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using System.Linq; … … 53 54 } 54 55 56 public static void Apply(IRandom random, LinearLinkage lle, int n) { 57 var grouping = lle.GetGroups().ToList(); 58 var groupsLargerOne = grouping.Select((v, i) => Tuple.Create(i, v)) 59 .Where(x => x.Item2.Count > 1) 60 .ToDictionary(x => x.Item1, x => x.Item2); 61 if (groupsLargerOne.Count == 0) return; 62 var toRemove = new List<int>(); 63 64 for (var i = 0; i < n; i++) { 65 var g = groupsLargerOne.Keys.SampleRandom(random); 66 var idx = random.Next(1, groupsLargerOne[g].Count); 67 // shuffle here to avoid a potential bias of grouping smaller and larger numbers together 68 var tmp = groupsLargerOne[g].Shuffle(random); 69 var before = new List<int>(); 70 var after = new List<int>(); 71 foreach (var t in tmp) { 72 if (idx > 0) before.Add(t); 73 else after.Add(t); 74 idx--; 75 } 76 if (before.Count > 1) groupsLargerOne[grouping.Count] = before; 77 grouping.Add(before); 78 if (after.Count > 1) groupsLargerOne[grouping.Count] = after; 79 grouping.Add(after); 80 toRemove.Add(g); 81 groupsLargerOne.Remove(g); 82 if (groupsLargerOne.Count == 0) break; 83 } 84 foreach (var r in toRemove.OrderByDescending(x => x)) 85 grouping.RemoveAt(r); 86 87 lle.SetGroups(grouping); 88 } 89 55 90 protected override void Manipulate(IRandom random, LinearLinkage lle) { 56 91 var N = NParameter.ActualValue.Value; 57 58 // shuffle here to avoid bias due to lowest-index-first encoding 59 var grouping = lle.GetGroups().Select(x => x.ToList()).Shuffle(random).ToList(); 60 var groupsLargerOne = grouping.Count(x => x.Count > 1); 61 if (groupsLargerOne == 0) return; 62 63 var i = -1; 64 while (N > 0) { 65 i = (i + 1) % grouping.Count; 66 if (grouping[i].Count <= 1) continue; 67 if (random.NextDouble() < N / (double)groupsLargerOne) { 68 var idx = random.Next(1, grouping[i].Count); 69 // shuffle here to avoid a potential bias of grouping smaller and larger numbers together 70 var tmp = grouping[i].Shuffle(random); 71 var before = new List<int>(); 72 var after = new List<int>(); 73 foreach (var t in tmp) { 74 if (idx > 0) before.Add(t); 75 else after.Add(t); 76 idx--; 77 } 78 grouping.Add(before); 79 grouping.Add(after); 80 grouping.RemoveAt(i); 81 i--; 82 N--; 83 if (before.Count > 1 && after.Count > 1) groupsLargerOne++; 84 else if (before.Count == 1 && after.Count == 1) groupsLargerOne--; 85 } 86 if (groupsLargerOne == 0) break; 87 } 88 lle.SetGroups(grouping); 92 Apply(random, lle, N); 89 93 } 90 94 } -
branches/LinearLinkage/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/Manipulators/SwapItemManipulator.cs
r12285 r12286 20 20 #endregion 21 21 22 using System.Collections.Generic;23 22 using System.Linq; 24 23 using HeuristicLab.Common; 25 24 using HeuristicLab.Core; 26 25 using HeuristicLab.Data; 27 using HeuristicLab.Operators;28 using HeuristicLab.Optimization;29 26 using HeuristicLab.Parameters; 30 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 28 32 29 namespace HeuristicLab.Encodings.LinearLinkageEncoding { 33 [Item("Swap -n Manipulator", "")]30 [Item("Swap Item Manipulator", "Swaps items between groups. Items may be swapped multiple times.")] 34 31 [StorableClass] 35 public sealed class SwapNManipulator : InstrumentedOperator, ILinearLinkageManipulator, IStochasticOperator { 36 public ILookupParameter<IRandom> RandomParameter { 37 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 38 } 39 40 public ILookupParameter<LinearLinkage> LLEParameter { 41 get { return (ILookupParameter<LinearLinkage>)Parameters["LLE"]; } 42 } 32 public sealed class SwapItemManipulator : LinearLinkageManipulator { 43 33 44 34 public IValueLookupParameter<IntValue> NParameter { … … 47 37 48 38 [StorableConstructor] 49 private SwapNManipulator(bool deserializing) : base(deserializing) { } 50 private SwapNManipulator(SwapNManipulator original, Cloner cloner) : base(original, cloner) { } 51 public SwapNManipulator() { 52 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use.")); 53 Parameters.Add(new LookupParameter<LinearLinkage>("LLE", "The encoding vector that is to be manipulated.")); 54 Parameters.Add(new ValueLookupParameter<IntValue>("N", "The number of swaps to perform.", new IntValue(2))); 39 private SwapItemManipulator(bool deserializing) : base(deserializing) { } 40 private SwapItemManipulator(SwapItemManipulator original, Cloner cloner) : base(original, cloner) { } 41 public SwapItemManipulator() { 42 Parameters.Add(new ValueLookupParameter<IntValue>("N", "The number of swaps to perform.", new IntValue(1))); 55 43 } 56 public Swap NManipulator(int n)44 public SwapItemManipulator(int n) 57 45 : this() { 58 46 NParameter.Value = new IntValue(n); … … 60 48 61 49 public override IDeepCloneable Clone(Cloner cloner) { 62 return new Swap NManipulator(this, cloner);50 return new SwapItemManipulator(this, cloner); 63 51 } 64 52 65 public override IOperation InstrumentedApply() { 66 var random = RandomParameter.ActualValue; 67 var vector = LLEParameter.ActualValue; 68 var N = NParameter.ActualValue.Value; 53 public static void Apply(IRandom random, LinearLinkage lle, int n) { 54 var grouping = lle.GetGroups().Select(x => x.ToList()).ToList(); 55 if (grouping.Count == 1) return; // nothing can be changed 69 56 70 var grouping = vector.GetGroups().Select(x => x.ToList()).ToList();71 if (grouping.Count == 1) return base.InstrumentedApply(); // nothing can be changed72 var touched = new HashSet<int>();73 57 var prevGroup = random.Next(grouping.Count); 74 58 var prevItem = random.Next(grouping[prevGroup].Count); 75 touched.Add(grouping[prevGroup][prevItem]); 76 for (var i = 0; i < N; i++) { 77 int nextGroup = -1, nextItem = -1, tries = 0; 59 for (var i = 0; i < n; i++) { 60 int nextGroup, nextItem; 78 61 do { 79 if (tries >= 100) break;80 62 nextGroup = random.Next(grouping.Count); 81 63 nextItem = random.Next(grouping[nextGroup].Count); 82 tries++; 83 } while (nextGroup == prevGroup || touched.Contains(grouping[nextGroup][nextItem])); 84 if (tries >= 100) break; 64 } while (nextGroup == prevGroup); 85 65 var h = grouping[nextGroup][nextItem]; 86 66 grouping[nextGroup][nextItem] = grouping[prevGroup][prevItem]; 87 67 grouping[prevGroup][prevItem] = h; 88 touched.Add(h);89 68 prevGroup = nextGroup; 90 69 prevItem = nextItem; 91 70 } 92 vector.SetGroups(grouping); 93 return base.InstrumentedApply(); 71 72 lle.SetGroups(grouping); 73 } 74 75 protected override void Manipulate(IRandom random, LinearLinkage lle) { 76 var N = NParameter.ActualValue.Value; 77 Apply(random, lle, N); 94 78 } 95 79 } -
branches/LinearLinkage/HeuristicLab.Encodings.LinearLinkageEncoding/3.3/ShakingOperators/LLEShakingOperator.cs
r12285 r12286 34 34 /// A shaking operator for VNS. 35 35 /// </summary> 36 [Item(" Split Group Shaking Operator", "A shaking operator for VNS which uses split groupmanipulators to perform the shaking.")]36 [Item("LLE Shaking Operator", "A shaking operator for VNS which LLE manipulators to perform the shaking.")] 37 37 [StorableClass] 38 public class SplitGroupShakingOperator : ShakingOperator<ILinearLinkageManipulator>, IStochasticOperator, ILinearLinkageShakingOperator {38 public class LLEShakingOperator : ShakingOperator<ILinearLinkageManipulator>, IStochasticOperator, ILinearLinkageShakingOperator { 39 39 40 40 public ILookupParameter<IRandom> RandomParameter { … … 51 51 52 52 [StorableConstructor] 53 protected SplitGroupShakingOperator(bool deserializing) : base(deserializing) { }54 protected SplitGroupShakingOperator(SplitGroupShakingOperator original, Cloner cloner) : base(original, cloner) { }55 public SplitGroupShakingOperator()53 protected LLEShakingOperator(bool deserializing) : base(deserializing) { } 54 protected LLEShakingOperator(LLEShakingOperator original, Cloner cloner) : base(original, cloner) { } 55 public LLEShakingOperator() 56 56 : base() { 57 57 Parameters.Add(new LookupParameter<LinearLinkage>("LLE", "The encoding to shake.")); 58 58 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator that will be used for stochastic shaking operators.")); 59 for (var i = 1; i < 6; i++) Operators.Add(new SplitGroupManipulator(i)); 59 for (var i = 1; i < 4; i++) { 60 Operators.Add(new MoveItemManipulator(i * 2)); 61 Operators.Add(new SwapItemManipulator(i * 2)); 62 Operators.Add(new SplitGroupManipulator(i * 2)); 63 Operators.Add(new MergeGroupManipulator(i * 2)); 64 } 60 65 } 61 66 62 67 public override IDeepCloneable Clone(Cloner cloner) { 63 return new SplitGroupShakingOperator(this, cloner);68 return new LLEShakingOperator(this, cloner); 64 69 } 65 70
Note: See TracChangeset
for help on using the changeset viewer.