Changeset 15562 for branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Moves
- Timestamp:
- 12/29/17 23:56:43 (7 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Moves/ExhaustiveOneMoveGenerator.cs
r15519 r15562 20 20 #endregion 21 21 22 using System; 22 23 using System.Collections.Generic; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; 25 using HeuristicLab.Data;26 26 using HeuristicLab.Encodings.IntegerVectorEncoding; 27 27 using HeuristicLab.Optimization; 28 28 using HeuristicLab.Parameters; 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.Random; 30 31 31 32 namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment { 32 [Item(" Stochastic N-Move MultiMoveGenerator", "Randomly samples a number of N-Moves.")]33 [Item("Exhaustive 1-Move MoveGenerator", "Exhaustively generates all possible 1-moves.")] 33 34 [StorableClass] 34 public class StochasticNMoveMultiMoveGenerator : GQAPNMoveGenerator, IStochasticOperator, IMultiMoveGenerator {35 public class ExhaustiveOneMoveGenerator : GQAPNMoveGenerator, IStochasticOperator, IExhaustiveMoveGenerator { 35 36 36 37 public ILookupParameter<IRandom> RandomParameter { 37 38 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 38 39 } 39 public IValueLookupParameter<IntValue> SampleSizeParameter {40 get { return (IValueLookupParameter<IntValue>)Parameters["SampleSize"]; }41 }42 40 43 41 [StorableConstructor] 44 protected StochasticNMoveMultiMoveGenerator(bool deserializing) : base(deserializing) { }45 protected StochasticNMoveMultiMoveGenerator(StochasticNMoveMultiMoveGenerator original, Cloner cloner) : base(original, cloner) { }46 public StochasticNMoveMultiMoveGenerator()42 protected ExhaustiveOneMoveGenerator(bool deserializing) : base(deserializing) { } 43 protected ExhaustiveOneMoveGenerator(ExhaustiveOneMoveGenerator original, Cloner cloner) : base(original, cloner) { } 44 public ExhaustiveOneMoveGenerator() 47 45 : base() { 48 46 Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator that should be used.")); 49 Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to generate.")); 47 NParameter.Value.Value = 1; 48 NParameter.Hidden = true; 50 49 } 51 50 52 51 public override IDeepCloneable Clone(Cloner cloner) { 53 return new StochasticNMoveMultiMoveGenerator(this, cloner);52 return new ExhaustiveOneMoveGenerator(this, cloner); 54 53 } 55 54 56 public static IEnumerable<NMove> Generate(IRandom random, IntegerVector assignment, int n, GQAPInstance problemInstance, int sampleSize) { 57 for (int i = 0; i < sampleSize; i++) 58 yield return StochasticNMoveSingleMoveGenerator.GenerateUpToN(random, assignment, n, problemInstance.Capacities); 55 public static IEnumerable<NMove> Generate(IntegerVector assignment, GQAPInstance problemInstance) { 56 var equipments = problemInstance.Demands.Length; 57 var locations = problemInstance.Capacities.Length; 58 var tmp = new int[equipments]; 59 for (var e = 0; e < equipments; e++) { 60 var indices = new List<int> { e }; 61 for (var l = 0; l < locations; l++) { 62 if (assignment[e] == l) continue; 63 var reassign = (int[])tmp.Clone(); 64 reassign[e] = l + 1; 65 yield return new NMove(reassign, indices); 66 } 67 } 68 } 69 70 public static IEnumerable<NMove> GenerateAllNxM(GQAPInstance problemInstance) { 71 var equipments = problemInstance.Demands.Length; 72 var locations = problemInstance.Capacities.Length; 73 var tmp = new int[equipments]; 74 for (var e = 0; e < equipments; e++) { 75 var indices = new List<int> { e }; 76 for (var l = 0; l < locations; l++) { 77 var reassign = (int[])tmp.Clone(); 78 reassign[e] = l + 1; 79 yield return new NMove(reassign, indices); 80 } 81 } 59 82 } 60 83 61 84 public override IEnumerable<NMove> GenerateMoves(IntegerVector assignment, int n, GQAPInstance problemInstance) { 62 return Generate(RandomParameter.ActualValue, assignment, n, problemInstance, SampleSizeParameter.ActualValue.Value); 85 if (n != 1) throw new ArgumentException("N must be equal to 1 for the exhaustive 1-move generator."); 86 var random = RandomParameter.ActualValue; 87 return Generate(assignment, problemInstance).Shuffle(random); 63 88 } 64 89 }
Note: See TracChangeset
for help on using the changeset viewer.