1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2013 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.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 |
|
---|
25 |
|
---|
26 | namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers {
|
---|
27 | /// <summary>
|
---|
28 | /// Based on the idea from:
|
---|
29 | /// http://stackoverflow.com/questions/4901959/find-if-a-point-is-inside-a-convex-hull-for-a-set-of-points-without-computing-th
|
---|
30 | /// </summary>
|
---|
31 | public class HyperHull {
|
---|
32 | public static List<double[]> CalculateUsingSMO(double[][] inputs) {
|
---|
33 | int[] labels = new int[inputs.Count()];
|
---|
34 | List<double[]> result = new List<double[]>();
|
---|
35 |
|
---|
36 | for (int i = 0; i < inputs.Length; i++) {
|
---|
37 | labels[i] = -1;
|
---|
38 | }
|
---|
39 | labels[0] = 1;
|
---|
40 |
|
---|
41 | for (int i = 0; i < labels.Count(); i++) {
|
---|
42 | if (i > 0) {
|
---|
43 | labels[i - 1] = -1;
|
---|
44 | labels[i] = 1;
|
---|
45 | }
|
---|
46 |
|
---|
47 | KernelSupportVectorMachine machine = new KernelSupportVectorMachine(new LinearKernel(), inputs[0].Length);
|
---|
48 | SequentialMinimalOptimization smo = new SequentialMinimalOptimization(machine, inputs, labels);
|
---|
49 | smo.Complexity = 1.0;
|
---|
50 | smo.Epsilon = 0.0000000000001;
|
---|
51 | //smo.Tolerance = 0.0001;
|
---|
52 |
|
---|
53 | double error = smo.Run();
|
---|
54 |
|
---|
55 | bool isConvexHullPoint = true;
|
---|
56 | for (int j = 0; j < inputs.Length; j++) {
|
---|
57 | int decision = System.Math.Sign(machine.Compute(inputs[j]));
|
---|
58 | if (decision != labels[j]) {
|
---|
59 | isConvexHullPoint = false;
|
---|
60 | break;
|
---|
61 | }
|
---|
62 | }
|
---|
63 | if (isConvexHullPoint) {
|
---|
64 | result.Add(inputs[i]);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | return result;
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|