-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
179 lines (161 loc) · 4.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<html>
<head>
<style>
canvas{
margin:10px;
}
#tools {
position: fixed;
right:0;
top: 0;
width: max-content;
display: flex;
flex-direction: column;
align-items: center;
}
#tools > label{
text-align: center;
margin:20px 20px 20px 0;
display: flex;
flex-direction: column;
}
#tools > label:last-child{
flex-direction: row;
align-items: center;
}
#tools > label:last-child > button{
height : max-content;
margin:5px;
}
</style>
</head>
<body>
<div id="tools">
<label id="n-container">
<span>n : 8</span>
<input style="width:200px;" id="n" type="range" min="1" max="100" value="8"/>
</label>
<label id="delay-container">
<span>delay : </span>
<input id="delay" type="number" value="100"/>
</label>
<label>
<button id="run">run</button>
<button id="stop" disabled>stop</button>
</label>
</div>
<div style="display: flex;">
<div id="genetic">
<div id="genetic-canvas"></div>
<div class="run-info">
<div>
algorithm : <span>Genetic</span>
</div>
<div>
status :
<span class="status">stopped</span>
</div>
<div>
execution time : <span class="exec-time">NaN</span>
</div>
<div>
progress : <progress max="100" value="0" id="genetic-progress"></progress>
</div>
</div>
</div>
<div id="backtracking">
<div id="backtracking-canvas"></div>
<div class="run-info">
<div>
algorithm : <span>Backtracking</span>
</div>
<div>
status : <span class="status">stopped</span>
</div>
<div>
execution time : <span class="exec-time">NaN</span>
</div>
</div>
</div>
</div>
<script src="js/genetic-tools.js"></script>
<script src="js/chess-ui.js"></script>
<script src="js/n-queens-genetic-engine.js"></script>
<script src="js/n-queens-backtracking-engine.js"></script>
<script>
function makeLinearDNA(matrix){
const linear = [];
for(let j in matrix){
for(let i in matrix[j]){
if(!matrix[j][i]) continue;
linear[j] = i;
}
}
return linear;
}
document.getElementById('n').oninput = function(e){
document.querySelector('#n-container > span').innerHTML = 'n : ' + this.value;
engineA.n = engineB.n = + this.value;
chessA.renderBoard();
chessB.renderBoard();
};
document.getElementById('run').onclick = function () {
const delay = + document.getElementById('delay').value;
runEvent();
let maxConflict = null;
const genTimer = (new Date()).getTime();
const backTimer = (new Date()).getTime();
chessA.run(delay, board => {
if(maxConflict === null){
maxConflict = engineA.conflictsCount(makeLinearDNA(board));
document.getElementById('genetic-progress').setAttribute('max', maxConflict);
document.getElementById('genetic-progress').value = 0;
}else{
const conflict = engineA.conflictsCount(makeLinearDNA(board));
document.getElementById('genetic-progress').value = maxConflict - conflict;
}
}).then(status => {
document.querySelector('#genetic .status').innerHTML = 'stopped';
document.querySelector('#genetic .exec-time').innerHTML = ((new Date()).getTime() - genTimer) / 1000 + 's';
stopEvent();
});
chessB.run(delay).then(status => {
document.querySelector('#backtracking .status').innerHTML = 'stopped';
document.querySelector('#backtracking .exec-time').innerHTML = ((new Date()).getTime() - genTimer) / 1000 + 's';
stopEvent();
});
};
document.getElementById('stop').onclick = function () {
chessA.stop();
chessB.stop();
document.querySelectorAll('.status').forEach(el => el.innerHTML = 'stopped');
stopEvent();
};
function runEvent(){
document.querySelectorAll('.status').forEach(el => el.innerHTML = 'running');
document.querySelectorAll('.exec-time').forEach(el => el.innerHTML = 'NaN');
for(let id of ['run', 'delay', 'n']){
document.getElementById(id).disabled = true;
}
for(let id of ['stop']){
document.getElementById(id).disabled = false;
}
}
function stopEvent(){
for(let id of ['run', 'delay', 'n']){
document.getElementById(id).disabled = false;
}
for(let id of ['stop']){
document.getElementById(id).disabled = true;
}
}
const initialN = 8;
const engineA = new NQueensGeneticEngine(initialN);
const engineB = new NQueensBacktrackingEngine(initialN);
const chessA = new ChessUI(engineA, { parent : document.getElementById('genetic-canvas') });
const chessB = new ChessUI(engineB, { parent : document.getElementById('backtracking-canvas') });
chessA.renderBoard();
chessB.renderBoard();
</script>
</body>
</html>