- Timestamp:
- 05/02/12 19:09:48 (13 years ago)
- Location:
- branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3
- Files:
-
- 19 added
- 17 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/DiversityCalculator.cs
r7744 r7775 42 42 } 43 43 44 public int ExecuteCalculation(IItem left, IItem right) { 45 if (left == null || right == null) throw new ArgumentException("Cannot calculate diversity because one of the provided objects is null."); 46 if (left.GetType() != right.GetType()) throw new ArgumentException("Cannot calculate diversity because the provided objects differ in type."); 44 public double ExecuteCalculation(IItem left, IItem right) { 45 if (left == null || right == null) 46 throw new ArgumentException("Cannot calculate diversity because one of the provided objects is null."); 47 if (left.GetType() != right.GetType()) 48 throw new ArgumentException("Cannot calculate diversity because the provided objects differ in type."); 49 47 50 if (left == right) return 0; 48 51 else return CalculateDiversity(left, right); 49 52 } 50 53 51 protected abstract intCalculateDiversity(IItem left, IItem right);54 protected abstract double CalculateDiversity(IItem left, IItem right); 52 55 } 53 56 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/HeuristicLab.Algorithms.ScatterSearch-3.3.csproj
r7756 r7775 39 39 </PropertyGroup> 40 40 <ItemGroup> 41 <Reference Include="HeuristicLab.Algorithms.LocalSearch-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" />42 41 <Reference Include="HeuristicLab.Analysis-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 43 42 <Reference Include="HeuristicLab.Collections-3.3"> … … 55 54 <Reference Include="HeuristicLab.Encodings.BinaryVectorEncoding-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 56 55 <Reference Include="HeuristicLab.Encodings.PermutationEncoding-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 56 <Reference Include="HeuristicLab.Encodings.RealVectorEncoding-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 57 57 <Reference Include="HeuristicLab.Operators-3.3"> 58 58 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Operators-3.3.dll</HintPath> … … 72 72 <Reference Include="HeuristicLab.PluginInfrastructure-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 73 73 <Reference Include="HeuristicLab.Problems.Knapsack-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 74 <Reference Include="HeuristicLab.Problems.TestFunctions-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 74 75 <Reference Include="HeuristicLab.Problems.TravelingSalesman-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 75 76 <Reference Include="HeuristicLab.Random-3.3"> … … 86 87 </ItemGroup> 87 88 <ItemGroup> 89 <Compile Include="SolutionPool2TierUpdateMethod.cs" /> 88 90 <Compile Include="DiversityCalculator.cs" /> 89 91 <Compile Include="IPathRelinker.cs" /> 90 92 <Compile Include="Knapsack\BinaryVectorDiversityCalculator.cs" /> 93 <Compile Include="Knapsack\KnapsackMultipleGuidesPathRelinker.cs" /> 94 <Compile Include="Knapsack\KnapsackSimultaneousPathRelinker.cs" /> 91 95 <Compile Include="Knapsack\KnapsackPathRelinker.cs" /> 96 <Compile Include="TestFunctions\TestFunctionsZakharovImprovementOperator.cs" /> 97 <Compile Include="TestFunctions\TestFunctionsSumSquaresImprovementOperator.cs" /> 98 <Compile Include="TestFunctions\TestFunctionsSchwefelImprovementOperator.cs" /> 99 <Compile Include="TestFunctions\TestFunctionsRosenbrockImprovementOperator.cs" /> 100 <Compile Include="TestFunctions\TestFunctionsMatyasImprovementOperator.cs" /> 101 <Compile Include="TestFunctions\TestFunctionsLevyImprovementOperator.cs" /> 102 <Compile Include="TestFunctions\TestFunctionsGriewankImprovementOperator.cs" /> 103 <Compile Include="TestFunctions\TestFunctionsBoothImprovementOperator.cs" /> 104 <Compile Include="TestFunctions\TestFunctionsBealeImprovementOperator.cs" /> 105 <Compile Include="TestFunctions\TestFunctionsAckleyImprovementOperator.cs" /> 106 <Compile Include="TestFunctions\TestFunctionsPathRelinker.cs" /> 107 <Compile Include="TestFunctions\RealVectorDiversityCalculator.cs" /> 108 <Compile Include="TestFunctions\TestFunctionsImprovementOperator.cs" /> 109 <Compile Include="TravelingSalesman\TravelingSalesmanMultipleGuidesPathRelinker.cs" /> 110 <Compile Include="TravelingSalesman\TravelingSalesmanSimultaneousPathRelinker.cs" /> 92 111 <Compile Include="TravelingSalesman\TravelingSalesmanPathRelinker.cs" /> 93 112 <Compile Include="PathRelinker.cs" /> -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/IScatterSearchTargetProcessor.cs
r7744 r7775 23 23 24 24 namespace HeuristicLab.Algorithms.ScatterSearch { 25 /// <summary> 26 /// An interface which represents an operator that uses a target parameter. 27 /// </summary> 25 28 public interface IScatterSearchTargetProcessor { 26 29 IValueLookupParameter<IItem> TargetParameter { get; } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/BinaryVectorDiversityCalculator.cs
r7756 r7775 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; … … 41 42 } 42 43 43 protected override int CalculateDiversity(IItem left, IItem right) { 44 protected override double CalculateDiversity(IItem left, IItem right) { 45 if (!(left is BinaryVector) || !(right is BinaryVector)) 46 throw new ArgumentException("Cannot calculate diversity because one of the provided objects or both have the wrong type."); 47 44 48 var v1 = left as BinaryVector; 45 49 var v2 = right as BinaryVector; 46 int diversity = 0; 47 if (v1 != null && v2 != null) 48 for (int i = 0; i < v1.Length; i++) 49 if (v1[i] != v2[i]) diversity++; 50 double diversity = 0; 51 for (int i = 0; i < v1.Length; i++) 52 if (v1[i] != v2[i]) diversity++; 50 53 return diversity; 51 54 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/KnapsackImprovementOperator.cs
r7756 r7775 67 67 get { return (IValueLookupParameter<IItem>)Parameters["Target"]; } 68 68 } 69 public IValueLookupParameter<DoubleValue> PenaltyParameter { 70 get { return (IValueLookupParameter<DoubleValue>)Parameters["Penalty"]; } 71 } 69 72 #region ILocalImprovementOperator Parameters 70 73 public IValueLookupParameter<IntValue> MaximumIterationsParameter { … … 99 102 get { return TargetParameter.ActualValue; } 100 103 } 104 private DoubleValue Penalty { 105 get { return PenaltyParameter.ActualValue; } 106 set { PenaltyParameter.ActualValue = value; } 107 } 101 108 #endregion 102 109 … … 115 122 Parameters.Add(new ValueLookupParameter<IntValue>("KnapsackCapacity")); 116 123 Parameters.Add(new ValueLookupParameter<IItem>("Target")); 124 Parameters.Add(new ValueLookupParameter<DoubleValue>("Penalty")); 117 125 #endregion 118 126 TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem … … 128 136 // calculate value-to-weight ratio 129 137 double[] ratio = new double[Values.Length]; 130 for (int i = 0; i < ratio.Length; i++) {138 for (int i = 0; i < ratio.Length; i++) 131 139 ratio[i] = (double)Values[i] / (double)Weights[i]; 132 } 140 133 141 134 142 // calculate order for ratio … … 140 148 141 149 int j = sol.Length - 1; 142 while (KnapsackEvaluator.Apply(sol, KnapsackCapacity, new DoubleValue(1.0), Weights, Values).SumWeights.Value > KnapsackCapacity.Value && j >= 0) { 150 while (KnapsackEvaluator.Apply(sol, KnapsackCapacity, Penalty, Weights, Values) 151 .SumWeights.Value > KnapsackCapacity.Value && j >= 0) 143 152 sol[order[j--]] = false; 144 }145 153 146 154 // calculate weight 147 int lhs = 0; 148 for (int i = 0; i < sol.Length; i++) { 149 if (sol[i]) lhs += Weights[i]; 150 } 155 int weight = 0; 156 for (int i = 0; i < sol.Length; i++) 157 if (sol[i]) weight += Weights[i]; 151 158 152 159 // improve solution … … 154 161 while (feasible && j < sol.Length) { 155 162 while (sol[order[j]]) j++; 156 if ( lhs+ Weights[order[j]] <= KnapsackCapacity.Value) {163 if (weight + Weights[order[j]] <= KnapsackCapacity.Value) { 157 164 sol[order[j]] = true; 158 lhs += Weights[order[j]]; 159 } else { 160 feasible = false; 161 } 165 weight += Weights[order[j]]; 166 } else feasible = false; 162 167 } 163 168 … … 166 171 return base.Apply(); 167 172 } 168 169 private bool IsFeasiblse(BinaryVector sol) {170 int sum = 0;171 for (int i = 0; i < sol.Length; i++)172 if (sol[i]) sum += Weights[i];173 return sum <= KnapsackCapacity.Value;174 }175 173 } 176 174 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/KnapsackPathRelinker.cs
r7756 r7775 49 49 50 50 public static ItemArray<IItem> Apply(IItem initiator, IItem guide, PercentValue n) { 51 if (!(initiator is BinaryVector) || !(guide is BinaryVector)) 52 throw new ArgumentException("Cannot relink path because one of the provided solutions or both have the wrong type."); 53 if (n.Value <= 0.0) 54 throw new ArgumentException("RelinkingAccuracy must be greater than 0."); 55 51 56 var v1 = initiator.Clone() as BinaryVector; 52 57 var v2 = guide as BinaryVector; 53 58 54 59 if (v1.Length != v2.Length) 55 throw new ArgumentException("KnapsackPathRelinker: The solutions are of different length."); 56 if (n.Value == 0.0) 57 throw new ArgumentException("KnapsackPathRelinker: RelinkingAccuracy is 0."); 60 throw new ArgumentException("The solutions are of different length."); 58 61 59 62 var solutions = new List<BinaryVector>(); 60 for (int i = 0; i < v1.Length; i++) {63 for (int i = 0; i < v1.Length; i++) 61 64 if (v1[i] != v2[i]) { 62 65 v1[i] = v2[i]; 63 solutions.Add( (BinaryVector)v1.Clone());66 solutions.Add(v1.Clone() as BinaryVector); 64 67 } 65 }66 68 67 var stepSize = 1 / n.Value;68 69 var selection = new List<IItem>(); 69 for (int i = 0; i < solutions.Count; i = (int)Math.Round(i + stepSize)) { 70 selection.Add(solutions.ElementAt(i)); 70 if (solutions.Count > 0) { 71 var noSol = (int)Math.Round(solutions.Count * n.Value); 72 if (noSol <= 0) noSol++; 73 var stepSize = (double)solutions.Count / (double)noSol; 74 for (int i = 0; i < noSol; i++) 75 selection.Add(solutions.ElementAt((int)Math.Round((i + 1) * stepSize - stepSize * 0.5))); 71 76 } 72 77 … … 75 80 76 81 protected override ItemArray<IItem> Relink(ItemArray<IItem> parents, PercentValue n) { 77 if (parents.Length != 2) throw new ArgumentException("KnapsackPathRelinker: The number of parents is not equal to 2."); 82 if (parents.Length != 2) 83 throw new ArgumentException("The number of parents is not equal to 2."); 78 84 return Apply(parents[0], parents[1], n); 79 85 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/NBinaryVectorCrossover.cs
r7756 r7775 72 72 public sealed override IOperation Apply() { 73 73 var offspringSolutions = Cross(RandomParameter.ActualValue, ParentsParameter.ActualValue); 74 for (int i = 0; i < offspringSolutions.Length; i++) {74 for (int i = 0; i < offspringSolutions.Length; i++) 75 75 CurrentScope.Variables.Add(new Variable(string.Format("Child {0}", i), offspringSolutions[i])); 76 }77 76 return base.Apply(); 78 77 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/NChildCrossover.cs
r7756 r7775 58 58 if (parent1.Length != parent2.Length) 59 59 throw new ArgumentException("NChildCrossover: The parents are of different length."); 60 61 60 if (n.Value > Math.Pow(2, parent1.Length) - 2) 62 61 throw new ArgumentException("NChildCrossover: There cannot be more children than 2^size of parents - 2."); 63 64 62 if (n.Value < 1) 65 63 throw new ArgumentException("NChildCrossover: N cannot be < 1."); … … 68 66 for (int i = 0; i < n.Value; i++) { 69 67 var solution = new BinaryVector(parent1.Length); 70 for (int j = 0; j < solution.Length; j++) {68 for (int j = 0; j < solution.Length; j++) 71 69 solution[j] = random.Next(2) % 2 == 0 ? parent1[j] : parent2[j]; 72 }73 70 solutions[i] = solution; 74 71 } … … 78 75 79 76 protected override ItemArray<BinaryVector> Cross(IRandom random, ItemArray<BinaryVector> parents) { 80 if (parents.Length != 2) throw new ArgumentException("NChildCrossover: The number of parents is not equal to 2.");81 82 if (NParameter.ActualValue == null) throw new InvalidOperationException("NChildCrossover: Parameter " + NParameter.ActualName + " could not be found.");83 77 if (parents.Length != 2) 78 throw new ArgumentException("NChildCrossover: The number of parents is not equal to 2."); 79 if (NParameter.ActualValue == null) 80 throw new InvalidOperationException("NChildCrossover: Parameter " + NParameter.ActualName + " could not be found."); 84 81 return Apply(random, parents[0], parents[1], NParameter.Value); 85 82 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/PathRelinker.cs
r7756 r7775 31 31 /// A base class for operators that perform path relinking. 32 32 /// </summary> 33 [Item("PathRelinker", "A base class for operators that perform a crossover of bool-valued vectors.")]33 [Item("PathRelinker", "A base class for operators that perform path relinking.")] 34 34 [StorableClass] 35 35 public abstract class PathRelinker : SingleSuccessorOperator, IPathRelinker { … … 38 38 get { return (ScopeParameter)Parameters["CurrentScope"]; } 39 39 } 40 public IValue LookupParameter<PercentValue> RelinkingAccuracyParameter {41 get { return (IValue LookupParameter<PercentValue>)Parameters["RelinkingAccuracy"]; }40 public IValueParameter<PercentValue> RelinkingAccuracyParameter { 41 get { return (IValueParameter<PercentValue>)Parameters["RelinkingAccuracy"]; } 42 42 } 43 43 public ILookupParameter<ItemArray<IItem>> ParentsParameter { … … 50 50 get { return CurrentScopeParameter.ActualValue; } 51 51 } 52 private PercentValue RelinkingAccuracy { 53 get { return RelinkingAccuracyParameter.Value; } 54 } 55 private ItemArray<IItem> Parents { 56 get { return ParentsParameter.ActualValue; } 57 } 52 58 #endregion 53 59 … … 59 65 #region Create parameters 60 66 Parameters.Add(new ScopeParameter("CurrentScope")); 61 Parameters.Add(new Value LookupParameter<PercentValue>("RelinkingAccuracy", new PercentValue(0.25)));67 Parameters.Add(new ValueParameter<PercentValue>("RelinkingAccuracy", new PercentValue(0.25))); 62 68 Parameters.Add(new ScopeTreeLookupParameter<IItem>("Parents")); 63 69 #endregion … … 65 71 66 72 public sealed override IOperation Apply() { 67 var relinkedSolutions = Relink(Parents Parameter.ActualValue, RelinkingAccuracyParameter.ActualValue);68 for (int i = 0; i < relinkedSolutions.Length; i++) {73 var relinkedSolutions = Relink(Parents, RelinkingAccuracy); 74 for (int i = 0; i < relinkedSolutions.Length; i++) 69 75 CurrentScope.Variables.Add(new Variable(string.Format("Child {0}", i), relinkedSolutions[i])); 70 }71 76 return base.Apply(); 72 77 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Plugin.cs.frame
r7722 r7775 26 26 /// Plugin class for HeuristicLab.Algorithms.ScatterSearch plugin. 27 27 /// </summary> 28 [Plugin("HeuristicLab.Algorithms.ScatterSearch", "3.3.6. $WCREV$")]28 [Plugin("HeuristicLab.Algorithms.ScatterSearch", "3.3.6.7756")] 29 29 [PluginFile("HeuristicLab.Algorithms.ScatterSearch-3.3.dll", PluginFileType.Assembly)] 30 30 [PluginDependency("HeuristicLab.Analysis", "3.3")] … … 33 33 [PluginDependency("HeuristicLab.Core", "3.3")] 34 34 [PluginDependency("HeuristicLab.Data", "3.3")] 35 [PluginDependency("HeuristicLab.Encodings.BinaryVectorEncoding", "3.3")] 36 [PluginDependency("HeuristicLab.Encodings.PermutationEncoding", "3.3")] 37 [PluginDependency("HeuristicLab.Encodings.RealVectorEncoding", "3.3")] 35 38 [PluginDependency("HeuristicLab.Operators", "3.3")] 36 39 [PluginDependency("HeuristicLab.Optimization", "3.3")] … … 38 41 [PluginDependency("HeuristicLab.Parameters", "3.3")] 39 42 [PluginDependency("HeuristicLab.Persistence", "3.3")] 43 [PluginDependency("HeuristicLab.Problems.Knapsack", "3.3")] 44 [PluginDependency("HeuristicLab.Problems.TestFunctions", "3.3")] 45 [PluginDependency("HeuristicLab.Problems.TravelingSalesman", "3.3")] 40 46 [PluginDependency("HeuristicLab.Random", "3.3")] 41 47 [PluginDependency("HeuristicLab.Selection", "3.3")] -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/ReferenceSetUpdateMethod.cs
r7744 r7775 95 95 var population = new Dictionary<IScope, double>(); 96 96 foreach (var pScope in CurrentScope.SubScopes[0].SubScopes) { 97 intdiversity = 0;97 double diversity = 0; 98 98 var pSol = pScope.Variables[TargetParameter.ActualName].Value; 99 99 foreach (var rScope in CurrentScope.SubScopes[1].SubScopes) { -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/ScatterSearch.cs
r7756 r7775 306 306 ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault(); 307 307 308 CrossoverParameter.ValidValues.Add(new Knapsack.KnapsackPathRelinker()); 309 CrossoverParameter.ValidValues.Add(new Knapsack.KnapsackSimultaneousPathRelinker()); 310 CrossoverParameter.ValidValues.Add(new TestFunctions.TestFunctionsPathRelinker()); 308 311 CrossoverParameter.ValidValues.Add(new TravelingSalesman.TravelingSalesmanPathRelinker()); 309 CrossoverParameter.ValidValues.Add(new Knapsack.KnapsackPathRelinker()); 312 CrossoverParameter.ValidValues.Add(new TravelingSalesman.TravelingSalesmanSimultaneousPathRelinker()); 313 CrossoverParameter.ValidValues.Add(new TravelingSalesman.TravelingSalesmanMultipleGuidesPathRelinker()); 310 314 311 315 foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) … … 329 333 330 334 DiversityCalculatorParameter.ValidValues.Add(new Knapsack.BinaryVectorDiversityCalculator()); 335 DiversityCalculatorParameter.ValidValues.Add(new TestFunctions.RealVectorDiversityCalculator()); 331 336 DiversityCalculatorParameter.ValidValues.Add(new TravelingSalesman.PermutationDiversityCalculator()); 332 337 … … 348 353 349 354 ImproverParameter.ValidValues.Add(new Knapsack.KnapsackImprovementOperator()); 355 ImproverParameter.ValidValues.Add(new TestFunctions.TestFunctionsAckleyImprovementOperator()); 356 ImproverParameter.ValidValues.Add(new TestFunctions.TestFunctionsBealeImprovementOperator()); 357 ImproverParameter.ValidValues.Add(new TestFunctions.TestFunctionsBoothImprovementOperator()); 358 ImproverParameter.ValidValues.Add(new TestFunctions.TestFunctionsGriewankImprovementOperator()); 359 ImproverParameter.ValidValues.Add(new TestFunctions.TestFunctionsLevyImprovementOperator()); 360 ImproverParameter.ValidValues.Add(new TestFunctions.TestFunctionsMatyasImprovementOperator()); 361 ImproverParameter.ValidValues.Add(new TestFunctions.TestFunctionsRosenbrockImprovementOperator()); 362 ImproverParameter.ValidValues.Add(new TestFunctions.TestFunctionsSchwefelImprovementOperator()); 363 ImproverParameter.ValidValues.Add(new TestFunctions.TestFunctionsSumSquaresImprovementOperator()); 364 ImproverParameter.ValidValues.Add(new TestFunctions.TestFunctionsZakharovImprovementOperator()); 350 365 ImproverParameter.ValidValues.Add(new TravelingSalesman.TravelingSalesmanImprovementOperator()); 351 366 -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/ScatterSearchMainLoop.cs
r7744 r7775 33 33 /// An operator which represents a scatter search. 34 34 /// </summary> 35 [Item("ScatterSearchMainLoop", "An operator which represents the main loop ofa scatter search.")]35 [Item("ScatterSearchMainLoop", "An operator which represents a scatter search.")] 36 36 [StorableClass] 37 37 public sealed class ScatterSearchMainLoop : AlgorithmOperator { -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.cs
r7744 r7775 123 123 var worstParentQuality = (orderedParents.Last().Variables[QualityParameter.ActualName].Value as DoubleValue).Value; 124 124 125 var Constraint= Maximization.Value ? (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value > worstParentQuality; }) :125 var hasBetterQuality = Maximization.Value ? (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value > worstParentQuality; }) : 126 126 (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value < worstParentQuality; }); 127 127 128 128 // is there any offspring better than the worst parent? 129 if (orderedOffspring.Any( Constraint)) {129 if (orderedOffspring.Any(hasBetterQuality)) { 130 130 // produce the set union 131 131 // attention: distinction might cause a too small reference set! (e.g. reference set = {1, 2, 2, 2, ..., 2} -> union = {1, 2} 132 var union = orderedParents.Union(orderedOffspring.Where( Constraint), new KeyEqualityComparer<IScope>(x => x.Variables[TargetParameter.ActualName].Value.ToString()));133 if (union.Count() > orderedParents ./*Distinct(new KeyEqualityComparer<IScope>(x => x.Variables[TargetParameter.ActualName].Value.ToString())).*/Count()) {132 var union = orderedParents.Union(orderedOffspring.Where(hasBetterQuality), new KeyEqualityComparer<IScope>(x => x.Variables[TargetParameter.ActualName].Value.ToString())); 133 if (union.Count() > orderedParents/*.Distinct(new KeyEqualityComparer<IScope>(x => x.Variables[TargetParameter.ActualName].Value.ToString()))*/.Count()) { 134 134 var orderedUnion = Maximization.Value ? union.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) : 135 135 union.OrderBy(x => x.Variables[QualityParameter.ActualName].Value); -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/TravelingSalesman/PermutationDiversityCalculator.cs
r7756 r7775 20 20 #endregion 21 21 22 using System; 22 23 using System.Linq; 23 24 using HeuristicLab.Common; … … 42 43 } 43 44 44 protected override int CalculateDiversity(IItem left, IItem right) { 45 protected override double CalculateDiversity(IItem left, IItem right) { 46 if (!(left is Permutation) || !(right is Permutation)) 47 throw new ArgumentException("Cannot calculate diversity because one of the provided objects or both have the wrong type."); 48 45 49 var v1 = left as Permutation; 46 50 var v2 = right as Permutation; 47 int diversity = 0; 48 if (v1 != null && v2 != null) 49 for (int i = 0; i < v1.Length; i++) { 50 var node = v2.Select((x, index) => new { Value = x, ValueIndex = index }).First(x => x.Value == v1[i]); 51 var pred = v2[(node.ValueIndex - 1 + v2.Length) % v2.Length]; 52 var succ = v2[(node.ValueIndex + 1 % v2.Length) % v2.Length]; 53 if (new[] { pred, succ }.All(x => x != v1[(i + 1) % v1.Length])) diversity++; 54 } 51 double diversity = 0.0; 52 for (int i = 0; i < v1.Length; i++) { 53 var node = v2.Select((x, index) => new { Value = x, ValueIndex = index }).First(x => x.Value == v1[i]); 54 var pred = v2[(node.ValueIndex - 1 + v2.Length) % v2.Length]; 55 var succ = v2[(node.ValueIndex + 1) % v2.Length]; 56 if (new[] { pred, succ }.All(x => x != v1[(i + 1) % v1.Length])) diversity++; 57 } 55 58 return diversity; 56 59 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/TravelingSalesman/TravelingSalesmanImprovementOperator.cs
r7756 r7775 138 138 139 139 for (int i = 0; i < ImprovementAttempts.Value; i++) { 140 int a = Random.Next(currSol.Length - 1);141 int b = Random.Next(currSol.Length - 1);140 int a = Random.Next(currSol.Length); 141 int b = Random.Next(currSol.Length); 142 142 Invert(currSol, a, b); 143 143 currLength = TSPEuclideanPathEvaluator.Apply(Evaluator as TSPCoordinatesPathEvaluator, Coordinates, currSol); 144 144 if (currLength < bestLength) { 145 145 bestLength = currLength; 146 bestSol = (Permutation)currSol.Clone();146 bestSol = currSol.Clone() as Permutation; 147 147 } 148 148 Invert(currSol, a, b); … … 155 155 156 156 private void Invert(Permutation sol, int i, int j) { 157 if (i != j) { 158 for (int a = 0; a < Math.Abs(i - j) / 2; a++) { 159 int tmp = sol[(i + a) % sol.Length]; 160 sol[(i + a) % sol.Length] = sol[(j - a + sol.Length) % sol.Length]; 161 sol[(j - a + sol.Length) % sol.Length] = tmp; 162 } 163 } 157 if (i != j) 158 for (int a = 0; a < Math.Abs(i - j) / 2; a++) 159 if (sol[(i + a) % sol.Length] != sol[(j - a + sol.Length) % sol.Length]) { 160 // XOR swap 161 sol[(i + a) % sol.Length] ^= sol[(j - a + sol.Length) % sol.Length]; 162 sol[(j - a + sol.Length) % sol.Length] ^= sol[(i + a) % sol.Length]; 163 sol[(i + a) % sol.Length] ^= sol[(j - a + sol.Length) % sol.Length]; 164 } 164 165 } 165 166 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/TravelingSalesman/TravelingSalesmanPathRelinker.cs
r7756 r7775 49 49 50 50 public static ItemArray<IItem> Apply(IItem initiator, IItem guide, PercentValue n) { 51 if (!(initiator is Permutation) || !(guide is Permutation)) 52 throw new ArgumentException("Cannot relink path because one of the provided solutions or both have the wrong type."); 53 if (n.Value <= 0.0) 54 throw new ArgumentException("RelinkingAccuracy must be greater than 0."); 55 51 56 var v1 = initiator.Clone() as Permutation; 52 57 var v2 = guide as Permutation; 53 58 54 59 if (v1.Length != v2.Length) 55 throw new ArgumentException("TravelingSalesmanPathRelinker: The solutions are of different length."); 56 if (n.Value == 0.0) 57 throw new ArgumentException("TravelingSalesmanPathRelinker: RelinkingAccuracy is 0."); 60 throw new ArgumentException("The solutions are of different length."); 58 61 59 62 var solutions = new List<Permutation>(); 60 for (int i = 0; i < v1.Length; i++) {63 for (int i = 0; i < v1.Length; i++) 61 64 if (v1[i] != v2[i]) { 62 65 var target = v1.Select((x, index) => new { Value = x, ValueIndex = index }).First(x => x.Value == v2[i]); 63 // XOR swap 64 v1[i] ^= v1[target.ValueIndex]; 65 v1[target.ValueIndex] ^= v1[i]; 66 v1[i] ^= v1[target.ValueIndex]; 67 solutions.Add((Permutation)v1.Clone()); 66 if (v1[i] != v1[target.ValueIndex]) { 67 // XOR swap 68 v1[i] ^= v1[target.ValueIndex]; 69 v1[target.ValueIndex] ^= v1[i]; 70 v1[i] ^= v1[target.ValueIndex]; 71 solutions.Add(v1.Clone() as Permutation); 72 } 68 73 } 69 }70 74 71 var stepSize = 1 / n.Value;72 75 var selection = new List<IItem>(); 73 for (int i = 0; i < solutions.Count; i = (int)Math.Round(i + stepSize)) { 74 selection.Add(solutions.ElementAt(i)); 76 if (solutions.Count > 0) { 77 var noSol = (int)Math.Round(solutions.Count * n.Value); 78 if (noSol <= 0) noSol++; 79 var stepSize = (double)solutions.Count / (double)noSol; 80 for (int i = 0; i < noSol; i++) 81 selection.Add(solutions.ElementAt((int)Math.Round((i + 1) * stepSize - stepSize * 0.5))); 75 82 } 76 83 … … 79 86 80 87 protected override ItemArray<IItem> Relink(ItemArray<IItem> parents, PercentValue n) { 81 if (parents.Length != 2) throw new ArgumentException("TravelingSalesmanPathRelinker: The number of parents is not equal to 2."); 88 if (parents.Length != 2) 89 throw new ArgumentException("The number of parents is not equal to 2."); 82 90 return Apply(parents[0], parents[1], n); 83 91 }
Note: See TracChangeset
for help on using the changeset viewer.