1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 HEAL.Attic;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Problems.Instances;
|
---|
32 | using HeuristicLab.Problems.Instances.Types;
|
---|
33 |
|
---|
34 | using DoubleVector = MathNet.Numerics.LinearAlgebra.Vector<double>;
|
---|
35 |
|
---|
36 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic.SegmentOptimization {
|
---|
37 | [Item("Segment Optimization Problem (SOP)", "")]
|
---|
38 | [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 1200)]
|
---|
39 | [StorableType("64107939-34A7-4530-BFAB-8EA1C321BF6F")]
|
---|
40 | public class SegmentOptimizationProblem : SingleObjectiveBasicProblem<IntegerVectorEncoding>, IProblemInstanceConsumer<SOPData> {
|
---|
41 |
|
---|
42 | public enum Aggregation {
|
---|
43 | Sum,
|
---|
44 | Mean,
|
---|
45 | StandardDeviation
|
---|
46 | }
|
---|
47 |
|
---|
48 | public override bool Maximization => false;
|
---|
49 |
|
---|
50 | [Storable]
|
---|
51 | private IValueParameter<DoubleArray> dataVectorParameter;
|
---|
52 | public IValueParameter<DoubleArray> DataVectorParameter {
|
---|
53 | get { return dataVectorParameter; }
|
---|
54 | }
|
---|
55 | [Storable]
|
---|
56 | private IValueParameter<IntRange> knownBoundsParameter;
|
---|
57 | public IValueParameter<IntRange> KnownBoundsParameter {
|
---|
58 | get { return knownBoundsParameter; }
|
---|
59 | }
|
---|
60 | [Storable]
|
---|
61 | private IValueParameter<EnumValue<Aggregation>> aggregationParameter;
|
---|
62 | public IValueParameter<EnumValue<Aggregation>> AggregationParameter {
|
---|
63 | get { return aggregationParameter; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public SegmentOptimizationProblem() {
|
---|
67 | Encoding = new IntegerVectorEncoding("bounds");
|
---|
68 |
|
---|
69 | Parameters.Add(dataVectorParameter = new ValueParameter<DoubleArray>("Data Vector", ""));
|
---|
70 | Parameters.Add(knownBoundsParameter = new ValueParameter<IntRange>("Known Bounds", ""));
|
---|
71 | Parameters.Add(aggregationParameter = new ValueParameter<EnumValue<Aggregation>>("Aggregation Function", ""));
|
---|
72 |
|
---|
73 | RegisterEventHandlers();
|
---|
74 |
|
---|
75 | #region Default Instance
|
---|
76 | Load(new SOPData() {
|
---|
77 | Values = Enumerable.Range(1, 50).Select(x => (double)x * x).ToArray(),
|
---|
78 | Lower = 20, Upper = 30,
|
---|
79 | Aggregation = "mean"
|
---|
80 | });
|
---|
81 | #endregion
|
---|
82 | }
|
---|
83 | private SegmentOptimizationProblem(SegmentOptimizationProblem original, Cloner cloner)
|
---|
84 | : base(original, cloner) {
|
---|
85 | dataVectorParameter = cloner.Clone(original.dataVectorParameter);
|
---|
86 | aggregationParameter = cloner.Clone(original.aggregationParameter);
|
---|
87 |
|
---|
88 | RegisterEventHandlers();
|
---|
89 | }
|
---|
90 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
91 | return new SegmentOptimizationProblem(this, cloner);
|
---|
92 | }
|
---|
93 |
|
---|
94 | [StorableConstructor]
|
---|
95 | private SegmentOptimizationProblem(StorableConstructorFlag _) : base(_) { }
|
---|
96 | [StorableHook(HookType.AfterDeserialization)]
|
---|
97 | private void AfterDeserialization() {
|
---|
98 | RegisterEventHandlers();
|
---|
99 | }
|
---|
100 |
|
---|
101 | private void RegisterEventHandlers() {
|
---|
102 | dataVectorParameter.ValueChanged += DataVectorChanged;
|
---|
103 | knownBoundsParameter.ValueChanged += KnownBoundsChanged;
|
---|
104 | aggregationParameter.Value.ValueChanged += AggregationFunctionChanged;
|
---|
105 | }
|
---|
106 | private void DataVectorChanged(object sender, EventArgs eventArgs) {
|
---|
107 | Encoding.Bounds = new IntMatrix(new[,] { { 0, DataVectorParameter.Value.Length } });
|
---|
108 | }
|
---|
109 | private void KnownBoundsChanged(object sender, EventArgs e) {
|
---|
110 | }
|
---|
111 | private void AggregationFunctionChanged(object sender, EventArgs eventArgs) {
|
---|
112 | }
|
---|
113 |
|
---|
114 | public override double Evaluate(Individual individual, IRandom random) {
|
---|
115 | var data = DataVectorParameter.Value;
|
---|
116 | var knownBounds = KnownBoundsParameter.Value;
|
---|
117 | var aggregation = aggregationParameter.Value.Value;
|
---|
118 |
|
---|
119 | var solution = individual.IntegerVector(Encoding.Name);
|
---|
120 | var bounds = new IntRange(solution.Min(), solution.Max());
|
---|
121 |
|
---|
122 | double target = BoundedAggregation(data, knownBounds, aggregation);
|
---|
123 | double prediction = BoundedAggregation(data, bounds, aggregation);
|
---|
124 |
|
---|
125 | return Math.Pow(target - prediction, 2);
|
---|
126 | }
|
---|
127 |
|
---|
128 | public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
|
---|
129 | var orderedIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).OrderBy(z => z.Quality);
|
---|
130 | var best = Maximization ? orderedIndividuals.Last().Individual.IntegerVector(Encoding.Name) : orderedIndividuals.First().Individual.IntegerVector(Encoding.Name);
|
---|
131 |
|
---|
132 | var bounds = new IntRange(best.Min(), best.Max());
|
---|
133 |
|
---|
134 | var data = DataVectorParameter.Value;
|
---|
135 | var knownBounds = KnownBoundsParameter.Value;
|
---|
136 | var aggregation = aggregationParameter.Value.Value;
|
---|
137 |
|
---|
138 | double target = BoundedAggregation(data, knownBounds, aggregation);
|
---|
139 | double prediction = BoundedAggregation(data, bounds, aggregation);
|
---|
140 | double diff = target - prediction;
|
---|
141 |
|
---|
142 | results.AddOrUpdateResult("Bounds", bounds);
|
---|
143 |
|
---|
144 | results.AddOrUpdateResult("AggValue Diff", new DoubleValue(diff));
|
---|
145 | results.AddOrUpdateResult("AggValue Squared Diff", new DoubleValue(Math.Pow(diff, 2)));
|
---|
146 |
|
---|
147 | results.AddOrUpdateResult("Lower Diff", new IntValue(knownBounds.Start - bounds.Start));
|
---|
148 | results.AddOrUpdateResult("Upper Diff", new IntValue(knownBounds.End - bounds.End));
|
---|
149 | results.AddOrUpdateResult("Length Diff", new IntValue(knownBounds.Size - bounds.Size));
|
---|
150 | }
|
---|
151 |
|
---|
152 | private static double BoundedAggregation(DoubleArray data, IntRange bounds, Aggregation aggregation) {
|
---|
153 | if (bounds.Size == 0) {
|
---|
154 | return 0;
|
---|
155 | }
|
---|
156 |
|
---|
157 | var vector = DoubleVector.Build.DenseOfEnumerable(data);
|
---|
158 | var segment = vector.SubVector(bounds.Start, bounds.Size);
|
---|
159 |
|
---|
160 | switch (aggregation) {
|
---|
161 | case Aggregation.Sum:
|
---|
162 | return segment.Sum();
|
---|
163 | case Aggregation.Mean:
|
---|
164 | return segment.Average();
|
---|
165 | case Aggregation.StandardDeviation:
|
---|
166 | return segment.StandardDeviationPop();
|
---|
167 | default:
|
---|
168 | throw new NotImplementedException();
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 | public void Load(SOPData data) {
|
---|
173 | DataVectorParameter.Value = new DoubleArray(data.Values);
|
---|
174 | KnownBoundsParameter.Value = new IntRange(data.Lower, data.Upper);
|
---|
175 | switch (data.Aggregation.ToLower()) {
|
---|
176 | case "sum":
|
---|
177 | AggregationParameter.Value.Value = Aggregation.Sum;
|
---|
178 | break;
|
---|
179 | case "mean":
|
---|
180 | case "avg":
|
---|
181 | AggregationParameter.Value.Value = Aggregation.Mean;
|
---|
182 | break;
|
---|
183 | case "standarddeviation":
|
---|
184 | case "std":
|
---|
185 | case "sd":
|
---|
186 | AggregationParameter.Value.Value = Aggregation.StandardDeviation;
|
---|
187 | break;
|
---|
188 | default:
|
---|
189 | throw new NotSupportedException();
|
---|
190 | }
|
---|
191 |
|
---|
192 | Encoding.Length = 2;
|
---|
193 | Encoding.Bounds = new IntMatrix(new[,] { { 0, DataVectorParameter.Value.Length } });
|
---|
194 |
|
---|
195 | BestKnownQuality = 0;
|
---|
196 | }
|
---|
197 | }
|
---|
198 | } |
---|