Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/29/15 16:00:38 (10 years ago)
Author:
ascheibe
Message:

#2267 added another offspring selector

Location:
branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm
Files:
2 edited
2 copied

Legend:

Unmodified
Added
Removed
  • branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/Comparators/EliteWeightedParentsQualityComparator.cs

    r12354 r12363  
    3131
    3232namespace HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm {
    33   [Item("WeightedParentsQualityComparator", "Compares the quality against that of its parents (assumes the parents are subscopes to the child scope). This operator works with any number of subscopes > 0.")]
     33  [Item("EliteWeightedParentsQualityComparator", "M2")]
    3434  [StorableClass]
    35   public class WeightedParentsQualityComparator : SingleSuccessorOperator, ISubScopesQualityComparatorOperator {
     35  public class EliteWeightedParentsQualityComparator : SingleSuccessorOperator, ISubScopesQualityComparatorOperator {
    3636    public IValueLookupParameter<BoolValue> MaximizationParameter {
    3737      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
     
    4646      get { return (ILookupParameter<BoolValue>)Parameters["Result"]; }
    4747    }
     48    public ILookupParameter<BoolValue> ResultImprovementParameter {
     49      get { return (ILookupParameter<BoolValue>)Parameters["ResultImprovement"]; }
     50    }
    4851    public ValueLookupParameter<DoubleValue> ComparisonFactorParameter {
    4952      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
     
    5154
    5255    [StorableConstructor]
    53     protected WeightedParentsQualityComparator(bool deserializing) : base(deserializing) { }
    54     protected WeightedParentsQualityComparator(WeightedParentsQualityComparator original, Cloner cloner) : base(original, cloner) { }
    55     public WeightedParentsQualityComparator()
     56    protected EliteWeightedParentsQualityComparator(bool deserializing) : base(deserializing) { }
     57    protected EliteWeightedParentsQualityComparator(EliteWeightedParentsQualityComparator original, Cloner cloner) : base(original, cloner) { }
     58    public EliteWeightedParentsQualityComparator()
    5659      : base() {
    5760      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
     
    5962      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("RightSide", "The qualities of the parents."));
    6063      Parameters.Add(new LookupParameter<BoolValue>("Result", "The result of the comparison: True means Quality is better, False means it is worse than parents."));
     64      Parameters.Add(new LookupParameter<BoolValue>("ResultImprovement", "A solution has improved if it is better than the worse parent."));
    6165      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactor", "Determines if the quality should be compared to the better parent (1.0), to the worse (0.0) or to any linearly interpolated value between them."));
    6266    }
    6367
    6468    public override IDeepCloneable Clone(Cloner cloner) {
    65       return new WeightedParentsQualityComparator(this, cloner);
     69      return new EliteWeightedParentsQualityComparator(this, cloner);
    6670    }
    6771
     
    122126      }
    123127
     128      bool resultImprovement = maximization && leftQuality > rightQualities.Min(x => x.Value) ||
     129                              !maximization && leftQuality < rightQualities.Max(x => x.Value);
     130      BoolValue resultImprovementValue = ResultImprovementParameter.ActualValue;
     131      if (resultImprovementValue == null) {
     132        ResultImprovementParameter.ActualValue = new BoolValue(resultImprovement);
     133      } else {
     134        resultImprovementValue.Value = resultImprovement;
     135      }
     136
    124137      return base.Apply();
    125138    }
  • branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm-3.3.csproj

    r12350 r12363  
    176176    <Compile Include="Comparators\UnwantedMutationsComparator.cs" />
    177177    <Compile Include="Comparators\WeightedParentsDiversityComparator.cs" />
     178    <Compile Include="Comparators\EliteWeightedParentsQualityComparator.cs" />
    178179    <Compile Include="Comparators\WeightedParentsQualityComparator.cs" />
    179180    <Compile Include="ISubScopesQualityComparatorOperator.cs" />
    180181    <Compile Include="OffspringCollector.cs" />
    181182    <Compile Include="OffspringSelectors\IOffspringSelector.cs" />
     183    <Compile Include="OffspringSelectors\EliteOffspringSelector.cs" />
    182184    <Compile Include="OffspringSelectors\PopDivOffspringSelector.cs" />
    183185    <Compile Include="OffspringSelectors\StandardOffspringSelector.cs" />
  • branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/OffspringSelectors/EliteOffspringSelector.cs

    r12354 r12363  
    3434
    3535namespace HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm {
    36   [Item("PopDivOffspringSelector", "")]
     36  [Item("EliteOffspringSelector", "M2")]
    3737  [StorableClass]
    38   public class PopDivOffspringSelector : InstrumentedOperator, IOffspringSelector {
     38  public class EliteOffspringSelector : InstrumentedOperator, IOffspringSelector {
    3939
    4040    private const string SuccessRatioChart = "SuccessRatioChart";
     
    9191
    9292    [StorableConstructor]
    93     protected PopDivOffspringSelector(bool deserializing) : base(deserializing) { }
    94     protected PopDivOffspringSelector(PopDivOffspringSelector original, Cloner cloner)
     93    protected EliteOffspringSelector(bool deserializing) : base(deserializing) { }
     94    protected EliteOffspringSelector(EliteOffspringSelector original, Cloner cloner)
    9595      : base(original, cloner) {
    9696    }
    9797    public override IDeepCloneable Clone(Cloner cloner) {
    98       return new PopDivOffspringSelector(this, cloner);
    99     }
    100     public PopDivOffspringSelector()
     98      return new EliteOffspringSelector(this, cloner);
     99    }
     100    public EliteOffspringSelector()
    101101      : base() {
    102102      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure which prematurely terminates the offspring selection step."));
     
    192192      int worseOffspringNeeded = (int)((1 - successRatio) * populationSize) - (population.Count - successfulOffspring.Value);
    193193      int successfulOffspringAdded = 0;
    194 
    195194      double avgPopDiv = DiversityComparisonFactorParameter.Value.Value;
    196       bool takeWorseOffspring = false;
    197195
    198196      // implement the ActualValue fetch here - otherwise the parent scope would also be included, given that there may be 1000 or more parents, this is quite unnecessary
    199197      string tname = SuccessfulOffspringParameter.TranslatedName;
    200198      double tmpSelPress = selectionPressure.Value, tmpSelPressInc = 1.0 / populationSize;
    201       int cnt = 0;
    202       int stepWidth = offspring.SubScopes.Count / 5;
    203199      for (int i = 0; i < offspring.SubScopes.Count; i++) {
    204         //calculate population diversity so far
    205         if (cnt != 0 && cnt % stepWidth == 0) {
    206           Scope tmpScope = new Scope();
    207           tmpScope.SubScopes.AddRange(population);
    208           double popDiversity = SimilarityCalculatorParameter.ActualValue.CalculateSolutionCrowdSimilarity(tmpScope).Average(x => x.Average());
    209           //this assumes that low-quality solutions are of high diversity
    210           if (popDiversity > avgPopDiv) {
    211             takeWorseOffspring = true;
    212           } else {
    213             takeWorseOffspring = false;
    214           }
    215         }
    216         cnt++;
    217 
    218200        // fetch value
    219201        IVariable tmpVar;
     
    222204        if (tmp == null) throw new InvalidOperationException(Name + ": The variable that indicates whether an offspring is successful or not must contain a BoolValue.");
    223205
    224         if (takeWorseOffspring) {
    225           IScope currentOffspring = offspring.SubScopes[i];
    226           Scope tmpScope = new Scope();
    227           tmpScope.SubScopes.AddRange(population);
    228           Scope tmpCurrentScope = new Scope();
    229           tmpCurrentScope.SubScopes.Add(currentOffspring);
    230           double curDiv = SimilarityCalculatorParameter.ActualValue.CalculateSolutionCrowdSimilarity(tmpCurrentScope, tmpScope)[0].Average();
    231           if (curDiv < avgPopDiv) {
    232             offspring.SubScopes.Remove(currentOffspring);
    233             i--;
    234             population.Add(currentOffspring);
    235             worseOffspringNeeded--;
    236           }
    237         } else if (tmp.Value) {
     206        if (!offspring.SubScopes[i].Variables.TryGetValue("ResultImprovement", out tmpVar)) throw new InvalidOperationException(Name + ": Could not find ResultImprovment.");
     207        BoolValue betterThanWorseParent = (tmpVar.Value as BoolValue);
     208        if (betterThanWorseParent == null) throw new InvalidOperationException(Name + ": Could not find ResultImprovment.");
     209
     210        //this is an "elite" solution candidate
     211        double tmpCurSuccRatio = (successfulOffspring.Value + successfulOffspringAdded) / (double)populationSize;
     212        if (tmp.Value && tmpCurSuccRatio <= successRatio) {
     213          //this overrides the diversity mechanism; always fill up elites if there is space
    238214          IScope currentOffspring = offspring.SubScopes[i];
    239215          offspring.SubScopes.Remove(currentOffspring);
     
    241217          population.Add(currentOffspring);
    242218          successfulOffspringAdded++;
    243         } else if (worseOffspringNeeded > 0 || tmpSelPress >= maxSelPress) {
    244           IScope currentOffspring;
    245           if (!fillPopulationWithParents || worseOffspringNeeded > 0) {
    246             currentOffspring = offspring.SubScopes[i];
     219        } else if (!tmp.Value && betterThanWorseParent.Value) {
     220          //quality is ok, check for diversity
     221          IScope currentOffspring = offspring.SubScopes[i];
     222
     223          if (!population.Any()) {
    247224            offspring.SubScopes.Remove(currentOffspring);
    248225            i--;
    249226            worseOffspringNeeded--;
     227            population.Add(currentOffspring);
    250228          } else {
    251             currentOffspring = parents.SubScopes[i];
     229            Scope tmpScope = new Scope();
     230            tmpScope.SubScopes.AddRange(population);
     231            Scope tmpCurrentScope = new Scope();
     232            tmpCurrentScope.SubScopes.Add(currentOffspring);
     233            double curDiv = SimilarityCalculatorParameter.ActualValue.CalculateSolutionCrowdSimilarity(tmpCurrentScope, tmpScope)[0].Average();
     234            if (curDiv < avgPopDiv) {
     235              if (worseOffspringNeeded > 0 || tmpSelPress >= maxSelPress) {
     236                if (!fillPopulationWithParents || worseOffspringNeeded > 0) {
     237                  offspring.SubScopes.Remove(currentOffspring);
     238                  i--;
     239                  worseOffspringNeeded--;
     240                } else {
     241                  currentOffspring = parents.SubScopes[i];
     242                }
     243                population.Add(currentOffspring);
     244              }
     245            }
    252246          }
    253           population.Add(currentOffspring);
    254247        }
     248
    255249        tmpSelPress += tmpSelPressInc;
    256250        if (population.Count >= populationSize) break;
  • branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/OffspringSelectors/PopDivOffspringSelector.cs

    r12352 r12363  
    3434
    3535namespace HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm {
    36   [Item("PopDivOffspringSelector", "")]
     36  [Item("PopDivOffspringSelector", "M1")]
    3737  [StorableClass]
    3838  public class PopDivOffspringSelector : InstrumentedOperator, IOffspringSelector {
Note: See TracChangeset for help on using the changeset viewer.