Changeset 12350


Ignore:
Timestamp:
04/28/15 14:37:20 (4 years ago)
Author:
ascheibe
Message:

#2267 added a new offspring selector

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

Legend:

Unmodified
Added
Removed
  • branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm-3.3.csproj

    r11884 r12350  
    180180    <Compile Include="OffspringCollector.cs" />
    181181    <Compile Include="OffspringSelectors\IOffspringSelector.cs" />
     182    <Compile Include="OffspringSelectors\PopDivOffspringSelector.cs" />
    182183    <Compile Include="OffspringSelectors\StandardOffspringSelector.cs" />
    183184    <Compile Include="ProbabilitiesGenerator.cs" />
  • branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/OffspringSelectors/PopDivOffspringSelector.cs

    r12347 r12350  
    2121
    2222using System;
     23using System.Linq;
     24using HeuristicLab.Analysis;
    2325using HeuristicLab.Common;
    2426using HeuristicLab.Core;
    2527using HeuristicLab.Data;
    2628using HeuristicLab.Operators;
     29using HeuristicLab.Optimization;
     30using HeuristicLab.Optimization.Operators;
    2731using HeuristicLab.Parameters;
    2832using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     33using HeuristicLab.PluginInfrastructure;
    2934
    3035namespace HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm {
    31   [Item("StandardOffspringSelector", "Selects among the offspring population those that are designated successful and discards the unsuccessful offspring, except for some lucky losers. It expects the parent scopes to be below the first sub-scope, and offspring scopes to be below the second sub-scope separated again in two sub-scopes, the first with the failed offspring and the second with successful offspring.")]
     36  [Item("PopDivOffspringSelector", "")]
    3237  [StorableClass]
    33   public class StandardOffspringSelector : InstrumentedOperator, IOffspringSelector {
     38  public class PopDivOffspringSelector : InstrumentedOperator, IOffspringSelector {
     39
     40    private const string SuccessRatioChart = "SuccessRatioChart";
     41    private const string SuccessRatioDataRow = "SuccessRatio";
     42
     43
    3444    public ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
    3545      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
     
    5969      get { return (ILookupParameter<BoolValue>)Parameters["EnoughChildrenGenerated"]; }
    6070    }
    61 
     71    public IValueLookupParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter {
     72      get { return (IValueLookupParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
     73    }
     74    public ValueLookupParameter<ResultCollection> ResultsParameter {
     75      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
     76    }
     77
     78    public ValueLookupParameter<DoubleValue> DiversityComparisonFactorParameter {
     79      get { return (ValueLookupParameter<DoubleValue>)Parameters["DiversityComparisonFactor"]; }
     80    }
     81    private ValueLookupParameter<DoubleValue> ComparisonFactorLowerBoundParameter {
     82      get { return (ValueLookupParameter<DoubleValue>)Parameters["DiversityComparisonFactorLowerBound"]; }
     83    }
     84    private ValueLookupParameter<DoubleValue> ComparisonFactorUpperBoundParameter {
     85      get { return (ValueLookupParameter<DoubleValue>)Parameters["DiversityComparisonFactorUpperBound"]; }
     86    }
     87
     88    public IConstrainedValueParameter<IDiscreteDoubleValueModifier> ComparisonFactorModifierParameter {
     89      get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["ComparisonFactorModifier"]; }
     90    }
    6291
    6392    [StorableConstructor]
    64     protected StandardOffspringSelector(bool deserializing) : base(deserializing) { }
    65     [StorableHook(HookType.AfterDeserialization)]
    66     private void AfterDeserialization() {
    67       // BackwardsCompatibility3.3
    68       #region Backwards compatible code, remove with 3.4
    69       if (Parameters.ContainsKey("FillPopulationWithParents") && Parameters["FillPopulationWithParents"] is FixedValueParameter<BoolValue>)
    70         Parameters.Remove("FillPopulationWithParents");
    71       if (!Parameters.ContainsKey("FillPopulationWithParents"))
    72         Parameters.Add(new ValueLookupParameter<BoolValue>("FillPopulationWithParents", "True if the population should be filled with parent individuals instead of lucky losers."));
    73       #endregion
    74     }
    75 
    76     protected StandardOffspringSelector(StandardOffspringSelector original, Cloner cloner) : base(original, cloner) { }
     93    protected PopDivOffspringSelector(bool deserializing) : base(deserializing) { }
     94    protected PopDivOffspringSelector(PopDivOffspringSelector original, Cloner cloner)
     95      : base(original, cloner) {
     96    }
    7797    public override IDeepCloneable Clone(Cloner cloner) {
    78       return new StandardOffspringSelector(this, cloner);
    79     }
    80     public StandardOffspringSelector()
     98      return new PopDivOffspringSelector(this, cloner);
     99    }
     100    public PopDivOffspringSelector()
    81101      : base() {
    82102      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure which prematurely terminates the offspring selection step."));
     
    89109      Parameters.Add(new ValueLookupParameter<BoolValue>("FillPopulationWithParents", "True if the population should be filled with parent individual or false if worse children should be used when the maximum selection pressure is exceeded."));
    90110      Parameters.Add(new LookupParameter<BoolValue>("EnoughChildrenGenerated", "True if enough children have been generated, otherwise false."));
     111      Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
     112      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the population diversity analysis results should be stored."));
     113
     114      Parameters.Add(new ValueLookupParameter<DoubleValue>("DiversityComparisonFactor", "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.", new DoubleValue(0.0)));
     115      Parameters.Add(new ValueLookupParameter<DoubleValue>("DiversityComparisonFactorLowerBound", "The lower bound of the comparison factor (start).", new DoubleValue(0.5)));
     116      Parameters.Add(new ValueLookupParameter<DoubleValue>("DiversityComparisonFactorUpperBound", "The upper bound of the comparison factor (end).", new DoubleValue(1.0)));
     117      Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("ComparisonFactorModifier", "The operator used to modify the comparison factor.", new ItemSet<IDiscreteDoubleValueModifier>(new IDiscreteDoubleValueModifier[] { new LinearDiscreteDoubleValueModifier() }), new LinearDiscreteDoubleValueModifier()));
     118
     119
     120      foreach (IDiscreteDoubleValueModifier modifier in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name))
     121        ComparisonFactorModifierParameter.ValidValues.Add(modifier);
     122      IDiscreteDoubleValueModifier linearModifier = ComparisonFactorModifierParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("LinearDiscreteDoubleValueModifier"));
     123      if (linearModifier != null) ComparisonFactorModifierParameter.Value = linearModifier;
     124      ParameterizeComparisonFactorModifiers();
     125
     126      this.AfterExecutionOperators.Clear();
     127      this.AfterExecutionOperators.Add(ComparisonFactorModifierParameter.Value);
     128      ComparisonFactorModifierParameter.ValueChanged += ComparisonFactorModifierParameter_ValueChanged;
     129    }
     130
     131    void ComparisonFactorModifierParameter_ValueChanged(object sender, EventArgs e) {
     132      this.AfterExecutionOperators.Clear();
     133      this.AfterExecutionOperators.Add(ComparisonFactorModifierParameter.Value);
     134    }
     135
     136    private void ParameterizeComparisonFactorModifiers() {
     137      //TODO: does not work if Generations parameter names are changed
     138      foreach (IDiscreteDoubleValueModifier modifier in ComparisonFactorModifierParameter.ValidValues) {
     139        modifier.IndexParameter.ActualName = "Generations";
     140        modifier.EndIndexParameter.ActualName = "MaximumGenerations";
     141        modifier.EndValueParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
     142        modifier.StartIndexParameter.Value = new IntValue(0);
     143        modifier.StartValueParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
     144        modifier.ValueParameter.ActualName = "DiversityComparisonFactor";
     145      }
    91146    }
    92147
     
    112167        currentSuccessRatio = new DoubleValue(0);
    113168        CurrentSuccessRatioParameter.ActualValue = currentSuccessRatio;
     169      }
     170
     171      DataTable successRatioDataTable;
     172      if (ResultsParameter.ActualValue.ContainsKey(SuccessRatioChart)) {
     173        successRatioDataTable = (DataTable)ResultsParameter.ActualValue[SuccessRatioChart].Value;
     174      } else {
     175        successRatioDataTable = new DataTable(SuccessRatioChart);
     176        successRatioDataTable.Rows.Add(new DataRow(SuccessRatioDataRow));
     177        ResultsParameter.ActualValue.Add(new Result(SuccessRatioChart, successRatioDataTable));
    114178      }
    115179
     
    129193      int successfulOffspringAdded = 0;
    130194
     195      double avgPopDiv = DiversityComparisonFactorParameter.Value.Value;
     196      bool takeWorseOffspring = false;
     197
    131198      // 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
    132199      string tname = SuccessfulOffspringParameter.TranslatedName;
    133200      double tmpSelPress = selectionPressure.Value, tmpSelPressInc = 1.0 / populationSize;
     201      int cnt = 0;
     202      int stepWidth = offspring.SubScopes.Count / 5;
    134203      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
    135218        // fetch value
    136219        IVariable tmpVar;
     
    139222        if (tmp == null) throw new InvalidOperationException(Name + ": The variable that indicates whether an offspring is successful or not must contain a BoolValue.");
    140223
    141         // add to population
    142         if (tmp.Value) {
     224        if (takeWorseOffspring) {
     225          IScope currentOffspring = offspring.SubScopes[i];
     226          offspring.SubScopes.Remove(currentOffspring);
     227          i--;
     228          population.Add(currentOffspring);
     229          worseOffspringNeeded--;
     230        } else if (tmp.Value) {
    143231          IScope currentOffspring = offspring.SubScopes[i];
    144232          offspring.SubScopes.Remove(currentOffspring);
     
    159247        }
    160248        tmpSelPress += tmpSelPressInc;
    161         if (population.Count == populationSize) break;
     249        if (population.Count >= populationSize) break;
    162250      }
    163251      successfulOffspring.Value += successfulOffspringAdded;
     
    166254      selectionPressure.Value = tmpSelPress;
    167255      currentSuccessRatio.Value = successfulOffspring.Value / ((double)populationSize);
    168 
     256      successRatioDataTable.Rows[SuccessRatioDataRow].Values.Add(currentSuccessRatio.Value);
    169257
    170258      if (EnoughChildrenGeneratedParameter.ActualValue == null) {
     
    192280        EnoughChildrenGeneratedParameter.ActualValue.Value = true;
    193281      }
     282
    194283      return base.InstrumentedApply();
    195284    }
Note: See TracChangeset for help on using the changeset viewer.