- Timestamp:
- 02/20/17 20:41:33 (8 years ago)
- Location:
- branches/PerformanceComparison/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PerformanceComparison/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/ExhaustiveHillClimb.cs
r14552 r14690 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 22 24 using System.Threading; 23 25 using HeuristicLab.Algorithms.MemPR.Interfaces; 24 using HeuristicLab.Algorithms.MemPR.Util;25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 using HeuristicLab.Encodings.PermutationEncoding;28 28 using HeuristicLab.Optimization; 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 34 34 public class ExhaustiveHillClimb<TContext> : NamedItem, ILocalSearch<TContext> 35 35 where TContext : ISingleSolutionHeuristicAlgorithmContext<ISingleObjectiveHeuristicOptimizationProblem, Encodings.PermutationEncoding.Permutation>, 36 IEvaluationServiceContext<Encodings.PermutationEncoding.Permutation> { 36 IEvaluationServiceContext<Encodings.PermutationEncoding.Permutation>, 37 ILocalSearchPathContext<Encodings.PermutationEncoding.Permutation> { 37 38 38 39 [StorableConstructor] … … 49 50 50 51 public void Optimize(TContext context) { 52 if (double.IsNaN(context.Solution.Fitness)) 53 context.Evaluate(context.Solution, CancellationToken.None); 51 54 var quality = context.Solution.Fitness; 52 55 try { 53 var result = Exhaustive.HillClimb(context.Random, context.Solution.Solution, ref quality, 56 var path = new List<Tuple<Encodings.PermutationEncoding.Permutation, double>>(); 57 path.Add(Tuple.Create((Encodings.PermutationEncoding.Permutation)context.Solution.Solution.Clone(), quality)); 58 59 var result = Exhaustive.HillClimb(context.Random, context.Solution.Solution, quality, 54 60 context.Maximization, context.Evaluate, CancellationToken.None); 55 context.IncrementEvaluatedSolutions(result.Item1); 56 context.Iterations = result.Item2; 61 62 Tuple<Encodings.PermutationEncoding.Permutation, double, int> last = null; 63 foreach (var step in result) { 64 path.Add(Tuple.Create((Encodings.PermutationEncoding.Permutation)step.Item1.Clone(), step.Item2)); 65 last = step; 66 } 67 context.LocalSearchPaths.AddPath(path); 68 context.IncrementEvaluatedSolutions(last.Item3); 69 context.Iterations = path.Count - 2; 57 70 } finally { 58 71 context.Solution.Fitness = quality; -
branches/PerformanceComparison/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/ExhaustiveHillClimbSubspace.cs
r14556 r14690 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 22 24 using System.Threading; 23 25 using HeuristicLab.Algorithms.MemPR.Interfaces; 24 using HeuristicLab.Algorithms.MemPR.Util;25 26 using HeuristicLab.Common; 26 27 using HeuristicLab.Core; 27 using HeuristicLab.Encodings.PermutationEncoding;28 28 using HeuristicLab.Optimization; 29 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 34 34 public class ExhaustiveHillClimbSubspace<TContext> : NamedItem, ILocalSearch<TContext> 35 35 where TContext : ISingleSolutionHeuristicAlgorithmContext<ISingleObjectiveHeuristicOptimizationProblem, Encodings.PermutationEncoding.Permutation>, 36 IPermutationSubspaceContext, IEvaluationServiceContext<Encodings.PermutationEncoding.Permutation> { 36 IPermutationSubspaceContext, IEvaluationServiceContext<Encodings.PermutationEncoding.Permutation>, 37 ILocalSearchPathContext<Encodings.PermutationEncoding.Permutation> { 37 38 38 39 [StorableConstructor] … … 49 50 50 51 public void Optimize(TContext context) { 52 if (double.IsNaN(context.Solution.Fitness)) 53 context.Evaluate(context.Solution, CancellationToken.None); 51 54 var quality = context.Solution.Fitness; 52 55 try { 53 var result = Exhaustive.HillClimb(context.Random, context.Solution.Solution, ref quality, 56 var path = new List<Tuple<Encodings.PermutationEncoding.Permutation, double>>(); 57 path.Add(Tuple.Create((Encodings.PermutationEncoding.Permutation)context.Solution.Solution.Clone(), quality)); 58 59 var result = Exhaustive.HillClimb(context.Random, context.Solution.Solution, quality, 54 60 context.Maximization, context.Evaluate, CancellationToken.None, context.Subspace.Subspace); 55 context.IncrementEvaluatedSolutions(result.Item1); 56 context.Iterations = result.Item2; 61 62 Tuple<Encodings.PermutationEncoding.Permutation, double, int> last = null; 63 foreach (var step in result) { 64 path.Add(Tuple.Create((Encodings.PermutationEncoding.Permutation)step.Item1.Clone(), step.Item2)); 65 last = step; 66 } 67 context.LocalSearchPaths.AddPath(path); 68 context.IncrementEvaluatedSolutions(last.Item3); 69 context.Iterations = path.Count - 2; 57 70 } finally { 58 71 context.Solution.Fitness = quality; -
branches/PerformanceComparison/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/StaticAPI/Exhaustive.cs
r14552 r14690 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Threading; 24 25 using HeuristicLab.Core; … … 33 34 #endif 34 35 35 public static Tuple<int, int> HillClimb(IRandom random, Encodings.PermutationEncoding.Permutation perm,36 refdouble quality, bool maximization, Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval,36 public static IEnumerable<Tuple<Encodings.PermutationEncoding.Permutation, double, int>> HillClimb(IRandom random, Encodings.PermutationEncoding.Permutation perm, 37 double quality, bool maximization, Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval, 37 38 CancellationToken token, bool[,] subspace = null) { 38 39 if (double.IsNaN(quality)) quality = eval(perm, token); 39 Tuple<int, int> changes;40 IEnumerable<Tuple<Encodings.PermutationEncoding.Permutation, double, int>> localSearchPath; 40 41 switch (perm.PermutationType) { 41 42 case PermutationTypes.Absolute: 42 changes = ExhaustiveSwap2.HillClimb(random, perm, refquality, maximization, eval, token, subspace);43 localSearchPath = ExhaustiveSwap2.HillClimb(random, perm, quality, maximization, eval, token, subspace); 43 44 break; 44 45 case PermutationTypes.RelativeDirected: 45 changes = Exhaustive1Shift.HillClimb(random, perm, refquality, maximization, eval, token, subspace);46 localSearchPath = Exhaustive1Shift.HillClimb(random, perm, quality, maximization, eval, token, subspace); 46 47 break; 47 48 case PermutationTypes.RelativeUndirected: 48 changes = Exhaustive2Opt.HillClimb(random, perm, refquality, maximization, eval, token, subspace);49 localSearchPath = Exhaustive2Opt.HillClimb(random, perm, quality, maximization, eval, token, subspace); 49 50 break; 50 51 default: throw new ArgumentException(string.Format("Permutation type {0} is not known", perm.PermutationType)); 51 52 } 52 53 if (VALIDATE && !perm.Validate()) throw new ArgumentException("HillClimb produced invalid child"); 53 return changes;54 return localSearchPath; 54 55 } 55 56 } -
branches/PerformanceComparison/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/StaticAPI/Exhaustive1Shift.cs
r14552 r14690 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using System.Threading; … … 30 31 namespace HeuristicLab.Algorithms.MemPR.Permutation.LocalSearch { 31 32 public static class Exhaustive1Shift { 32 public static Tuple<int, int> HillClimb(IRandom random, Encodings.PermutationEncoding.Permutation perm, 33 ref double quality, bool maximization, Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval, 33 public static IEnumerable<Tuple<Encodings.PermutationEncoding.Permutation, double, int>> 34 HillClimb(IRandom random, Encodings.PermutationEncoding.Permutation perm, 35 double quality, bool maximization, Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval, 34 36 CancellationToken token, bool[,] subspace = null) { 35 37 var evaluations = 0; … … 40 42 } 41 43 TranslocationMove lastSuccessMove = null; 42 var steps = 0;43 44 var neighborhood = ExhaustiveInsertionMoveGenerator.Generate(current).Shuffle(random).ToList(); 44 45 while (true) { … … 62 63 evaluations++; 63 64 if (FitnessComparer.IsBetter(maximization, q, quality)) { 64 steps++;65 yield return Tuple.Create(current, q, evaluations); 65 66 quality = q; 66 67 lastSuccessMove = shift; … … 74 75 if (lastSuccessMove == null) break; 75 76 } 76 return Tuple.Create(evaluations, steps);77 yield return Tuple.Create(current, quality, evaluations); 77 78 } 78 79 } -
branches/PerformanceComparison/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/StaticAPI/Exhaustive2Opt.cs
r14556 r14690 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using System.Threading; … … 30 31 namespace HeuristicLab.Algorithms.MemPR.Permutation.LocalSearch { 31 32 public static class Exhaustive2Opt { 32 public static Tuple<int, int> HillClimb(IRandom random, Encodings.PermutationEncoding.Permutation perm, 33 ref double quality, bool maximization, Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval, 33 public static IEnumerable<Tuple<Encodings.PermutationEncoding.Permutation, double, int>> 34 HillClimb(IRandom random, Encodings.PermutationEncoding.Permutation perm, 35 double quality, bool maximization, Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval, 34 36 CancellationToken token, bool[,] subspace = null) { 35 37 var evaluations = 0; … … 40 42 } 41 43 InversionMove lastSuccessMove = null; 42 var steps = 0;43 44 var neighborhood = ExhaustiveInversionMoveGenerator.Generate(current).Shuffle(random).ToList(); 44 45 while (true) { … … 59 60 evaluations++; 60 61 if (FitnessComparer.IsBetter(maximization, q, quality)) { 61 steps++;62 yield return Tuple.Create(current, q, evaluations); 62 63 quality = q; 63 64 lastSuccessMove = opt; … … 71 72 if (lastSuccessMove == null) break; 72 73 } 73 return Tuple.Create(evaluations, steps);74 yield return Tuple.Create(current, quality, evaluations); 74 75 } 75 76 } -
branches/PerformanceComparison/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/StaticAPI/ExhaustiveSwap2.cs
r14552 r14690 21 21 22 22 using System; 23 using System.Collections.Generic; 23 24 using System.Linq; 24 25 using System.Threading; … … 30 31 namespace HeuristicLab.Algorithms.MemPR.Permutation.LocalSearch { 31 32 public static class ExhaustiveSwap2 { 32 public static Tuple<int, int> HillClimb(IRandom random, Encodings.PermutationEncoding.Permutation perm, 33 ref double quality, bool maximization, Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval, 33 public static IEnumerable<Tuple<Encodings.PermutationEncoding.Permutation, double, int>> 34 HillClimb(IRandom random, Encodings.PermutationEncoding.Permutation perm, 35 double quality, bool maximization, Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval, 34 36 CancellationToken token, bool[,] subspace = null) { 35 37 var evaluations = 0; … … 40 42 } 41 43 Swap2Move lastSuccessMove = null; 42 var steps = 0;43 44 var neighborhood = ExhaustiveSwap2MoveGenerator.Generate(current).Shuffle(random).ToList(); 44 45 while (true) { … … 58 59 evaluations++; 59 60 if (FitnessComparer.IsBetter(maximization, q, quality)) { 60 steps++;61 yield return Tuple.Create(current, q, evaluations); 61 62 quality = q; 62 63 lastSuccessMove = swap; … … 73 74 if (lastSuccessMove == null) break; 74 75 } 75 return Tuple.Create(evaluations, steps);76 yield return Tuple.Create(current, quality, evaluations); 76 77 } 77 78 }
Note: See TracChangeset
for help on using the changeset viewer.