1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
30 | /// <summary>
|
---|
31 | ///
|
---|
32 | /// </summary>
|
---|
33 | [StorableClass]
|
---|
34 | [Item("ContinuousPointCertaintyWeightCalculator", "")]
|
---|
35 | public class ContinuousPointCertaintyWeightCalculator : DiscriminantClassificationWeightCalculator {
|
---|
36 |
|
---|
37 | public ContinuousPointCertaintyWeightCalculator()
|
---|
38 | : base() {
|
---|
39 | }
|
---|
40 |
|
---|
41 | [StorableConstructor]
|
---|
42 | protected ContinuousPointCertaintyWeightCalculator(bool deserializing) : base(deserializing) { }
|
---|
43 | protected ContinuousPointCertaintyWeightCalculator(ContinuousPointCertaintyWeightCalculator original, Cloner cloner)
|
---|
44 | : base(original, cloner) {
|
---|
45 | }
|
---|
46 |
|
---|
47 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
48 | return new ContinuousPointCertaintyWeightCalculator(this, cloner);
|
---|
49 | }
|
---|
50 |
|
---|
51 | protected override IEnumerable<double> DiscriminantCalculateWeights(ItemCollection<IDiscriminantFunctionClassificationSolution> discriminantSolutions) {
|
---|
52 | List<double> weights = new List<double>();
|
---|
53 | IClassificationProblemData problemData = discriminantSolutions.ElementAt(0).ProblemData;
|
---|
54 | IEnumerable<double> targetValues = GetValues(problemData.Dataset.GetDoubleValues(problemData.TargetVariable).ToList(), problemData.TrainingIndizes);
|
---|
55 | IEnumerator<double> trainingValues;
|
---|
56 |
|
---|
57 | //only works for binary classification
|
---|
58 | if (!problemData.ClassValues.Count().Equals(2))
|
---|
59 | return Enumerable.Repeat<double>(1, discriminantSolutions.Count);
|
---|
60 |
|
---|
61 | double maxClass = problemData.ClassValues.Max();
|
---|
62 | double minClass = problemData.ClassValues.Min();
|
---|
63 | double halfDistanceBetweenClasses = (maxClass - minClass) / 2;
|
---|
64 |
|
---|
65 | foreach (var solution in discriminantSolutions) {
|
---|
66 | IEnumerator<double> estimatedTrainingVal = solution.EstimatedTrainingValues.GetEnumerator();
|
---|
67 | IEnumerator<double> estimatedTrainingClassVal = solution.EstimatedTrainingClassValues.GetEnumerator();
|
---|
68 |
|
---|
69 | trainingValues = targetValues.GetEnumerator();
|
---|
70 | double curWeight = 0.0;
|
---|
71 | while (estimatedTrainingVal.MoveNext() && estimatedTrainingClassVal.MoveNext() && trainingValues.MoveNext()) {
|
---|
72 | //if (estimatedTrainingClassVal.Current.Equals(trainingValues.Current)) {
|
---|
73 | if (trainingValues.Current.Equals(maxClass)) {
|
---|
74 | if (estimatedTrainingVal.Current >= maxClass)
|
---|
75 | curWeight += 1.0;
|
---|
76 | else {
|
---|
77 | double distanceToPoint = Math.Abs(estimatedTrainingVal.Current - maxClass);
|
---|
78 | curWeight += Math.Max(1.0 - (1.0 / halfDistanceBetweenClasses) * distanceToPoint, -1.0);
|
---|
79 | }
|
---|
80 | } else if (trainingValues.Current.Equals(minClass)) {
|
---|
81 | if (estimatedTrainingVal.Current <= minClass)
|
---|
82 | curWeight += 1.0;
|
---|
83 | else {
|
---|
84 | double distanceToPoint = Math.Abs(estimatedTrainingVal.Current - minClass);
|
---|
85 | curWeight += Math.Max(1.0 - (1.0 / halfDistanceBetweenClasses) * distanceToPoint, -1.0);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | //}
|
---|
89 | }
|
---|
90 | weights.Add(curWeight);
|
---|
91 | }
|
---|
92 | return weights;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|