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