-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestIntegerizeMatrix.cs
More file actions
132 lines (124 loc) · 5.34 KB
/
Copy pathTestIntegerizeMatrix.cs
File metadata and controls
132 lines (124 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
Copyright 2021 University of Toronto
This file is part of TMG-Framework for XTMF2.
TMG-Framework for XTMF2 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.
TMG-Framework for XTMF2 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 TMG-Framework for XTMF2. If not, see <http://www.gnu.org/licenses/>.
*/
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.IO;
using TMG;
using TMG.Frameworks.Data.Processing.AST;
using TMG.Processing;
using TMG.Test.Utilities;
namespace TMG.Test.Processing
{
[TestClass]
public class TestIntegerizeMatrix
{
/// <summary>
/// This test just wants to make sure that the most basic case passes.
/// </summary>
[TestMethod]
public void IntegerizeZeroMatrix()
{
string error = null;
var zones = Categories.CreateCategories(new List<int>() { 1, 2, 3, 4 }, ref error);
var pds = Categories.CreateCategories(new List<int>() { 1, 2, }, ref error);
Assert.IsTrue(CategoryMap.CreateCategoryMap(zones, pds, new List<(int originFlatIndex, int destinationFlatIndex)>()
{
new (0, 0),
new (1, 0),
new (2, 1),
new (3, 1)
}, out var map, ref error));
var inputMatrix = new Matrix(zones, zones);
var module = new IntegerizeMatrix()
{
RandomSeed = Helper.CreateParameter(12345),
InputMatrix = Helper.CreateParameter(inputMatrix),
ZoneToPDMap = Helper.CreateParameter(map)
};
var integerMatrix = module.Invoke();
Assert.IsNotNull(integerMatrix);
}
/// <summary>
/// This test just wants to make sure that a simple already integer matrix doesn't change.
/// </summary>
[TestMethod]
public void IntegerizeIdentityMatrix()
{
string error = null;
var zones = Categories.CreateCategories(new List<int>() { 1, 2, 3, 4 }, ref error);
var pds = Categories.CreateCategories(new List<int>() { 1, 2, }, ref error);
Assert.IsTrue(CategoryMap.CreateCategoryMap(zones, pds, new List<(int originFlatIndex, int destinationFlatIndex)>()
{
new (0, 0),
new (1, 0),
new (2, 1),
new (3, 1)
}, out var map, ref error));
var inputMatrix = new Matrix(zones, zones);
inputMatrix.Data[0] = 1.0f;
inputMatrix.Data[3] = 1.0f;
var module = new IntegerizeMatrix()
{
RandomSeed = Helper.CreateParameter(12345),
InputMatrix = Helper.CreateParameter(inputMatrix),
ZoneToPDMap = Helper.CreateParameter(map)
};
var integerMatrix = module.Invoke();
Assert.IsNotNull(integerMatrix);
Assert.AreEqual(1.0f, integerMatrix.Data[0], 0.00000001f);
Assert.AreEqual(0.0f, integerMatrix.Data[1], 0.00000001f);
Assert.AreEqual(0.0f, integerMatrix.Data[2], 0.00000001f);
Assert.AreEqual(1.0f, integerMatrix.Data[3], 0.00000001f);
}
/// <summary>
/// Test that we can actually integerize a real matrix.
/// </summary>
[TestMethod]
public void IntegerizeRealMatrix()
{
string error = null;
var zones = Categories.CreateCategories(new List<int>() { 1, 2, 3, 4 }, ref error);
var pds = Categories.CreateCategories(new List<int>() { 1, 2, }, ref error);
Assert.IsTrue(CategoryMap.CreateCategoryMap(zones, pds, new List<(int originFlatIndex, int destinationFlatIndex)>()
{
new (0, 0),
new (1, 0),
new (2, 1),
new (3, 1)
}, out var map, ref error));
var random = new Random(12345);
var inputMatrix = new Matrix(zones, zones);
for (int i = 0; i < inputMatrix.Data.Length; i++)
{
inputMatrix.Data[i] = (float)random.NextDouble();
}
var module = new IntegerizeMatrix()
{
RandomSeed = Helper.CreateParameter(12345),
InputMatrix = Helper.CreateParameter(inputMatrix.Clone()),
ZoneToPDMap = Helper.CreateParameter(map)
};
var integerMatrix = module.Invoke();
Assert.IsNotNull(integerMatrix);
// Make sure they are close to integers and only +- 1 from the original values
for (int i = 0; i < integerMatrix.Data.Length; i++)
{
Assert.AreEqual(inputMatrix.Data[i], integerMatrix.Data[i], 1.000001f);
Assert.AreEqual(Math.Truncate(integerMatrix.Data[i]), (double)integerMatrix.Data[0], 1.000001f);
}
}
}
}