[3378] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14185] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[3378] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[4722] | 25 | using HeuristicLab.Common;
|
---|
[3378] | 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Optimization.Operators {
|
---|
[3413] | 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.")]
|
---|
[3378] | 34 | [StorableClass]
|
---|
[3413] | 35 | public class WeightedParentsQualityComparator : SingleSuccessorOperator, ISubScopesQualityComparator {
|
---|
[3378] | 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 ValueLookupParameter<DoubleValue> ComparisonFactorParameter {
|
---|
| 49 | get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
[4722] | 52 | [StorableConstructor]
|
---|
| 53 | protected WeightedParentsQualityComparator(bool deserializing) : base(deserializing) { }
|
---|
| 54 | protected WeightedParentsQualityComparator(WeightedParentsQualityComparator original, Cloner cloner) : base(original, cloner) { }
|
---|
[3413] | 55 | public WeightedParentsQualityComparator()
|
---|
[3378] | 56 | : base() {
|
---|
| 57 | Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem, false otherwise"));
|
---|
| 58 | Parameters.Add(new LookupParameter<DoubleValue>("LeftSide", "The quality of the child."));
|
---|
[3659] | 59 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("RightSide", "The qualities of the parents."));
|
---|
[3378] | 60 | Parameters.Add(new LookupParameter<BoolValue>("Result", "The result of the comparison: True means Quality is better, False means it is worse than parents."));
|
---|
| 61 | 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."));
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[4722] | 64 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 65 | return new WeightedParentsQualityComparator(this, cloner);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
[3378] | 68 | public override IOperation Apply() {
|
---|
| 69 | ItemArray<DoubleValue> rightQualities = RightSideParameter.ActualValue;
|
---|
| 70 | if (rightQualities.Length < 1) throw new InvalidOperationException(Name + ": No subscopes found.");
|
---|
| 71 | double compFact = ComparisonFactorParameter.ActualValue.Value;
|
---|
| 72 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
| 73 | double leftQuality = LeftSideParameter.ActualValue.Value;
|
---|
| 74 |
|
---|
| 75 | double threshold = 0;
|
---|
| 76 |
|
---|
| 77 | #region Calculate threshold
|
---|
| 78 | if (rightQualities.Length == 2) { // this case will probably be used most often
|
---|
[3868] | 79 | double minQuality = Math.Min(rightQualities[0].Value, rightQualities[1].Value);
|
---|
| 80 | double maxQuality = Math.Max(rightQualities[0].Value, rightQualities[1].Value);
|
---|
| 81 | if (maximization)
|
---|
| 82 | threshold = minQuality + (maxQuality - minQuality) * compFact;
|
---|
| 83 | else
|
---|
| 84 | threshold = maxQuality - (maxQuality - minQuality) * compFact;
|
---|
[3378] | 85 | } else if (rightQualities.Length == 1) { // case for just one parent
|
---|
| 86 | threshold = rightQualities[0].Value;
|
---|
| 87 | } else { // general case extended to 3 or more parents
|
---|
| 88 | List<double> sortedQualities = rightQualities.Select(x => x.Value).ToList();
|
---|
| 89 | sortedQualities.Sort();
|
---|
[3868] | 90 | double minimumQuality = sortedQualities.First();
|
---|
[3378] | 91 |
|
---|
[3868] | 92 | double integral = 0;
|
---|
[3378] | 93 | for (int i = 0; i < sortedQualities.Count - 1; i++) {
|
---|
[3868] | 94 | integral += (sortedQualities[i] + sortedQualities[i + 1]) / 2.0; // sum of the trapezoid
|
---|
[3378] | 95 | }
|
---|
[3868] | 96 | integral -= minimumQuality * sortedQualities.Count;
|
---|
| 97 | if (integral == 0) threshold = sortedQualities[0]; // all qualities are equal
|
---|
[3378] | 98 | else {
|
---|
[3868] | 99 | double selectedArea = integral * (maximization ? compFact : (1 - compFact));
|
---|
| 100 | integral = 0;
|
---|
[3378] | 101 | for (int i = 0; i < sortedQualities.Count - 1; i++) {
|
---|
[3868] | 102 | double currentSliceArea = (sortedQualities[i] + sortedQualities[i + 1]) / 2.0;
|
---|
| 103 | double windowedSliceArea = currentSliceArea - minimumQuality;
|
---|
| 104 | if (windowedSliceArea == 0) continue;
|
---|
| 105 | integral += windowedSliceArea;
|
---|
| 106 | if (integral >= selectedArea) {
|
---|
| 107 | double factor = 1 - ((integral - selectedArea) / (windowedSliceArea));
|
---|
[3378] | 108 | threshold = sortedQualities[i] + (sortedQualities[i + 1] - sortedQualities[i]) * factor;
|
---|
| 109 | break;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | #endregion
|
---|
| 115 |
|
---|
| 116 | bool result = maximization && leftQuality > threshold || !maximization && leftQuality < threshold;
|
---|
| 117 | BoolValue resultValue = ResultParameter.ActualValue;
|
---|
| 118 | if (resultValue == null) {
|
---|
| 119 | ResultParameter.ActualValue = new BoolValue(result);
|
---|
| 120 | } else {
|
---|
| 121 | resultValue.Value = result;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | return base.Apply();
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | }
|
---|