- Timestamp:
- 09/10/12 17:10:27 (12 years ago)
- Location:
- branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3
- Files:
-
- 5 edited
- 3 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/Calculator.cs
r8615 r8622 25 25 using HeuristicLab.Data; 26 26 using HeuristicLab.Operators; 27 using HeuristicLab.Optimization;28 27 using HeuristicLab.Parameters; 29 28 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 33 32 /// An operator that evaluates an expression. 34 33 /// </summary> 35 [Item(" ExpressionEvaluator", "An operator that evaluates an expression.")]34 [Item("Calculator", "An operator that evaluates an expression.")] 36 35 [StorableClass] 37 public class ExpressionEvaluator : ValuesCollector {36 public class Calculator : ValuesCollector { 38 37 [Storable] 39 public Calculator Calculator{ get; set; }38 public Optimization.Calculator Calc { get; set; } 40 39 41 40 #region Parameter Properties … … 58 57 59 58 [StorableConstructor] 60 protected ExpressionEvaluator(bool deserializing) : base(deserializing) { }61 protected ExpressionEvaluator(ExpressionEvaluator original, Cloner cloner)59 protected Calculator(bool deserializing) : base(deserializing) { } 60 protected Calculator(Calculator original, Cloner cloner) 62 61 : base(original, cloner) { 63 this.Calc ulator = cloner.Clone(original.Calculator);62 this.Calc = cloner.Clone(original.Calc); 64 63 } 65 public ExpressionEvaluator()64 public Calculator() 66 65 : base() { 67 66 Parameters.Add(new ValueLookupParameter<IItem>("ExpressionResult", "The result of the evaluated expression.")); … … 90 89 If the final value is null, the result variable is removed if it exists.")); 91 90 92 Calc ulator = newCalculator();91 Calc = new Optimization.Calculator(); 93 92 } 94 93 95 94 public override IDeepCloneable Clone(Cloner cloner) { 96 return new ExpressionEvaluator(this, cloner);95 return new Calculator(this, cloner); 97 96 } 98 97 99 98 public override IOperation Apply() { 100 Calc ulator.Formula = Formula;99 Calc.Formula = Formula; 101 100 var variables = new Dictionary<string, IItem>(); 102 101 foreach (var collectedValue in CollectedValues) 103 102 variables[collectedValue.Name] = collectedValue.ActualValue; 104 ExpressionResult = Calc ulator.GetValue(variables);103 ExpressionResult = Calc.GetValue(variables); 105 104 return base.Apply(); 106 105 } -
branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/HeuristicLab.Algorithms.RAPGA-3.3.csproj
r8406 r8622 100 100 <Compile Include="Analyzers\OffspringSuccessAnalyzer.cs" /> 101 101 <Compile Include="Analyzers\PopulationSizeAnalyzer.cs" /> 102 <Compile Include="Calculator.cs" /> 102 103 <Compile Include="DuplicatesSelector.cs" /> 103 <Compile Include="ExpressionEvaluator.cs" />104 <Compile Include="OffspringRestorer.cs" />105 104 <Compile Include="ProgressiveOffspringPreserver.cs" /> 106 <Compile Include="SimilarityCalculator\BlindSimilarityCalculator.cs" /> 105 <Compile Include="ScopeRestorer.cs" /> 106 <Compile Include="SimilarityCalculator\NoSimilarityCalculator.cs" /> 107 107 <Compile Include="SimilarityCalculator\QualitySimilarityCalculator.cs" /> 108 108 <Compile Include="RAPGA.cs" /> -
branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/ProgressiveOffspringPreserver.cs
r8406 r8622 93 93 94 94 public override IOperation Apply() { 95 if (CurrentScope.SubScopes.Any()) { 96 if (OffspringList.Any()) { 97 var currentOffspring = new Scope(); 98 currentOffspring.SubScopes.AddRange(OffspringList); 99 var similarityMatrix = SimilarityCalculator.CalculateSolutionCrowdSimilarity(CurrentScope, currentOffspring); 100 for (int i = CurrentScope.SubScopes.Count - 1; i >= 0 && OffspringList.Count < MaximumPopulationSize.Value - Elites.Value; i--) 101 if (similarityMatrix[i].Any(x => x == 1.0)) CurrentScope.SubScopes.RemoveAt(i); 102 else OffspringList.Add(CurrentScope.SubScopes[i]); 103 } else OffspringList.AddRange(CurrentScope.SubScopes); 95 if (CurrentScope.SubScopes.Any()) { // offspring created 96 if (!OffspringList.Any()) OffspringList.AddRange(CurrentScope.SubScopes); 97 else { // stored offspring exists 98 var storedOffspringScope = new Scope(); 99 storedOffspringScope.SubScopes.AddRange(OffspringList); 100 var similarityMatrix = SimilarityCalculator.CalculateSolutionCrowdSimilarity(CurrentScope, storedOffspringScope); 101 102 var createdOffspring = CurrentScope.SubScopes.ToArray(); 103 104 int i = 0; 105 // as long as offspring is available and not enough offspring has been preserved 106 while (i < createdOffspring.Length && OffspringList.Count < MaximumPopulationSize.Value - Elites.Value) { 107 if (similarityMatrix[i].Any(x => x == 1.0)) createdOffspring[i] = null; // discard duplicates 108 else OffspringList.Add(createdOffspring[i]); 109 i++; 110 } 111 112 // discard remaining offspring 113 while (i < createdOffspring.Length) createdOffspring[i++] = null; 114 115 // clean current scope 116 CurrentScope.SubScopes.Replace(createdOffspring.Where(x => x != null)); 117 } 104 118 } 105 119 return base.Apply(); -
branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/RAPGA.cs
r8407 r8622 483 483 484 484 SimilarityCalculatorParameter.ValidValues.Add(new QualitySimilarityCalculator { QualityVariableName = Problem.Evaluator.QualityParameter.ActualName }); 485 SimilarityCalculatorParameter.ValidValues.Add(new BlindSimilarityCalculator());485 SimilarityCalculatorParameter.ValidValues.Add(new NoSimilarityCalculator()); 486 486 487 487 foreach (ISingleObjectiveSolutionSimilarityCalculator similarityCalculator in Problem.Operators.OfType<ISingleObjectiveSolutionSimilarityCalculator>()) -
branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/RAPGAMainLoop.cs
r8400 r8622 166 166 ProgressiveOffspringPreserver progressiveOffspringSelector = new ProgressiveOffspringPreserver(); 167 167 SubScopesCounter subScopesCounter2 = new SubScopesCounter(); 168 Calculator calculator1 = new Calculator(); 169 ConditionalBranch conditionalBranch1 = new ConditionalBranch(); 168 170 Comparator comparator1 = new Comparator(); 169 ConditionalBranch conditionalBranch1 = new ConditionalBranch();170 Comparator comparator2 = new Comparator();171 171 ConditionalBranch conditionalBranch2 = new ConditionalBranch(); 172 172 LeftReducer leftReducer2 = new LeftReducer(); … … 175 175 RightReducer rightReducer2 = new RightReducer(); 176 176 ScopeCleaner scopeCleaner = new ScopeCleaner(); 177 OffspringRestorer offspringRestorer = new OffspringRestorer();177 ScopeRestorer scopeRestorer = new ScopeRestorer(); 178 178 MergingReducer mergingReducer = new MergingReducer(); 179 Assigner assigner2 = new Assigner();180 179 IntCounter intCounter3 = new IntCounter(); 181 180 SubScopesCounter subScopesCounter3 = new SubScopesCounter(); 182 ExpressionEvaluator expressionEvaluator = new ExpressionEvaluator(); 181 Calculator calculator2 = new Calculator(); 182 Comparator comparator2 = new Comparator(); 183 ConditionalBranch conditionalBranch3 = new ConditionalBranch(); 183 184 Placeholder analyzer2 = new Placeholder(); 184 185 Comparator comparator3 = new Comparator(); 185 ConditionalBranch conditionalBranch 3= new ConditionalBranch();186 ConditionalBranch conditionalBranch4 = new ConditionalBranch(); 186 187 Comparator comparator4 = new Comparator(); 187 ConditionalBranch conditionalBranch 4= new ConditionalBranch();188 ConditionalBranch conditionalBranch5 = new ConditionalBranch(); 188 189 Assigner assigner3 = new Assigner(); 189 190 Assigner assigner4 = new Assigner(); … … 192 193 variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class RAPGA expects this to be called Generations 193 194 variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("CurrentPopulationSize", new IntValue(0))); 194 variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("LastPopulationSize", new IntValue(0)));195 195 variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("NumberOfCreatedOffspring", new IntValue(0))); 196 196 variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("NumberOfSuccessfulOffspring", new IntValue(0))); … … 258 258 subScopesCounter2.ValueParameter.ActualName = "NumberOfSuccessfulOffspring"; 259 259 260 comparator1.Name = "NumberOfSuccessfulOffspring >= MaximumPopulationSize"; 261 comparator1.Comparison = new Comparison(ComparisonType.GreaterOrEqual); 262 comparator1.LeftSideParameter.ActualName = "NumberOfSuccessfulOffspring"; 263 comparator1.RightSideParameter.ActualName = MaximumPopulationSizeParameter.Name; 264 comparator1.ResultParameter.ActualName = "Break"; 260 calculator1.Name = "NumberOfSuccessfulOffspring == MaximumPopulationSize - Elites"; 261 calculator1.CollectedValues.Add(new ValueLookupParameter<IntValue>("NumberOfSuccessfulOffspring")); 262 calculator1.CollectedValues.Add(new ValueLookupParameter<IntValue>("MaximumPopulationSize")); 263 calculator1.CollectedValues.Add(new ValueLookupParameter<IntValue>("Elites")); 264 calculator1.ExpressionParameter.Value = new StringValue("NumberOfSuccessfulOffspring MaximumPopulationSize Elites - =="); 265 calculator1.ExpressionResultParameter.ActualName = "Break"; 265 266 266 267 conditionalBranch1.Name = "Break?"; 267 268 conditionalBranch1.ConditionParameter.ActualName = "Break"; 268 269 269 comparator 2.Name = "NumberOfCreatedOffspring >= Effort";270 comparator 2.Comparison = new Comparison(ComparisonType.GreaterOrEqual);271 comparator 2.LeftSideParameter.ActualName = "NumberOfCreatedOffspring";272 comparator 2.RightSideParameter.ActualName = EffortParameter.Name;273 comparator 2.ResultParameter.ActualName = "Break";270 comparator1.Name = "NumberOfCreatedOffspring >= Effort"; 271 comparator1.Comparison = new Comparison(ComparisonType.GreaterOrEqual); 272 comparator1.LeftSideParameter.ActualName = "NumberOfCreatedOffspring"; 273 comparator1.RightSideParameter.ActualName = EffortParameter.Name; 274 comparator1.ResultParameter.ActualName = "Break"; 274 275 275 276 conditionalBranch2.Name = "Break?"; … … 281 282 bestSelector.QualityParameter.ActualName = QualityParameter.Name; 282 283 283 assigner2.Name = "Update LastPopulationSize";284 assigner2.LeftSideParameter.ActualName = "LastPopulationSize";285 assigner2.RightSideParameter.ActualName = "CurrentPopulationSize";286 287 284 intCounter3.Name = "Increment Generations"; 288 285 intCounter3.Increment = new IntValue(1); … … 293 290 subScopesCounter3.AccumulateParameter.Value = new BoolValue(false); 294 291 295 expressionEvaluator.Name = "Evaluate ActualSelectionPressure"; 296 expressionEvaluator.CollectedValues.Add(new ValueLookupParameter<IntValue>("CurrentPopulationSize")); 297 expressionEvaluator.CollectedValues.Add(new ValueLookupParameter<IntValue>("LastPopulationSize")); 298 expressionEvaluator.CollectedValues.Add(new ValueLookupParameter<IntValue>("NumberOfCreatedOffspring")); 299 expressionEvaluator.CollectedValues.Add(new ValueLookupParameter<IntValue>("NumberOfSuccessfulOffspring")); 300 expressionEvaluator.ExpressionParameter.Value = new StringValue("CurrentPopulationSize NumberOfCreatedOffspring NumberOfSuccessfulOffspring - + LastPopulationSize /"); 301 expressionEvaluator.ExpressionResultParameter.ActualName = "ActualSelectionPressure"; 292 calculator2.Name = "Evaluate ActualSelectionPressure"; 293 calculator2.CollectedValues.Add(new ValueLookupParameter<IntValue>("NumberOfCreatedOffspring")); 294 calculator2.CollectedValues.Add(new ValueLookupParameter<IntValue>("Elites")); 295 calculator2.CollectedValues.Add(new ValueLookupParameter<IntValue>("CurrentPopulationSize")); 296 calculator2.ExpressionParameter.Value = new StringValue("NumberOfCreatedOffspring Elites + CurrentPopulationSize /"); 297 calculator2.ExpressionResultParameter.ActualName = "ActualSelectionPressure"; 298 299 comparator2.Name = "CurrentPopulationSize < 1"; 300 comparator2.Comparison = new Comparison(ComparisonType.Less); 301 comparator2.LeftSideParameter.ActualName = "CurrentPopulationSize"; 302 comparator2.RightSideParameter.Value = new IntValue(1); 303 comparator2.ResultParameter.ActualName = "Terminate"; 304 305 conditionalBranch3.Name = "Terminate?"; 306 conditionalBranch3.ConditionParameter.ActualName = "Terminate"; 302 307 303 308 analyzer2.Name = "Analyzer"; … … 310 315 comparator3.RightSideParameter.ActualName = MaximumGenerationsParameter.Name; 311 316 312 conditionalBranch 3.Name = "Terminate?";313 conditionalBranch 3.ConditionParameter.ActualName = "Terminate";317 conditionalBranch4.Name = "Terminate?"; 318 conditionalBranch4.ConditionParameter.ActualName = "Terminate"; 314 319 315 320 comparator4.Name = "CurrentPopulationSize < MinimumPopulationSize"; … … 319 324 comparator4.ResultParameter.ActualName = "Terminate"; 320 325 321 conditionalBranch 4.Name = "Terminate?";322 conditionalBranch 4.ConditionParameter.ActualName = "Terminate";326 conditionalBranch5.Name = "Terminate?"; 327 conditionalBranch5.ConditionParameter.ActualName = "Terminate"; 323 328 324 329 assigner3.Name = "Reset NumberOfCreatedOffspring"; … … 344 349 subScopesProcessor1.Operators.Add(new EmptyOperator()); 345 350 subScopesProcessor1.Operators.Add(childrenCreator); 346 subScopesProcessor1.Successor = c omparator1;351 subScopesProcessor1.Successor = calculator1; 347 352 childrenCreator.Successor = uniformSubScopesProcessor; 348 353 uniformSubScopesProcessor.Operator = crossover; … … 362 367 leftReducer1.Successor = progressiveOffspringSelector; 363 368 progressiveOffspringSelector.Successor = subScopesCounter2; 364 c omparator1.Successor = conditionalBranch1;365 conditionalBranch1.FalseBranch = comparator 2;369 calculator1.Successor = conditionalBranch1; 370 conditionalBranch1.FalseBranch = comparator1; 366 371 conditionalBranch1.TrueBranch = subScopesProcessor2; 367 comparator 2.Successor = conditionalBranch2;372 comparator1.Successor = conditionalBranch2; 368 373 conditionalBranch2.FalseBranch = leftReducer2; 369 374 conditionalBranch2.TrueBranch = subScopesProcessor2; … … 374 379 bestSelector.Successor = rightReducer2; 375 380 rightReducer2.Successor = null; 376 scopeCleaner.Successor = offspringRestorer; 377 mergingReducer.Successor = assigner2; 378 assigner2.Successor = intCounter3; 381 scopeCleaner.Successor = scopeRestorer; 382 mergingReducer.Successor = intCounter3; 379 383 intCounter3.Successor = subScopesCounter3; 380 subScopesCounter3.Successor = expressionEvaluator; 381 expressionEvaluator.Successor = analyzer2; 384 subScopesCounter3.Successor = calculator2; 385 calculator2.Successor = comparator2; 386 comparator2.Successor = conditionalBranch3; 387 conditionalBranch3.FalseBranch = analyzer2; 388 conditionalBranch3.TrueBranch = null; 382 389 analyzer2.Successor = comparator3; 383 comparator3.Successor = conditionalBranch3; 384 conditionalBranch3.FalseBranch = comparator4; 385 conditionalBranch3.TrueBranch = null; 386 conditionalBranch3.Successor = null; 387 comparator4.Successor = conditionalBranch4; 388 conditionalBranch4.FalseBranch = assigner3; 390 comparator3.Successor = conditionalBranch4; 391 conditionalBranch4.FalseBranch = comparator4; 389 392 conditionalBranch4.TrueBranch = null; 390 393 conditionalBranch4.Successor = null; 394 comparator4.Successor = conditionalBranch5; 395 conditionalBranch5.FalseBranch = assigner3; 396 conditionalBranch5.TrueBranch = null; 397 conditionalBranch5.Successor = null; 391 398 assigner3.Successor = assigner4; 392 399 assigner4.Successor = assigner5; -
branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/ScopeRestorer.cs
r8615 r8622 33 33 /// It adds all scopes in the list as sub-scopes to the current scope. 34 34 /// </remarks> 35 [Item(" OffspringRestorer", "An operator that restores created offspring from a scope list. It adds all scopes in the list as sub-scopes to the current scope.")]35 [Item("ScopeRestorer", "An operator that restores created offspring from a scope list. It adds all scopes in the list as sub-scopes to the current scope.")] 36 36 [StorableClass] 37 public class OffspringRestorer : SingleSuccessorOperator {37 public class ScopeRestorer : SingleSuccessorOperator { 38 38 #region Parameter Properties 39 39 public ScopeParameter CurrentScopeParameter { … … 55 55 56 56 [StorableConstructor] 57 protected OffspringRestorer(bool deserializing) : base(deserializing) { }58 protected OffspringRestorer(OffspringRestorer original, Cloner cloner) : base(original, cloner) { }59 public OffspringRestorer()57 protected ScopeRestorer(bool deserializing) : base(deserializing) { } 58 protected ScopeRestorer(ScopeRestorer original, Cloner cloner) : base(original, cloner) { } 59 public ScopeRestorer() 60 60 : base() { 61 61 Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the offspring.")); … … 64 64 65 65 public override IDeepCloneable Clone(Cloner cloner) { 66 return new OffspringRestorer(this, cloner);66 return new ScopeRestorer(this, cloner); 67 67 } 68 68 -
branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/SimilarityCalculator/NoSimilarityCalculator.cs
r8615 r8622 31 31 /// The item always considers two solutions to be distinct. 32 32 /// </remarks> 33 [Item(" BlindSimilarityCalculator", "An item that performs similarity calculation between two solutions. The item always considers two solutions to be distinct.")]34 public sealed class BlindSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {35 private BlindSimilarityCalculator(bool deserializing) : base(deserializing) { }36 private BlindSimilarityCalculator(BlindSimilarityCalculator original, Cloner cloner) : base(original, cloner) { }37 public BlindSimilarityCalculator() : base() { }33 [Item("NoSimilarityCalculator", "An item that performs similarity calculation between two solutions. The item always considers two solutions to be distinct.")] 34 public sealed class NoSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator { 35 private NoSimilarityCalculator(bool deserializing) : base(deserializing) { } 36 private NoSimilarityCalculator(NoSimilarityCalculator original, Cloner cloner) : base(original, cloner) { } 37 public NoSimilarityCalculator() : base() { } 38 38 39 39 public override IDeepCloneable Clone(Cloner cloner) { 40 return new BlindSimilarityCalculator(this, cloner);40 return new NoSimilarityCalculator(this, cloner); 41 41 } 42 42 -
branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/SimilarityCalculator/QualitySimilarityCalculator.cs
r8406 r8622 20 20 #endregion 21 21 22 using System; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Core; … … 43 44 44 45 public static double CalculateSimilarity(IScope left, IScope right, string qualityVariableName) { 45 return ((DoubleValue)left.Variables[qualityVariableName].Value).Value == ((DoubleValue)right.Variables[qualityVariableName].Value).Value ? 1.0 : 0.0; 46 double leftQuality = ((DoubleValue)left.Variables[qualityVariableName].Value).Value; 47 double rightQuality = ((DoubleValue)right.Variables[qualityVariableName].Value).Value; 48 double delta = leftQuality.IsAlmost(rightQuality) ? 0.0 : Math.Abs(leftQuality - rightQuality); 49 return 1.0 - delta / Math.Max(leftQuality, rightQuality); 46 50 } 47 51
Note: See TracChangeset
for help on using the changeset viewer.