Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/Comparators/EliteWeightedParentsQualityComparator.cs @ 12363

Last change on this file since 12363 was 12363, checked in by ascheibe, 9 years ago

#2267 added another offspring selector

File size: 6.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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm {
33  [Item("EliteWeightedParentsQualityComparator", "M2")]
34  [StorableClass]
35  public class EliteWeightedParentsQualityComparator : SingleSuccessorOperator, ISubScopesQualityComparatorOperator {
36    public IValueLookupParameter<BoolValue> MaximizationParameter {
37      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
38    }
39    public ILookupParameter<DoubleValue> LeftSideParameter {
40      get { return (ILookupParameter<DoubleValue>)Parameters["LeftSide"]; }
41    }
42    public ILookupParameter<ItemArray<DoubleValue>> RightSideParameter {
43      get { return (ILookupParameter<ItemArray<DoubleValue>>)Parameters["RightSide"]; }
44    }
45    public ILookupParameter<BoolValue> ResultParameter {
46      get { return (ILookupParameter<BoolValue>)Parameters["Result"]; }
47    }
48    public ILookupParameter<BoolValue> ResultImprovementParameter {
49      get { return (ILookupParameter<BoolValue>)Parameters["ResultImprovement"]; }
50    }
51    public ValueLookupParameter<DoubleValue> ComparisonFactorParameter {
52      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
53    }
54
55    [StorableConstructor]
56    protected EliteWeightedParentsQualityComparator(bool deserializing) : base(deserializing) { }
57    protected EliteWeightedParentsQualityComparator(EliteWeightedParentsQualityComparator original, Cloner cloner) : base(original, cloner) { }
58    public EliteWeightedParentsQualityComparator()
59      : base() {
60      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
61      Parameters.Add(new LookupParameter<DoubleValue>("LeftSide", "The quality of the child."));
62      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("RightSide", "The qualities of the parents."));
63      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."));
65      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."));
66    }
67
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new EliteWeightedParentsQualityComparator(this, cloner);
70    }
71
72    public override IOperation Apply() {
73      ItemArray<DoubleValue> rightQualities = RightSideParameter.ActualValue;
74      if (rightQualities.Length < 1) throw new InvalidOperationException(Name + ": No subscopes found.");
75      double compFact = ComparisonFactorParameter.ActualValue.Value;
76      bool maximization = MaximizationParameter.ActualValue.Value;
77      double leftQuality = LeftSideParameter.ActualValue.Value;
78
79      double threshold = 0;
80
81      #region Calculate threshold
82      if (rightQualities.Length == 2) { // this case will probably be used most often
83        double minQuality = Math.Min(rightQualities[0].Value, rightQualities[1].Value);
84        double maxQuality = Math.Max(rightQualities[0].Value, rightQualities[1].Value);
85        if (maximization)
86          threshold = minQuality + (maxQuality - minQuality) * compFact;
87        else
88          threshold = maxQuality - (maxQuality - minQuality) * compFact;
89      } else if (rightQualities.Length == 1) { // case for just one parent
90        threshold = rightQualities[0].Value;
91      } else { // general case extended to 3 or more parents
92        List<double> sortedQualities = rightQualities.Select(x => x.Value).ToList();
93        sortedQualities.Sort();
94        double minimumQuality = sortedQualities.First();
95
96        double integral = 0;
97        for (int i = 0; i < sortedQualities.Count - 1; i++) {
98          integral += (sortedQualities[i] + sortedQualities[i + 1]) / 2.0; // sum of the trapezoid
99        }
100        integral -= minimumQuality * sortedQualities.Count;
101        if (integral == 0) threshold = sortedQualities[0]; // all qualities are equal
102        else {
103          double selectedArea = integral * (maximization ? compFact : (1 - compFact));
104          integral = 0;
105          for (int i = 0; i < sortedQualities.Count - 1; i++) {
106            double currentSliceArea = (sortedQualities[i] + sortedQualities[i + 1]) / 2.0;
107            double windowedSliceArea = currentSliceArea - minimumQuality;
108            if (windowedSliceArea == 0) continue;
109            integral += windowedSliceArea;
110            if (integral >= selectedArea) {
111              double factor = 1 - ((integral - selectedArea) / (windowedSliceArea));
112              threshold = sortedQualities[i] + (sortedQualities[i + 1] - sortedQualities[i]) * factor;
113              break;
114            }
115          }
116        }
117      }
118      #endregion
119
120      bool result = maximization && leftQuality > threshold || !maximization && leftQuality < threshold;
121      BoolValue resultValue = ResultParameter.ActualValue;
122      if (resultValue == null) {
123        ResultParameter.ActualValue = new BoolValue(result);
124      } else {
125        resultValue.Value = result;
126      }
127
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
137      return base.Apply();
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.