Changeset 14556
- Timestamp:
- 01/11/17 01:53:44 (8 years ago)
- Location:
- branches/MemPRAlgorithm
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Binary/BinaryMemPR.cs
r14552 r14556 160 160 while (evaluations < N) { 161 161 BinaryVector c = null; 162 var xochoice = Context.Random.Next (3);163 switch (xochoice) {164 c ase 0: c = NPointCrossover.Apply(Context.Random, p1.Solution, p2.Solution, new IntValue(1)); break;165 case 1: c = NPointCrossover.Apply(Context.Random, p1.Solution, p2.Solution, new IntValue(2)); break;166 c ase 2: c = UniformCrossover.Apply(Context.Random, p1.Solution, p2.Solution); break;167 }162 var xochoice = Context.Random.NextDouble(); 163 if (xochoice < 0.2) 164 c = NPointCrossover.Apply(Context.Random, p1.Solution, p2.Solution, new IntValue(1)); 165 else if (xochoice < 0.5) 166 c = NPointCrossover.Apply(Context.Random, p1.Solution, p2.Solution, new IntValue(2)); 167 else c = UniformCrossover.Apply(Context.Random, p1.Solution, p2.Solution); 168 168 if (cache.Contains(c)) { 169 169 cacheHits++; 170 if (cacheHits > 10) break;170 if (cacheHits > 20) break; 171 171 continue; 172 172 } 173 probe.Solution = c; 173 174 Context.Evaluate(probe, token); 174 175 evaluations++; -
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/LinearLinkage/LinearLinkageMemPR.cs
r14552 r14556 260 260 continue; 261 261 } 262 probe.Solution = c; 262 263 Context.Evaluate(probe, token); 263 264 evaluations++; -
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/MemPRAlgorithm.cs
r14552 r14556 43 43 where TPopulationContext : MemPRPopulationContext<TProblem, TSolution, TPopulationContext, TSolutionContext>, new() 44 44 where TSolutionContext : MemPRSolutionContext<TProblem, TSolution, TPopulationContext, TSolutionContext> { 45 private const double MutationProbabilityMagicConst = 0.1;46 45 47 46 public override Type ProblemType { -
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/ExhaustiveHillClimbSubspace.cs
r14552 r14556 33 33 [StorableClass] 34 34 public class ExhaustiveHillClimbSubspace<TContext> : NamedItem, ILocalSearch<TContext> 35 where TContext : ISingleSolutionHeuristicAlgorithmContext< SingleObjectiveBasicProblem<PermutationEncoding>, Encodings.PermutationEncoding.Permutation>,35 where TContext : ISingleSolutionHeuristicAlgorithmContext<ISingleObjectiveHeuristicOptimizationProblem, Encodings.PermutationEncoding.Permutation>, 36 36 IPermutationSubspaceContext, IEvaluationServiceContext<Encodings.PermutationEncoding.Permutation> { 37 37 … … 52 52 try { 53 53 var result = Exhaustive.HillClimb(context.Random, context.Solution.Solution, ref quality, 54 context. Problem.Maximization, context.Evaluate, CancellationToken.None, context.Subspace.Subspace);54 context.Maximization, context.Evaluate, CancellationToken.None, context.Subspace.Subspace); 55 55 context.IncrementEvaluatedSolutions(result.Item1); 56 56 context.Iterations = result.Item2; -
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/StaticAPI/Exhaustive2Opt.cs
r14552 r14556 55 55 continue; 56 56 57 InversionManipulator.Apply(current, opt.Index1, opt.Index2);57 current.Reverse(opt.Index1, opt.Index2 - opt.Index1 + 1); 58 58 var q = eval(current, token); 59 59 evaluations++; … … 62 62 quality = q; 63 63 lastSuccessMove = opt; 64 } else InversionManipulator.Apply(current, opt.Index1, opt.Index2);64 } else current.Reverse(opt.Index1, opt.Index2 - opt.Index1 + 1); 65 65 66 66 if (token.IsCancellationRequested) { -
branches/MemPRAlgorithm/HeuristicLab.Algorithms.MemPR/3.3/Permutation/PermutationMemPR.cs
r14552 r14556 23 23 using System.Collections.Generic; 24 24 using System.Linq; 25 using System.Runtime.CompilerServices; 25 26 using System.Threading; 26 27 using HeuristicLab.Algorithms.MemPR.Interfaces; … … 297 298 continue; 298 299 299 InversionManipulator.Apply(current, opt.Index1, opt.Index2);300 current.Reverse(opt.Index1, opt.Index2 - opt.Index1 + 1); 300 301 301 302 var q = eval(current, token); … … 338 339 bestOfTheRestF = q; 339 340 } 340 341 InversionManipulator.Apply(current, opt.Index1, opt.Index2);341 342 current.Reverse(opt.Index1, opt.Index2 - opt.Index1 + 1); 342 343 } 343 344 if (evaluations >= maxEvals) break; … … 359 360 tabu[current[next], current[bestOfTheRest.Index2]] = Math.Min(currentF, tabu[current[next], current[bestOfTheRest.Index2]]); 360 361 } 361 InversionManipulator.Apply(current, bestOfTheRest.Index1, bestOfTheRest.Index2);362 current.Reverse(bestOfTheRest.Index1, bestOfTheRest.Index2 - bestOfTheRest.Index1 + 1); 362 363 363 364 currentF = bestOfTheRestF; … … 397 398 var evaluations = 0; 398 399 ISingleObjectiveSolutionScope<Encodings.PermutationEncoding.Permutation> offspring = null; 400 var probe = Context.ToScope((Encodings.PermutationEncoding.Permutation)p1.Solution.Clone()); 399 401 while (evaluations < p1.Solution.Length) { 400 402 Encodings.PermutationEncoding.Permutation c = null; … … 410 412 continue; 411 413 } 412 var probe = Context.ToScope(c);414 probe.Solution = c; 413 415 Context.Evaluate(probe, token); 414 416 evaluations++; … … 432 434 var evaluations = 0; 433 435 ISingleObjectiveSolutionScope<Encodings.PermutationEncoding.Permutation> offspring = null; 434 while(evaluations < p1.Solution.Length) { 436 var probe = Context.ToScope((Encodings.PermutationEncoding.Permutation)p1.Solution.Clone()); 437 while (evaluations < p1.Solution.Length) { 435 438 Encodings.PermutationEncoding.Permutation c = null; 436 439 var xochoice = Context.Random.Next(3); … … 445 448 continue; 446 449 } 447 var probe = Context.ToScope(c);450 probe.Solution = c; 448 451 Context.Evaluate(probe, token); 449 452 evaluations++; … … 467 470 var evaluations = 0; 468 471 ISingleObjectiveSolutionScope<Encodings.PermutationEncoding.Permutation> offspring = null; 469 while(evaluations <= p1.Solution.Length) { 472 var probe = Context.ToScope((Encodings.PermutationEncoding.Permutation)p1.Solution.Clone()); 473 while (evaluations <= p1.Solution.Length) { 470 474 Encodings.PermutationEncoding.Permutation c = null; 471 475 var xochoice = Context.Random.Next(3); … … 480 484 continue; 481 485 } 482 var probe = Context.ToScope(c);486 probe.Solution = c; 483 487 Context.Evaluate(probe, token); 484 488 evaluations++; … … 507 511 return RelinkShift(random, p1, p2, eval, token, delink, out best); 508 512 case PermutationTypes.RelativeUndirected: 509 return RelinkOpt(random, p1, p2, eval, token, delink, out best);513 return delink ? DelinkOpt(random, p1, p2, eval, token, out best) : RelinkOpt(random, p1, p2, eval, token, out best); 510 514 default: throw new ArgumentException(string.Format("Unknown permutation type {0}", p1.PermutationType)); 511 515 } … … 684 688 } 685 689 686 public Encodings.PermutationEncoding.Permutation RelinkOpt(IRandom random, Encodings.PermutationEncoding.Permutation p1, Encodings.PermutationEncoding.Permutation p2, Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval, CancellationToken token, bool delink,out double best) {690 public Encodings.PermutationEncoding.Permutation RelinkOpt(IRandom random, Encodings.PermutationEncoding.Permutation p1, Encodings.PermutationEncoding.Permutation p2, Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval, CancellationToken token, out double best) { 687 691 var maximization = Context.Maximization; 688 692 var evaluations = 0; … … 777 781 var m = bestQueue.Dequeue(); 778 782 Opt(child, m.Item1, m.Item2); 779 }780 for (var i = 0; i < child.Length; i++) invChild[child[i]] = i;783 for (var i = m.Item1; i <= m.Item2; i++) invChild[child[i]] = i; 784 } 781 785 if (FitnessComparer.IsBetter(maximization, bestChange, best)) { 782 786 best = bestChange; … … 797 801 } 798 802 803 public Encodings.PermutationEncoding.Permutation DelinkOpt(IRandom random, Encodings.PermutationEncoding.Permutation p1, Encodings.PermutationEncoding.Permutation p2, Func<Encodings.PermutationEncoding.Permutation, CancellationToken, double> eval, CancellationToken token, out double best) { 804 var evaluations = 0; 805 var child = (Encodings.PermutationEncoding.Permutation)p1.Clone(); 806 807 best = double.NaN; 808 Encodings.PermutationEncoding.Permutation bestChild = null; 809 810 var invChild = new int[child.Length]; 811 var invP2 = new int[child.Length]; 812 for (var i = 0; i < child.Length; i++) { 813 invChild[child[i]] = i; 814 invP2[p2[i]] = i; 815 } 816 817 var order = Enumerable.Range(0, p2.Length).Where(x => IsUndirectedEdge(invP2, child[x], child.GetCircular(x + 1))).Shuffle(Context.Random).ToList(); 818 while (order.Count > 0) { 819 var idx = order.First(); 820 var bestChange = double.NaN; 821 var bestIdx = -1; 822 for (var m = 0; m < p2.Length; m++) { 823 if (Math.Abs(m - idx) <= 1 || Math.Abs(m - idx) >= p2.Length - 2) continue; 824 if (m < idx) { 825 if (IsUndirectedEdge(invP2, child.GetCircular(m - 1), child[idx]) 826 || IsUndirectedEdge(invP2, child[m], child.GetCircular(idx + 1))) continue; 827 Opt(child, m, idx); 828 var moveF = eval(child, token); 829 evaluations++; 830 if (Context.IsBetter(moveF, bestChange)) { 831 bestChange = moveF; 832 bestIdx = m; 833 } 834 // undo 835 Opt(child, m, idx); 836 } else { 837 if (IsUndirectedEdge(invP2, child[idx], child[m]) 838 || IsUndirectedEdge(invP2, child.GetCircular(idx + 1), child.GetCircular(m + 1))) continue; 839 Opt(child, idx + 1, m); 840 var moveF = eval(child, token); 841 evaluations++; 842 if (Context.IsBetter(moveF, bestChange)) { 843 bestChange = moveF; 844 bestIdx = m; 845 } 846 // undo 847 Opt(child, idx + 1, m); 848 } 849 } 850 if (bestIdx >= 0) { 851 if (bestIdx > idx) 852 Opt(child, idx + 1, bestIdx); 853 else Opt(child, bestIdx, idx); 854 for (var i = Math.Min(idx, bestIdx); i <= Math.Max(idx, bestIdx); i++) 855 invChild[child[i]] = i; 856 857 order = Enumerable.Range(0, p2.Length).Where(x => IsUndirectedEdge(invP2, child[x], child.GetCircular(x + 1))).Shuffle(Context.Random).ToList(); 858 if (Context.IsBetter(bestChange, best)) { 859 best = bestChange; 860 bestChild = (Encodings.PermutationEncoding.Permutation)child.Clone(); 861 } 862 } 863 } 864 865 if (bestChild == null) { 866 best = eval(child, token); 867 evaluations++; 868 } 869 Context.IncrementEvaluatedSolutions(evaluations); 870 871 if (VALIDATE && bestChild != null && !bestChild.Validate()) throw new ArgumentException("Delinking produced invalid child"); 872 if (VALIDATE && Dist(child, p2) < 1) throw new InvalidOperationException("Child is not different from p2 after delinking"); 873 return bestChild ?? child; 874 } 875 799 876 private static bool IsUndirectedEdge(int[] invP, int a, int b) { 800 877 var d = Math.Abs(invP[a] - invP[b]); … … 810 887 } 811 888 889 [MethodImpl(MethodImplOptions.AggressiveInlining)] 812 890 private static void Opt(Encodings.PermutationEncoding.Permutation child, int from, int to) { 813 if (from > to) { 814 var h = from; 815 from = to; 816 to = h; 817 } 818 InversionManipulator.Apply(child, from, to); 891 if (from > to) child.Reverse(to, from - to + 1); 892 else child.Reverse(from, to - from + 1); 819 893 } 820 894 } -
branches/MemPRAlgorithm/HeuristicLab.Encodings.PermutationEncoding/3.3/Manipulators/InversionManipulator.cs
r14185 r14556 60 60 61 61 public static void Apply(Permutation permutation, int breakPoint1, int breakPoint2) { 62 for (int i = 0; i <= (breakPoint2 - breakPoint1) / 2; i++) { // invert permutation between breakpoints 63 int temp = permutation[breakPoint1 + i]; 64 permutation[breakPoint1 + i] = permutation[breakPoint2 - i]; 65 permutation[breakPoint2 - i] = temp; 66 } 62 permutation.Reverse(breakPoint1, breakPoint2 - breakPoint1 + 1); 67 63 } 68 64 -
branches/MemPRAlgorithm/HeuristicLab.Encodings.PermutationEncoding/3.3/Permutation.cs
r14185 r14556 121 121 } 122 122 123 public virtual void Reverse(int startIndex, int length) { 124 Array.Reverse(array, startIndex, length); 125 } 126 123 127 public event EventHandler PermutationTypeChanged; 124 128
Note: See TracChangeset
for help on using the changeset viewer.