[5372] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[14186] | 3 | * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5372] | 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;
|
---|
[7172] | 25 | using HeuristicLab.Analysis;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
[5372] | 27 | using HeuristicLab.Core;
|
---|
[7172] | 28 | using HeuristicLab.Data;
|
---|
[5372] | 29 | using HeuristicLab.Operators;
|
---|
| 30 | using HeuristicLab.Optimization;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
[7172] | 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[5372] | 33 |
|
---|
[5682] | 34 | namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
|
---|
[5372] | 35 | /// <summary>
|
---|
| 36 | /// An operator for analyzing the solution diversity in a population.
|
---|
| 37 | /// </summary>
|
---|
[5682] | 38 | [Item("SuccessfulOffspringAnalyzer", "An operator for analyzing certain properties in the successful offspring. The properties to be analyzed can be specified in the CollectedValues parameter.")]
|
---|
[5372] | 39 | [StorableClass]
|
---|
[5682] | 40 | public sealed class SuccessfulOffspringAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
[7172] | 41 | public bool EnabledByDefault {
|
---|
| 42 | get { return false; }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[5725] | 45 | public ValueParameter<StringValue> SuccessfulOffspringFlagParameter {
|
---|
[5492] | 46 | get { return (ValueParameter<StringValue>)Parameters["SuccessfulOffspringFlag"]; }
|
---|
[5372] | 47 | }
|
---|
| 48 |
|
---|
[5725] | 49 | public ValueParameter<ItemCollection<StringValue>> CollectedValuesParameter {
|
---|
[5372] | 50 | get { return (ValueParameter<ItemCollection<StringValue>>)Parameters["CollectedValues"]; }
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public ValueLookupParameter<ResultCollection> ResultsParameter {
|
---|
| 54 | get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
[5725] | 57 | public LookupParameter<ResultCollection> SuccessfulOffspringAnalysisParameter {
|
---|
[7172] | 58 | get { return (LookupParameter<ResultCollection>)Parameters["SuccessfulOffspringAnalysis"]; }
|
---|
[5725] | 59 | }
|
---|
| 60 |
|
---|
[5682] | 61 | public ILookupParameter<IntValue> GenerationsParameter {
|
---|
[5372] | 62 | get { return (LookupParameter<IntValue>)Parameters["Generations"]; }
|
---|
| 63 | }
|
---|
[5682] | 64 |
|
---|
| 65 | public ValueParameter<IntValue> DepthParameter {
|
---|
| 66 | get { return (ValueParameter<IntValue>)Parameters["Depth"]; }
|
---|
| 67 | }
|
---|
[7172] | 68 |
|
---|
[5372] | 69 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[5682] | 70 | return new SuccessfulOffspringAnalyzer(this, cloner);
|
---|
[7172] | 71 | }
|
---|
[5372] | 72 | [StorableConstructor]
|
---|
[5682] | 73 | private SuccessfulOffspringAnalyzer(bool deserializing) : base(deserializing) { }
|
---|
| 74 | private SuccessfulOffspringAnalyzer(SuccessfulOffspringAnalyzer original, Cloner cloner) : base(original, cloner) { }
|
---|
| 75 | public SuccessfulOffspringAnalyzer()
|
---|
[5372] | 76 | : base() {
|
---|
[7172] | 77 | Parameters.Add(new ValueParameter<StringValue>("SuccessfulOffspringFlag", "The name of the flag which indicates if the individual was successful.", new StringValue("SuccessfulOffspring")));
|
---|
| 78 | Parameters.Add(new ValueParameter<ItemCollection<StringValue>>("CollectedValues", "The properties of the successful offspring that should be collected.", new ItemCollection<StringValue>()));
|
---|
| 79 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the succedd progress analysis results should be stored."));
|
---|
| 80 | Parameters.Add(new LookupParameter<IntValue>("Generations", "The current number of generations."));
|
---|
| 81 | Parameters.Add(new LookupParameter<ResultCollection>("SuccessfulOffspringAnalysis", "The successful offspring analysis which is created."));
|
---|
| 82 | Parameters.Add(new ValueParameter<IntValue>("Depth", "The depth of the individuals in the scope tree.", new IntValue(1)));
|
---|
[10917] | 83 |
|
---|
| 84 | CollectedValuesParameter.Value.Add(new StringValue("SelectedCrossoverOperator"));
|
---|
| 85 | CollectedValuesParameter.Value.Add(new StringValue("SelectedManipulationOperator"));
|
---|
[5372] | 86 | }
|
---|
| 87 |
|
---|
| 88 | public override IOperation Apply() {
|
---|
| 89 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
[5682] | 90 |
|
---|
| 91 | List<IScope> scopes = new List<IScope>() { ExecutionContext.Scope };
|
---|
| 92 | for (int i = 0; i < DepthParameter.Value.Value; i++)
|
---|
| 93 | scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b)).ToList();
|
---|
| 94 |
|
---|
[5725] | 95 | ItemCollection<StringValue> collectedValues = CollectedValuesParameter.Value;
|
---|
[7172] | 96 | foreach (StringValue collected in collectedValues) {
|
---|
| 97 | //collect the values of the successful offspring
|
---|
| 98 | Dictionary<String, int> counts = new Dictionary<String, int>();
|
---|
| 99 | for (int i = 0; i < scopes.Count; i++) {
|
---|
| 100 | IScope child = scopes[i];
|
---|
| 101 | string successfulOffspringFlag = SuccessfulOffspringFlagParameter.Value.Value;
|
---|
| 102 | if (child.Variables.ContainsKey(collected.Value) &&
|
---|
| 103 | child.Variables.ContainsKey(successfulOffspringFlag) &&
|
---|
| 104 | (child.Variables[successfulOffspringFlag].Value is BoolValue) &&
|
---|
| 105 | (child.Variables[successfulOffspringFlag].Value as BoolValue).Value) {
|
---|
| 106 | String key = child.Variables[collected.Value].Value.ToString();
|
---|
[5372] | 107 |
|
---|
[7172] | 108 | if (!counts.ContainsKey(key))
|
---|
| 109 | counts.Add(key, 1);
|
---|
| 110 | else
|
---|
| 111 | counts[key]++;
|
---|
[5372] | 112 | }
|
---|
[7172] | 113 | }
|
---|
[5372] | 114 |
|
---|
[10917] | 115 | if (counts.Count > 0) {
|
---|
| 116 | //create a data table containing the collected values
|
---|
| 117 | ResultCollection successfulOffspringAnalysis;
|
---|
[5725] | 118 |
|
---|
[10917] | 119 | if (SuccessfulOffspringAnalysisParameter.ActualValue == null) {
|
---|
| 120 | successfulOffspringAnalysis = new ResultCollection();
|
---|
| 121 | SuccessfulOffspringAnalysisParameter.ActualValue = successfulOffspringAnalysis;
|
---|
| 122 | } else {
|
---|
| 123 | successfulOffspringAnalysis = SuccessfulOffspringAnalysisParameter.ActualValue;
|
---|
| 124 | }
|
---|
[5725] | 125 |
|
---|
[10917] | 126 | string resultKey = "SuccessfulOffspringAnalyzer Results";
|
---|
| 127 | if (!results.ContainsKey(resultKey)) {
|
---|
| 128 | results.Add(new Result(resultKey, successfulOffspringAnalysis));
|
---|
| 129 | } else {
|
---|
| 130 | results[resultKey].Value = successfulOffspringAnalysis;
|
---|
| 131 | }
|
---|
[5725] | 132 |
|
---|
[10917] | 133 | DataTable successProgressAnalysis;
|
---|
| 134 | if (!successfulOffspringAnalysis.ContainsKey(collected.Value)) {
|
---|
| 135 | successProgressAnalysis = new DataTable();
|
---|
| 136 | successProgressAnalysis.Name = collected.Value;
|
---|
| 137 | successfulOffspringAnalysis.Add(new Result(collected.Value, successProgressAnalysis));
|
---|
| 138 | } else {
|
---|
| 139 | successProgressAnalysis = successfulOffspringAnalysis[collected.Value].Value as DataTable;
|
---|
| 140 | }
|
---|
[5372] | 141 |
|
---|
[10917] | 142 | int successfulCount = 0;
|
---|
| 143 | foreach (string key in counts.Keys) {
|
---|
| 144 | successfulCount += counts[key];
|
---|
| 145 | }
|
---|
[5492] | 146 |
|
---|
[10917] | 147 | foreach (String value in counts.Keys) {
|
---|
| 148 | DataRow row;
|
---|
| 149 | if (!successProgressAnalysis.Rows.ContainsKey(value)) {
|
---|
| 150 | row = new DataRow(value);
|
---|
| 151 | int iterations = GenerationsParameter.ActualValue.Value;
|
---|
[5372] | 152 |
|
---|
[10917] | 153 | //fill up all values seen the first time
|
---|
| 154 | for (int i = 1; i < iterations; i++)
|
---|
| 155 | row.Values.Add(0);
|
---|
[5372] | 156 |
|
---|
[10917] | 157 | successProgressAnalysis.Rows.Add(row);
|
---|
| 158 | } else {
|
---|
| 159 | row = successProgressAnalysis.Rows[value];
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | row.Values.Add(counts[value] / (double)successfulCount);
|
---|
[5372] | 163 | }
|
---|
| 164 |
|
---|
[10917] | 165 | //fill up all values that are not present in the current generation
|
---|
| 166 | foreach (DataRow row in successProgressAnalysis.Rows) {
|
---|
| 167 | if (!counts.ContainsKey(row.Name))
|
---|
| 168 | row.Values.Add(0);
|
---|
| 169 | }
|
---|
[7172] | 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[5372] | 173 | return base.Apply();
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 | }
|
---|