[5372] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9456] | 3 | * Copyright (C) 2002-2013 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)));
|
---|
[5372] | 83 | }
|
---|
| 84 |
|
---|
| 85 | public override IOperation Apply() {
|
---|
| 86 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
[5682] | 87 |
|
---|
| 88 | List<IScope> scopes = new List<IScope>() { ExecutionContext.Scope };
|
---|
| 89 | for (int i = 0; i < DepthParameter.Value.Value; i++)
|
---|
| 90 | scopes = scopes.Select(x => (IEnumerable<IScope>)x.SubScopes).Aggregate((a, b) => a.Concat(b)).ToList();
|
---|
| 91 |
|
---|
[5725] | 92 | ItemCollection<StringValue> collectedValues = CollectedValuesParameter.Value;
|
---|
[7172] | 93 | foreach (StringValue collected in collectedValues) {
|
---|
| 94 | //collect the values of the successful offspring
|
---|
| 95 | Dictionary<String, int> counts = new Dictionary<String, int>();
|
---|
| 96 | for (int i = 0; i < scopes.Count; i++) {
|
---|
| 97 | IScope child = scopes[i];
|
---|
| 98 | string successfulOffspringFlag = SuccessfulOffspringFlagParameter.Value.Value;
|
---|
| 99 | if (child.Variables.ContainsKey(collected.Value) &&
|
---|
| 100 | child.Variables.ContainsKey(successfulOffspringFlag) &&
|
---|
| 101 | (child.Variables[successfulOffspringFlag].Value is BoolValue) &&
|
---|
| 102 | (child.Variables[successfulOffspringFlag].Value as BoolValue).Value) {
|
---|
| 103 | String key = child.Variables[collected.Value].Value.ToString();
|
---|
[5372] | 104 |
|
---|
[7172] | 105 | if (!counts.ContainsKey(key))
|
---|
| 106 | counts.Add(key, 1);
|
---|
| 107 | else
|
---|
| 108 | counts[key]++;
|
---|
[5372] | 109 | }
|
---|
[7172] | 110 | }
|
---|
[5372] | 111 |
|
---|
[7172] | 112 | //create a data table containing the collected values
|
---|
| 113 | ResultCollection successfulOffspringAnalysis;
|
---|
[5725] | 114 |
|
---|
[7172] | 115 | if (SuccessfulOffspringAnalysisParameter.ActualValue == null) {
|
---|
| 116 | successfulOffspringAnalysis = new ResultCollection();
|
---|
| 117 | SuccessfulOffspringAnalysisParameter.ActualValue = successfulOffspringAnalysis;
|
---|
| 118 | } else {
|
---|
| 119 | successfulOffspringAnalysis = SuccessfulOffspringAnalysisParameter.ActualValue;
|
---|
| 120 | }
|
---|
[5725] | 121 |
|
---|
[7172] | 122 | string resultKey = "SuccessfulOffspringAnalyzer Results";
|
---|
| 123 | if (!results.ContainsKey(resultKey)) {
|
---|
| 124 | results.Add(new Result(resultKey, successfulOffspringAnalysis));
|
---|
| 125 | } else {
|
---|
| 126 | results[resultKey].Value = successfulOffspringAnalysis;
|
---|
| 127 | }
|
---|
[5725] | 128 |
|
---|
[7172] | 129 | DataTable successProgressAnalysis;
|
---|
| 130 | if (!successfulOffspringAnalysis.ContainsKey(collected.Value)) {
|
---|
| 131 | successProgressAnalysis = new DataTable();
|
---|
| 132 | successProgressAnalysis.Name = collected.Value;
|
---|
| 133 | successfulOffspringAnalysis.Add(new Result(collected.Value, successProgressAnalysis));
|
---|
| 134 | } else {
|
---|
| 135 | successProgressAnalysis = successfulOffspringAnalysis[collected.Value].Value as DataTable;
|
---|
| 136 | }
|
---|
[5372] | 137 |
|
---|
[7172] | 138 | int successfulCount = 0;
|
---|
| 139 | foreach (string key in counts.Keys) {
|
---|
| 140 | successfulCount += counts[key];
|
---|
| 141 | }
|
---|
[5492] | 142 |
|
---|
[7172] | 143 | foreach (String value in counts.Keys) {
|
---|
| 144 | DataRow row;
|
---|
| 145 | if (!successProgressAnalysis.Rows.ContainsKey(value)) {
|
---|
| 146 | row = new DataRow(value);
|
---|
| 147 | int iterations = GenerationsParameter.ActualValue.Value;
|
---|
[5372] | 148 |
|
---|
[7172] | 149 | //fill up all values seen the first time
|
---|
| 150 | for (int i = 1; i < iterations; i++)
|
---|
| 151 | row.Values.Add(0);
|
---|
[5372] | 152 |
|
---|
[7172] | 153 | successProgressAnalysis.Rows.Add(row);
|
---|
| 154 | } else {
|
---|
| 155 | row = successProgressAnalysis.Rows[value];
|
---|
[5372] | 156 | }
|
---|
| 157 |
|
---|
[7172] | 158 | row.Values.Add(counts[value] / (double)successfulCount);
|
---|
| 159 | }
|
---|
[5372] | 160 |
|
---|
[7172] | 161 | //fill up all values that are not present in the current generation
|
---|
| 162 | foreach (DataRow row in successProgressAnalysis.Rows) {
|
---|
| 163 | if (!counts.ContainsKey(row.Name))
|
---|
| 164 | row.Values.Add(0);
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[5372] | 168 | return base.Apply();
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 | }
|
---|