Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/Comparators/PopulationDiversityComparator.cs @ 12966

Last change on this file since 12966 was 12079, checked in by ascheibe, 10 years ago

#2267 adapted branch to changes from #2332

File size: 13.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm {
36  [Item("PopulationDiversityComparator", "Structural comparison of child to population.")]
37  [StorableClass]
38  public class PopulationDiversityComparator : SingleSuccessorOperator, ISubScopesQualityComparatorOperator {
39    [Storable]
40    public ISolutionSimilarityCalculator SimilarityCalculator { get; set; }
41    public IValueLookupParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter {
42      get { return (IValueLookupParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
43    }
44    public IValueLookupParameter<BoolValue> MaximizationParameter {
45      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
46    }
47    public ILookupParameter<DoubleValue> LeftSideParameter {
48      get { return (ILookupParameter<DoubleValue>)Parameters["LeftSide"]; }
49    }
50    public ILookupParameter<ItemArray<DoubleValue>> RightSideParameter {
51      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["RightSide"]; }
52    }
53    public ILookupParameter<BoolValue> ResultParameter {
54      get { return (ILookupParameter<BoolValue>)Parameters["Result"]; }
55    }
56    public ValueLookupParameter<DoubleValue> ComparisonFactorParameter {
57      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
58    }
59    public ValueLookupParameter<DoubleValue> DiversityComparisonFactorParameter {
60      get { return (ValueLookupParameter<DoubleValue>)Parameters["DiversityComparisonFactor"]; }
61    }
62    private ValueLookupParameter<DoubleValue> ComparisonFactorLowerBoundParameter {
63      get { return (ValueLookupParameter<DoubleValue>)Parameters["DiversityComparisonFactorLowerBound"]; }
64    }
65    private ValueLookupParameter<DoubleValue> ComparisonFactorUpperBoundParameter {
66      get { return (ValueLookupParameter<DoubleValue>)Parameters["DiversityComparisonFactorUpperBound"]; }
67    }
68    public IConstrainedValueParameter<IDiscreteDoubleValueModifier> ComparisonFactorModifierParameter {
69      get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["ComparisonFactorModifier"]; }
70    }
71    public ValueLookupParameter<ResultCollection> ResultsParameter {
72      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
73    }
74    public ILookupParameter<IntValue> GenerationsParameter {
75      get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
76    }
77    public ValueParameter<BoolValue> EnableDivCriteriaParameter {
78      get { return (ValueParameter<BoolValue>)Parameters["EnableDivCriteria"]; }
79    }
80
81    private const string spDetailsParameterName = "SPDetails";
82    private const string divDataRowName = "DiversitySuccessCount";
83    private const string qualityDataRowName = "QualitySuccessCount";
84    private const string divFailDataRowName = "DiversityFailCount";
85    private const string qualityFailDataRowName = "QualityFailCount";
86    private const string overallCountDataRowName = "OverallCount";
87    private const string successCountDataRowName = "SuccessCount";
88
89    [Storable]
90    private int currentGeneration;
91    [Storable]
92    private int divCount;
93    [Storable]
94    private int qualityCount;
95    [Storable]
96    private int badQualityCount;
97    [Storable]
98    private int badDivCount;
99    [Storable]
100    private int overallCount;
101    [Storable]
102    private int successCount;
103
104    [StorableConstructor]
105    protected PopulationDiversityComparator(bool deserializing) : base(deserializing) { }
106    protected PopulationDiversityComparator(PopulationDiversityComparator original, Cloner cloner)
107      : base(original, cloner) {
108      SimilarityCalculator = cloner.Clone(original.SimilarityCalculator);
109      currentGeneration = original.currentGeneration;
110      divCount = original.divCount;
111      qualityCount = original.qualityCount;
112      overallCount = original.overallCount;
113      successCount = original.successCount;
114      badDivCount = original.badDivCount;
115      badQualityCount = original.badQualityCount;
116    }
117    public PopulationDiversityComparator()
118      : base() {
119      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
120      Parameters.Add(new LookupParameter<DoubleValue>("LeftSide", "The quality of the child."));
121      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("RightSide", "The qualities of the parents."));
122      Parameters.Add(new LookupParameter<BoolValue>("Result", "The result of the comparison: True means Quality is better, False means it is worse than parents."));
123      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."));
124      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)));
125      Parameters.Add(new ValueLookupParameter<DoubleValue>("DiversityComparisonFactorLowerBound", "The lower bound of the comparison factor (start).", new DoubleValue(0.5)));
126      Parameters.Add(new ValueLookupParameter<DoubleValue>("DiversityComparisonFactorUpperBound", "The upper bound of the comparison factor (end).", new DoubleValue(1.0)));
127      Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("ComparisonFactorModifier", "The operator used to modify the comparison factor.", new ItemSet<IDiscreteDoubleValueModifier>(new IDiscreteDoubleValueModifier[] { new LinearDiscreteDoubleValueModifier() }), new LinearDiscreteDoubleValueModifier()));
128      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the population diversity analysis results should be stored."));
129      Parameters.Add(new LookupParameter<IntValue>("Generations", "The current number of generations."));
130      Parameters.Add(new ValueParameter<BoolValue>("EnableDivCriteria", "Use diversity as additional offspring selection criteria.", new BoolValue(true)));
131      Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
132
133      foreach (IDiscreteDoubleValueModifier modifier in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name))
134        ComparisonFactorModifierParameter.ValidValues.Add(modifier);
135      IDiscreteDoubleValueModifier linearModifier = ComparisonFactorModifierParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("LinearDiscreteDoubleValueModifier"));
136      if (linearModifier != null) ComparisonFactorModifierParameter.Value = linearModifier;
137      ParameterizeComparisonFactorModifiers();
138    }
139
140    public override IDeepCloneable Clone(Cloner cloner) {
141      return new PopulationDiversityComparator(this, cloner);
142    }
143
144    [StorableHook(HookType.AfterDeserialization)]
145    private void AfterDeserialization() {
146      // BackwardsCompatibility3.3
147      #region Backwards compatible code, remove with 3.4
148      if (!Parameters.ContainsKey("SimilarityCalculator"))
149        Parameters.Add(new ValueLookupParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The similarity calculator that should be used to calculate solution similarity."));
150      #endregion
151    }
152
153    private void ParameterizeComparisonFactorModifiers() {
154      //TODO: does not work if Generations parameter names are changed
155      foreach (IDiscreteDoubleValueModifier modifier in ComparisonFactorModifierParameter.ValidValues) {
156        modifier.IndexParameter.ActualName = "Generations";
157        modifier.EndIndexParameter.ActualName = "MaximumGenerations";
158        modifier.EndValueParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
159        modifier.StartIndexParameter.Value = new IntValue(0);
160        modifier.StartValueParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
161        modifier.ValueParameter.ActualName = "DiversityComparisonFactor";
162      }
163    }
164
165    private IScope GetRemainingScope() {
166      var scope = ExecutionContext.Scope;
167
168      while (scope != null) {
169        if (scope.SubScopes.Any(x => x.Name == "Remaining")) {
170          return scope.SubScopes.Single(x => x.Name == "Remaining");
171        }
172        scope = scope.Parent;
173      }
174      return null;
175    }
176
177    public override IOperation Apply() {
178      ItemArray<DoubleValue> rightQualities = RightSideParameter.ActualValue;
179      if (rightQualities.Length < 1) throw new InvalidOperationException(Name + ": No subscopes found.");
180      double compFact = ComparisonFactorParameter.ActualValue.Value;
181      double diversityComFact = DiversityComparisonFactorParameter.ActualValue.Value;
182      bool maximization = MaximizationParameter.ActualValue.Value;
183      double leftQuality = LeftSideParameter.ActualValue.Value;
184      bool resultDiversity;
185      double threshold = 0;
186
187      DataTable spDetailsTable;
188      if (ResultsParameter.ActualValue.ContainsKey(spDetailsParameterName)) {
189        spDetailsTable = (DataTable)ResultsParameter.ActualValue[spDetailsParameterName].Value;
190      } else {
191        spDetailsTable = new DataTable(spDetailsParameterName);
192        spDetailsTable.Rows.Add(new DataRow(divDataRowName));
193        spDetailsTable.Rows.Add(new DataRow(qualityDataRowName));
194        spDetailsTable.Rows.Add(new DataRow(overallCountDataRowName));
195        spDetailsTable.Rows.Add(new DataRow(successCountDataRowName));
196        spDetailsTable.Rows.Add(new DataRow(divFailDataRowName));
197        spDetailsTable.Rows.Add(new DataRow(qualityFailDataRowName));
198        ResultsParameter.ActualValue.Add(new Result(spDetailsParameterName, spDetailsTable));
199      }
200
201      if (GenerationsParameter.ActualValue.Value != currentGeneration) {
202        spDetailsTable.Rows[divDataRowName].Values.Add(divCount);
203        divCount = 0;
204        spDetailsTable.Rows[qualityDataRowName].Values.Add(qualityCount);
205        qualityCount = 0;
206        spDetailsTable.Rows[overallCountDataRowName].Values.Add(overallCount);
207        overallCount = 0;
208        spDetailsTable.Rows[successCountDataRowName].Values.Add(successCount);
209        successCount = 0;
210        spDetailsTable.Rows[qualityFailDataRowName].Values.Add(badQualityCount);
211        badQualityCount = 0;
212        spDetailsTable.Rows[divFailDataRowName].Values.Add(badDivCount);
213        badDivCount = 0;
214        currentGeneration = GenerationsParameter.ActualValue.Value;
215      }
216
217      #region Calculate threshold
218      if (rightQualities.Length == 2) {
219        var remainingScope = GetRemainingScope();
220        Scope fakeScope = new Scope();
221        fakeScope.SubScopes.Add((IScope)ExecutionContext.Scope.Clone());
222        var similarities = SimilarityCalculatorParameter.ActualValue.CalculateSolutionCrowdSimilarity(fakeScope, remainingScope);
223        double averageSimilarity = similarities[0].Average();
224        resultDiversity = averageSimilarity < diversityComFact;
225
226        double minQuality = Math.Min(rightQualities[0].Value, rightQualities[1].Value);
227        double maxQuality = Math.Max(rightQualities[0].Value, rightQualities[1].Value);
228        if (maximization)
229          threshold = minQuality + (maxQuality - minQuality) * compFact;
230        else
231          threshold = maxQuality - (maxQuality - minQuality) * compFact;
232      } else {
233        throw new NotImplementedException("Only 2 parents are supported.");
234      }
235      #endregion
236
237      bool result = maximization && leftQuality > threshold || !maximization && leftQuality < threshold;
238
239      //collect statistics
240      if (result) {
241        qualityCount++;
242      } else {
243        badQualityCount++;
244      }
245      if (resultDiversity) {
246        divCount++;
247      } else {
248        badDivCount++;
249      }
250      if (result && resultDiversity) {
251        successCount++;
252      }
253      overallCount++;
254
255      //use diveristiy criteria or not
256      if (EnableDivCriteriaParameter.Value.Value) {
257        result = result && resultDiversity;
258      }
259
260      BoolValue resultValue = ResultParameter.ActualValue;
261      if (resultValue == null) {
262        ResultParameter.ActualValue = new BoolValue(result);
263      } else {
264        resultValue.Value = result;
265      }
266
267      //like the placeholder, though we create child operations
268      OperationCollection next = new OperationCollection(base.Apply());
269      IOperator op = ComparisonFactorModifierParameter.Value;
270      if (op != null)
271        next.Insert(0, ExecutionContext.CreateChildOperation(op));
272      return next;
273    }
274  }
275}
Note: See TracBrowser for help on using the repository browser.