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