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/rl.js

213 lines
5.6 KiB

5 years ago
class RL_machine {
constructor(actions_per_state,
transactions,
rewards,
start_state,
end_states,
start_score,
5 years ago
end_score,
learning_rate,
discount_factor,
epsilon=0) {
5 years ago
this.actions_per_state = actions_per_state;
5 years ago
this.transactions = transactions;
this.rewards = rewards;
this.lr = learning_rate;
this.df = discount_factor;
this.start_state = start_state;
this.start_score = start_score;
5 years ago
this.end_score = end_score;
this.end_states = end_states;
this.epsilon = epsilon;
5 years ago
this.q_table = this.actions_per_state.map((c) => c.reduce((o,n) => {o[n]=0; return o},{}));
this.reset_machine();
5 years ago
}
reset_machine(){
5 years ago
for (var q in this.q_table){
for (var key in this.q_table[q]){
this.q_table[q][key] = 0;
}
}
5 years ago
this.episode = 0;
5 years ago
this.running = false;
this.score_history = [];
this.state = this.start_state;
this.score = this.start_score;
5 years ago
}
new_episode(){
// add_new_episode_callback
this.episode++;
5 years ago
this.score_history.push(this.score);
this.state = this.start_state;
this.score = this.start_score;
5 years ago
}
auto_step(){
if (Math.random() < this.epsilon){
return this.step(choose(Object.keys(this.q_table[this.state])));
5 years ago
} else{
return this.greedy_step();
}
}
greedy_step(){
return this.step(keyMax(this.q_table[this.state]));
5 years ago
}
step(action){
this.state = this.update_q_table(this.state, action);
// add_new_step_callback
if (this.end_states.indexOf(this.state) >= 0) {
var succ_event = new CustomEvent("episode",{
detail: "success"
});
if (!this.running) {
window.dispatchEvent(succ_event);
}
this.new_episode();
return 2
}
if (this.score <= this.end_score){
var fail_event = new CustomEvent("episode",{
detail: "failed"
});
if (!this.running) {
window.dispatchEvent(fail_event);
}
5 years ago
this.new_episode();
return 2
}
return 1
}
update_q_table(state, action){
let new_state = this.transactions(state, action);
this.q_table[state][action] = (1-this.lr)*this.q_table[state][action] + this.lr*(this.rewards[new_state] + this.df*Math.max(...Object.values(this.q_table[new_state])));
5 years ago
this.score += this.rewards[new_state];
return new_state;
}
run(episodes, max_steps_per_episode=10000){
5 years ago
this.running = true;
5 years ago
for (var i = 0; i < episodes; i++) {
for (var j = 0; j < max_steps_per_episode; j++) {
5 years ago
if (this.auto_step() != 1) {
5 years ago
break;
}
}
5 years ago
// this.new_episode();
5 years ago
}
5 years ago
this.running = false;
5 years ago
}
}
function keyMax(obj) {
return Object.entries(obj).reduce((r, a) => (a[1] > r[1] ? a : r),[0,Number.MIN_SAFE_INTEGER])[0];
}
5 years ago
function argMax(array) {
return array.map((x, i) => [x, i]).reduce((r, a) => (a[0] > r[0] ? a : r))[1];
}
function choose(array) {
return array[array.length * Math.random() << 0];
};
5 years ago
// ------------------ maze stuff --------------------------------------------
const tile = {
regular: 0,
5 years ago
wall: 1,
start: 2,
5 years ago
dangerous: 4,
5 years ago
end: 8,
5 years ago
};
const dir = {
UP: "UP",
RIGHT: "RIGHT",
DOWN: "DOWN",
LEFT: "LEFT",
5 years ago
};
class Maze {
constructor(map, reward_map) {
this.map = map
this.height = map.length;
this.width = map[0].length;
this.start_state = this.get_states(tile.start)[0];
this.end_states = this.get_states(tile.end);
this.actions = this.get_actions();
this.transactions = this.get_transactions();
this.rewards = this.get_rewards(reward_map);
}
get_states(tile) {
var res = [];
for (var idy = 0; idy < this.map.length; idy++) {
for (var idx = 0; idx < this.map[idy].length; idx++) {
if (this.map[idy][idx] == tile) {
res.push(idy*this.map[0].length+idx);
}
}
}
return res;
}
get_actions() {
var actions = [];
for (let idy=0; idy<this.map.length; idy++){
for (let idx=0; idx<this.map[0].length; idx++){
5 years ago
var action = [];
5 years ago
if (this.map[idy][idx] == tile.wall){
actions.push(action);
continue;
}
5 years ago
if (idy != 0){
if(this.map[idy-1][idx] != tile.wall){
action.push(dir.UP);
}
}
if (idy != this.map.length-1){
if(this.map[idy+1][idx] != tile.wall){
action.push(dir.DOWN);
}
}
5 years ago
if (idx != 0){
5 years ago
if(this.map[idy][idx-1] != tile.wall){
action.push(dir.LEFT);
}
5 years ago
}
if (idx != this.map[0].length-1){
5 years ago
if(this.map[idy][idx+1] != tile.wall){
action.push(dir.RIGHT);
}
5 years ago
}
5 years ago
actions.push(action);
5 years ago
}
}
return actions;
}
get_transactions(){
return function(state, action){
switch (action) {
5 years ago
case dir.UP:
return state-this.width;
case dir.RIGHT:
return state+1;
case dir.DOWN:
return state+this.width;
case dir.LEFT:
return state-1;
}
}.bind(this);
}
get_rewards(rewards){
rewards = [];
for (let idy=0; idy<this.map.length; idy++){
for (let idx=0; idx<this.map[0].length; idx++){
rewards.push(reward[this.map[idy][idx]]);
}
}
return rewards;
}
}
5 years ago
const reward = {[tile.regular]:-1,[tile.dangerous]:-100,[tile.end]:1000,[tile.start]:-1};
5 years ago
var maze = new Maze(map, reward);
5 years ago
var learning_rate = 0.75;
var discount_factor = 0.8;
5 years ago
var machine = new RL_machine(maze.actions, maze.transactions, maze.rewards, maze.start_state, maze.end_states, 50, 0, learning_rate, discount_factor, 0.2);