Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/AlgorithmBehaviorUnitTests/LPConvexHullTest.cs @ 10201

Last change on this file since 10201 was 10198, checked in by ascheibe, 11 years ago

#1886

  • added liblrs and c# wrapper for vertex enumeration/volume calculation
  • use MIConvexHull for triangulation and volume calculation
  • fixed vertex conversion code for MIConvexHull lib
  • added a unit test for measuring performance of convex hull/volume calculation
File size: 5.2 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 LPConvexHullTest {
33    [TestMethod]
34    public void TestMethod1() {
35      int nrOfSamples = 70;
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    [TestMethod]
73    public void TestExt() {
74      var inputs = CreateDataExtremePoint1().ToList();
75      double[] alpha = inputs.Last();
76      bool result = LPHull.EXT(inputs, alpha, inputs.Count() - 1);
77      Assert.IsTrue(result);
78
79      inputs = CreateDataExtremePoint2().ToList();
80      alpha = inputs.Last();
81      result = LPHull.EXT(inputs, alpha, inputs.Count() - 1);
82      Assert.IsTrue(result);
83
84      inputs = CreateDataNonExtremePoint1().ToList();
85      alpha = inputs.Last();
86      result = LPHull.EXT(inputs, alpha, inputs.Count() - 1);
87      Assert.IsFalse(result);
88
89      inputs = CreateDataOnHull().ToList();
90      alpha = inputs.Last();
91      result = LPHull.EXT(inputs, alpha, inputs.Count() - 1);
92      Assert.IsFalse(result);
93    }
94
95    private double[][] CreateDataExtremePoint1() {
96      double[][] result = new double[5][];
97
98      result[0] = new double[] { 0.1, 0.1 };
99      result[1] = new double[] { 1, 1 };
100      result[2] = new double[] { 1, 0 };
101      result[3] = new double[] { 0, 1 };
102      result[4] = new double[] { 2.0, 1.4 };
103
104      return result;
105    }
106
107    private double[][] CreateDataExtremePoint2() {
108      double[][] result = new double[5][];
109
110      result[0] = new double[] { 0.1, 0.1 };
111      result[1] = new double[] { 1, 1 };
112      result[2] = new double[] { 1, 0 };
113      result[3] = new double[] { 0, 1 };
114      result[4] = new double[] { 1.0, 1.4 };
115
116      return result;
117    }
118
119    private double[][] CreateDataNonExtremePoint1() {
120      double[][] result = new double[5][];
121
122      result[0] = new double[] { 0.1, 0.1 };
123      result[1] = new double[] { 1, 1 };
124      result[2] = new double[] { 1, 0 };
125      result[3] = new double[] { 0, 1 };
126      result[4] = new double[] { 0.8, 0.4 };
127
128      return result;
129    }
130
131    private double[][] CreateDataOnHull() {
132      double[][] result = new double[5][];
133
134      result[0] = new double[] { 0.1, 0.1 };
135      result[1] = new double[] { 1, 1 };
136      result[2] = new double[] { 1, 0 };
137      result[3] = new double[] { 0, 1 };
138      result[4] = new double[] { 1.0, 0.5 };
139
140      return result;
141    }
142
143    private List<DefaultVertex> ConvertPermutationToVertex(double[][] data) {
144      List<DefaultVertex> result = new List<DefaultVertex>();
145      for (int i = 0; i < data.Count(); i++) {
146        double[] d = data[i];
147        DefaultVertex vertex = new DefaultVertex();
148        vertex.Position = d;
149        result.Add(vertex);
150      }
151      return result;
152    }
153
154    private double[][] CreateRandomData(int n, int m) {
155      double[][] result = new double[n][];
156      Random rand = new Random();
157
158      for (int i = 0; i < n; i++) {
159        result[i] = new double[m];
160        for (int j = 0; j < m; j++) {
161          result[i][j] = (double)rand.Next(1, 60);
162        }
163      }
164      return result;
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.