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("PopulationQualityComparator", "Compares the quality of the child to the population.")]
|
---|
37 | [StorableClass]
|
---|
38 | public class PopulationQualityComparator : 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 PopulationQualityComparator(bool deserializing) : base(deserializing) { }
|
---|
103 | protected PopulationQualityComparator(PopulationQualityComparator 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 PopulationQualityComparator()
|
---|
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(false)));
|
---|
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 PopulationQualityComparator(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 | double compFact = ComparisonFactorParameter.ActualValue.Value;
|
---|
166 | double diversityComFact = DiversityComparisonFactorParameter.ActualValue.Value;
|
---|
167 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
168 | double leftQuality = LeftSideParameter.ActualValue.Value;
|
---|
169 | bool resultDiversity;
|
---|
170 |
|
---|
171 | DataTable spDetailsTable;
|
---|
172 | if (ResultsParameter.ActualValue.ContainsKey(spDetailsParameterName)) {
|
---|
173 | spDetailsTable = (DataTable)ResultsParameter.ActualValue[spDetailsParameterName].Value;
|
---|
174 | } else {
|
---|
175 | spDetailsTable = new DataTable(spDetailsParameterName);
|
---|
176 | spDetailsTable.Rows.Add(new DataRow(divDataRowName));
|
---|
177 | spDetailsTable.Rows.Add(new DataRow(qualityDataRowName));
|
---|
178 | spDetailsTable.Rows.Add(new DataRow(overallCountDataRowName));
|
---|
179 | spDetailsTable.Rows.Add(new DataRow(successCountDataRowName));
|
---|
180 | spDetailsTable.Rows.Add(new DataRow(divFailDataRowName));
|
---|
181 | spDetailsTable.Rows.Add(new DataRow(qualityFailDataRowName));
|
---|
182 | ResultsParameter.ActualValue.Add(new Result(spDetailsParameterName, spDetailsTable));
|
---|
183 | }
|
---|
184 |
|
---|
185 | if (GenerationsParameter.ActualValue.Value != currentGeneration) {
|
---|
186 | spDetailsTable.Rows[divDataRowName].Values.Add(divCount);
|
---|
187 | divCount = 0;
|
---|
188 | spDetailsTable.Rows[qualityDataRowName].Values.Add(qualityCount);
|
---|
189 | qualityCount = 0;
|
---|
190 | spDetailsTable.Rows[overallCountDataRowName].Values.Add(overallCount);
|
---|
191 | overallCount = 0;
|
---|
192 | spDetailsTable.Rows[successCountDataRowName].Values.Add(successCount);
|
---|
193 | successCount = 0;
|
---|
194 | spDetailsTable.Rows[qualityFailDataRowName].Values.Add(badQualityCount);
|
---|
195 | badQualityCount = 0;
|
---|
196 | spDetailsTable.Rows[divFailDataRowName].Values.Add(badDivCount);
|
---|
197 | badDivCount = 0;
|
---|
198 | currentGeneration = GenerationsParameter.ActualValue.Value;
|
---|
199 | }
|
---|
200 |
|
---|
201 | string qualityVariableName = ((ISingleObjectiveSolutionSimilarityCalculator)SimilarityCalculator).QualityVariableName;
|
---|
202 | string solutionVariableName = ((ISingleObjectiveSolutionSimilarityCalculator)SimilarityCalculator).SolutionVariableName;
|
---|
203 |
|
---|
204 | var remainingScope = GetRemainingScope();
|
---|
205 | double oldPopQuality = 0.0;
|
---|
206 | int popSize = 0;
|
---|
207 | foreach (IScope oldSolScope in remainingScope.SubScopes) {
|
---|
208 | double curQuality = ((DoubleValue)oldSolScope.Variables[qualityVariableName].Value).Value;
|
---|
209 | IItem curSol = oldSolScope.Variables[solutionVariableName].Value;
|
---|
210 | oldPopQuality += curQuality;
|
---|
211 | popSize++;
|
---|
212 | }
|
---|
213 |
|
---|
214 | //diversity compared to population
|
---|
215 | Scope fakeScope = new Scope();
|
---|
216 | fakeScope.SubScopes.Add((IScope)ExecutionContext.Scope.Clone());
|
---|
217 | var similarities = SimilarityCalculator.CalculateSolutionCrowdSimilarity(fakeScope, remainingScope);
|
---|
218 | double averageSimilarity = similarities[0].Average();
|
---|
219 | resultDiversity = averageSimilarity < diversityComFact;
|
---|
220 |
|
---|
221 |
|
---|
222 | //quality compared to population
|
---|
223 | double averageQuality = oldPopQuality / popSize;
|
---|
224 | bool result = maximization && leftQuality > averageQuality || !maximization && leftQuality < averageQuality;
|
---|
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 | }
|
---|