Changeset 8338
- Timestamp:
- 07/27/12 01:22:49 (12 years ago)
- Location:
- trunk/sources
- Files:
-
- 3 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/ThreeOpt/ExhaustiveInsertionMoveGenerator.cs
r7259 r8338 21 21 22 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 23 25 using HeuristicLab.Common; 24 26 using HeuristicLab.Core; … … 39 41 } 40 42 41 public static TranslocationMove[] Apply(Permutation permutation) {43 public static IEnumerable<TranslocationMove> Generate(Permutation permutation) { 42 44 int length = permutation.Length; 43 45 if (length == 1) throw new ArgumentException("ExhaustiveInsertionMoveGenerator: There cannot be an insertion move given a permutation of length 1.", "permutation"); 44 TranslocationMove[] moves = null; 45 int count = 0; 46 46 47 if (permutation.PermutationType == PermutationTypes.Absolute) { 47 moves = new TranslocationMove[length * (length - 1)];48 48 for (int i = 0; i < length; i++) { 49 49 for (int j = 1; j <= length - 1; j++) { 50 moves[count++] =new TranslocationMove(i, i, (i + j) % length);50 yield return new TranslocationMove(i, i, (i + j) % length); 51 51 } 52 52 } 53 53 } else { 54 54 if (length > 2) { 55 moves = new TranslocationMove[length * (length - 1) - 2];56 55 for (int i = 0; i < length; i++) { 57 56 for (int j = 1; j <= length - 1; j++) { 58 57 if (i == 0 && j == length - 1 59 58 || i == length - 1 && j == 1) continue; 60 moves[count++] =new TranslocationMove(i, i, (i + j) % length);59 yield return new TranslocationMove(i, i, (i + j) % length); 61 60 } 62 61 } 63 62 } else { // doesn't make sense, but just create a dummy move to not crash the algorithms 64 moves = new TranslocationMove[1]; 65 moves[0] = new TranslocationMove(0, 0, 1); 63 yield return new TranslocationMove(0, 0, 1); 66 64 } 67 65 } 68 System.Diagnostics.Debug.Assert(count == moves.Length); 69 return moves; 66 } 67 68 public static TranslocationMove[] Apply(Permutation permutation) { 69 return Generate(permutation).ToArray(); 70 70 } 71 71 -
trunk/sources/HeuristicLab.Encodings.PermutationEncoding/3.3/Moves/TwoOpt/ExhaustiveInversionMoveGenerator.cs
r7259 r8338 21 21 22 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 23 25 using HeuristicLab.Common; 24 26 using HeuristicLab.Core; … … 40 42 41 43 public static InversionMove[] Apply(Permutation permutation) { 44 return Generate(permutation).ToArray(); 45 } 46 47 public static IEnumerable<InversionMove> Generate(Permutation permutation) { 42 48 int length = permutation.Length; 43 49 if (length == 1) throw new ArgumentException("ExhaustiveInversionMoveGenerator: There cannot be an inversion move given a permutation of length 1.", "permutation"); 44 50 int totalMoves = (length) * (length - 1) / 2; 45 InversionMove[] moves = null;46 int count = 0;47 51 48 52 if (permutation.PermutationType == PermutationTypes.RelativeUndirected) { 49 53 if (totalMoves - 3 > 0) { 50 moves = new InversionMove[totalMoves - 3];51 54 for (int i = 0; i < length - 1; i++) { 52 55 for (int j = i + 1; j < length; j++) { 53 56 // doesn't make sense to inverse the whole permutation or the whole but one in case of relative undirected permutations 54 57 if (j - i >= length - 2) continue; 55 moves[count++] =new InversionMove(i, j);58 yield return new InversionMove(i, j); 56 59 } 57 60 } 58 61 } else { // when length is 3 or less, there's actually no difference, but for the sake of not crashing the algorithm create a dummy move 59 moves = new InversionMove[1]; 60 moves[0] = new InversionMove(0, 1); 62 yield return new InversionMove(0, 1); 61 63 } 62 64 } else { 63 moves = new InversionMove[totalMoves];64 65 for (int i = 0; i < length - 1; i++) 65 66 for (int j = i + 1; j < length; j++) { 66 moves[count++] =new InversionMove(i, j);67 yield return new InversionMove(i, j); 67 68 } 68 69 } 69 return moves;70 70 } 71 71 -
trunk/sources/HeuristicLab.Problems.QuadraticAssignment/3.3/HeuristicLab.Problems.QuadraticAssignment-3.3.csproj
r7877 r8338 125 125 <Compile Include="Interfaces\IQAPEvaluator.cs" /> 126 126 <Compile Include="Interfaces\IQAPMoveEvaluator.cs" /> 127 <Compile Include="LocalImprovement\QAPExhaustiveInsertionLocalImprovement.cs" /> 128 <Compile Include="LocalImprovement\QAPStochasticScrambleLocalImprovement.cs" /> 127 129 <Compile Include="LocalImprovement\QAPExhaustiveSwap2LocalImprovement.cs" /> 130 <Compile Include="LocalImprovement\QAPExhaustiveInversionLocalImprovement.cs" /> 128 131 <Compile Include="QAPAssignment.cs" /> 129 132 <Compile Include="QAPPermutationProximityCalculator.cs" /> -
trunk/sources/HeuristicLab.Problems.QuadraticAssignment/3.3/LocalImprovement/QAPExhaustiveSwap2LocalImprovement.cs
r7878 r8338 21 21 22 22 using System; 23 using System.Threading; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 44 45 get { return problem; } 45 46 set { problem = (QuadraticAssignmentProblem)value; } 47 } 48 49 public ILookupParameter<IntValue> LocalIterationsParameter { 50 get { return (ILookupParameter<IntValue>)Parameters["LocalIterations"]; } 46 51 } 47 52 … … 86 91 public QAPExhaustiveSwap2LocalImprovement() 87 92 : base() { 88 Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum amount of iterations that should be performed (note that this operator will abort earlier when a local optimum is reached.", new IntValue(10000))); 93 Parameters.Add(new LookupParameter<IntValue>("LocalIterations", "The number of iterations that have already been performed.")); 94 Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum amount of iterations that should be performed (note that this operator will abort earlier when a local optimum is reached).", new IntValue(10000))); 89 95 Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The amount of evaluated solutions (here a move is counted only as 4/n evaluated solutions with n being the length of the permutation).")); 90 96 Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection where to store results.")); … … 100 106 } 101 107 102 public static double Improve(Permutation assignment, double quality, DoubleMatrix weights, DoubleMatrix distances, bool maximization, int maxIterations, out double evaluatedSolutions) { 103 evaluatedSolutions = 0.0; 108 // BackwardsCompatibility3.3 109 #region Backwards compatible code, remove with 3.4 110 [StorableHook(HookType.AfterDeserialization)] 111 private void AfterDeserialization() { 112 if (!Parameters.ContainsKey("LocalIterations")) 113 Parameters.Add(new LookupParameter<IntValue>("LocalIterations", "The number of iterations that have already been performed.")); 114 } 115 #endregion 116 117 public static void Improve(Permutation assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, CancellationToken cancellation) { 104 118 double evalSolPerMove = 4.0 / assignment.Length; 105 119 106 for (int i = 0; i < maxIterations; i++) {120 for (int i = localIterations.Value; i < maxIterations; i++) { 107 121 Swap2Move bestMove = null; 108 122 double bestQuality = 0; // we have to make an improvement, so 0 is the baseline 123 double evaluations = 0.0; 109 124 foreach (Swap2Move move in ExhaustiveSwap2MoveGenerator.Generate(assignment)) { 110 125 double moveQuality = QAPSwap2MoveEvaluator.Apply(assignment, move, weights, distances); 111 evaluat edSolutions += evalSolPerMove;126 evaluations += evalSolPerMove; 112 127 if (maximization && moveQuality > bestQuality 113 128 || !maximization && moveQuality < bestQuality) { … … 116 131 } 117 132 } 133 evaluatedSolutions.Value += (int)Math.Ceiling(evaluations); 118 134 if (bestMove == null) break; 119 135 Swap2Manipulator.Apply(assignment, bestMove.Index1, bestMove.Index2); 120 quality += bestQuality; 136 quality.Value += bestQuality; 137 localIterations.Value++; 138 cancellation.ThrowIfCancellationRequested(); 121 139 } 122 return quality;123 140 } 124 141 125 142 public override IOperation Apply() { 126 int maxIterations = MaximumIterationsParameter.ActualValue.Value; 127 Permutation assignment = AssignmentParameter.ActualValue; 128 bool maximization = MaximizationParameter.ActualValue.Value; 129 DoubleMatrix weights = WeightsParameter.ActualValue; 130 DoubleMatrix distances = DistancesParameter.ActualValue; 131 double quality = QualityParameter.ActualValue.Value; 143 var maxIterations = MaximumIterationsParameter.ActualValue.Value; 144 var assignment = AssignmentParameter.ActualValue; 145 var maximization = MaximizationParameter.ActualValue.Value; 146 var weights = WeightsParameter.ActualValue; 147 var distances = DistancesParameter.ActualValue; 148 var quality = QualityParameter.ActualValue; 149 var localIterations = LocalIterationsParameter.ActualValue; 150 var evaluations = EvaluatedSolutionsParameter.ActualValue; 151 if (localIterations == null) { 152 localIterations = new IntValue(0); 153 LocalIterationsParameter.ActualValue = localIterations; 154 } 132 155 133 double evaluations; 134 QualityParameter.ActualValue.Value = Improve(assignment, quality, weights, distances, maximization, maxIterations, out evaluations); 135 EvaluatedSolutionsParameter.ActualValue.Value += (int)Math.Ceiling(evaluations); 156 Improve(assignment, weights, distances, quality, localIterations, evaluations, maximization, maxIterations, CancellationToken); 136 157 return base.Apply(); 137 158 }
Note: See TracChangeset
for help on using the changeset viewer.