-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
187 lines (145 loc) · 3.73 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
180
181
182
183
184
185
186
187
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Library-->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<!-- The game used in the solution -->
<script src="js/game.js"></script>
<!-- Generic Simulated Annealing object -->
<script src="js/simulated-annealing.js"></script>
<style>
div#input-boxes {
width: 250px;
float: left;
margin-right: 50px;
}
div#input-boxes input {
width: 50px;
margin: 10px;
}
div#solution {
width: 200px;
margin: 0 auto;
}
table, tr, td {
border: 1px solid black;
border-collapse:collapse;
}
.a {
background: white;
}
.b {
background: blue;
}
tr, td {
padding: 20px;
}
label {
display: block;
}
</style>
<script>
var renderSolution = function(solution) {
$solution.empty();
$table = $('<table/>');
for (var i = 0; i < solution.length; i++) {
$tr = $('<tr />');
for (var j = 0; j < solution[0].length; j++) {
var val = (solution[i][j]);
var string = '<td class="' + (val == 1 ? 'b': 'a') + '">' + (val == 1 ? '☺': '') + '</td>';
$td = $(string);
$tr.append($td);
}
$table.append($tr);
}
$solution.append($table);
}
var game;
var solution;
var $solution;
var $table
var $inTmax;
var $inTd;
var $inTarget;
var $inNodes;
var $inM;
var $inN;
var $inK;
$(document).ready(function() {
// Selecting game
game = PuzzleGame;
// References to the page elements
$form = $('form');
$msg = $('#msg');
$solution = $('#solution');
// Reference to the input boxes
$inTmax = $('#tmax');
$inTd = $('#td');
$inTarget = $('#target');
$inNodes = $('#nodes');
$inM = $('#m');
$inN = $('#n');
$inK = $('#k');
// Register changes
$form.on('submit', function(e) {
e.preventDefault();
// Validate each input
var valid = true;
$form.find('input:not(input[type=submit])').each(function() {
if (isNaN($(this).val())) {
$msg.get(0).innerHTML = 'Only write numeric values';
valid = false;
}
});
if (valid) {
// Simulated annealing configurations
var tMax = $inTmax.val();
var td = $inTd.val();
var target = $inTarget.val();
var nodes = $inNodes.val();
// Game configurations
var m = $inM.val();
var n = $inN.val();
var k = $inK.val();
// Set up the game
game.start(m, n, k);
// Generate start node
var start = game.generateRandom();
// Configure simulated annealing
SimulatedAnnealing.configure(tMax, td, target, nodes, game);
// Start solving
solution = SimulatedAnnealing.search(start);
if (solution === false) {
$msg.get(0).innerHTML = "Couldn't solve the specified problem inside the given iteration limit";
} else {
renderSolution(solution);
$msg.get(0).innerHTML = SimulatedAnnealing.numIter() + " iterations before finding a solution";
}
}
});
});
</script>
<title>Simulated Annealing</title>
</head>
<body>
<div id="input-boxes">
<form>
<h2>Simulated Annealing</h2>
<label>Max temperature: <input type="text" id="tmax" value="0.2" /></label>
<label>Temperature degradation: <input type="tex" id="td" value="0.001" /></label>
<label>Target value: <input type="text" id="target" value="1" /></label>
<label># of neighbors: <input type="number" id="nodes" value="30" /></label>
<br />
<h2>Puzzle game</h2>
<label>M: <input type="number" id="m" value="5" /></label>
<label>N: <input type="number" id="n" value="5" /></label>
<label>K: <input type="number" id="k" value="2" /></label>
<br />
<span id="msg"></span><br />
<input type="submit" name="submit" value="Run" />
</form>
</div>
<div id="solution">
</div>
</body>
</html>