Changeset 8622


Ignore:
Timestamp:
09/10/12 17:10:27 (10 years ago)
Author:
jkarder
Message:

#1247:

  • fixed some bugs
  • minor code improvements
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  
    2525using HeuristicLab.Data;
    2626using HeuristicLab.Operators;
    27 using HeuristicLab.Optimization;
    2827using HeuristicLab.Parameters;
    2928using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    3332  /// An operator that evaluates an expression.
    3433  /// </summary>
    35   [Item("ExpressionEvaluator", "An operator that evaluates an expression.")]
     34  [Item("Calculator", "An operator that evaluates an expression.")]
    3635  [StorableClass]
    37   public class ExpressionEvaluator : ValuesCollector {
     36  public class Calculator : ValuesCollector {
    3837    [Storable]
    39     public Calculator Calculator { get; set; }
     38    public Optimization.Calculator Calc { get; set; }
    4039
    4140    #region Parameter Properties
     
    5857
    5958    [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)
    6261      : base(original, cloner) {
    63       this.Calculator = cloner.Clone(original.Calculator);
     62      this.Calc = cloner.Clone(original.Calc);
    6463    }
    65     public ExpressionEvaluator()
     64    public Calculator()
    6665      : base() {
    6766      Parameters.Add(new ValueLookupParameter<IItem>("ExpressionResult", "The result of the evaluated expression."));
     
    9089If the final value is null, the result variable is removed if it exists."));
    9190
    92       Calculator = new Calculator();
     91      Calc = new Optimization.Calculator();
    9392    }
    9493
    9594    public override IDeepCloneable Clone(Cloner cloner) {
    96       return new ExpressionEvaluator(this, cloner);
     95      return new Calculator(this, cloner);
    9796    }
    9897
    9998    public override IOperation Apply() {
    100       Calculator.Formula = Formula;
     99      Calc.Formula = Formula;
    101100      var variables = new Dictionary<string, IItem>();
    102101      foreach (var collectedValue in CollectedValues)
    103102        variables[collectedValue.Name] = collectedValue.ActualValue;
    104       ExpressionResult = Calculator.GetValue(variables);
     103      ExpressionResult = Calc.GetValue(variables);
    105104      return base.Apply();
    106105    }
  • branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/HeuristicLab.Algorithms.RAPGA-3.3.csproj

    r8406 r8622  
    100100    <Compile Include="Analyzers\OffspringSuccessAnalyzer.cs" />
    101101    <Compile Include="Analyzers\PopulationSizeAnalyzer.cs" />
     102    <Compile Include="Calculator.cs" />
    102103    <Compile Include="DuplicatesSelector.cs" />
    103     <Compile Include="ExpressionEvaluator.cs" />
    104     <Compile Include="OffspringRestorer.cs" />
    105104    <Compile Include="ProgressiveOffspringPreserver.cs" />
    106     <Compile Include="SimilarityCalculator\BlindSimilarityCalculator.cs" />
     105    <Compile Include="ScopeRestorer.cs" />
     106    <Compile Include="SimilarityCalculator\NoSimilarityCalculator.cs" />
    107107    <Compile Include="SimilarityCalculator\QualitySimilarityCalculator.cs" />
    108108    <Compile Include="RAPGA.cs" />
  • branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/ProgressiveOffspringPreserver.cs

    r8406 r8622  
    9393
    9494    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        }
    104118      }
    105119      return base.Apply();
  • branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/RAPGA.cs

    r8407 r8622  
    483483
    484484      SimilarityCalculatorParameter.ValidValues.Add(new QualitySimilarityCalculator { QualityVariableName = Problem.Evaluator.QualityParameter.ActualName });
    485       SimilarityCalculatorParameter.ValidValues.Add(new BlindSimilarityCalculator());
     485      SimilarityCalculatorParameter.ValidValues.Add(new NoSimilarityCalculator());
    486486
    487487      foreach (ISingleObjectiveSolutionSimilarityCalculator similarityCalculator in Problem.Operators.OfType<ISingleObjectiveSolutionSimilarityCalculator>())
  • branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/RAPGAMainLoop.cs

    r8400 r8622  
    166166      ProgressiveOffspringPreserver progressiveOffspringSelector = new ProgressiveOffspringPreserver();
    167167      SubScopesCounter subScopesCounter2 = new SubScopesCounter();
     168      Calculator calculator1 = new Calculator();
     169      ConditionalBranch conditionalBranch1 = new ConditionalBranch();
    168170      Comparator comparator1 = new Comparator();
    169       ConditionalBranch conditionalBranch1 = new ConditionalBranch();
    170       Comparator comparator2 = new Comparator();
    171171      ConditionalBranch conditionalBranch2 = new ConditionalBranch();
    172172      LeftReducer leftReducer2 = new LeftReducer();
     
    175175      RightReducer rightReducer2 = new RightReducer();
    176176      ScopeCleaner scopeCleaner = new ScopeCleaner();
    177       OffspringRestorer offspringRestorer = new OffspringRestorer();
     177      ScopeRestorer scopeRestorer = new ScopeRestorer();
    178178      MergingReducer mergingReducer = new MergingReducer();
    179       Assigner assigner2 = new Assigner();
    180179      IntCounter intCounter3 = new IntCounter();
    181180      SubScopesCounter subScopesCounter3 = new SubScopesCounter();
    182       ExpressionEvaluator expressionEvaluator = new ExpressionEvaluator();
     181      Calculator calculator2 = new Calculator();
     182      Comparator comparator2 = new Comparator();
     183      ConditionalBranch conditionalBranch3 = new ConditionalBranch();
    183184      Placeholder analyzer2 = new Placeholder();
    184185      Comparator comparator3 = new Comparator();
    185       ConditionalBranch conditionalBranch3 = new ConditionalBranch();
     186      ConditionalBranch conditionalBranch4 = new ConditionalBranch();
    186187      Comparator comparator4 = new Comparator();
    187       ConditionalBranch conditionalBranch4 = new ConditionalBranch();
     188      ConditionalBranch conditionalBranch5 = new ConditionalBranch();
    188189      Assigner assigner3 = new Assigner();
    189190      Assigner assigner4 = new Assigner();
     
    192193      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class RAPGA expects this to be called Generations
    193194      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("CurrentPopulationSize", new IntValue(0)));
    194       variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("LastPopulationSize", new IntValue(0)));
    195195      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("NumberOfCreatedOffspring", new IntValue(0)));
    196196      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("NumberOfSuccessfulOffspring", new IntValue(0)));
     
    258258      subScopesCounter2.ValueParameter.ActualName = "NumberOfSuccessfulOffspring";
    259259
    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";
    265266
    266267      conditionalBranch1.Name = "Break?";
    267268      conditionalBranch1.ConditionParameter.ActualName = "Break";
    268269
    269       comparator2.Name = "NumberOfCreatedOffspring >= Effort";
    270       comparator2.Comparison = new Comparison(ComparisonType.GreaterOrEqual);
    271       comparator2.LeftSideParameter.ActualName = "NumberOfCreatedOffspring";
    272       comparator2.RightSideParameter.ActualName = EffortParameter.Name;
    273       comparator2.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";
    274275
    275276      conditionalBranch2.Name = "Break?";
     
    281282      bestSelector.QualityParameter.ActualName = QualityParameter.Name;
    282283
    283       assigner2.Name = "Update LastPopulationSize";
    284       assigner2.LeftSideParameter.ActualName = "LastPopulationSize";
    285       assigner2.RightSideParameter.ActualName = "CurrentPopulationSize";
    286 
    287284      intCounter3.Name = "Increment Generations";
    288285      intCounter3.Increment = new IntValue(1);
     
    293290      subScopesCounter3.AccumulateParameter.Value = new BoolValue(false);
    294291
    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";
    302307
    303308      analyzer2.Name = "Analyzer";
     
    310315      comparator3.RightSideParameter.ActualName = MaximumGenerationsParameter.Name;
    311316
    312       conditionalBranch3.Name = "Terminate?";
    313       conditionalBranch3.ConditionParameter.ActualName = "Terminate";
     317      conditionalBranch4.Name = "Terminate?";
     318      conditionalBranch4.ConditionParameter.ActualName = "Terminate";
    314319
    315320      comparator4.Name = "CurrentPopulationSize < MinimumPopulationSize";
     
    319324      comparator4.ResultParameter.ActualName = "Terminate";
    320325
    321       conditionalBranch4.Name = "Terminate?";
    322       conditionalBranch4.ConditionParameter.ActualName = "Terminate";
     326      conditionalBranch5.Name = "Terminate?";
     327      conditionalBranch5.ConditionParameter.ActualName = "Terminate";
    323328
    324329      assigner3.Name = "Reset NumberOfCreatedOffspring";
     
    344349      subScopesProcessor1.Operators.Add(new EmptyOperator());
    345350      subScopesProcessor1.Operators.Add(childrenCreator);
    346       subScopesProcessor1.Successor = comparator1;
     351      subScopesProcessor1.Successor = calculator1;
    347352      childrenCreator.Successor = uniformSubScopesProcessor;
    348353      uniformSubScopesProcessor.Operator = crossover;
     
    362367      leftReducer1.Successor = progressiveOffspringSelector;
    363368      progressiveOffspringSelector.Successor = subScopesCounter2;
    364       comparator1.Successor = conditionalBranch1;
    365       conditionalBranch1.FalseBranch = comparator2;
     369      calculator1.Successor = conditionalBranch1;
     370      conditionalBranch1.FalseBranch = comparator1;
    366371      conditionalBranch1.TrueBranch = subScopesProcessor2;
    367       comparator2.Successor = conditionalBranch2;
     372      comparator1.Successor = conditionalBranch2;
    368373      conditionalBranch2.FalseBranch = leftReducer2;
    369374      conditionalBranch2.TrueBranch = subScopesProcessor2;
     
    374379      bestSelector.Successor = rightReducer2;
    375380      rightReducer2.Successor = null;
    376       scopeCleaner.Successor = offspringRestorer;
    377       mergingReducer.Successor = assigner2;
    378       assigner2.Successor = intCounter3;
     381      scopeCleaner.Successor = scopeRestorer;
     382      mergingReducer.Successor = intCounter3;
    379383      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;
    382389      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;
    389392      conditionalBranch4.TrueBranch = null;
    390393      conditionalBranch4.Successor = null;
     394      comparator4.Successor = conditionalBranch5;
     395      conditionalBranch5.FalseBranch = assigner3;
     396      conditionalBranch5.TrueBranch = null;
     397      conditionalBranch5.Successor = null;
    391398      assigner3.Successor = assigner4;
    392399      assigner4.Successor = assigner5;
  • branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/ScopeRestorer.cs

    r8615 r8622  
    3333  /// It adds all scopes in the list as sub-scopes to the current scope.
    3434  /// </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.")]
    3636  [StorableClass]
    37   public class OffspringRestorer : SingleSuccessorOperator {
     37  public class ScopeRestorer : SingleSuccessorOperator {
    3838    #region Parameter Properties
    3939    public ScopeParameter CurrentScopeParameter {
     
    5555
    5656    [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()
    6060      : base() {
    6161      Parameters.Add(new ScopeParameter("CurrentScope", "The current scope that contains the offspring."));
     
    6464
    6565    public override IDeepCloneable Clone(Cloner cloner) {
    66       return new OffspringRestorer(this, cloner);
     66      return new ScopeRestorer(this, cloner);
    6767    }
    6868
  • branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/SimilarityCalculator/NoSimilarityCalculator.cs

    r8615 r8622  
    3131  /// The item always considers two solutions to be distinct.
    3232  /// </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() { }
    3838
    3939    public override IDeepCloneable Clone(Cloner cloner) {
    40       return new BlindSimilarityCalculator(this, cloner);
     40      return new NoSimilarityCalculator(this, cloner);
    4141    }
    4242
  • branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/SimilarityCalculator/QualitySimilarityCalculator.cs

    r8406 r8622  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    4344
    4445    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);
    4650    }
    4751
Note: See TracChangeset for help on using the changeset viewer.