Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/AlgorithmBehaviorUnitTests/ConvexHullTest.cs @ 10129

Last change on this file since 10129 was 10081, checked in by ascheibe, 11 years ago

#1886 fixed LPHull, seems to work now

File size: 3.1 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Diagnostics;
25using System.Linq;
26using HeuristicLab.Analysis.AlgorithmBehavior.Analyzers;
27using MIConvexHull;
28using Microsoft.VisualStudio.TestTools.UnitTesting;
29
30namespace AlgorithmBehaviorUnitTests {
31  [TestClass]
32  public class ConvexHullTest {
33    [TestMethod]
34    public void TestMethod1() {
35      int nrOfSamples = 50;
36      int sampleSize = 120;
37      double[][] inputs = CreateRandomData(nrOfSamples, sampleSize);
38      var convAlgData = ConvertPermutationToVertex(inputs);
39
40      Stopwatch watch = new Stopwatch();
41      watch.Start();
42      var result2 = HyperHull.CalculateUsingSMO(inputs);
43      watch.Stop();
44      Console.WriteLine("HyperHull: " + watch.ElapsedMilliseconds);
45      watch.Restart();
46      var result1 = ConvexHull.Create(convAlgData).Points.Select(x => x.Position).ToList();
47      watch.Stop();
48      Console.WriteLine("MIConvexHull: " + watch.ElapsedMilliseconds);
49
50      int k = 0;
51      foreach (var d in result1) {
52        bool found = false;
53        foreach (var e in result2) {
54          int i = 0;
55          for (i = 0; i < e.Count(); i++) {
56            if (d[i] != e[i]) {
57              break;
58            }
59          }
60          if (i == e.Count()) {
61            found = true;
62            k++;
63            break;
64          }
65        }
66        Assert.IsTrue(found);
67      }
68      Console.WriteLine("Ratio: " + k + "/" + result1.Count);
69      Assert.AreEqual(k, result1.Count);
70    }
71
72    private List<DefaultVertex> ConvertPermutationToVertex(double[][] data) {
73      List<DefaultVertex> result = new List<DefaultVertex>();
74      for (int i = 0; i < data.Count(); i++) {
75        double[] d = data[i];
76        for (int j = 0; j < d.Length; j++) {
77          DefaultVertex vertex = new DefaultVertex();
78          vertex.Position = d.Select(x => x).ToArray();
79          result.Add(vertex);
80        }
81      }
82      return result;
83    }
84
85    private double[][] CreateRandomData(int n, int m) {
86      double[][] result = new double[n][];
87      Random rand = new Random();
88
89      for (int i = 0; i < n; i++) {
90        result[i] = new double[m];
91        for (int j = 0; j < m; j++) {
92          result[i][j] = (double)rand.Next(1, 60);
93        }
94      }
95      return result;
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.