#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using HeuristicLab.Analysis.AlgorithmBehavior.Analyzers;
using MIConvexHull;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace AlgorithmBehaviorUnitTests {
[TestClass]
public class LPConvexHullTest {
[TestMethod]
public void TestMethod1() {
int nrOfSamples = 70;
int sampleSize = 2;
double[][] inputs = CreateRandomData(nrOfSamples, sampleSize);
var convAlgData = ConvertPermutationToVertex(inputs);
Stopwatch watch = new Stopwatch();
watch.Start();
var result2 = LPHull.Calculate(inputs);
watch.Stop();
Console.WriteLine("LPHull: " + watch.ElapsedMilliseconds);
watch.Restart();
var result1 = ConvexHull.Create(convAlgData).Points.Select(x => x.Position).ToList();
watch.Stop();
Console.WriteLine("MIConvexHull: " + watch.ElapsedMilliseconds);
int k = 0;
foreach (var d in result1) {
bool found = false;
foreach (var e in result2) {
int i = 0;
for (i = 0; i < e.Count(); i++) {
if (d[i] != e[i]) {
break;
}
}
if (i == e.Count()) {
found = true;
k++;
break;
}
}
Assert.IsTrue(found);
}
Console.WriteLine("Ratio: " + k + "/" + result1.Count);
Assert.AreEqual(k, result1.Count);
}
[TestMethod]
public void TestExt() {
var inputs = CreateDataExtremePoint1().ToList();
double[] alpha = inputs.Last();
bool result = LPHull.EXT(inputs, alpha, inputs.Count() - 1);
Assert.IsTrue(result);
inputs = CreateDataExtremePoint2().ToList();
alpha = inputs.Last();
result = LPHull.EXT(inputs, alpha, inputs.Count() - 1);
Assert.IsTrue(result);
inputs = CreateDataNonExtremePoint1().ToList();
alpha = inputs.Last();
result = LPHull.EXT(inputs, alpha, inputs.Count() - 1);
Assert.IsFalse(result);
inputs = CreateDataOnHull().ToList();
alpha = inputs.Last();
result = LPHull.EXT(inputs, alpha, inputs.Count() - 1);
Assert.IsFalse(result);
}
private double[][] CreateDataExtremePoint1() {
double[][] result = new double[5][];
result[0] = new double[] { 0.1, 0.1 };
result[1] = new double[] { 1, 1 };
result[2] = new double[] { 1, 0 };
result[3] = new double[] { 0, 1 };
result[4] = new double[] { 2.0, 1.4 };
return result;
}
private double[][] CreateDataExtremePoint2() {
double[][] result = new double[5][];
result[0] = new double[] { 0.1, 0.1 };
result[1] = new double[] { 1, 1 };
result[2] = new double[] { 1, 0 };
result[3] = new double[] { 0, 1 };
result[4] = new double[] { 1.0, 1.4 };
return result;
}
private double[][] CreateDataNonExtremePoint1() {
double[][] result = new double[5][];
result[0] = new double[] { 0.1, 0.1 };
result[1] = new double[] { 1, 1 };
result[2] = new double[] { 1, 0 };
result[3] = new double[] { 0, 1 };
result[4] = new double[] { 0.8, 0.4 };
return result;
}
private double[][] CreateDataOnHull() {
double[][] result = new double[5][];
result[0] = new double[] { 0.1, 0.1 };
result[1] = new double[] { 1, 1 };
result[2] = new double[] { 1, 0 };
result[3] = new double[] { 0, 1 };
result[4] = new double[] { 1.0, 0.5 };
return result;
}
private List ConvertPermutationToVertex(double[][] data) {
List result = new List();
for (int i = 0; i < data.Count(); i++) {
double[] d = data[i];
for (int j = 0; j < d.Length; j++) {
DefaultVertex vertex = new DefaultVertex();
vertex.Position = d.Select(x => x).ToArray();
result.Add(vertex);
}
}
return result;
}
private double[][] CreateRandomData(int n, int m) {
double[][] result = new double[n][];
Random rand = new Random();
for (int i = 0; i < n; i++) {
result[i] = new double[m];
for (int j = 0; j < m; j++) {
result[i][j] = (double)rand.Next(1, 60);
}
}
return result;
}
}
}