You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RL_exhibit/js/controls.js

52 lines
1.1 KiB

5 years ago
function dir_to_action(dir){
let actions = [...Object.keys(machine.q_table[machine.state])];
if (actions.indexOf(dir) > -1){
return dir;
5 years ago
}
return undefined;
}
var animate = false;
function key_callback(e) {
var tmp;
if (animate){
return
}
switch (e.keyCode) {
case 37:
tmp = dir_to_action(dir.LEFT);
break;
case 38:
tmp = dir_to_action(dir.UP);
break;
case 39:
tmp = dir_to_action(dir.RIGHT);
break;
case 40:
tmp = dir_to_action(dir.DOWN);
break;
}
5 years ago
var ret = 1;
5 years ago
if (tmp != undefined){
5 years ago
agent.do_action(tmp, true);
ret = machine.step(tmp);
5 years ago
}
5 years ago
update_agent(machine.state, true);
5 years ago
}
document.addEventListener('keydown', key_callback);
function show_solution() {
var sol = machine.current_solution();
animate = true;
show_path(sol.states, 0);
}
function show_path(path, i){
if (path.length == i) {
animate = false;
return
}
agent.set_state(path[i]);
window.setTimeout(function(){ show_path(path, ++i) }, 1000);
}