- Timestamp:
- 07/24/12 15:04:37 (12 years ago)
- Location:
- branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3
- Files:
-
- 1 deleted
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/HeuristicLab.Algorithms.ScatterSearch-3.3.csproj
r8306 r8319 85 85 </ItemGroup> 86 86 <ItemGroup> 87 <Compile Include="IScatterSearchOperator.cs" />88 87 <Compile Include="OffspringProcessor.cs" /> 89 88 <None Include="HeuristicLab.snk" /> -
branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/OffspringProcessor.cs
r8086 r8319 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 , IScatterSearchOperator{34 public sealed class OffspringProcessor : SingleSuccessorOperator { 35 35 #region Parameter properties 36 36 public ScopeParameter CurrentScopeParameter { 37 37 get { return (ScopeParameter)Parameters["CurrentScope"]; } 38 }39 public IValueLookupParameter<IItem> TargetParameter {40 get { return (IValueLookupParameter<IItem>)Parameters["Target"]; }41 38 } 42 39 #endregion … … 45 42 private IScope CurrentScope { 46 43 get { return CurrentScopeParameter.ActualValue; } 47 }48 private IItem Target {49 get { return TargetParameter.ActualValue; }50 44 } 51 45 #endregion … … 58 52 #region Create parameters 59 53 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the offspring as variables.")); 60 Parameters.Add(new ValueLookupParameter<IItem>("Target", "This parameter is used for name translation only."));61 54 #endregion 62 55 } … … 67 60 68 61 public override IOperation Apply() { 69 VariableCollection offspringSolutions = CurrentScope.Variables; 70 IScope offspringScope = new Scope("Offspring"); 71 foreach (var solution in offspringSolutions) { 72 IScope scope = new Scope(); 73 scope.Variables.Add(new Variable(TargetParameter.ActualName, solution.Value)); 74 offspringScope.SubScopes.Add(scope); 75 } 62 var child = new Scope(); 63 child.Variables.AddRange(CurrentScope.Variables); 64 var offspringScope = new Scope(); 65 offspringScope.SubScopes.Add(child); 66 var parents = CurrentScope.SubScopes.ToArray(); 67 CurrentScope.Variables.Clear(); 68 CurrentScope.SubScopes.Clear(); 69 CurrentScope.SubScopes.AddRange(parents); 76 70 CurrentScope.SubScopes.Add(offspringScope); 77 CurrentScope.Variables.Clear();78 71 return base.Apply(); 79 72 } -
branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/PopulationRebuildMethod.cs
r8086 r8319 34 34 [Item("PopulationRebuildMethod", "An operator that updates the reference set and rebuilds the population.")] 35 35 [StorableClass] 36 public sealed class PopulationRebuildMethod : SingleSuccessorOperator , IScatterSearchOperator{36 public sealed class PopulationRebuildMethod : SingleSuccessorOperator { 37 37 #region Parameter properties 38 38 public ScopeParameter CurrentScopeParameter { -
branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/ReferenceSetUpdateMethod.cs
r8304 r8319 39 39 public sealed class ReferenceSetUpdateMethod : SingleSuccessorOperator, ISimilarityBasedOperator { 40 40 #region ISimilarityBasedOperator Members 41 public IS imilarityCalculator SimilarityCalculator { get; set; }41 public ISolutionSimilarityCalculator SimilarityCalculator { get; set; } 42 42 #endregion 43 43 … … 76 76 77 77 public override IOperation Apply() { 78 IDictionary<IScope, double> population = new Dictionary<IScope, double>(); 79 foreach (var pScope in CurrentScope.SubScopes[0].SubScopes) { 80 double similarity = 0; 81 foreach (var rScope in CurrentScope.SubScopes[1].SubScopes) { 82 similarity += SimilarityCalculator.CalculateIndividualSimilarity(pScope, rScope); 83 } 84 population[pScope] = similarity; 78 var populationSimilarity = new Dictionary<IScope, double>(); 79 var populationScope = CurrentScope.SubScopes[0]; 80 var refSetScope = CurrentScope.SubScopes[1]; 81 var similarityMatrix = SimilarityCalculator.CalculateSolutionCrowdSimilarity(populationScope, refSetScope); 82 for (int i = 0; i < populationScope.SubScopes.Count; i++) { 83 populationSimilarity[populationScope.SubScopes[i]] = similarityMatrix[i].Sum(); 85 84 } 86 85 int numberOfHighQualitySolutions = CurrentScope.SubScopes[1].SubScopes.Count; 87 foreach (var entry in population .OrderBy(x => x.Value).Take(ReferenceSetSize.Value - numberOfHighQualitySolutions)) {86 foreach (var entry in populationSimilarity.OrderBy(x => x.Value).Take(ReferenceSetSize.Value - numberOfHighQualitySolutions)) { 88 87 CurrentScope.SubScopes[1].SubScopes.Add(entry.Key); 89 88 CurrentScope.SubScopes[0].SubScopes.Remove(entry.Key); -
branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/ScatterSearch.cs
r8299 r8319 22 22 using System; 23 23 using System.Linq; 24 using System.Reflection;25 24 using HeuristicLab.Analysis; 26 25 using HeuristicLab.Common; … … 89 88 get { return (IValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; } 90 89 } 91 public ConstrainedValueParameter<IS imilarityCalculator> SimilarityCalculatorParameter {92 get { return (ConstrainedValueParameter<IS imilarityCalculator>)Parameters["SimilarityCalculator"]; }90 public ConstrainedValueParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter { 91 get { return (ConstrainedValueParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; } 93 92 } 94 93 #endregion … … 139 138 set { SetSeedRandomlyParameter.Value = value; } 140 139 } 141 private IS imilarityCalculator SimilarityCalculator {140 private ISolutionSimilarityCalculator SimilarityCalculator { 142 141 get { return SimilarityCalculatorParameter.Value; } 143 142 set { SimilarityCalculatorParameter.Value = value; } … … 185 184 Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0))); 186 185 Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true))); 187 Parameters.Add(new ConstrainedValueParameter<IS imilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions."));186 Parameters.Add(new ConstrainedValueParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions.")); 188 187 #endregion 189 188 … … 382 381 } 383 382 private void UpdateSimilarityCalculators() { 384 IS imilarityCalculator oldDiversityCalculator = SimilarityCalculatorParameter.Value;383 ISolutionSimilarityCalculator oldSimilarityCalculator = SimilarityCalculatorParameter.Value; 385 384 SimilarityCalculatorParameter.ValidValues.Clear(); 386 IS imilarityCalculator defaultDiversityCalculator = Problem.Operators.OfType<ISimilarityCalculator>().FirstOrDefault();387 388 foreach (IS imilarityCalculator diversityCalculator in Problem.Operators.OfType<ISimilarityCalculator>())389 SimilarityCalculatorParameter.ValidValues.Add( diversityCalculator);390 391 if (old DiversityCalculator != null) {392 IS imilarityCalculator diversityCalculator = SimilarityCalculatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldDiversityCalculator.GetType());393 if ( diversityCalculator != null) SimilarityCalculatorParameter.Value = diversityCalculator;394 else old DiversityCalculator = null;395 } 396 if (old DiversityCalculator == null && defaultDiversityCalculator != null)397 SimilarityCalculatorParameter.Value = default DiversityCalculator;385 ISolutionSimilarityCalculator defaultSimilarityCalculator = Problem.Operators.OfType<ISolutionSimilarityCalculator>().FirstOrDefault(); 386 387 foreach (ISolutionSimilarityCalculator similarityCalculator in Problem.Operators.OfType<ISolutionSimilarityCalculator>()) 388 SimilarityCalculatorParameter.ValidValues.Add(similarityCalculator); 389 390 if (oldSimilarityCalculator != null) { 391 ISolutionSimilarityCalculator similarityCalculator = SimilarityCalculatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSimilarityCalculator.GetType()); 392 if (similarityCalculator != null) SimilarityCalculatorParameter.Value = similarityCalculator; 393 else oldSimilarityCalculator = null; 394 } 395 if (oldSimilarityCalculator == null && defaultSimilarityCalculator != null) 396 SimilarityCalculatorParameter.Value = defaultSimilarityCalculator; 398 397 } 399 398 private void ParameterizeBestSelector() { … … 410 409 MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name; 411 410 MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; 412 MainLoop.TargetParameter.ActualName = Improver.TargetParameter.ActualName; 413 foreach (var op in MainLoop.OperatorGraph.Operators.OfType<IScatterSearchOperator>()) { 414 // parameter should be accessed direclty (using an interface definition) 415 PropertyInfo propInfo = op.GetType().GetProperty(MainLoop.TargetParameter.Name + "Parameter"); 416 if (propInfo != null && propInfo.CanRead) 417 (propInfo.GetValue(op, null) as IValueLookupParameter<IItem>).ActualName = MainLoop.TargetParameter.ActualName; 418 propInfo = op.GetType().GetProperty(MainLoop.QualityParameter.Name + "Parameter"); 419 if (propInfo != null && propInfo.CanRead) 420 (propInfo.GetValue(op, null) as IValueLookupParameter<IItem>).ActualName = MainLoop.QualityParameter.ActualName; 421 } 411 MainLoop.OperatorGraph.Operators.OfType<PopulationRebuildMethod>().Single().QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; 412 MainLoop.OperatorGraph.Operators.OfType<SolutionPoolUpdateMethod>().Single().QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName; 422 413 foreach (ISimilarityBasedOperator op in MainLoop.OperatorGraph.Operators.OfType<ISimilarityBasedOperator>()) 423 414 op.SimilarityCalculator = SimilarityCalculator; -
branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/ScatterSearchMainLoop.cs
r8086 r8319 89 89 get { return (IValueLookupParameter<VariableCollection>)Parameters["Results"]; } 90 90 } 91 public IValueLookupParameter<ISimilarityCalculator> SimilarityCalculatorParameter { 92 get { return (IValueLookupParameter<ISimilarityCalculator>)Parameters["SimilarityCalculator"]; } 93 } 94 public IValueLookupParameter<IItem> TargetParameter { 95 get { return (IValueLookupParameter<IItem>)Parameters["Target"]; } 91 public IValueLookupParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter { 92 get { return (IValueLookupParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; } 96 93 } 97 94 #endregion … … 166 163 set { ResultsParameter.ActualValue = value; } 167 164 } 168 private IS imilarityCalculator SimilarityCalculator {165 private ISolutionSimilarityCalculator SimilarityCalculator { 169 166 get { return SimilarityCalculatorParameter.ActualValue; } 170 167 set { SimilarityCalculatorParameter.ActualValue = value; } 171 }172 private IItem Target {173 get { return TargetParameter.ActualValue; }174 set { TargetParameter.ActualValue = value; }175 168 } 176 169 #endregion … … 204 197 Parameters.Add(new ValueLookupParameter<IRandom>("Random", "A pseudo random number generator.")); 205 198 Parameters.Add(new ValueLookupParameter<VariableCollection>("Results", "The variable collection where results should be stored.")); 206 Parameters.Add(new ValueLookupParameter<ISimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions.")); 207 Parameters.Add(new ValueLookupParameter<IItem>("Target", "This parameter is used for name translation only.")); 199 Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions.")); 208 200 #endregion 209 201 … … 298 290 pathRelinker.Name = "PathRelinker"; 299 291 pathRelinker.OperatorParameter.ActualName = PathRelinkerParameter.Name; 300 pathRelinker.Successor = offspringProcessor;292 pathRelinker.Successor = rightSelector; 301 293 302 294 crossover.Name = "Crossover"; … … 304 296 crossover.Successor = offspringProcessor; 305 297 306 offspringProcessor.TargetParameter.ActualName = TargetParameter.ActualName;307 298 offspringProcessor.Successor = rightSelector; 308 299 -
branches/ScatterSearch (trunk integration)/HeuristicLab.Algorithms.ScatterSearch/3.3/SolutionPoolUpdateMethod.cs
r8304 r8319 28 28 using HeuristicLab.Operators; 29 29 using HeuristicLab.Optimization; 30 using HeuristicLab.Optimization.Operators;31 30 using HeuristicLab.Parameters; 32 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 38 37 [Item("SolutionPoolUpdateMethod", "An operator that updates the solution pool.")] 39 38 [StorableClass] 40 public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator, IS catterSearchOperator, ISimilarityBasedOperator {39 public sealed class SolutionPoolUpdateMethod : SingleSuccessorOperator, ISimilarityBasedOperator { 41 40 #region ISimilarityBasedOperator Members 42 public IS imilarityCalculator SimilarityCalculator { get; set; }41 public ISolutionSimilarityCalculator SimilarityCalculator { get; set; } 43 42 #endregion 44 43 … … 130 129 if (orderedOffspring.Any(hasBetterQuality)) { 131 130 // produce the set union 132 var union = orderedParents.Union(orderedOffspring.Where(hasBetterQuality), new SolutionEqualityComparer<IScope>(SimilarityCalculator.Calculate IndividualSimilarity));131 var union = orderedParents.Union(orderedOffspring.Where(hasBetterQuality), new SolutionEqualityComparer<IScope>(SimilarityCalculator.CalculateSolutionSimilarity)); 133 132 if (union.Count() > orderedParents.Count()) { 134 133 var orderedUnion = Maximization.Value ? union.OrderByDescending(x => x.Variables[QualityParameter.ActualName].Value) : … … 142 141 } 143 142 143 // derive SingleObjectiveSolutionSimilarityCalculator from EqualityComparer 144 // delete this ... 144 145 public class SolutionEqualityComparer<T> : EqualityComparer<T> { 145 146 private readonly Func<T, T, double> similarityCalculator;
Note: See TracChangeset
for help on using the changeset viewer.