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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Diagnostics;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Analysis.AlgorithmBehavior.Analyzers;
|
---|
27 | using MIConvexHull;
|
---|
28 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
29 |
|
---|
30 | namespace Visualization {
|
---|
31 | [TestClass]
|
---|
32 | public class LPConvexHullTest {
|
---|
33 | [TestMethod]
|
---|
34 | public void TestMethod1() {
|
---|
35 | int nrOfSamples = 10;
|
---|
36 | int sampleSize = 2;
|
---|
37 | double[][] inputs = CreateRandomData(nrOfSamples, sampleSize);
|
---|
38 | var convAlgData = ConvertPermutationToVertex(inputs);
|
---|
39 |
|
---|
40 | Stopwatch watch = new Stopwatch();
|
---|
41 | watch.Start();
|
---|
42 | var result2 = LPHull.Calculate(inputs);
|
---|
43 | watch.Stop();
|
---|
44 | Console.WriteLine("LPHull: " + 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 | }
|
---|