Changeset 7786
- Timestamp:
- 05/09/12 12:22:33 (12 years ago)
- Location:
- branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3
- Files:
-
- 5 added
- 18 deleted
- 20 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/HeuristicLab.Algorithms.ScatterSearch-3.3.csproj
r7775 r7786 87 87 </ItemGroup> 88 88 <ItemGroup> 89 <Compile Include="IImprovementOperator.cs" /> 90 <Compile Include="IPathRelinker.cs" /> 89 91 <Compile Include="SolutionPool2TierUpdateMethod.cs" /> 90 <Compile Include="DiversityCalculator.cs" /> 91 <Compile Include="IPathRelinker.cs" /> 92 <Compile Include="Knapsack\BinaryVectorDiversityCalculator.cs" /> 92 <Compile Include="SimilarityCalculator.cs" /> 93 <Compile Include="Knapsack\KnapsackSimilarityCalculator.cs" /> 93 94 <Compile Include="Knapsack\KnapsackMultipleGuidesPathRelinker.cs" /> 94 95 <Compile Include="Knapsack\KnapsackSimultaneousPathRelinker.cs" /> 95 96 <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 97 <Compile Include="TestFunctions\TestFunctionsPathRelinker.cs" /> 107 <Compile Include="TestFunctions\ RealVectorDiversityCalculator.cs" />98 <Compile Include="TestFunctions\TestFunctionsSimilarityCalculator.cs" /> 108 99 <Compile Include="TestFunctions\TestFunctionsImprovementOperator.cs" /> 109 100 <Compile Include="TravelingSalesman\TravelingSalesmanMultipleGuidesPathRelinker.cs" /> … … 111 102 <Compile Include="TravelingSalesman\TravelingSalesmanPathRelinker.cs" /> 112 103 <Compile Include="PathRelinker.cs" /> 113 <Compile Include="Knapsack\INBinaryVectorCrossover.cs" />114 <Compile Include="IScatterSearchTargetProcessor.cs" />115 <Compile Include="Knapsack\NBinaryVectorCrossover.cs" />116 <Compile Include="Knapsack\NChildCrossover.cs" />117 104 <Compile Include="OffspringProcessor.cs" /> 118 <Compile Include="TravelingSalesman\ PermutationDiversityCalculator.cs" />105 <Compile Include="TravelingSalesman\TravelingSalesmanSimilarityCalculator.cs" /> 119 106 <Compile Include="TravelingSalesman\TravelingSalesmanImprovementOperator.cs" /> 120 107 <None Include="HeuristicLab.snk" /> -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/IPathRelinker.cs
r7756 r7786 20 20 #endregion 21 21 22 using System; 23 using HeuristicLab.Common; 22 24 using HeuristicLab.Core; 23 using HeuristicLab.Optimization;24 25 25 26 namespace HeuristicLab.Algorithms.ScatterSearch { … … 27 28 /// An interface which represents an operator for path relinking. 28 29 /// </summary> 29 public interface IPathRelinker : ICrossover { 30 ILookupParameter<ItemArray<IItem>> ParentsParameter { get; } 30 public interface IPathRelinker : IOperator, IParameterizedNamedItem, IDeepCloneable, ICloneable { 31 31 } 32 32 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/KnapsackImprovementOperator.cs
r7775 r7786 20 20 #endregion 21 21 22 using System;23 22 using System.Linq; 24 23 using HeuristicLab.Common; … … 27 26 using HeuristicLab.Encodings.BinaryVectorEncoding; 28 27 using HeuristicLab.Operators; 29 using HeuristicLab.Optimization;30 28 using HeuristicLab.Parameters; 31 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 38 36 [Item("KnapsackImprovementOperator", "An operator that improves knapsack solutions.")] 39 37 [StorableClass] 40 public sealed class KnapsackImprovementOperator : SingleSuccessorOperator, ILocalImprovementOperator, IScatterSearchTargetProcessor { 41 #region Problem properties 42 public Type ProblemType { 43 get { return typeof(KnapsackProblem); } 44 } 45 [Storable] 46 private KnapsackProblem problem; 47 public IProblem Problem { 48 get { return problem; } 49 set { problem = (KnapsackProblem)value; } 50 } 51 #endregion 52 38 public sealed class KnapsackImprovementOperator : SingleSuccessorOperator, IImprovementOperator { 53 39 #region Parameter properties 54 40 public ScopeParameter CurrentScopeParameter { 55 41 get { return (ScopeParameter)Parameters["CurrentScope"]; } 56 42 } 57 public I ValueLookupParameter<IntArray> ValuesParameter {58 get { return (I ValueLookupParameter<IntArray>)Parameters["Values"]; }43 public ILookupParameter<IntValue> KnapsackCapacityParameter { 44 get { return (ILookupParameter<IntValue>)Parameters["KnapsackCapacity"]; } 59 45 } 60 public IValueLookupParameter<IntArray> WeightsParameter { 61 get { return (IValueLookupParameter<IntArray>)Parameters["Weights"]; } 62 } 63 public IValueLookupParameter<IntValue> KnapsackCapacityParameter { 64 get { return (IValueLookupParameter<IntValue>)Parameters["KnapsackCapacity"]; } 46 public ILookupParameter<DoubleValue> PenaltyParameter { 47 get { return (ILookupParameter<DoubleValue>)Parameters["Penalty"]; } 65 48 } 66 49 public IValueLookupParameter<IItem> TargetParameter { 67 50 get { return (IValueLookupParameter<IItem>)Parameters["Target"]; } 68 51 } 69 public I ValueLookupParameter<DoubleValue> PenaltyParameter {70 get { return (I ValueLookupParameter<DoubleValue>)Parameters["Penalty"]; }52 public ILookupParameter<IntArray> ValuesParameter { 53 get { return (ILookupParameter<IntArray>)Parameters["Values"]; } 71 54 } 72 #region ILocalImprovementOperator Parameters 73 public IValueLookupParameter<IntValue> MaximumIterationsParameter { 74 get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; } 55 public ILookupParameter<IntArray> WeightsParameter { 56 get { return (ILookupParameter<IntArray>)Parameters["Weights"]; } 75 57 } 76 public ILookupParameter<IntValue> EvaluatedSolutionsParameter {77 get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }78 }79 public ILookupParameter<ResultCollection> ResultsParameter {80 get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }81 }82 #endregion83 58 #endregion 84 59 … … 86 61 private IScope CurrentScope { 87 62 get { return CurrentScopeParameter.ActualValue; } 63 } 64 private IntValue KnapsackCapacity { 65 get { return KnapsackCapacityParameter.ActualValue; } 66 set { KnapsackCapacityParameter.ActualValue = value; } 67 } 68 private DoubleValue Penalty { 69 get { return PenaltyParameter.ActualValue; } 70 set { PenaltyParameter.ActualValue = value; } 71 } 72 private IItem Target { 73 get { return TargetParameter.ActualValue; } 88 74 } 89 75 private IntArray Values { … … 95 81 set { WeightsParameter.ActualValue = value; } 96 82 } 97 private IntValue KnapsackCapacity {98 get { return KnapsackCapacityParameter.ActualValue; }99 set { KnapsackCapacityParameter.ActualValue = value; }100 }101 private IItem Target {102 get { return TargetParameter.ActualValue; }103 }104 private DoubleValue Penalty {105 get { return PenaltyParameter.ActualValue; }106 set { PenaltyParameter.ActualValue = value; }107 }108 83 #endregion 109 84 110 85 [StorableConstructor] 111 86 private KnapsackImprovementOperator(bool deserializing) : base(deserializing) { } 112 private KnapsackImprovementOperator(KnapsackImprovementOperator original, Cloner cloner) 113 : base(original, cloner) { 114 this.problem = cloner.Clone(original.problem); 115 } 87 private KnapsackImprovementOperator(KnapsackImprovementOperator original, Cloner cloner) : base(original, cloner) { } 116 88 public KnapsackImprovementOperator() 117 89 : base() { 118 90 #region Create parameters 119 91 Parameters.Add(new ScopeParameter("CurrentScope")); 120 Parameters.Add(new ValueLookupParameter<IntArray>("Values")); 121 Parameters.Add(new ValueLookupParameter<IntArray>("Weights")); 122 Parameters.Add(new ValueLookupParameter<IntValue>("KnapsackCapacity")); 92 Parameters.Add(new LookupParameter<IntValue>("KnapsackCapacity")); 93 Parameters.Add(new LookupParameter<DoubleValue>("Penalty")); 123 94 Parameters.Add(new ValueLookupParameter<IItem>("Target")); 124 Parameters.Add(new ValueLookupParameter<DoubleValue>("Penalty")); 95 Parameters.Add(new LookupParameter<IntArray>("Values")); 96 Parameters.Add(new LookupParameter<IntArray>("Weights")); 125 97 #endregion 126 98 TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem … … 132 104 133 105 public override IOperation Apply() { 134 var sol = CurrentScope.Variables[TargetParameter.ActualName].Value as BinaryVector;106 BinaryVector sol = CurrentScope.Variables[TargetParameter.ActualName].Value as BinaryVector; 135 107 136 108 // calculate value-to-weight ratio -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/KnapsackMultipleGuidesPathRelinker.cs
r7778 r7786 54 54 throw new ArgumentException("RelinkingAccuracy must be greater than 0."); 55 55 56 var v1 = initiator.Clone() as BinaryVector;57 vartargets = new BinaryVector[guides.Length];56 BinaryVector v1 = initiator.Clone() as BinaryVector; 57 BinaryVector[] targets = new BinaryVector[guides.Length]; 58 58 Array.Copy(guides, targets, guides.Length); 59 59 … … 61 61 throw new ArgumentException("At least one solution is of different length."); 62 62 63 varsolutions = new List<BinaryVector>();63 IList<BinaryVector> solutions = new List<BinaryVector>(); 64 64 for (int i = 0; i < v1.Length; i++) { 65 65 // TODO: path relinking 66 66 } 67 67 68 varselection = new List<IItem>();68 IList<IItem> selection = new List<IItem>(); 69 69 if (solutions.Count > 0) { 70 var noSol = (int)Math.Round(solutions.Count * n.Value);70 int noSol = (int)(solutions.Count * n.Value); 71 71 if (noSol <= 0) noSol++; 72 varstepSize = (double)solutions.Count / (double)noSol;72 double stepSize = (double)solutions.Count / (double)noSol; 73 73 for (int i = 0; i < noSol; i++) 74 selection.Add(solutions.ElementAt((int) Math.Round((i + 1) * stepSize - stepSize * 0.5)));74 selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5))); 75 75 } 76 76 -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/KnapsackPathRelinker.cs
r7775 r7786 54 54 throw new ArgumentException("RelinkingAccuracy must be greater than 0."); 55 55 56 var v1 = initiator.Clone() as BinaryVector;57 var v2 = guide as BinaryVector;56 BinaryVector v1 = initiator.Clone() as BinaryVector; 57 BinaryVector v2 = guide as BinaryVector; 58 58 59 59 if (v1.Length != v2.Length) 60 60 throw new ArgumentException("The solutions are of different length."); 61 61 62 varsolutions = new List<BinaryVector>();62 IList<BinaryVector> solutions = new List<BinaryVector>(); 63 63 for (int i = 0; i < v1.Length; i++) 64 64 if (v1[i] != v2[i]) { … … 67 67 } 68 68 69 varselection = new List<IItem>();69 IList<IItem> selection = new List<IItem>(); 70 70 if (solutions.Count > 0) { 71 var noSol = (int)Math.Round(solutions.Count * n.Value);71 int noSol = (int)(solutions.Count * n.Value); 72 72 if (noSol <= 0) noSol++; 73 varstepSize = (double)solutions.Count / (double)noSol;73 double stepSize = (double)solutions.Count / (double)noSol; 74 74 for (int i = 0; i < noSol; i++) 75 selection.Add(solutions.ElementAt((int) Math.Round((i + 1) * stepSize - stepSize * 0.5)));75 selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5))); 76 76 } 77 77 -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/Knapsack/KnapsackSimultaneousPathRelinker.cs
r7775 r7786 54 54 throw new ArgumentException("RelinkingAccuracy must be greater than 0."); 55 55 56 var firstInitiator = initiator.Clone() as BinaryVector;57 var firstGuide = guide.Clone() as BinaryVector;58 var secondInitiator = firstGuide.Clone() as BinaryVector;59 var secondGuide = firstInitiator.Clone() as BinaryVector;56 BinaryVector firstInitiator = initiator.Clone() as BinaryVector; 57 BinaryVector firstGuide = guide.Clone() as BinaryVector; 58 BinaryVector secondInitiator = firstGuide.Clone() as BinaryVector; 59 BinaryVector secondGuide = firstInitiator.Clone() as BinaryVector; 60 60 61 61 if (firstInitiator.Length != firstGuide.Length) 62 62 throw new ArgumentException("The solutions are of different length."); 63 63 64 varsolutions = new List<BinaryVector>();64 IList<BinaryVector> solutions = new List<BinaryVector>(); 65 65 for (int i = 0; i < firstInitiator.Length / 2; i++) { 66 66 if (firstInitiator[i] != firstGuide[i]) { … … 68 68 solutions.Add(firstInitiator.Clone() as BinaryVector); 69 69 } 70 varj = secondInitiator.Length - 1 - i;70 int j = secondInitiator.Length - 1 - i; 71 71 if (secondInitiator[j] != secondGuide[j]) { 72 72 secondInitiator[j] = secondGuide[j]; … … 75 75 } 76 76 77 varselection = new List<IItem>();77 IList<IItem> selection = new List<IItem>(); 78 78 if (solutions.Count > 0) { 79 var noSol = (int)Math.Round(solutions.Count * n.Value);79 int noSol = (int)(solutions.Count * n.Value); 80 80 if (noSol <= 0) noSol++; 81 varstepSize = (double)solutions.Count / (double)noSol;81 double stepSize = (double)solutions.Count / (double)noSol; 82 82 for (int i = 0; i < noSol; i++) 83 selection.Add(solutions.ElementAt((int) Math.Round((i + 1) * stepSize - stepSize * 0.5)));83 selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5))); 84 84 } 85 85 -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/OffspringProcessor.cs
r7744 r7786 32 32 [Item("OffspringProcessor", "An operator that creates a subscope with subscopes for every variable in the current scope.")] 33 33 [StorableClass] 34 public sealed class OffspringProcessor : SingleSuccessorOperator , IScatterSearchTargetProcessor{34 public sealed class OffspringProcessor : SingleSuccessorOperator { 35 35 #region Parameter properties 36 36 public ScopeParameter CurrentScopeParameter { … … 54 54 private OffspringProcessor(bool deserializing) : base(deserializing) { } 55 55 private OffspringProcessor(OffspringProcessor original, Cloner cloner) : base(original, cloner) { } 56 public OffspringProcessor() : base() { Initialize(); } 56 public OffspringProcessor() 57 : base() { 58 #region Create parameters 59 Parameters.Add(new ScopeParameter("CurrentScope")); 60 Parameters.Add(new ValueLookupParameter<IItem>("Target")); 61 #endregion 62 } 57 63 58 64 public override IDeepCloneable Clone(Cloner cloner) { … … 60 66 } 61 67 62 private void Initialize() {63 #region Create parameters64 Parameters.Add(new ScopeParameter("CurrentScope"));65 Parameters.Add(new ValueLookupParameter<IItem>("Target"));66 #endregion67 TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem68 }69 70 68 public override IOperation Apply() { 71 varoffspringSolutions = CurrentScope.Variables;72 varoffspringScope = new Scope("Offspring");69 VariableCollection offspringSolutions = CurrentScope.Variables; 70 IScope offspringScope = new Scope("Offspring"); 73 71 foreach (var solution in offspringSolutions) { 74 varscope = new Scope();72 IScope scope = new Scope(); 75 73 scope.Variables.Add(new Variable(TargetParameter.ActualName, solution.Value)); 76 74 offspringScope.SubScopes.Add(scope); -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/PathRelinker.cs
r7775 r7786 38 38 get { return (ScopeParameter)Parameters["CurrentScope"]; } 39 39 } 40 public ILookupParameter<ItemArray<IItem>> ParentsParameter { 41 get { return (IScopeTreeLookupParameter<IItem>)Parameters["Parents"]; } 42 } 40 43 public IValueParameter<PercentValue> RelinkingAccuracyParameter { 41 44 get { return (IValueParameter<PercentValue>)Parameters["RelinkingAccuracy"]; } 42 }43 public ILookupParameter<ItemArray<IItem>> ParentsParameter {44 get { return (IScopeTreeLookupParameter<IItem>)Parameters["Parents"]; }45 45 } 46 46 #endregion … … 50 50 get { return CurrentScopeParameter.ActualValue; } 51 51 } 52 private ItemArray<IItem> Parents { 53 get { return ParentsParameter.ActualValue; } 54 } 52 55 private PercentValue RelinkingAccuracy { 53 56 get { return RelinkingAccuracyParameter.Value; } 54 }55 private ItemArray<IItem> Parents {56 get { return ParentsParameter.ActualValue; }57 57 } 58 58 #endregion … … 65 65 #region Create parameters 66 66 Parameters.Add(new ScopeParameter("CurrentScope")); 67 Parameters.Add(new ScopeTreeLookupParameter<IItem>("Parents")); 67 68 Parameters.Add(new ValueParameter<PercentValue>("RelinkingAccuracy", new PercentValue(0.25))); 68 Parameters.Add(new ScopeTreeLookupParameter<IItem>("Parents"));69 69 #endregion 70 70 } 71 71 72 72 public sealed override IOperation Apply() { 73 varrelinkedSolutions = Relink(Parents, RelinkingAccuracy);73 ItemArray<IItem> relinkedSolutions = Relink(Parents, RelinkingAccuracy); 74 74 for (int i = 0; i < relinkedSolutions.Length; i++) 75 75 CurrentScope.Variables.Add(new Variable(string.Format("Child {0}", i), relinkedSolutions[i])); -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/PopulationRebuildMethod.cs
r7744 r7786 45 45 get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfHighQualitySolutions"]; } 46 46 } 47 public IValueLookupParameter<IItem> QualityParameter { 48 get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; } 49 } 47 50 public IValueLookupParameter<IntValue> ReferenceSetSizeParameter { 48 51 get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; } 49 }50 public IValueLookupParameter<IItem> QualityParameter {51 get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; }52 52 } 53 53 #endregion … … 65 65 set { NumberOfHighQualitySolutionsParameter.ActualValue = value; } 66 66 } 67 private IItem Quality { 68 get { return QualityParameter.ActualValue; } 69 } 67 70 private IntValue ReferenceSetSize { 68 71 get { return ReferenceSetSizeParameter.ActualValue; } 69 72 set { ReferenceSetSizeParameter.ActualValue = value; } 70 }71 private IItem Quality {72 get { return QualityParameter.ActualValue; }73 73 } 74 74 #endregion … … 77 77 private PopulationRebuildMethod(bool deserializing) : base(deserializing) { } 78 78 private PopulationRebuildMethod(PopulationRebuildMethod original, Cloner cloner) : base(original, cloner) { } 79 public PopulationRebuildMethod() : base() { Initialize(); } 79 public PopulationRebuildMethod() 80 : base() { 81 #region Create parameters 82 Parameters.Add(new ScopeParameter("CurrentScope")); 83 Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization")); 84 Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfHighQualitySolutions")); 85 Parameters.Add(new ValueLookupParameter<IItem>("Quality")); 86 Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize")); 87 #endregion 88 } 80 89 81 90 public override IDeepCloneable Clone(Cloner cloner) { … … 83 92 } 84 93 85 private void Initialize() {86 #region Create parameters87 Parameters.Add(new ScopeParameter("CurrentScope"));88 Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization"));89 Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfHighQualitySolutions"));90 Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize"));91 Parameters.Add(new ValueLookupParameter<IItem>("Quality"));92 #endregion93 }94 95 94 public override IOperation Apply() { 96 varpopulationScope = CurrentScope.SubScopes[0];97 varreferenceSetScope = CurrentScope.SubScopes[1];95 IScope populationScope = CurrentScope.SubScopes[0]; 96 IScope referenceSetScope = CurrentScope.SubScopes[1]; 98 97 var orderedReferenceSet = Maximization.Value ? referenceSetScope.SubScopes.OrderByDescending(r => r.Variables[QualityParameter.ActualName].Value) : 99 98 referenceSetScope.SubScopes.OrderBy(r => r.Variables[QualityParameter.ActualName].Value); -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/ReferenceSetUpdateMethod.cs
r7775 r7786 40 40 get { return (ScopeParameter)Parameters["CurrentScope"]; } 41 41 } 42 public IValueLookupParameter<IntValue> NumberOfHighQualitySolutionsParameter {43 get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfHighQualitySolutions"]; }44 }45 42 public IValueLookupParameter<IntValue> ReferenceSetSizeParameter { 46 43 get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; } 47 44 } 45 public IValueLookupParameter<SimilarityCalculator> SimilarityCalculatorParameter { 46 get { return (IValueLookupParameter<SimilarityCalculator>)Parameters["SimilarityCalculator"]; } 47 } 48 48 public IValueLookupParameter<IItem> TargetParameter { 49 49 get { return (IValueLookupParameter<IItem>)Parameters["Target"]; } 50 }51 public IValueLookupParameter<DiversityCalculator> DiversityCalculatorParameter {52 get { return (IValueLookupParameter<DiversityCalculator>)Parameters["DiversityCalculator"]; }53 50 } 54 51 #endregion … … 58 55 get { return CurrentScopeParameter.ActualValue; } 59 56 } 60 private IntValue NumberOfHighQualitySolutions {61 get { return NumberOfHighQualitySolutionsParameter.ActualValue; }62 }63 57 private IntValue ReferenceSetSize { 64 58 get { return ReferenceSetSizeParameter.ActualValue; } 65 59 } 60 private SimilarityCalculator SimilarityCalculator { 61 get { return SimilarityCalculatorParameter.ActualValue; } 62 } 66 63 private IItem Target { 67 64 get { return TargetParameter.ActualValue; } 68 }69 private DiversityCalculator DiversityCalculator {70 get { return DiversityCalculatorParameter.ActualValue; }71 65 } 72 66 #endregion … … 75 69 private ReferenceSetUpdateMethod(bool deserializing) : base(deserializing) { } 76 70 private ReferenceSetUpdateMethod(ReferenceSetUpdateMethod original, Cloner cloner) : base(original, cloner) { } 77 public ReferenceSetUpdateMethod() : base() { Initialize(); } 71 public ReferenceSetUpdateMethod() 72 : base() { 73 #region Create parameters 74 Parameters.Add(new ScopeParameter("CurrentScope")); 75 Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize")); 76 Parameters.Add(new ValueLookupParameter<SimilarityCalculator>("SimilarityCalculator")); 77 Parameters.Add(new ValueLookupParameter<IItem>("Target")); 78 #endregion 79 } 78 80 79 81 public override IDeepCloneable Clone(Cloner cloner) { … … 81 83 } 82 84 83 private void Initialize() {84 #region Create parameters85 Parameters.Add(new ScopeParameter("CurrentScope"));86 Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfHighQualitySolutions"));87 Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize"));88 Parameters.Add(new ValueLookupParameter<IItem>("Target"));89 Parameters.Add(new ValueLookupParameter<DiversityCalculator>("DiversityCalculator"));90 #endregion91 TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem92 }93 94 85 public override IOperation Apply() { 95 varpopulation = new Dictionary<IScope, double>();86 IDictionary<IScope, double> population = new Dictionary<IScope, double>(); 96 87 foreach (var pScope in CurrentScope.SubScopes[0].SubScopes) { 97 double diversity = 0; 98 var pSol = pScope.Variables[TargetParameter.ActualName].Value; 88 double similarity = 0; 99 89 foreach (var rScope in CurrentScope.SubScopes[1].SubScopes) { 100 var rSol = rScope.Variables[TargetParameter.ActualName].Value; 101 diversity += DiversityCalculator.ExecuteCalculation(pSol, rSol); 90 similarity += SimilarityCalculator.ExecuteCalculation(pScope, rScope); 102 91 } 103 population[pScope] = diversity;92 population[pScope] = similarity; 104 93 } 105 foreach (var entry in population.OrderByDescending(x => x.Value).Take(ReferenceSetSize.Value - NumberOfHighQualitySolutions.Value)) { 94 int numberOfHighQualitySolutions = CurrentScope.SubScopes[1].SubScopes.Count; 95 foreach (var entry in population.OrderBy(x => x.Value).Take(ReferenceSetSize.Value - numberOfHighQualitySolutions)) { 106 96 CurrentScope.SubScopes[1].SubScopes.Add(entry.Key); 107 97 CurrentScope.SubScopes[0].SubScopes.Remove(entry.Key); -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/ScatterSearch.cs
r7775 r7786 31 31 using HeuristicLab.Parameters; 32 32 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 33 using HeuristicLab.PluginInfrastructure; 33 34 using HeuristicLab.Random; 34 35 using HeuristicLab.Selection; … … 61 62 get { return (ConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; } 62 63 } 63 public ConstrainedValueParameter<ILocalImprovementOperator> ImproverParameter {64 get { return ( ConstrainedValueParameter<ILocalImprovementOperator>)Parameters["Improver"]; }65 } 66 public ConstrainedValueParameter< DiversityCalculator> DiversityCalculatorParameter {67 get { return (ConstrainedValueParameter< DiversityCalculator>)Parameters["DiversityCalculator"]; }64 public IValueParameter<BoolValue> ExecutePathRelinkingParameter { 65 get { return (IValueLookupParameter<BoolValue>)Parameters["ExecutePathRelinking"]; } 66 } 67 public ConstrainedValueParameter<IImprovementOperator> ImproverParameter { 68 get { return (ConstrainedValueParameter<IImprovementOperator>)Parameters["Improver"]; } 68 69 } 69 70 public IValueParameter<IntValue> MaximumIterationsParameter { … … 73 74 get { return (IValueParameter<IntValue>)Parameters["NumberOfHighQualitySolutions"]; } 74 75 } 76 public ConstrainedValueParameter<IPathRelinker> PathRelinkerParameter { 77 get { return (ConstrainedValueParameter<IPathRelinker>)Parameters["PathRelinker"]; } 78 } 75 79 public IValueParameter<IntValue> PopulationSizeParameter { 76 80 get { return (IValueParameter<IntValue>)Parameters["PopulationSize"]; } … … 84 88 public IValueParameter<BoolValue> SetSeedRandomlyParameter { 85 89 get { return (IValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; } 90 } 91 public ConstrainedValueParameter<SimilarityCalculator> SimilarityCalculatorParameter { 92 get { return (ConstrainedValueParameter<SimilarityCalculator>)Parameters["SimilarityCalculator"]; } 86 93 } 87 94 #endregion … … 96 103 set { CrossoverParameter.Value = value; } 97 104 } 98 private ILocalImprovementOperator Improver { 105 private BoolValue ExecutePathRelinking { 106 get { return ExecutePathRelinkingParameter.Value; } 107 set { ExecutePathRelinkingParameter.Value = value; } 108 } 109 private IImprovementOperator Improver { 99 110 get { return ImproverParameter.Value; } 100 111 set { ImproverParameter.Value = value; } 101 }102 private DiversityCalculator DiversityCalculator {103 get { return DiversityCalculatorParameter.Value; }104 set { DiversityCalculatorParameter.Value = value; }105 112 } 106 113 private IntValue MaximumIterations { … … 112 119 set { NumberOfHighQualitySolutionsParameter.Value = value; } 113 120 } 121 private IPathRelinker PathRelinker { 122 get { return PathRelinkerParameter.Value; } 123 set { PathRelinkerParameter.Value = value; } 124 } 114 125 private IntValue PopulationSize { 115 126 get { return PopulationSizeParameter.Value; } … … 128 139 set { SetSeedRandomlyParameter.Value = value; } 129 140 } 130 public RandomCreator RandomCreator { 141 private SimilarityCalculator SimilarityCalculator { 142 get { return SimilarityCalculatorParameter.Value; } 143 set { SimilarityCalculatorParameter.Value = value; } 144 } 145 private RandomCreator RandomCreator { 131 146 get { return (RandomCreator)OperatorGraph.InitialOperator; } 132 147 } 133 p ublicSolutionsCreator SolutionsCreator {148 private SolutionsCreator SolutionsCreator { 134 149 get { return (SolutionsCreator)RandomCreator.Successor; } 135 150 } 136 p ublicScatterSearchMainLoop MainLoop {151 private ScatterSearchMainLoop MainLoop { 137 152 get { return FindMainLoop(SolutionsCreator.Successor); } 138 153 } … … 159 174 : base() { 160 175 #region Create parameters 161 Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the solution and moves.", new MultiAnalyzer())); 162 Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to combine solutions.")); 163 Parameters.Add(new ConstrainedValueParameter<ILocalImprovementOperator>("Improver", "The operator used to improve solutions.")); 164 Parameters.Add(new ConstrainedValueParameter<DiversityCalculator>("DiversityCalculator", "The operator used to calculate the diversity of two solutions.")); 165 Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.", new IntValue(100))); 166 Parameters.Add(new ValueParameter<IntValue>("NumberOfHighQualitySolutions", "The number of high quality solutions that should be added to the reference set.", new IntValue(10))); 167 Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population.", new IntValue(300))); 168 Parameters.Add(new ValueParameter<IntValue>("ReferenceSetSize", "The size of the reference set.", new IntValue(100))); 169 Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0))); 170 Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true))); 176 Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", new MultiAnalyzer())); 177 Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover")); 178 Parameters.Add(new ValueParameter<BoolValue>("ExecutePathRelinking", new BoolValue(true))); 179 Parameters.Add(new ConstrainedValueParameter<IImprovementOperator>("Improver")); 180 Parameters.Add(new ValueParameter<IntValue>("MaximumIterations", new IntValue(100))); 181 Parameters.Add(new ValueParameter<IntValue>("NumberOfHighQualitySolutions", new IntValue(10))); 182 Parameters.Add(new ConstrainedValueParameter<IPathRelinker>("PathRelinker")); 183 Parameters.Add(new ValueParameter<IntValue>("PopulationSize", new IntValue(300))); 184 Parameters.Add(new ValueParameter<IntValue>("ReferenceSetSize", new IntValue(100))); 185 Parameters.Add(new ValueParameter<IntValue>("Seed", new IntValue(0))); 186 Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", new BoolValue(true))); 187 Parameters.Add(new ConstrainedValueParameter<SimilarityCalculator>("SimilarityCalculator")); 171 188 #endregion 172 189 … … 178 195 Placeholder solutionImprover = new Placeholder(); 179 196 BestSelector bestSelector = new BestSelector(); 180 VariableCreator variableCreator = new VariableCreator();181 ResultsCollector resultsCollector = new ResultsCollector();182 197 ScatterSearchMainLoop mainLoop = new ScatterSearchMainLoop(); 183 198 #endregion … … 207 222 bestSelector.NumberOfSelectedSubScopesParameter.ActualName = NumberOfHighQualitySolutionsParameter.Name; 208 223 bestSelector.CopySelected = new BoolValue(false); 209 bestSelector.Successor = variableCreator; 210 211 variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0))); 212 variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("NewSolutions", new BoolValue(false))); 213 variableCreator.Successor = resultsCollector; 214 215 resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Iterations")); 216 resultsCollector.ResultsParameter.ActualName = "Results"; 217 resultsCollector.Successor = mainLoop; 224 bestSelector.Successor = mainLoop; 218 225 219 226 mainLoop.MaximumIterationsParameter.ActualName = MaximumIterationsParameter.Name; … … 236 243 237 244 public override void Prepare() { 238 base.Prepare();245 if (Problem != null) base.Prepare(); 239 246 } 240 247 … … 249 256 UpdateAnalyzers(); 250 257 UpdateCrossovers(); 251 UpdateDiversityCalculators(); 258 UpdatePathRelinkers(); 259 UpdateSimilarityCalculators(); 252 260 UpdateImprovers(); 253 Update DiversityCalculators();261 UpdateSimilarityCalculators(); 254 262 Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged); 255 263 base.OnProblemChanged(); … … 272 280 UpdateAnalyzers(); 273 281 UpdateCrossovers(); 274 UpdateDiversityCalculators(); 282 UpdatePathRelinkers(); 283 UpdateSimilarityCalculators(); 275 284 UpdateImprovers(); 276 285 ParameterizeMainLoop(); … … 306 315 ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault(); 307 316 308 CrossoverParameter.ValidValues.Add(new Knapsack.KnapsackPathRelinker());309 CrossoverParameter.ValidValues.Add(new Knapsack.KnapsackSimultaneousPathRelinker());310 CrossoverParameter.ValidValues.Add(new TestFunctions.TestFunctionsPathRelinker());311 CrossoverParameter.ValidValues.Add(new TravelingSalesman.TravelingSalesmanPathRelinker());312 CrossoverParameter.ValidValues.Add(new TravelingSalesman.TravelingSalesmanSimultaneousPathRelinker());313 CrossoverParameter.ValidValues.Add(new TravelingSalesman.TravelingSalesmanMultipleGuidesPathRelinker());314 315 317 foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) 316 318 CrossoverParameter.ValidValues.Add(crossover); 317 318 foreach (var crossover in CrossoverParameter.ValidValues.OfType<Knapsack.INBinaryVectorCrossover>())319 crossover.ParentsParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem320 319 321 320 if (oldCrossover != null) { … … 327 326 CrossoverParameter.Value = defaultCrossover; 328 327 } 329 private void UpdateDiversityCalculators() {330 DiversityCalculator oldDiversityCalculator = DiversityCalculatorParameter.Value;331 DiversityCalculatorParameter.ValidValues.Clear();332 DiversityCalculator defaultDiversityCalculator = Problem.Operators.OfType<DiversityCalculator>().FirstOrDefault();333 334 DiversityCalculatorParameter.ValidValues.Add(new Knapsack.BinaryVectorDiversityCalculator());335 DiversityCalculatorParameter.ValidValues.Add(new TestFunctions.RealVectorDiversityCalculator());336 DiversityCalculatorParameter.ValidValues.Add(new TravelingSalesman.PermutationDiversityCalculator());337 338 foreach (DiversityCalculator diversityCalculator in Problem.Operators.OfType<DiversityCalculator>().OrderBy(x => x.Name))339 DiversityCalculatorParameter.ValidValues.Add(diversityCalculator);340 341 if (oldDiversityCalculator != null) {342 DiversityCalculator diversityCalculator = DiversityCalculatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldDiversityCalculator.GetType());343 if (diversityCalculator != null) DiversityCalculatorParameter.Value = diversityCalculator;344 else oldDiversityCalculator = null;345 }346 if (oldDiversityCalculator == null && defaultDiversityCalculator != null)347 DiversityCalculatorParameter.Value = defaultDiversityCalculator;348 }349 328 private void UpdateImprovers() { 350 I LocalImprovementOperator oldImprover = ImproverParameter.Value;329 IImprovementOperator oldImprover = ImproverParameter.Value; 351 330 ImproverParameter.ValidValues.Clear(); 352 ILocalImprovementOperator defaultImprover = Problem.Operators.OfType<ILocalImprovementOperator>().FirstOrDefault(); 353 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()); 365 ImproverParameter.ValidValues.Add(new TravelingSalesman.TravelingSalesmanImprovementOperator()); 366 367 foreach (ILocalImprovementOperator improver in Problem.Operators.OfType<ILocalImprovementOperator>().OrderBy(x => x.Name)) 331 IImprovementOperator defaultImprover = Problem.Operators.OfType<IImprovementOperator>().FirstOrDefault(); 332 333 foreach (IImprovementOperator improver in ApplicationManager.Manager.GetInstances<IImprovementOperator>().OrderBy(x => x.Name)) 368 334 ImproverParameter.ValidValues.Add(improver); 369 335 370 foreach ( var improver in ImproverParameter.ValidValues.OfType<IScatterSearchTargetProcessor>())371 improver.TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem336 foreach (IImprovementOperator improver in Problem.Operators.OfType<IImprovementOperator>().OrderBy(x => x.Name)) 337 ImproverParameter.ValidValues.Add(improver); 372 338 373 339 if (oldImprover != null) { 374 I LocalImprovementOperator improver = ImproverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldImprover.GetType());340 IImprovementOperator improver = ImproverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldImprover.GetType()); 375 341 if (improver != null) ImproverParameter.Value = improver; 376 342 else oldImprover = null; … … 378 344 if (oldImprover == null && defaultImprover != null) 379 345 ImproverParameter.Value = defaultImprover; 346 } 347 private void UpdatePathRelinkers() { 348 IPathRelinker oldPathRelinker = PathRelinkerParameter.Value; 349 PathRelinkerParameter.ValidValues.Clear(); 350 IPathRelinker defaultPathRelinker = Problem.Operators.OfType<IPathRelinker>().FirstOrDefault(); 351 352 foreach (IPathRelinker pathRelinker in ApplicationManager.Manager.GetInstances<IPathRelinker>().OrderBy(x => x.Name)) 353 PathRelinkerParameter.ValidValues.Add(pathRelinker); 354 355 foreach (IPathRelinker pathRelinker in Problem.Operators.OfType<IPathRelinker>().OrderBy(x => x.Name)) 356 PathRelinkerParameter.ValidValues.Add(pathRelinker); 357 358 if (oldPathRelinker != null) { 359 IPathRelinker pathRelinker = PathRelinkerParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldPathRelinker.GetType()); 360 if (pathRelinker != null) PathRelinkerParameter.Value = pathRelinker; 361 else oldPathRelinker = null; 362 } 363 if (oldPathRelinker == null && defaultPathRelinker != null) 364 PathRelinkerParameter.Value = defaultPathRelinker; 365 } 366 private void UpdateSimilarityCalculators() { 367 SimilarityCalculator oldDiversityCalculator = SimilarityCalculatorParameter.Value; 368 SimilarityCalculatorParameter.ValidValues.Clear(); 369 SimilarityCalculator defaultDiversityCalculator = Problem.Operators.OfType<SimilarityCalculator>().FirstOrDefault(); 370 371 foreach (SimilarityCalculator diversityCalculator in ApplicationManager.Manager.GetInstances<SimilarityCalculator>().OrderBy(x => x.Name)) 372 SimilarityCalculatorParameter.ValidValues.Add(diversityCalculator); 373 374 foreach (SimilarityCalculator diversityCalculator in Problem.Operators.OfType<SimilarityCalculator>().OrderBy(x => x.Name)) 375 SimilarityCalculatorParameter.ValidValues.Add(diversityCalculator); 376 377 if (oldDiversityCalculator != null) { 378 SimilarityCalculator diversityCalculator = SimilarityCalculatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldDiversityCalculator.GetType()); 379 if (diversityCalculator != null) SimilarityCalculatorParameter.Value = diversityCalculator; 380 else oldDiversityCalculator = null; 381 } 382 if (oldDiversityCalculator == null && defaultDiversityCalculator != null) 383 SimilarityCalculatorParameter.Value = defaultDiversityCalculator; 380 384 } 381 385 private void ParameterizeSolutionsCreator() { … … 397 401 } 398 402 } 399 //private void ParameterizeScatterSearchTargetProcessor(IOperator op) {400 // if (op is IScatterSearchTargetProcessor) {401 // IScatterSearchTargetProcessor ssOp = (IScatterSearchTargetProcessor)op;402 // ssOp.TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem403 // ssOp.TargetParameter.Hidden = true;404 // }405 //}406 403 private void ParameterizeAnalyzers() { 407 404 qualityAnalyzer.ResultsParameter.ActualName = "Results"; … … 422 419 while (mainLoop != null && !(mainLoop is ScatterSearchMainLoop)) 423 420 mainLoop = ((SingleSuccessorOperator)mainLoop).Successor; 424 if (mainLoop == null) return null; 425 else return (ScatterSearchMainLoop)mainLoop; 421 return mainLoop as ScatterSearchMainLoop; 426 422 } 427 423 #endregion -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/ScatterSearchMainLoop.cs
r7778 r7786 41 41 get { return (IValueLookupParameter<IMultiAnalyzer>)Parameters["Analyzer"]; } 42 42 } 43 public IValueLookupParameter<DoubleValue> BestKnownQualityParameter { 44 get { return (IValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; } 45 } 43 46 public IValueLookupParameter<ICrossover> CrossoverParameter { 44 47 get { return (IValueLookupParameter<ICrossover>)Parameters["Crossover"]; } 45 48 } 46 public IValueLookupParameter<IOperator> ImproverParameter { 47 get { return (IValueLookupParameter<IOperator>)Parameters["Improver"]; } 48 } 49 public IValueLookupParameter<DiversityCalculator> DiversityCalculatorParameter { 50 get { return (IValueLookupParameter<DiversityCalculator>)Parameters["DiversityCalculator"]; } 49 public IValueLookupParameter<IEvaluator> EvaluatorParameter { 50 get { return (IValueLookupParameter<IEvaluator>)Parameters["Evaluator"]; } 51 } 52 public IValueLookupParameter<BoolValue> ExecutePathRelinkingParameter { 53 get { return (IValueLookupParameter<BoolValue>)Parameters["ExecutePathRelinking"]; } 54 } 55 public IValueLookupParameter<IImprovementOperator> ImproverParameter { 56 get { return (IValueLookupParameter<IImprovementOperator>)Parameters["Improver"]; } 57 } 58 public IValueLookupParameter<IntValue> IterationsParameter { 59 get { return (IValueLookupParameter<IntValue>)Parameters["Iterations"]; } 60 } 61 public IValueLookupParameter<BoolValue> MaximizationParameter { 62 get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; } 63 } 64 public IValueLookupParameter<IntValue> MaximumIterationsParameter { 65 get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; } 51 66 } 52 67 public IValueLookupParameter<IntValue> NumberOfHighQualitySolutionsParameter { 53 68 get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfHighQualitySolutions"]; } 54 69 } 70 public IValueLookupParameter<IPathRelinker> PathRelinkerParameter { 71 get { return (IValueLookupParameter<IPathRelinker>)Parameters["PathRelinker"]; } 72 } 55 73 public IValueLookupParameter<IntValue> PopulationSizeParameter { 56 74 get { return (IValueLookupParameter<IntValue>)Parameters["PopulationSize"]; } … … 59 77 get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; } 60 78 } 61 public IValueLookupParameter<DoubleValue> BestKnownQualityParameter {62 get { return (IValueLookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }63 }64 public IValueLookupParameter<IEvaluator> EvaluatorParameter {65 get { return (IValueLookupParameter<IEvaluator>)Parameters["Evaluator"]; }66 }67 public IValueLookupParameter<IntValue> IterationsParameter {68 get { return (IValueLookupParameter<IntValue>)Parameters["Iterations"]; }69 }70 public IValueLookupParameter<BoolValue> MaximizationParameter {71 get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }72 }73 public IValueLookupParameter<IntValue> MaximumIterationsParameter {74 get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }75 }76 79 public IValueLookupParameter<DoubleValue> QualityParameter { 77 80 get { return (IValueLookupParameter<DoubleValue>)Parameters["Quality"]; } … … 83 86 get { return (IValueLookupParameter<VariableCollection>)Parameters["Results"]; } 84 87 } 85 public IValueLookupParameter< IntValue> SampleSizeParameter {86 get { return (IValueLookupParameter< IntValue>)Parameters["SampleSize"]; }88 public IValueLookupParameter<SimilarityCalculator> SimilarityCalculatorParameter { 89 get { return (IValueLookupParameter<SimilarityCalculator>)Parameters["SimilarityCalculator"]; } 87 90 } 88 91 public IValueLookupParameter<ISolutionCreator> SolutionCreatorParameter { … … 96 99 set { AnalyzerParameter.ActualValue = value; } 97 100 } 101 private DoubleValue BestKnownQuality { 102 get { return BestKnownQualityParameter.ActualValue; } 103 set { BestKnownQualityParameter.ActualValue = value; } 104 } 98 105 private ICrossover Crossover { 99 106 get { return CrossoverParameter.ActualValue; } 100 107 set { CrossoverParameter.ActualValue = value; } 101 108 } 102 private IOperator Improver { 109 private IEvaluator Evaluator { 110 get { return EvaluatorParameter.ActualValue; } 111 set { EvaluatorParameter.ActualValue = value; } 112 } 113 private BoolValue ExecutePathRelinking { 114 get { return ExecutePathRelinkingParameter.ActualValue; } 115 set { ExecutePathRelinkingParameter.ActualValue = value; } 116 } 117 private IImprovementOperator Improver { 103 118 get { return ImproverParameter.ActualValue; } 104 119 set { ImproverParameter.ActualValue = value; } 105 120 } 106 private DiversityCalculator DiversityCalculator { 107 get { return DiversityCalculatorParameter.ActualValue; } 108 set { DiversityCalculatorParameter.ActualValue = value; } 121 private IntValue Iterations { 122 get { return IterationsParameter.ActualValue; } 123 set { IterationsParameter.ActualValue = value; } 124 } 125 private BoolValue Maximization { 126 get { return MaximizationParameter.ActualValue; } 127 set { MaximizationParameter.ActualValue = value; } 128 } 129 private IntValue MaximumIterations { 130 get { return MaximumIterationsParameter.ActualValue; } 131 set { MaximumIterationsParameter.ActualValue = value; } 109 132 } 110 133 private IntValue NumberOfHighQualitySolutions { … … 112 135 set { NumberOfHighQualitySolutionsParameter.ActualValue = value; } 113 136 } 137 private IPathRelinker PathRelinker { 138 get { return PathRelinkerParameter.ActualValue; } 139 set { PathRelinkerParameter.ActualValue = value; } 140 } 114 141 private IntValue PopulationSize { 115 142 get { return PopulationSizeParameter.ActualValue; } … … 120 147 set { ReferenceSetSizeParameter.ActualValue = value; } 121 148 } 122 private DoubleValue BestKnownQuality {123 get { return BestKnownQualityParameter.ActualValue; }124 set { BestKnownQualityParameter.ActualValue = value; }125 }126 private IEvaluator Evaluator {127 get { return EvaluatorParameter.ActualValue; }128 set { EvaluatorParameter.ActualValue = value; }129 }130 private IntValue Iterations {131 get { return IterationsParameter.ActualValue; }132 set { IterationsParameter.ActualValue = value; }133 }134 private BoolValue Maximization {135 get { return MaximizationParameter.ActualValue; }136 set { MaximizationParameter.ActualValue = value; }137 }138 private IntValue MaximumIterations {139 get { return MaximumIterationsParameter.ActualValue; }140 set { MaximumIterationsParameter.ActualValue = value; }141 }142 149 private DoubleValue Quality { 143 150 get { return QualityParameter.ActualValue; } … … 152 159 set { ResultsParameter.ActualValue = value; } 153 160 } 154 private IntValue SampleSize{155 get { return S ampleSizeParameter.ActualValue; }156 set { S ampleSizeParameter.ActualValue = value; }161 private SimilarityCalculator SimilarityCalculator { 162 get { return SimilarityCalculatorParameter.ActualValue; } 163 set { SimilarityCalculatorParameter.ActualValue = value; } 157 164 } 158 165 private ISolutionCreator SolutionCreator { … … 173 180 private void Initialize() { 174 181 #region Create parameters 175 Parameters.Add(new ValueLookupParameter<IMultiAnalyzer>("Analyzer", "The operator used to analyze the solution and moves.")); 176 Parameters.Add(new ValueLookupParameter<ICrossover>("Crossover", "The operator used to combine solutions.")); 177 Parameters.Add(new ValueLookupParameter<IOperator>("Improver", "The operator used to improve solutions.")); 178 Parameters.Add(new ValueLookupParameter<DiversityCalculator>("DiversityCalculator", "The operator used to calculate the diversity of two solutions.")); 179 Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfHighQualitySolutions", "The number of high quality solutions that should be added to the reference set.")); 180 Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize", "The size of the population.")); 181 Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize", "The size of the reference set.")); 182 Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality", "The problem's best known quality value found so far.")); 183 Parameters.Add(new ValueLookupParameter<IEvaluator>("Evaluator", "The operator which is used to evaluate new solutions.")); 184 Parameters.Add(new ValueLookupParameter<IntValue>("Iterations", "The number of iterations performed.")); 185 Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, otherwise false.")); 186 Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of generations which should be processed.")); 187 Parameters.Add(new ValueLookupParameter<DoubleValue>("Quality", "The value which represents the quality of a solution.")); 188 Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator.")); 189 Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored.")); 190 Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "Number of moves that MultiMoveGenerators should create. This is ignored for Exhaustive- and SingleMoveGenerators.")); 191 Parameters.Add(new ValueLookupParameter<ISolutionCreator>("SolutionCreator", "The operator which is used to create new solutions.")); 182 Parameters.Add(new ValueLookupParameter<IMultiAnalyzer>("Analyzer")); 183 Parameters.Add(new ValueLookupParameter<DoubleValue>("BestKnownQuality")); 184 Parameters.Add(new ValueLookupParameter<ICrossover>("Crossover")); 185 Parameters.Add(new ValueLookupParameter<IEvaluator>("Evaluator")); 186 Parameters.Add(new ValueLookupParameter<BoolValue>("ExecutePathRelinking")); 187 Parameters.Add(new ValueLookupParameter<IOperator>("Improver")); 188 Parameters.Add(new ValueLookupParameter<IntValue>("Iterations")); 189 Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization")); 190 Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations")); 191 Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfHighQualitySolutions")); 192 Parameters.Add(new ValueLookupParameter<IPathRelinker>("PathRelinker")); 193 Parameters.Add(new ValueLookupParameter<IntValue>("PopulationSize")); 194 Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize")); 195 Parameters.Add(new ValueLookupParameter<DoubleValue>("Quality")); 196 Parameters.Add(new ValueLookupParameter<IRandom>("Random")); 197 Parameters.Add(new ValueLookupParameter<VariableCollection>("Results")); 198 Parameters.Add(new ValueLookupParameter<SimilarityCalculator>("SimilarityCalculator")); 199 Parameters.Add(new ValueLookupParameter<ISolutionCreator>("SolutionCreator")); 192 200 #endregion 193 201 194 202 #region Create operators 203 Placeholder analyzer = new Placeholder(); 195 204 Assigner assigner1 = new Assigner(); 196 205 Assigner assigner2 = new Assigner(); 197 206 ChildrenCreator childrenCreator = new ChildrenCreator(); 207 Placeholder crossover = new Placeholder(); 198 208 Comparator iterationsChecker = new Comparator(); 209 IntCounter iterationsCounter = new IntCounter(); 210 MergingReducer mergingReducer = new MergingReducer(); 211 ConditionalBranch executePathRelinkingBranch = new ConditionalBranch(); 199 212 ConditionalBranch newSolutionsBranch = new ConditionalBranch(); 200 ConditionalBranch terminateBranch = new ConditionalBranch();201 IntCounter interationsCounter = new IntCounter();202 MergingReducer mergingReducer = new MergingReducer();203 213 OffspringProcessor offspringProcessor = new OffspringProcessor(); 204 Placeholder analyzer = new Placeholder(); 205 Placeholder crossover = new Placeholder(); 214 Placeholder pathRelinker = new Placeholder(); 215 PopulationRebuildMethod populationRebuildMethod = new PopulationRebuildMethod(); 216 ReferenceSetUpdateMethod referenceSetUpdateMethod = new ReferenceSetUpdateMethod(); 217 ResultsCollector resultsCollector = new ResultsCollector(); 218 RightSelector rightSelector = new RightSelector(); 206 219 Placeholder solutionEvaluator1 = new Placeholder(); 207 220 Placeholder solutionEvaluator2 = new Placeholder(); 208 221 Placeholder solutionImprover1 = new Placeholder(); 209 222 Placeholder solutionImprover2 = new Placeholder(); 210 PopulationRebuildMethod populationRebuildMethod = new PopulationRebuildMethod();211 ResultsCollector resultsCollector = new ResultsCollector();212 ReferenceSetUpdateMethod referenceSetUpdateMethod = new ReferenceSetUpdateMethod();213 RightSelector rightSelector = new RightSelector();214 223 SolutionPoolUpdateMethod solutionPoolUpdateMethod = new SolutionPoolUpdateMethod(); 215 224 SolutionsCreator solutionsCreator = new SolutionsCreator(); … … 217 226 SubScopesProcessor subScopesProcessor2 = new SubScopesProcessor(); 218 227 SubScopesProcessor subScopesProcessor3 = new SubScopesProcessor(); 228 ConditionalBranch terminateBranch = new ConditionalBranch(); 219 229 UniformSubScopesProcessor uniformSubScopesProcessor1 = new UniformSubScopesProcessor(); 220 230 UniformSubScopesProcessor uniformSubScopesProcessor2 = new UniformSubScopesProcessor(); 221 231 UniformSubScopesProcessor uniformSubScopesProcessor3 = new UniformSubScopesProcessor(); 232 VariableCreator variableCreator = new VariableCreator(); 222 233 #endregion 223 234 224 235 #region Create operator graph 225 OperatorGraph.InitialOperator = interationsCounter; 226 assigner1.Name = "NewSolutions = true"; 227 assigner1.LeftSideParameter.ActualName = "NewSolutions"; 228 assigner1.RightSideParameter.Value = new BoolValue(true); 229 assigner1.Successor = newSolutionsBranch; 230 231 assigner2.Name = "NewSolutions = false"; 232 assigner2.LeftSideParameter.ActualName = "NewSolutions"; 233 assigner2.RightSideParameter.Value = new BoolValue(false); 234 assigner2.Successor = uniformSubScopesProcessor1; 235 236 childrenCreator.Name = "SubsetGenerator"; 237 childrenCreator.ParentsPerChildParameter.Value = new IntValue(2); 238 childrenCreator.Successor = assigner2; 236 OperatorGraph.InitialOperator = variableCreator; 237 variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Iterations", new IntValue(0))); 238 variableCreator.CollectedValues.Add(new ValueParameter<BoolValue>("NewSolutions", new BoolValue(false))); 239 variableCreator.Successor = resultsCollector; 240 241 resultsCollector.CopyValue = new BoolValue(false); 242 resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>(IterationsParameter.Name)); 243 resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name; 244 resultsCollector.Successor = iterationsChecker; 239 245 240 246 iterationsChecker.Name = "IterationsChecker"; … … 245 251 iterationsChecker.Successor = terminateBranch; 246 252 247 solutionImprover2.Name = "SolutionImprover";248 solutionImprover2.OperatorParameter.ActualName = "Improver";249 solutionImprover2.Successor = solutionEvaluator2;250 251 solutionEvaluator2.Name = "SolutionEvaluator";252 solutionEvaluator2.OperatorParameter.ActualName = "Evaluator";253 solutionEvaluator2.Successor = null;254 255 newSolutionsBranch.Name = "NewSolutionChecker";256 newSolutionsBranch.ConditionParameter.ActualName = "NewSolutions";257 newSolutionsBranch.TrueBranch = subScopesProcessor1;258 newSolutionsBranch.FalseBranch = populationRebuildMethod;259 newSolutionsBranch.Successor = null;260 261 253 terminateBranch.Name = "TerminateChecker"; 262 254 terminateBranch.ConditionParameter.ActualName = "Terminate"; 263 terminateBranch.TrueBranch = new EmptyOperator();264 255 terminateBranch.FalseBranch = referenceSetUpdateMethod; 265 terminateBranch.Successor = null;266 267 interationsCounter.Name = "IterationCounter";268 interationsCounter.IncrementParameter.Value = new IntValue(1);269 interationsCounter.ValueParameter.ActualName = "Iterations";270 interationsCounter.Successor = resultsCollector;271 272 offspringProcessor.Successor = rightSelector;273 274 rightSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1);275 rightSelector.CopySelected = new BoolValue(false);276 rightSelector.Successor = subScopesProcessor2;277 278 analyzer.Name = "Analyzer";279 analyzer.OperatorParameter.ActualName = "Analyzer";280 281 crossover.Name = "Crossover";282 crossover.OperatorParameter.ActualName = "Crossover";283 crossover.Successor = offspringProcessor;284 285 solutionImprover1.Name = "SolutionImprover";286 solutionImprover1.OperatorParameter.ActualName = "Improver";287 solutionImprover1.Successor = solutionEvaluator1;288 289 solutionEvaluator1.Name = "SolutionEvaluator";290 solutionEvaluator1.OperatorParameter.ActualName = "Evaluator";291 solutionEvaluator1.Successor = null;292 293 populationRebuildMethod.Successor = subScopesProcessor3;294 295 resultsCollector.CopyValue = new BoolValue(false);296 resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>(IterationsParameter.Name));297 resultsCollector.CollectedValues.Add(new LookupParameter<DoubleValue>(BestKnownQualityParameter.Name, null, BestKnownQualityParameter.Name));298 resultsCollector.ResultsParameter.ActualName = ResultsParameter.Name;299 resultsCollector.Successor = iterationsChecker;300 256 301 257 referenceSetUpdateMethod.Successor = assigner1; 302 258 303 solutionPoolUpdateMethod.Successor = analyzer; 304 305 solutionsCreator.Name = "DiversificationGenerationMethod"; 306 solutionsCreator.NumberOfSolutionsParameter.ActualName = "PopulationSize"; 307 solutionsCreator.Successor = uniformSubScopesProcessor3; 259 assigner1.Name = "NewSolutions = true"; 260 assigner1.LeftSideParameter.ActualName = "NewSolutions"; 261 assigner1.RightSideParameter.Value = new BoolValue(true); 262 assigner1.Successor = subScopesProcessor1; 308 263 309 264 subScopesProcessor1.DepthParameter.Value = new IntValue(1); … … 312 267 subScopesProcessor1.Successor = newSolutionsBranch; 313 268 269 childrenCreator.Name = "SubsetGenerator"; 270 childrenCreator.ParentsPerChildParameter.Value = new IntValue(2); 271 childrenCreator.Successor = assigner2; 272 273 assigner2.Name = "NewSolutions = false"; 274 assigner2.LeftSideParameter.ActualName = "NewSolutions"; 275 assigner2.RightSideParameter.Value = new BoolValue(false); 276 assigner2.Successor = uniformSubScopesProcessor1; 277 278 uniformSubScopesProcessor1.DepthParameter.Value = new IntValue(1); 279 uniformSubScopesProcessor1.Operator = executePathRelinkingBranch; 280 uniformSubScopesProcessor1.Successor = solutionPoolUpdateMethod; 281 282 executePathRelinkingBranch.Name = "ExecutePathRelinkingChecker"; 283 executePathRelinkingBranch.ConditionParameter.ActualName = ExecutePathRelinkingParameter.ActualName; 284 executePathRelinkingBranch.TrueBranch = pathRelinker; 285 executePathRelinkingBranch.FalseBranch = crossover; 286 287 pathRelinker.Name = "PathRelinker"; 288 pathRelinker.OperatorParameter.ActualName = "PathRelinker"; 289 pathRelinker.Successor = offspringProcessor; 290 291 crossover.Name = "Crossover"; 292 crossover.OperatorParameter.ActualName = "Crossover"; 293 crossover.Successor = offspringProcessor; 294 295 offspringProcessor.Successor = rightSelector; 296 297 rightSelector.NumberOfSelectedSubScopesParameter.Value = new IntValue(1); 298 rightSelector.CopySelected = new BoolValue(false); 299 rightSelector.Successor = subScopesProcessor2; 300 314 301 subScopesProcessor2.DepthParameter.Value = new IntValue(1); 315 302 subScopesProcessor2.Operators.Add(new EmptyOperator()); … … 317 304 subScopesProcessor2.Successor = mergingReducer; 318 305 306 uniformSubScopesProcessor2.DepthParameter.Value = new IntValue(2); 307 uniformSubScopesProcessor2.Operator = solutionImprover1; 308 309 solutionImprover1.Name = "SolutionImprover"; 310 solutionImprover1.OperatorParameter.ActualName = "Improver"; 311 solutionImprover1.Successor = solutionEvaluator1; 312 313 solutionEvaluator1.Name = "SolutionEvaluator"; 314 solutionEvaluator1.OperatorParameter.ActualName = "Evaluator"; 315 316 solutionPoolUpdateMethod.Successor = analyzer; 317 318 analyzer.Name = "Analyzer"; 319 analyzer.OperatorParameter.ActualName = "Analyzer"; 320 321 newSolutionsBranch.Name = "NewSolutionsChecker"; 322 newSolutionsBranch.ConditionParameter.ActualName = "NewSolutions"; 323 newSolutionsBranch.TrueBranch = subScopesProcessor1; 324 newSolutionsBranch.FalseBranch = populationRebuildMethod; 325 326 populationRebuildMethod.Successor = subScopesProcessor3; 327 319 328 subScopesProcessor3.DepthParameter.Value = new IntValue(1); 320 329 subScopesProcessor3.Operators.Add(solutionsCreator); 321 330 subScopesProcessor3.Operators.Add(new EmptyOperator()); 322 subScopesProcessor3.Successor = interationsCounter; 323 324 uniformSubScopesProcessor1.DepthParameter.Value = new IntValue(1); 325 uniformSubScopesProcessor1.Operator = crossover; 326 uniformSubScopesProcessor1.Successor = solutionPoolUpdateMethod; 327 328 uniformSubScopesProcessor2.DepthParameter.Value = new IntValue(2); 329 uniformSubScopesProcessor2.Operator = solutionImprover1; 330 uniformSubScopesProcessor2.Successor = null; 331 subScopesProcessor3.Successor = iterationsCounter; 332 333 solutionsCreator.Name = "DiversificationGenerationMethod"; 334 solutionsCreator.NumberOfSolutionsParameter.ActualName = "PopulationSize"; 335 solutionsCreator.Successor = uniformSubScopesProcessor3; 331 336 332 337 uniformSubScopesProcessor3.DepthParameter.Value = new IntValue(1); 333 338 uniformSubScopesProcessor3.Operator = solutionImprover2; 334 uniformSubScopesProcessor3.Successor = null; 339 340 solutionImprover2.Name = "SolutionImprover"; 341 solutionImprover2.OperatorParameter.ActualName = "Improver"; 342 solutionImprover2.Successor = solutionEvaluator2; 343 344 solutionEvaluator2.Name = "SolutionEvaluator"; 345 solutionEvaluator2.OperatorParameter.ActualName = "Evaluator"; 346 347 iterationsCounter.Name = "IterationCounter"; 348 iterationsCounter.IncrementParameter.Value = new IntValue(1); 349 iterationsCounter.ValueParameter.ActualName = "Iterations"; 350 iterationsCounter.Successor = resultsCollector; 335 351 #endregion 336 352 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPool2TierUpdateMethod.cs
r7775 r7786 36 36 [Item("SolutionPool2TierUpdateMethod", "An operator that updates the solution pool using a 2-tier strategy.")] 37 37 [StorableClass] 38 public sealed class SolutionPool2TierUpdateMethod : SingleSuccessorOperator , IScatterSearchTargetProcessor{38 public sealed class SolutionPool2TierUpdateMethod : SingleSuccessorOperator { 39 39 #region Parameter properties 40 40 public ScopeParameter CurrentScopeParameter { 41 41 get { return (ScopeParameter)Parameters["CurrentScope"]; } 42 42 } 43 public IValueLookupParameter< DiversityCalculator> DiversityCalculatorParameter {44 get { return (IValueLookupParameter< DiversityCalculator>)Parameters["DiversityCalculator"]; }43 public IValueLookupParameter<SimilarityCalculator> SimilarityCalculatorParameter { 44 get { return (IValueLookupParameter<SimilarityCalculator>)Parameters["SimilarityCalculator"]; } 45 45 } 46 46 public IValueLookupParameter<BoolValue> MaximizationParameter { … … 53 53 get { return (IValueLookupParameter<IntValue>)Parameters["NumberOfHighQualitySolutions"]; } 54 54 } 55 public IValueLookupParameter<IItem> QualityParameter { 56 get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; } 57 } 55 58 public IValueLookupParameter<IntValue> ReferenceSetSizeParameter { 56 59 get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; } 57 }58 public IValueLookupParameter<IItem> QualityParameter {59 get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; }60 60 } 61 61 public IValueLookupParameter<IItem> TargetParameter { … … 68 68 get { return CurrentScopeParameter.ActualValue; } 69 69 } 70 private DiversityCalculator DiversityCalculator {71 get { return DiversityCalculatorParameter.ActualValue; }70 private SimilarityCalculator SimilarityCalculator { 71 get { return SimilarityCalculatorParameter.ActualValue; } 72 72 } 73 73 private BoolValue Maximization { … … 81 81 get { return NumberOfHighQualitySolutionsParameter.ActualValue; } 82 82 } 83 private IItem Quality { 84 get { return QualityParameter.ActualValue; } 85 } 83 86 private IntValue ReferenceSetSize { 84 87 get { return ReferenceSetSizeParameter.ActualValue; } 85 88 set { ReferenceSetSizeParameter.ActualValue = value; } 86 }87 private IItem Quality {88 get { return QualityParameter.ActualValue; }89 89 } 90 90 private IItem Target { … … 105 105 #region Create parameters 106 106 Parameters.Add(new ScopeParameter("CurrentScope")); 107 Parameters.Add(new ValueLookupParameter<DiversityCalculator>("DiversityCalculator"));108 107 Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization")); 109 108 Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions")); 110 109 Parameters.Add(new ValueLookupParameter<IntValue>("NumberOfHighQualitySolutions")); 110 Parameters.Add(new ValueLookupParameter<IItem>("Quality")); 111 111 Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize")); 112 Parameters.Add(new ValueLookupParameter< IItem>("Quality"));112 Parameters.Add(new ValueLookupParameter<SimilarityCalculator>("SimilarityCalculator")); 113 113 Parameters.Add(new ValueLookupParameter<IItem>("Target")); 114 114 #endregion 115 TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem116 115 } 117 116 118 117 public override IOperation Apply() { 119 varparentsScope = new Scope("Parents");120 varoffspringScope = new Scope("Offspring");118 IScope parentsScope = new Scope("Parents"); 119 IScope offspringScope = new Scope("Offspring"); 121 120 122 121 // split parents and offspring … … 132 131 133 132 var highQualityParents = orderedParents.Take(NumberOfHighQualitySolutions.Value).ToList(); 134 var highDiversityParents = new List<Tuple<IScope, double>>();133 IList<Tuple<IScope, double>> highSimilarityParents = new List<Tuple<IScope, double>>(); 135 134 foreach (var oScope in orderedParents.Skip(NumberOfHighQualitySolutions.Value)) { 136 double diversity = 0.0; 137 var oSol = oScope.Variables[TargetParameter.ActualName].Value; 135 double similarity = 0.0; 138 136 foreach (var hScope in highQualityParents) { 139 var hSol = hScope.Variables[TargetParameter.ActualName].Value; 140 diversity += DiversityCalculator.ExecuteCalculation(oSol, hSol); 137 similarity += SimilarityCalculator.ExecuteCalculation(oScope, hScope); 141 138 } 142 high DiversityParents.Add(new Tuple<IScope, double>(oScope, diversity));139 highSimilarityParents.Add(new Tuple<IScope, double>(oScope, similarity)); 143 140 } 144 141 145 varoffspring = new List<Tuple<IScope, double>>();142 IList<Tuple<IScope, double>> offspring = new List<Tuple<IScope, double>>(); 146 143 foreach (var oScope in orderedOffspring) { 147 double diversity = 0.0; 148 var oSol = oScope.Variables[TargetParameter.ActualName].Value; 144 double similarity = 0.0; 149 145 foreach (var hScope in highQualityParents) { 150 var hSol = hScope.Variables[TargetParameter.ActualName].Value; 151 diversity += DiversityCalculator.ExecuteCalculation(oSol, hSol); 146 similarity += SimilarityCalculator.ExecuteCalculation(oScope, hScope); 152 147 } 153 offspring.Add(new Tuple<IScope, double>(oScope, diversity));148 offspring.Add(new Tuple<IScope, double>(oScope, similarity)); 154 149 } 155 150 … … 171 166 172 167 // update diversity part of the reference set 173 var hasBetterDiversity = (Func<Tuple<IScope, double>, bool>)(x => { return x.Item2 > highDiversityParents.OrderBy(y => y.Item2).First().Item2; });168 var hasBetterDiversity = (Func<Tuple<IScope, double>, bool>)(x => { return x.Item2 < highSimilarityParents.OrderByDescending(y => y.Item2).First().Item2; }); 174 169 if (offspring.Any(hasBetterDiversity)) NewSolutions.Value = true; 175 170 while (offspring.Any(hasBetterDiversity)) { // better offspring available 176 171 // select best offspring 177 var bestChild = offspring.OrderBy Descending(x => x.Item2).First();172 var bestChild = offspring.OrderBy(x => x.Item2).First(); 178 173 // select worst parent 179 var worstParent = high DiversityParents.OrderBy(x => x.Item2).First();180 high DiversityParents.Remove(worstParent);181 high DiversityParents.Add(bestChild);174 var worstParent = highSimilarityParents.OrderByDescending(x => x.Item2).First(); 175 highSimilarityParents.Remove(worstParent); 176 highSimilarityParents.Add(bestChild); 182 177 offspring.Remove(bestChild); 183 178 } 184 179 185 CurrentScope.SubScopes.Replace(highQualityParents.Concat(high DiversityParents.Select(x => x.Item1)).ToList());180 CurrentScope.SubScopes.Replace(highQualityParents.Concat(highSimilarityParents.Select(x => x.Item1)).ToList()); 186 181 187 182 return base.Apply(); 188 183 } 189 190 private class KeyEqualityComparer<T> : IEqualityComparer<T> {191 private readonly Func<T, object> keyExtractor;192 193 public KeyEqualityComparer(Func<T, object> keyExtractor) {194 this.keyExtractor = keyExtractor;195 }196 197 public bool Equals(T x, T y) {198 return keyExtractor(x).Equals(keyExtractor(y));199 }200 201 public int GetHashCode(T obj) {202 return keyExtractor(obj).GetHashCode();203 }204 }205 184 } 206 185 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.cs
r7775 r7786 36 36 [Item("SolutionPoolUpdateMethod", "An operator that updates the solution pool.")] 37 37 [StorableClass] 38 public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator , IScatterSearchTargetProcessor{38 public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator { 39 39 #region Parameter properties 40 40 public ScopeParameter CurrentScopeParameter { … … 47 47 get { return (IValueLookupParameter<BoolValue>)Parameters["NewSolutions"]; } 48 48 } 49 public IValueLookupParameter<IItem> QualityParameter { 50 get { return (IValueLookupParameter<IItem>)Parameters["Quality"]; } 51 } 49 52 public IValueLookupParameter<IntValue> ReferenceSetSizeParameter { 50 53 get { return (IValueLookupParameter<IntValue>)Parameters["ReferenceSetSize"]; } 51 54 } 52 public IValueLookupParameter< IItem> QualityParameter {53 get { return (IValueLookupParameter< IItem>)Parameters["Quality"]; }55 public IValueLookupParameter<SimilarityCalculator> SimilarityCalculatorParameter { 56 get { return (IValueLookupParameter<SimilarityCalculator>)Parameters["SimilarityCalculator"]; } 54 57 } 55 58 public IValueLookupParameter<IItem> TargetParameter { … … 69 72 get { return NewSolutionsParameter.ActualValue; } 70 73 } 74 private IItem Quality { 75 get { return QualityParameter.ActualValue; } 76 } 71 77 private IntValue ReferenceSetSize { 72 78 get { return ReferenceSetSizeParameter.ActualValue; } 73 79 set { ReferenceSetSizeParameter.ActualValue = value; } 74 80 } 75 private IItem Quality{76 get { return QualityParameter.ActualValue; }81 private SimilarityCalculator SimilarityCalculator { 82 get { return SimilarityCalculatorParameter.ActualValue; } 77 83 } 78 84 private IItem Target { … … 95 101 Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization")); 96 102 Parameters.Add(new ValueLookupParameter<BoolValue>("NewSolutions")); 103 Parameters.Add(new ValueLookupParameter<IItem>("Quality")); 97 104 Parameters.Add(new ValueLookupParameter<IntValue>("ReferenceSetSize")); 98 Parameters.Add(new ValueLookupParameter< IItem>("Quality"));105 Parameters.Add(new ValueLookupParameter<SimilarityCalculator>("SimilarityCalculator")); 99 106 Parameters.Add(new ValueLookupParameter<IItem>("Target")); 100 107 #endregion 101 TargetParameter.ActualName = "KnapsackSolution"; // temporary solution for the knapsack problem102 108 } 103 109 104 110 public override IOperation Apply() { 105 varparentsScope = new Scope("Parents");106 varoffspringScope = new Scope("Offspring");111 IScope parentsScope = new Scope("Parents"); 112 IScope offspringScope = new Scope("Offspring"); 107 113 108 114 // split parents and offspring … … 121 127 CurrentScope.SubScopes.AddRange(orderedParents); 122 128 123 varworstParentQuality = (orderedParents.Last().Variables[QualityParameter.ActualName].Value as DoubleValue).Value;129 double worstParentQuality = (orderedParents.Last().Variables[QualityParameter.ActualName].Value as DoubleValue).Value; 124 130 125 131 var hasBetterQuality = Maximization.Value ? (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value > worstParentQuality; }) : 126 (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value < worstParentQuality; });132 (Func<IScope, bool>)(x => { return (x.Variables[QualityParameter.ActualName].Value as DoubleValue).Value < worstParentQuality; }); 127 133 128 134 // is there any offspring better than the worst parent? … … 130 136 // produce the set union 131 137 // 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(hasBetterQuality), new KeyEqualityComparer<IScope>(x => x.Variables[TargetParameter.ActualName].Value.ToString()));138 var union = orderedParents.Union(orderedOffspring.Where(hasBetterQuality), new SolutionEqualityComparer<IScope>(SimilarityCalculator.ExecuteCalculation)); 133 139 if (union.Count() > orderedParents/*.Distinct(new KeyEqualityComparer<IScope>(x => x.Variables[TargetParameter.ActualName].Value.ToString()))*/.Count()) { 134 140 var orderedUnion = Maximization.Value ? union.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) : … … 142 148 } 143 149 144 private class KeyEqualityComparer<T> : IEqualityComparer<T> {145 private readonly Func<T, object> keyExtractor;150 private class SolutionEqualityComparer<T> : EqualityComparer<T> { 151 private readonly Func<T, T, double> diversityCalculator; 146 152 147 public KeyEqualityComparer(Func<T, object> keyExtractor) {148 this. keyExtractor = keyExtractor;153 public SolutionEqualityComparer(Func<T, T, double> diversityCalculator) { 154 this.diversityCalculator = diversityCalculator; 149 155 } 150 156 151 public bool Equals(T x, T y) {152 return keyExtractor(x).Equals(keyExtractor(y));157 public override bool Equals(T x, T y) { 158 return diversityCalculator(x, y) == 0.0; 153 159 } 154 160 155 public int GetHashCode(T obj) {156 return keyExtractor(obj).GetHashCode();161 public override int GetHashCode(T obj) { 162 return obj.GetHashCode(); 157 163 } 158 164 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/TestFunctions/TestFunctionsImprovementOperator.cs
r7775 r7786 21 21 22 22 using System; 23 using System.Reflection; 23 24 using HeuristicLab.Common; 24 25 using HeuristicLab.Core; … … 26 27 using HeuristicLab.Encodings.RealVectorEncoding; 27 28 using HeuristicLab.Operators; 28 using HeuristicLab.Optimization;29 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Problems.Knapsack;32 31 using HeuristicLab.Problems.TestFunctions; 33 32 … … 38 37 [Item("TestFunctionsImprovementOperator", "An operator that improves test functions solutions.")] 39 38 [StorableClass] 40 public abstract class TestFunctionsImprovementOperator : SingleSuccessorOperator, ILocalImprovementOperator, IScatterSearchTargetProcessor { 41 #region Problem properties 42 public Type ProblemType { 43 get { return typeof(KnapsackProblem); } 44 } 45 [Storable] 46 private KnapsackProblem problem; 47 public IProblem Problem { 48 get { return problem; } 49 set { problem = (KnapsackProblem)value; } 50 } 51 #endregion 52 39 public sealed class TestFunctionsImprovementOperator : SingleSuccessorOperator, IImprovementOperator { 53 40 #region Parameter properties 41 public IValueParameter<DoubleValue> AlphaParameter { 42 get { return (IValueParameter<DoubleValue>)Parameters["Alpha"]; } 43 } 44 public IValueParameter<DoubleValue> BetaParameter { 45 get { return (IValueParameter<DoubleValue>)Parameters["Beta"]; } 46 } 54 47 public ScopeParameter CurrentScopeParameter { 55 48 get { return (ScopeParameter)Parameters["CurrentScope"]; } 56 49 } 57 public IValue LookupParameter<DoubleMatrix> CoordinatesParameter {58 get { return (IValue LookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }50 public IValueParameter<DoubleValue> DeltaParameter { 51 get { return (IValueParameter<DoubleValue>)Parameters["Delta"]; } 59 52 } 60 53 public IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator> EvaluatorParameter { 61 54 get { return (IValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>)Parameters["Evaluator"]; } 62 55 } 56 public IValueParameter<DoubleValue> GammaParameter { 57 get { return (IValueParameter<DoubleValue>)Parameters["Gamma"]; } 58 } 63 59 public IValueLookupParameter<IntValue> ImprovementAttemptsParameter { 64 60 get { return (IValueLookupParameter<IntValue>)Parameters["ImprovementAttempts"]; } … … 70 66 get { return (IValueLookupParameter<IItem>)Parameters["Target"]; } 71 67 } 72 public IValueParameter<DoubleValue> AlphaParameter {73 get { return (IValueParameter<DoubleValue>)Parameters["Alpha"]; }74 }75 public IValueParameter<DoubleValue> BetaParameter {76 get { return (IValueParameter<DoubleValue>)Parameters["Beta"]; }77 }78 public IValueParameter<DoubleValue> GammaParameter {79 get { return (IValueParameter<DoubleValue>)Parameters["Gamma"]; }80 }81 public IValueParameter<DoubleValue> DeltaParameter {82 get { return (IValueParameter<DoubleValue>)Parameters["Delta"]; }83 }84 #region ILocalImprovementOperator Parameters85 public IValueLookupParameter<IntValue> MaximumIterationsParameter {86 get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }87 }88 public ILookupParameter<IntValue> EvaluatedSolutionsParameter {89 get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }90 }91 public ILookupParameter<ResultCollection> ResultsParameter {92 get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }93 }94 68 #endregion 95 #endregion96 69 97 70 #region Properties 98 private IItem Target {99 get { return TargetParameter.ActualValue; }100 }101 71 private DoubleValue Alpha { 102 72 get { return AlphaParameter.Value; } … … 105 75 get { return BetaParameter.Value; } 106 76 } 107 p rivate DoubleValue Gamma{108 get { return GammaParameter.Value; }77 public IScope CurrentScope { 78 get { return CurrentScopeParameter.ActualValue; } 109 79 } 110 80 private DoubleValue Delta { 111 81 get { return DeltaParameter.Value; } 112 }113 protected Func<RealVector, double> FunctionEvaluator { get; set; }114 public IScope CurrentScope {115 get { return CurrentScopeParameter.ActualValue; }116 }117 public DoubleMatrix Coordinates {118 get { return CoordinatesParameter.ActualValue; }119 set { CoordinatesParameter.ActualValue = value; }120 82 } 121 83 public ISingleObjectiveTestFunctionProblemEvaluator Evaluator { … … 123 85 set { EvaluatorParameter.ActualValue = value; } 124 86 } 87 private DoubleValue Gamma { 88 get { return GammaParameter.Value; } 89 } 125 90 public IntValue ImprovementAttempts { 126 91 get { return ImprovementAttemptsParameter.ActualValue; } … … 131 96 set { RandomParameter.ActualValue = value; } 132 97 } 98 private IItem Target { 99 get { return TargetParameter.ActualValue; } 100 } 133 101 #endregion 134 102 135 103 [StorableConstructor] 136 protected TestFunctionsImprovementOperator(bool deserializing) : base(deserializing) { } 137 protected TestFunctionsImprovementOperator(TestFunctionsImprovementOperator original, Cloner cloner) 138 : base(original, cloner) { 139 this.problem = cloner.Clone(original.problem); 140 } 104 private TestFunctionsImprovementOperator(bool deserializing) : base(deserializing) { } 105 private TestFunctionsImprovementOperator(TestFunctionsImprovementOperator original, Cloner cloner) : base(original, cloner) { } 141 106 public TestFunctionsImprovementOperator() 142 107 : base() { 143 108 #region Create parameters 144 Parameters.Add(new ScopeParameter("CurrentScope"));145 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Coordinates"));146 Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator"));147 Parameters.Add(new ValueLookupParameter<IntValue>("ImprovementAttempts", new IntValue(100)));148 Parameters.Add(new ValueLookupParameter<IRandom>("Random"));149 Parameters.Add(new ValueLookupParameter<IItem>("Target"));150 109 Parameters.Add(new ValueParameter<DoubleValue>("Alpha", new DoubleValue(1.0))); 151 110 Parameters.Add(new ValueParameter<DoubleValue>("Beta", new DoubleValue(2.0))); 111 Parameters.Add(new ScopeParameter("CurrentScope")); 112 Parameters.Add(new ValueParameter<DoubleValue>("Delta", new DoubleValue(0.5))); 113 Parameters.Add(new ValueLookupParameter<ISingleObjectiveTestFunctionProblemEvaluator>("Evaluator")); 152 114 Parameters.Add(new ValueParameter<DoubleValue>("Gamma", new DoubleValue(0.5))); 153 Parameters.Add(new ValueParameter<DoubleValue>("Delta", new DoubleValue(0.5))); 115 Parameters.Add(new ValueLookupParameter<IntValue>("ImprovementAttempts", new IntValue(100))); 116 Parameters.Add(new ValueLookupParameter<IItem>("Target")); 117 Parameters.Add(new ValueLookupParameter<IRandom>("Random")); 154 118 #endregion 155 119 TargetParameter.ActualName = "Point"; // temporary solution for the test functions problem 156 120 } 157 121 122 public override IDeepCloneable Clone(Cloner cloner) { 123 return new TestFunctionsImprovementOperator(this, cloner); 124 } 125 158 126 public override IOperation Apply() { 159 var bestSol = CurrentScope.Variables[TargetParameter.ActualName].Value as RealVector; 160 var bestSolQuality = FunctionEvaluator(bestSol); 127 RealVector bestSol = CurrentScope.Variables[TargetParameter.ActualName].Value as RealVector; 128 MethodInfo evaluationMethod = Evaluator.GetType().GetMethod("Apply", 129 BindingFlags.Public | BindingFlags.Static, 130 null, 131 new Type[] { typeof(RealVector) }, null); 132 Func<RealVector, double> functionEvaluator = x => (double)evaluationMethod.Invoke(Evaluator, new object[] { x }); 133 double bestSolQuality = functionEvaluator(bestSol); 161 134 162 135 // create perturbed solutions 163 varsimplex = new RealVector[bestSol.Length];136 RealVector[] simplex = new RealVector[bestSol.Length]; 164 137 for (int i = 0; i < simplex.Length; i++) { 165 138 simplex[i] = bestSol.Clone() as RealVector; … … 172 145 for (int i = 0; i < ImprovementAttempts.Value; i++) { 173 146 // order according to their objective function value 174 Array.Sort(simplex, (x, y) => FunctionEvaluator(x).CompareTo(FunctionEvaluator(y)));147 Array.Sort(simplex, (x, y) => functionEvaluator(x).CompareTo(functionEvaluator(y))); 175 148 176 149 // calculate centroid 177 var centroid = new RealVector(bestSol.Length);150 RealVector centroid = new RealVector(bestSol.Length); 178 151 foreach (var vector in simplex) 179 152 for (int j = 0; j < centroid.Length; j++) … … 183 156 184 157 // reflection 185 var reflectionPoint = new RealVector(bestSol.Length);158 RealVector reflectionPoint = new RealVector(bestSol.Length); 186 159 for (int j = 0; j < reflectionPoint.Length; j++) 187 160 reflectionPoint[j] = centroid[j] + Alpha.Value * (centroid[j] - simplex[simplex.Length - 1][j]); 188 double reflectionPointQuality = FunctionEvaluator(reflectionPoint);189 if ( FunctionEvaluator(simplex[0]) <= reflectionPointQuality190 && reflectionPointQuality < FunctionEvaluator(simplex[simplex.Length - 2]))161 double reflectionPointQuality = functionEvaluator(reflectionPoint); 162 if (functionEvaluator(simplex[0]) <= reflectionPointQuality 163 && reflectionPointQuality < functionEvaluator(simplex[simplex.Length - 2])) 191 164 simplex[simplex.Length - 1] = reflectionPoint; 192 165 193 166 // expansion 194 if (reflectionPointQuality < FunctionEvaluator(simplex[0])) {195 var expansionPoint = new RealVector(bestSol.Length);167 if (reflectionPointQuality < functionEvaluator(simplex[0])) { 168 RealVector expansionPoint = new RealVector(bestSol.Length); 196 169 for (int j = 0; j < expansionPoint.Length; j++) 197 170 expansionPoint[j] = centroid[j] + Beta.Value * (reflectionPoint[j] - centroid[j]); 198 simplex[simplex.Length - 1] = FunctionEvaluator(expansionPoint) < reflectionPointQuality ? expansionPoint : reflectionPoint;171 simplex[simplex.Length - 1] = functionEvaluator(expansionPoint) < reflectionPointQuality ? expansionPoint : reflectionPoint; 199 172 } 200 173 201 174 // contraction 202 if ( FunctionEvaluator(simplex[simplex.Length - 2]) <= reflectionPointQuality203 && reflectionPointQuality < FunctionEvaluator(simplex[simplex.Length - 1])) {204 var outsideContractionPoint = new RealVector(bestSol.Length);175 if (functionEvaluator(simplex[simplex.Length - 2]) <= reflectionPointQuality 176 && reflectionPointQuality < functionEvaluator(simplex[simplex.Length - 1])) { 177 RealVector outsideContractionPoint = new RealVector(bestSol.Length); 205 178 for (int j = 0; j < outsideContractionPoint.Length; j++) 206 179 outsideContractionPoint[j] = centroid[j] + Gamma.Value * (reflectionPoint[j] - centroid[j]); 207 if ( FunctionEvaluator(outsideContractionPoint) <= reflectionPointQuality) {180 if (functionEvaluator(outsideContractionPoint) <= reflectionPointQuality) { 208 181 simplex[simplex.Length - 1] = outsideContractionPoint; 209 if ( FunctionEvaluator(reflectionPoint) >= FunctionEvaluator(simplex[simplex.Length - 1])) {210 var insideContractionPoint = new RealVector(bestSol.Length);182 if (functionEvaluator(reflectionPoint) >= functionEvaluator(simplex[simplex.Length - 1])) { 183 RealVector insideContractionPoint = new RealVector(bestSol.Length); 211 184 for (int j = 0; j < insideContractionPoint.Length; j++) 212 185 insideContractionPoint[j] = centroid[j] - Gamma.Value * (reflectionPoint[j] - centroid[j]); 213 if ( FunctionEvaluator(insideContractionPoint) < FunctionEvaluator(simplex[simplex.Length - 1])) simplex[simplex.Length - 1] = insideContractionPoint;186 if (functionEvaluator(insideContractionPoint) < functionEvaluator(simplex[simplex.Length - 1])) simplex[simplex.Length - 1] = insideContractionPoint; 214 187 } 215 188 } -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/TestFunctions/TestFunctionsPathRelinker.cs
r7775 r7786 71 71 throw new ArgumentException("RelinkingAccuracy must be greater than 0."); 72 72 73 var v1 = initiator.Clone() as RealVector;74 var v2 = guide as RealVector;73 RealVector v1 = initiator.Clone() as RealVector; 74 RealVector v2 = guide as RealVector; 75 75 76 76 if (v1.Length != v2.Length) 77 77 throw new ArgumentException("The solutions are of different length."); 78 78 79 varsolutions = new List<RealVector>();79 IList<RealVector> solutions = new List<RealVector>(); 80 80 for (int i = 0; i < k.Value; i++) { 81 var solution = v1.Clone() as RealVector;81 RealVector solution = v1.Clone() as RealVector; 82 82 for (int j = 0; j < solution.Length; j++) 83 83 solution[j] = v1[j] + 1 / (k.Value - i) * (v2[j] - v1[j]); … … 85 85 } 86 86 87 varselection = new List<IItem>();87 IList<IItem> selection = new List<IItem>(); 88 88 if (solutions.Count > 0) { 89 var noSol = (int)Math.Round(solutions.Count * n.Value);89 int noSol = (int)(solutions.Count * n.Value); 90 90 if (noSol <= 0) noSol++; 91 varstepSize = (double)solutions.Count / (double)noSol;91 double stepSize = (double)solutions.Count / (double)noSol; 92 92 for (int i = 0; i < noSol; i++) 93 selection.Add(solutions.ElementAt((int) Math.Round((i + 1) * stepSize - stepSize * 0.5)));93 selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5))); 94 94 } 95 95 -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/TravelingSalesman/TravelingSalesmanImprovementOperator.cs
r7775 r7786 29 29 using HeuristicLab.Parameters; 30 30 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 using HeuristicLab.Problems.Knapsack;32 31 using HeuristicLab.Problems.TravelingSalesman; 33 32 … … 38 37 [Item("TravelingSalesmanImprovementOperator", "An operator that improves traveling salesman solutions.")] 39 38 [StorableClass] 40 public sealed class TravelingSalesmanImprovementOperator : SingleSuccessorOperator, ILocalImprovementOperator, IScatterSearchTargetProcessor { 41 #region Problem properties 42 public Type ProblemType { 43 get { return typeof(KnapsackProblem); } 44 } 45 [Storable] 46 private KnapsackProblem problem; 47 public IProblem Problem { 48 get { return problem; } 49 set { problem = (KnapsackProblem)value; } 50 } 51 #endregion 52 39 public sealed class TravelingSalesmanImprovementOperator : SingleSuccessorOperator, IImprovementOperator { 53 40 #region Parameter properties 54 41 public ScopeParameter CurrentScopeParameter { 55 42 get { return (ScopeParameter)Parameters["CurrentScope"]; } 56 43 } 57 public I ValueLookupParameter<DoubleMatrix> CoordinatesParameter {58 get { return (I ValueLookupParameter<DoubleMatrix>)Parameters["Coordinates"]; }44 public ILookupParameter<DistanceMatrix> DistanceMatrixParameter { 45 get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; } 59 46 } 60 public I ValueLookupParameter<IEvaluator> EvaluatorParameter {61 get { return (I ValueLookupParameter<IEvaluator>)Parameters["Evaluator"]; }47 public ILookupParameter<IEvaluator> EvaluatorParameter { 48 get { return (ILookupParameter<IEvaluator>)Parameters["Evaluator"]; } 62 49 } 63 public IValue LookupParameter<IntValue> ImprovementAttemptsParameter {64 get { return (IValue LookupParameter<IntValue>)Parameters["ImprovementAttempts"]; }50 public IValueParameter<IntValue> ImprovementAttemptsParameter { 51 get { return (IValueParameter<IntValue>)Parameters["ImprovementAttempts"]; } 65 52 } 66 public I ValueLookupParameter<IRandom> RandomParameter {67 get { return (I ValueLookupParameter<IRandom>)Parameters["Random"]; }53 public ILookupParameter<IRandom> RandomParameter { 54 get { return (ILookupParameter<IRandom>)Parameters["Random"]; } 68 55 } 69 56 public IValueLookupParameter<IItem> TargetParameter { 70 57 get { return (IValueLookupParameter<IItem>)Parameters["Target"]; } 71 58 } 72 #region ILocalImprovementOperator Parameters73 public IValueLookupParameter<IntValue> MaximumIterationsParameter {74 get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }75 }76 public ILookupParameter<IntValue> EvaluatedSolutionsParameter {77 get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }78 }79 public ILookupParameter<ResultCollection> ResultsParameter {80 get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }81 }82 #endregion83 59 #endregion 84 60 … … 87 63 get { return CurrentScopeParameter.ActualValue; } 88 64 } 89 public D oubleMatrix Coordinates{90 get { return CoordinatesParameter.ActualValue; }91 set { CoordinatesParameter.ActualValue = value; }65 public DistanceMatrix DistanceMatrix { 66 get { return DistanceMatrixParameter.ActualValue; } 67 set { DistanceMatrixParameter.ActualValue = value; } 92 68 } 93 69 public IEvaluator Evaluator { … … 96 72 } 97 73 public IntValue ImprovementAttempts { 98 get { return ImprovementAttemptsParameter. ActualValue; }99 set { ImprovementAttemptsParameter. ActualValue = value; }74 get { return ImprovementAttemptsParameter.Value; } 75 set { ImprovementAttemptsParameter.Value = value; } 100 76 } 101 77 public IRandom Random { … … 110 86 [StorableConstructor] 111 87 private TravelingSalesmanImprovementOperator(bool deserializing) : base(deserializing) { } 112 private TravelingSalesmanImprovementOperator(TravelingSalesmanImprovementOperator original, Cloner cloner) 113 : base(original, cloner) { 114 this.problem = cloner.Clone(original.problem); 115 } 88 private TravelingSalesmanImprovementOperator(TravelingSalesmanImprovementOperator original, Cloner cloner) : base(original, cloner) { } 116 89 public TravelingSalesmanImprovementOperator() 117 90 : base() { 118 91 #region Create parameters 119 92 Parameters.Add(new ScopeParameter("CurrentScope")); 120 Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Coordinates"));121 Parameters.Add(new ValueLookupParameter<IEvaluator>("Evaluator"));122 Parameters.Add(new Value LookupParameter<IntValue>("ImprovementAttempts", new IntValue(100)));123 Parameters.Add(new ValueLookupParameter<IRandom>("Random"));93 Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix")); 94 Parameters.Add(new LookupParameter<IEvaluator>("Evaluator")); 95 Parameters.Add(new ValueParameter<IntValue>("ImprovementAttempts", new IntValue(100))); 96 Parameters.Add(new LookupParameter<IRandom>("Random")); 124 97 Parameters.Add(new ValueLookupParameter<IItem>("Target")); 125 98 #endregion … … 132 105 133 106 public override IOperation Apply() { 134 var currSol = CurrentScope.Variables[TargetParameter.ActualName].Value as Permutation; 135 var bestSol = currSol; 136 double currLength = TSPEuclideanPathEvaluator.Apply(Evaluator as TSPCoordinatesPathEvaluator, Coordinates, currSol); 137 double bestLength = currLength; 107 Permutation currSol = CurrentScope.Variables[TargetParameter.ActualName].Value as Permutation; 108 if (currSol.PermutationType != PermutationTypes.RelativeUndirected) 109 throw new ArgumentException("Cannot improve solution because the permutation type is not supported."); 138 110 139 111 for (int i = 0; i < ImprovementAttempts.Value; i++) { 140 112 int a = Random.Next(currSol.Length); 141 113 int b = Random.Next(currSol.Length); 142 Invert(currSol, a, b); 143 currLength = TSPEuclideanPathEvaluator.Apply(Evaluator as TSPCoordinatesPathEvaluator, Coordinates, currSol); 144 if (currLength < bestLength) { 145 bestLength = currLength; 146 bestSol = currSol.Clone() as Permutation; 147 } 148 Invert(currSol, a, b); 114 double oldFirstEdgeLength = DistanceMatrix[currSol[a], currSol[(a - 1 + currSol.Length) % currSol.Length]]; 115 double oldSecondEdgeLength = DistanceMatrix[currSol[b], currSol[(b + 1) % currSol.Length]]; 116 double newFirstEdgeLength = DistanceMatrix[currSol[b], currSol[(a - 1 + currSol.Length) % currSol.Length]]; 117 double newSecondEdgeLength = DistanceMatrix[currSol[a], currSol[(b + 1 + currSol.Length) % currSol.Length]]; 118 if (newFirstEdgeLength + newSecondEdgeLength < oldFirstEdgeLength + oldSecondEdgeLength) 119 Invert(currSol, a, b); 149 120 } 150 151 CurrentScope.Variables[TargetParameter.ActualName].Value = bestSol;152 121 153 122 return base.Apply(); -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/TravelingSalesman/TravelingSalesmanMultipleGuidesPathRelinker.cs
r7778 r7786 72 72 throw new ArgumentException("RelinkingAccuracy must be greater than 0."); 73 73 74 varv1 = initiator.Clone() as Permutation;75 vartargets = new Permutation[guides.Length];74 Permutation v1 = initiator.Clone() as Permutation; 75 Permutation[] targets = new Permutation[guides.Length]; 76 76 Array.Copy(guides, targets, guides.Length); 77 77 … … 79 79 throw new ArgumentException("At least one solution is of different length."); 80 80 81 varsolutions = new List<Permutation>();81 IList<Permutation> solutions = new List<Permutation>(); 82 82 for (int i = 0; i < v1.Length; i++) { 83 varcurrCityIndex = i;84 varbestCityIndex = (i + 1) % v1.Length;85 varcurrDistance = distances[v1[currCityIndex], v1[bestCityIndex]];83 int currCityIndex = i; 84 int bestCityIndex = (i + 1) % v1.Length; 85 double currDistance = distances[v1[currCityIndex], v1[bestCityIndex]]; 86 86 targets.ToList().ForEach(solution => { 87 87 var node = solution.Select((x, index) => new { Id = x, Index = index }).First(x => x.Id == v1[currCityIndex]); 88 varpred = solution[(node.Index - 1 + solution.Length) % solution.Length];89 varsucc = solution[(node.Index + 1) % solution.Length];88 int pred = solution[(node.Index - 1 + solution.Length) % solution.Length]; 89 int succ = solution[(node.Index + 1) % solution.Length]; 90 90 var results = new[] { pred, succ }.Select(x => new { Id = x, Distance = distances[x, node.Id] }); 91 91 if (results.Any(x => x.Distance < currDistance)) { … … 99 99 } 100 100 101 varselection = new List<IItem>();101 IList<IItem> selection = new List<IItem>(); 102 102 if (solutions.Count > 0) { 103 var noSol = (int)Math.Round(solutions.Count * n.Value);103 int noSol = (int)(solutions.Count * n.Value); 104 104 if (noSol <= 0) noSol++; 105 varstepSize = (double)solutions.Count / (double)noSol;105 double stepSize = (double)solutions.Count / (double)noSol; 106 106 for (int i = 0; i < noSol; i++) 107 selection.Add(solutions.ElementAt((int) Math.Round((i + 1) * stepSize - stepSize * 0.5)));107 selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5))); 108 108 } 109 109 -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/TravelingSalesman/TravelingSalesmanPathRelinker.cs
r7775 r7786 54 54 throw new ArgumentException("RelinkingAccuracy must be greater than 0."); 55 55 56 varv1 = initiator.Clone() as Permutation;57 varv2 = guide as Permutation;56 Permutation v1 = initiator.Clone() as Permutation; 57 Permutation v2 = guide as Permutation; 58 58 59 59 if (v1.Length != v2.Length) 60 60 throw new ArgumentException("The solutions are of different length."); 61 61 62 varsolutions = new List<Permutation>();62 IList<Permutation> solutions = new List<Permutation>(); 63 63 for (int i = 0; i < v1.Length; i++) 64 64 if (v1[i] != v2[i]) { … … 73 73 } 74 74 75 varselection = new List<IItem>();75 IList<IItem> selection = new List<IItem>(); 76 76 if (solutions.Count > 0) { 77 var noSol = (int)Math.Round(solutions.Count * n.Value);77 int noSol = (int)(solutions.Count * n.Value); 78 78 if (noSol <= 0) noSol++; 79 varstepSize = (double)solutions.Count / (double)noSol;79 double stepSize = (double)solutions.Count / (double)noSol; 80 80 for (int i = 0; i < noSol; i++) 81 selection.Add(solutions.ElementAt((int) Math.Round((i + 1) * stepSize - stepSize * 0.5)));81 selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5))); 82 82 } 83 83 -
branches/ScatterSearch/HeuristicLab.Algorithms.ScatterSearch/3.3/TravelingSalesman/TravelingSalesmanSimultaneousPathRelinker.cs
r7775 r7786 54 54 throw new ArgumentException("RelinkingAccuracy must be greater than 0."); 55 55 56 varfirstInitiator = initiator.Clone() as Permutation;57 varfirstGuide = guide.Clone() as Permutation;58 varsecondInitiator = firstGuide.Clone() as Permutation;59 varsecondGuide = firstInitiator.Clone() as Permutation;56 Permutation firstInitiator = initiator.Clone() as Permutation; 57 Permutation firstGuide = guide.Clone() as Permutation; 58 Permutation secondInitiator = firstGuide.Clone() as Permutation; 59 Permutation secondGuide = firstInitiator.Clone() as Permutation; 60 60 61 61 if (firstInitiator.Length != firstGuide.Length) 62 62 throw new ArgumentException("The solutions are of different length."); 63 63 64 varsolutions = new List<Permutation>();64 IList<Permutation> solutions = new List<Permutation>(); 65 65 for (int i = 0; i < firstInitiator.Length / 2; i++) { 66 66 if (firstInitiator[i] != firstGuide[i]) { … … 72 72 solutions.Add(firstInitiator.Clone() as Permutation); 73 73 } 74 varj = secondInitiator.Length - 1 - i;74 int j = secondInitiator.Length - 1 - i; 75 75 if (secondInitiator[j] != secondGuide[j]) { 76 76 var target = secondInitiator.Select((x, index) => new { Value = x, ValueIndex = index }).First(x => x.Value == secondGuide[j]); … … 83 83 } 84 84 85 varselection = new List<IItem>();85 IList<IItem> selection = new List<IItem>(); 86 86 if (solutions.Count > 0) { 87 var noSol = (int)Math.Round(solutions.Count * n.Value);87 int noSol = (int)(solutions.Count * n.Value); 88 88 if (noSol <= 0) noSol++; 89 varstepSize = (double)solutions.Count / (double)noSol;89 double stepSize = (double)solutions.Count / (double)noSol; 90 90 for (int i = 0; i < noSol; i++) 91 selection.Add(solutions.ElementAt((int) Math.Round((i + 1) * stepSize - stepSize * 0.5)));91 selection.Add(solutions.ElementAt((int)((i + 1) * stepSize - stepSize * 0.5))); 92 92 } 93 93
Note: See TracChangeset
for help on using the changeset viewer.