1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 alglib;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
32 | using System.Collections.Generic;
|
---|
33 |
|
---|
34 | namespace HeuristicLab.Problems.DataAnalysis.Operators {
|
---|
35 | [Item("DynOpEqHistogramInitializer", "Dynamic Operator Equalization Histogram Initializer.")]
|
---|
36 | [StorableClass]
|
---|
37 | public class DynOpEqHistogramInitializer : SingleSuccessorOperator {
|
---|
38 | public ILookupParameter<ItemList<IntValue>> BinCapacityParameter {
|
---|
39 | get { return (ILookupParameter<ItemList<IntValue>>)Parameters["BinCapacity"]; }
|
---|
40 | }
|
---|
41 | public ILookupParameter<ItemList<ItemList<DoubleValue>>> AcceptedBinQualitiesParameter {
|
---|
42 | get { return (ILookupParameter<ItemList<ItemList<DoubleValue>>>)Parameters["AcceptedBinQualities"]; }
|
---|
43 | }
|
---|
44 | public ILookupParameter<IntValue> PopulationSizeParameter {
|
---|
45 | get { return (ILookupParameter<IntValue>)Parameters["PopulationSize"]; }
|
---|
46 | }
|
---|
47 | public IScopeTreeLookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
48 | get { return (IScopeTreeLookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
|
---|
49 | }
|
---|
50 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
51 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
52 | }
|
---|
53 | public ILookupParameter BinSizeParameter {
|
---|
54 | get { return (ILookupParameter<IntValue>)Parameters["BinSize"]; }
|
---|
55 | }
|
---|
56 | public ILookupParameter<ItemList<IntValue>> AcceptedCountsParameter {
|
---|
57 | get { return (ILookupParameter<ItemList<IntValue>>)Parameters["AcceptedCounts"]; }
|
---|
58 | }
|
---|
59 | public ILookupParameter<ItemList<IntValue>> TotalCountsParameter {
|
---|
60 | get { return (ILookupParameter<ItemList<IntValue>>)Parameters["TotalCounts"]; }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public DynOpEqHistogramInitializer()
|
---|
64 | : base() {
|
---|
65 | Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree"));
|
---|
66 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality"));
|
---|
67 | Parameters.Add(new LookupParameter<IntValue>("PopulationSize"));
|
---|
68 | Parameters.Add(new ValueLookupParameter<IntValue>("BinSize", new IntValue(5)));
|
---|
69 | Parameters.Add(new LookupParameter<ItemList<IntValue>>("BinCapacity"));
|
---|
70 | Parameters.Add(new LookupParameter<ItemList<ItemList<DoubleValue>>>("AcceptedBinQualities"));
|
---|
71 | Parameters.Add(new LookupParameter<ItemList<IntValue>>("AcceptedCounts"));
|
---|
72 | Parameters.Add(new LookupParameter<ItemList<IntValue>>("TotalCounts"));
|
---|
73 | }
|
---|
74 |
|
---|
75 | public override IOperation Apply() {
|
---|
76 | if (BinCapacityParameter.ActualValue == null) {
|
---|
77 | InitDefaultCapacityHistogram();
|
---|
78 | }
|
---|
79 | ItemList<ItemList<DoubleValue>> acceptedBinQualities = AcceptedBinQualitiesParameter.ActualValue;
|
---|
80 | ItemList<IntValue> acceptedCounts = AcceptedCountsParameter.ActualValue;
|
---|
81 | ItemList<IntValue> totalCounts = TotalCountsParameter.ActualValue;
|
---|
82 |
|
---|
83 | int popSize = PopulationSizeParameter.ActualValue.Value;
|
---|
84 | //double minQuality = (from binAccepted in acceptedBinQualities
|
---|
85 | // where binAccepted.Count > 0
|
---|
86 | // select binAccepted.Min(x => x.Value)).Min();
|
---|
87 | //double maxQuality = (from binAccepted in acceptedBinQualities
|
---|
88 | // where binAccepted.Count > 0
|
---|
89 | // select binAccepted.Max(x => x.Value)).Max();
|
---|
90 | //double range = maxQuality - minQuality;
|
---|
91 | double avgQualitySum = (from binAccepted in acceptedBinQualities
|
---|
92 | //select binAccepted.Count)
|
---|
93 | where binAccepted.Count > 0
|
---|
94 | select (from quality in binAccepted
|
---|
95 | select quality.Value).Average())
|
---|
96 | .Sum();
|
---|
97 | ItemList<IntValue> binCapacity = BinCapacityParameter.ActualValue;
|
---|
98 | for (int i = 0; i < binCapacity.Count; i++) {
|
---|
99 | double avgBinQuality = (from quality in acceptedBinQualities[i]
|
---|
100 | select quality.Value)
|
---|
101 | .DefaultIfEmpty(0.0)
|
---|
102 | .Average();
|
---|
103 |
|
---|
104 | //double avgBinQuality = acceptedBinQualities[i].Count;
|
---|
105 | binCapacity[i].Value = (int)Math.Ceiling(popSize * (avgBinQuality / avgQualitySum));
|
---|
106 | acceptedBinQualities[i].Clear();
|
---|
107 | acceptedCounts[i].Value = 0;
|
---|
108 | }
|
---|
109 | for (int i = 0; i < totalCounts.Count; i++)
|
---|
110 | totalCounts[i].Value = 0;
|
---|
111 | return base.Apply();
|
---|
112 | }
|
---|
113 |
|
---|
114 | private void InitDefaultCapacityHistogram() {
|
---|
115 | ItemArray<SymbolicExpressionTree> trees = SymbolicExpressionTreeParameter.ActualValue;
|
---|
116 | ItemArray<DoubleValue> quality = QualityParameter.ActualValue;
|
---|
117 | var binCapacities = new ItemList<IntValue>();
|
---|
118 | var acceptedQuality = new ItemList<ItemList<DoubleValue>>(20);
|
---|
119 | var acceptedCounts = new ItemList<IntValue>();
|
---|
120 | var totalCounts = new ItemList<IntValue>();
|
---|
121 | BinCapacityParameter.ActualValue = binCapacities;
|
---|
122 | AcceptedBinQualitiesParameter.ActualValue = acceptedQuality;
|
---|
123 | TotalCountsParameter.ActualValue = totalCounts;
|
---|
124 | AcceptedCountsParameter.ActualValue = acceptedCounts;
|
---|
125 | for (int i = 0; i < trees.Length; i++) {
|
---|
126 | int binIndex = GetBinIndexForSize(trees[i].Size);
|
---|
127 | if (Exists(binIndex)) {
|
---|
128 | AddToBin(binIndex, quality[i].Value);
|
---|
129 | } else {
|
---|
130 | CreateNewBin(binIndex);
|
---|
131 | AddToBin(binIndex, quality[i].Value);
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | private void CreateNewBin(int binIndex) {
|
---|
137 | ItemList<IntValue> binCapacities = BinCapacityParameter.ActualValue;
|
---|
138 | ItemList<ItemList<DoubleValue>> acceptedQualities = AcceptedBinQualitiesParameter.ActualValue;
|
---|
139 | ItemList<IntValue> acceptedCounts = AcceptedCountsParameter.ActualValue;
|
---|
140 | ItemList<IntValue> totalCounts = TotalCountsParameter.ActualValue;
|
---|
141 | for (int i = binCapacities.Count; i <= binIndex; i++) {
|
---|
142 | binCapacities.Add(new IntValue(1));
|
---|
143 | acceptedQualities.Add(new ItemList<DoubleValue>(10));
|
---|
144 | acceptedCounts.Add(new IntValue(0));
|
---|
145 | totalCounts.Add(new IntValue(0));
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | private void AddToBin(int binIndex, double quality) {
|
---|
150 | ItemList<ItemList<DoubleValue>> acceptedQualities = AcceptedBinQualitiesParameter.ActualValue;
|
---|
151 | acceptedQualities[binIndex].Add(new DoubleValue(quality));
|
---|
152 | }
|
---|
153 |
|
---|
154 | private bool Exists(int binIndex) {
|
---|
155 | // if the bin has a capacity set then it exists
|
---|
156 | ItemList<IntValue> binCapacities = BinCapacityParameter.ActualValue;
|
---|
157 | return binIndex < binCapacities.Count;
|
---|
158 | }
|
---|
159 |
|
---|
160 | private int GetBinIndexForSize(int size) {
|
---|
161 | int binSize = ((IntValue)BinSizeParameter.ActualValue).Value;
|
---|
162 | return (int)Math.Floor((size - 3.0) / binSize);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|