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("NeighbourhoodWeightCalculator", "")]
|
---|
35 | public class NeighbourhoodWeightCalculator : WeightCalculator {
|
---|
36 |
|
---|
37 | public NeighbourhoodWeightCalculator()
|
---|
38 | : base() {
|
---|
39 | }
|
---|
40 |
|
---|
41 | [StorableConstructor]
|
---|
42 | protected NeighbourhoodWeightCalculator(bool deserializing) : base(deserializing) { }
|
---|
43 | protected NeighbourhoodWeightCalculator(NeighbourhoodWeightCalculator original, Cloner cloner)
|
---|
44 | : base(original, cloner) {
|
---|
45 | }
|
---|
46 |
|
---|
47 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
48 | return new NeighbourhoodWeightCalculator(this, cloner);
|
---|
49 | }
|
---|
50 |
|
---|
51 | public override IEnumerable<double> CalculateWeights(ItemCollection<IClassificationSolution> classificationSolutions) {
|
---|
52 | if (classificationSolutions.Count <= 0)
|
---|
53 | return new List<double>();
|
---|
54 |
|
---|
55 | if (!classificationSolutions.All(x => x is IDiscriminantFunctionClassificationSolution))
|
---|
56 | return Enumerable.Repeat<double>(1, classificationSolutions.Count);
|
---|
57 |
|
---|
58 | ItemCollection<IDiscriminantFunctionClassificationSolution> discriminantSolutions = new ItemCollection<IDiscriminantFunctionClassificationSolution>();
|
---|
59 | List<List<double>> estimatedTestValEnumerators = new List<List<double>>();
|
---|
60 | List<List<double>> estimatedTestClassValEnumerators = new List<List<double>>();
|
---|
61 | foreach (var solution in classificationSolutions) {
|
---|
62 | discriminantSolutions.Add((IDiscriminantFunctionClassificationSolution)solution);
|
---|
63 | estimatedTestValEnumerators.Add(discriminantSolutions.Last().EstimatedTestValues.ToList());
|
---|
64 | estimatedTestClassValEnumerators.Add(discriminantSolutions.Last().EstimatedTestClassValues.ToList());
|
---|
65 | }
|
---|
66 |
|
---|
67 | List<double> weights = Enumerable.Repeat<double>(0, classificationSolutions.Count()).ToList<double>();
|
---|
68 |
|
---|
69 | IClassificationProblemData problemData = classificationSolutions.ElementAt(0).ProblemData;
|
---|
70 | IList<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable).ToList();
|
---|
71 | List<double> testVal = GetValues(targetValues, problemData.TestIndizes).ToList();
|
---|
72 |
|
---|
73 | double pointAvg, help;
|
---|
74 | int count;
|
---|
75 | for (int point = 0; point < estimatedTestClassValEnumerators.First().Count; point++) {
|
---|
76 | pointAvg = 0.0;
|
---|
77 | count = 0;
|
---|
78 | for (int solution = 0; solution < estimatedTestClassValEnumerators.Count; solution++) {
|
---|
79 | if (estimatedTestClassValEnumerators[solution][point].Equals(testVal[point])) {
|
---|
80 | pointAvg += estimatedTestValEnumerators[solution][point];
|
---|
81 | count++;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | pointAvg /= (double)count;
|
---|
85 | for (int solution = 0; solution < estimatedTestClassValEnumerators.Count; solution++) {
|
---|
86 | if (estimatedTestClassValEnumerators[solution][point].Equals(testVal[point])) {
|
---|
87 | weights[solution] += 0.5;
|
---|
88 | help = Math.Abs(estimatedTestValEnumerators[solution][point] - 0.5);
|
---|
89 | weights[solution] += help < 0.5 ? 0.5 - help : 0.0;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 | return weights.Select(x => x / weights.Sum());
|
---|
94 | }
|
---|
95 |
|
---|
96 | private IEnumerable<double> GetValues(IList<double> targetValues, IEnumerable<int> indizes) {
|
---|
97 | return from i in indizes
|
---|
98 | select targetValues[i];
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|