-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
executable file
·104 lines (96 loc) · 3.56 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hungarian.js Test Cases</title>
<script src="hungarian.js"></script>
<script type="text/javascript">
window.onload = function() {
// Test JavaScript goes here!
var matrix, assignments, assignmentsSeen, results, i, j;
// matrix = [ [10,19, 8,15],
// [10,18, 7,17],
// [13,16, 9,14],
// [14,17,10,19] ];
// matrix = [ [10,19, 8,15],
// [10,18, 7,17],
// [13,16, 9,14],
// [12,19, 8,18],
// [14,17,10,19] ];
matrix = [ [10,19,8,15,14],
[10,18,7,17,17],
[13,16,9,14,10],
[12,19,8,18,19] ];
assignments = hungarian(matrix);
// Generate HTML to display the result
// Run tests assuming it's a cost matrix
resultsHTML = "<b>Cost matrix with assignments highlighted:</b>"
resultsHTML += "<table>";
assignmentsSeen = 0;
for(i=0; i<matrix.length; i++) {
resultsHTML += "<tr>";
for(j=0; j<matrix[0].length; j++) {
if(assignmentsSeen < assignments.length && assignments[assignmentsSeen][0] === i && assignments[assignmentsSeen][1] === j) {
assignmentsSeen++;
resultsHTML += "<td style=\"background-color: #FFCC99; text-align: right;\">";
} else {
resultsHTML += "<td style=\"text-align: right;\">";
}
resultsHTML += matrix[i][j];
resultsHTML += "</td>";
}
resultsHTML += "</tr>";
}
resultsHTML += "</table><br />\n";
resultsHTML += "<b>Total cost of assignment:</b> ";
// This is obviously inefficient to run the entire algorithm again just
// to get the sum of the assigned costs. In practice, it would be easy
// for the end user to use the assignments and the input matrix to
// compute the sum, but this is to test the built in functionality, when
// that's all the user needs.
resultsHTML += hungarian(matrix, false, true);
resultsHTML += "<br /><br /><br />";
// Run the same tests assuming it's a profit matrix
assignments = hungarian(matrix, true);
resultsHTML += "<b>Profit matrix with assignments highlighted:</b>"
resultsHTML += "<table>";
assignmentsSeen = 0;
for(i=0; i<matrix.length; i++) {
resultsHTML += "<tr>";
for(j=0; j<matrix[0].length; j++) {
if(assignmentsSeen < assignments.length && assignments[assignmentsSeen][0] === i && assignments[assignmentsSeen][1] === j) {
assignmentsSeen++;
resultsHTML += "<td style=\"background-color: #99CCFF; text-align: right;\">";
} else {
resultsHTML += "<td style=\"text-align: right;\">";
}
resultsHTML += matrix[i][j];
resultsHTML += "</td>";
}
resultsHTML += "</tr>";
}
resultsHTML += "</table><br />\n";
resultsHTML += "<b>Total profit from assignment:</b> ";
// This is obviously inefficient to run the entire algorithm again just
// to get the sum of the assigned costs. In practice, it would be easy
// for the end user to use the assignments and the input matrix to
// compute the sum, but this is to test the built in functionality, when
// that's all the user needs.
resultsHTML += hungarian(matrix, true, true);
resultsHTML += "<br />";
// Write the HTML to the webpage
document.getElementById("results").innerHTML = resultsHTML;
}
</script>
</head>
<body>
<div id="container">
<h2>Hungarian.js Test Cases</h2>
<p>
This document is a nice place to quickly test out Hungarian.js.<br />
See the source code for the input and see below for the output:
</p>
<div id="results"></div>
</div>
</body>
</html>