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

535 lines
15 KiB

5 years ago
Vue.component('line-chart', {
extends: VueChartJs.Line,
mixins: [VueChartJs.mixins.reactiveProp],
props: ['options'],
// mixins: [VueChartJs.mixins.reactiveData],
// props: ['options','labels', 'datasets'],
// watch: {
// 'labels': function(new_val) {
// this.chartData = {
// 'labels': new_val,
// 'datasets': this.datasets};
// },
// 'datasets': {
// deep:true,
// handler: function(new_val) {
// this.chartData = {
// 'labels': this.labels,
// 'datasets': new_val};
// }
// }
// },
5 years ago
mounted() {
5 years ago
this.renderChart(this.chartData, this.options);
},
})
var palette = ['#d2000d', '#d30512', '#d40a17', '#d50f1c', '#d61420', '#d71a25', '#d71f2a', '#d8242f', '#d92934', '#da2e39', '#db333d', '#dc3842', '#dd3d47', '#de424c', '#df4751', '#e04d56', '#e0525a', '#e1575f', '#e25c64', '#e36169', '#e4666e', '#e56b73', '#e67077', '#e7757c', '#e87a81', '#e98086', '#e9858b', '#ea8a90', '#eb8f95', '#ec9499', '#ed999e', '#ee9ea3', '#efa3a8', '#f0a8ad', '#f1adb2', '#f2b3b6', '#f2b8bb', '#f3bdc0', '#f4c2c5', '#f5c7ca', '#f6cccf', '#f7d1d3', '#f8d6d8', '#f9dbdd', '#fae0e2', '#fbe6e7', '#fbebec', '#fcf0f0', '#fdf5f5', '#fefafa', '#ffffff', '#fafcfa', '#f5f9f5', '#f0f6f0', '#ebf3ec', '#e6f1e7', '#e1eee2', '#dcebdd', '#d7e8d8', '#d3e5d3', '#cee2cf', '#c9dfca', '#c4dcc5', '#bfd9c0', '#bad6bb', '#b5d4b6', '#b0d1b2', '#abcead', '#a6cba8', '#a1c8a3', '#9cc59e', '#97c299', '#92bf95', '#8dbc90', '#88b98b', '#84b786', '#7fb481', '#7ab17c', '#75ae77', '#70ab73', '#6ba86e', '#66a569', '#61a264', '#5c9f5f', '#579c5a', '#529a56', '#4d9751', '#48944c', '#439147', '#3e8e42', '#398b3d', '#348839', '#308534', '#2b822f', '#267f2a', '#217d25', '#1c7a20', '#17771c', '#127417', '#0d7112', '#086e0d']
5 years ago
Array.prototype.simpleSMA = function(N) {
return this.map(
function(el, index, _arr) {
5 years ago
return _arr.filter(
5 years ago
function(x2, i2) {
return i2 <= index && i2 > index - N;
})
.reduce(
function(last, current, index, arr) {
return (current / arr.length + last);
}, 0);
});
5 years ago
};
5 years ago
Array.prototype.max = function() {
return this.map(
function(el, index, _arr) {
5 years ago
return _arr.filter(
5 years ago
function(x2, i2) {
return i2 <= index;
})
.reduce(
function(last, current) {
return last > current ? last : current;
}, -1000000000);
});
5 years ago
};
5 years ago
app = new Vue({
el: '#app',
5 years ago
components: {
VueSlider: window['vue-slider-component']
},
5 years ago
data: {
width: 0,
height: 0,
q_table: machine.q_table,
maze: maze,
5 years ago
state: {
x: 0,
y: 0
},
5 years ago
state_tween: new TimelineLite(),
5 years ago
score: machine.score,
score_history: machine.score_history,
labels: [],
5 years ago
learning_rate: machine.lr,
discount_factor: machine.df,
epsilon: machine.epsilon,
slider_config: {
min: 0,
max: 1,
duration: 0,
interval: 0.01,
tooltip: 'none'
5 years ago
},
robot_image: null,
energy_image: null,
5 years ago
},
created() {
// Resize handler
window.addEventListener('resize', this.handleResize)
this.handleResize();
// State wrapper
var s = machine.state;
var $this = this;
this.state = this.s2p(s);
Object.defineProperty(machine, 'state', {
5 years ago
get: function() {
return this._state
},
set: function(ne) {
this._state = ne;
$this.handleState(this._state);
}
5 years ago
});
machine.state = s;
5 years ago
// Score wrapper
var s = machine.score;
var $this = this;
this.score = s;
Object.defineProperty(machine, 'score', {
5 years ago
get: function() {
return this._score
},
set: function(ne) {
this._score = ne;
$this.score = ne
}
5 years ago
});
machine.score = s;
// Score history wrapper
var s = machine.score_history;
var $this = this;
this.score_history = s;
Object.defineProperty(machine, 'score_history', {
5 years ago
get: function() {
return this._score_history
},
set: function(ne) {
this._score_history = ne;
$this.score_history = ne
}
5 years ago
});
machine.score_history = s;
5 years ago
const robot_image = new window.Image();
robot_image.src = "img/robot.png";
// robot_image.src = "https://konvajs.org/assets/yoda.jpg";
robot_image.onload = () => {
// set image only when it is loaded
this.robot_image = robot_image;
};
const energy_image = new window.Image();
energy_image.src = "img/station.png";
// energy_image.src = "https://konvajs.org/assets/yoda.jpg";
energy_image.onload = () => {
// set image only when it is loaded
this.energy_image = energy_image;
};
5 years ago
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
computed: {
5 years ago
datacollection: function() {
5 years ago
return {
labels: Array.from(Array(this.score_history.length).keys()),
5 years ago
datasets: [{
5 years ago
label: 'Data One',
backgroundColor: 'rgb(0,0,0,0)',
5 years ago
data: this.score_history,//.simpleSMA(Math.round(50)),
5 years ago
fill: false,
borderColor: 'rgb(255, 159, 64)',
pointRadius: 1,
},
5 years ago
// {
// label: 'Data One',
// backgroundColor: 'rgb(0,0,0,0)',
// data: this.score_history.max(),
// fill: false,
// borderColor: 'rgb(64, 159, 255)',
// pointRadius: 1,
// },
5 years ago
]
}
},
5 years ago
plot_options: function() {
var $this = this;
return {
responsive: true,
maintainAspectRatio: false,
scales: {
xAxes: [{
// type: 'linear',
ticks: {
maxTicksLimit: 8,
maxRotation: 0,
}
}]
},
legend: {
display: false
}
}
},
stage_config: function() {
5 years ago
return {
width: this.width,
height: this.height,
}
},
5 years ago
mini_map_config: function() {
5 years ago
return {
5 years ago
x: this.width / 2 - (this.base_size * (this.maze.width) / 2),
y: this.height / 2 - (this.base_size * (this.maze.height) / 2),
scale: {
5 years ago
x: 1,
y: 1
5 years ago
}
}
},
5 years ago
local_layer: function() {
5 years ago
return {
5 years ago
x: this.width / 2,
y: this.height / 2,
scale: {
5 years ago
x: 2,
y: 2
}
}
},
5 years ago
map_config: function() {
5 years ago
return {
5 years ago
x: this.base_size * (this.maze.width - this.state.x),
y: this.base_size * (this.maze.height - this.state.y),
5 years ago
offset: {
5 years ago
x: this.base_size * this.maze.width + this.base_size / 2,
y: this.base_size * this.maze.height + this.base_size / 2,
5 years ago
}
}
},
5 years ago
agent_config: function() {
5 years ago
return {
sides: 5,
radius: this.base_size / 3,
fill: '#00D2FF',
stroke: 'black',
strokeWidth: this.strokeW,
offset: {
x: -this.base_size / 2,
y: -this.base_size / 2
},
5 years ago
x: this.base_size * this.state.x,
y: this.base_size * this.state.y,
5 years ago
}
},
5 years ago
robot_config: function() {
return {
height: this.base_size,
width: this.base_size,
x: this.base_size * this.state.x,
y: this.base_size * this.state.y,
image: this.robot_image,
}
},
energy_config: function() {
return {
height: this.base_size,
width: this.base_size,
offset: {
x: this.base_size/2,
y: this.base_size/2
},
image: this.energy_image,
}
},
5 years ago
base_size: function() {
return Math.min(this.stage_config.height * 0.8 / this.maze.height, this.stage_config.width * 0.5 / this.maze.width);
5 years ago
},
5 years ago
strokeW: function() {
5 years ago
return this.base_size / 50;
},
5 years ago
extreme_q_values: function(){
var max = -10*30;
var min = 10*30;
for (field in this.q_table) {
for (key in this.q_table[field]){
if (this.q_table[field][key]<min){
min = this.q_table[field][key];
} else if (this.q_table[field][key]>max){
max = this.q_table[field][key];
}
}
}
return {min:min,max:max};
}
5 years ago
},
methods: {
5 years ago
s2p: function(state) {
5 years ago
return {
5 years ago
x: (state % this.maze.width),
y: Math.floor(state / this.maze.width),
5 years ago
}
},
5 years ago
p2s: function(x, y) {
return x + y * this.maze.width;
5 years ago
},
handleResize: function() {
this.width = window.innerWidth;
this.height = window.innerHeight;
},
handleState: function(s) {
5 years ago
if (!machine.running) {
this.state_tween.to(this.state, 0.2, {
x: this.s2p(s).x,
y: this.s2p(s).y
});
5 years ago
} else {
this.state = this.s2p(s);
}
// this.hidden_state = s;
},
5 years ago
get_grid_line_config: function(idx, y = false) {
var offset = this.strokeW / 2;
if (y) {
var points = [-offset, Math.round(idx * this.base_size), this.base_size * this.maze.width + offset, Math.round(idx * this.base_size)];
5 years ago
} else {
var points = [Math.round(idx * this.base_size), -offset, Math.round(idx * this.base_size), this.base_size * this.maze.height + offset];
}
return {
points: points,
stroke: '#ddd',
strokeWidth: this.strokeW,
}
},
5 years ago
get_tile_type: function(state) {
5 years ago
var pos = this.s2p(state);
5 years ago
if (pos.y > maze.height) {
5 years ago
return null;
5 years ago
} else if (pos.x > maze.width) {
5 years ago
return null;
} else {
return maze.map[pos.y][pos.x];
}
},
5 years ago
in_plus: function(pos1, pos2) {
if (Math.abs(pos1.x - pos2.x) + Math.abs(pos1.y - pos2.y) < 2) {
5 years ago
return true;
}
return false;
},
5 years ago
get_field_config: function(state) {
var pos = this.s2p(state);
return {
x: this.base_size * pos.x+this.base_size/2,
y: this.base_size * pos.y+this.base_size/2,
}
},
get_q_text_config: function (val, i) {
var off, key;
switch (i) {
case 1:
off = {
align: "center",
verticalAlign: "top",
};
key = dir.UP;
break;
case 2:
off = {
align: "right",
verticalAlign: "middle",
};
key = dir.RIGHT;
break;
case 3:
off = {
align: "center",
verticalAlign: "bottom",
};
key = dir.DOWN;
break;
case 4:
off = {
align: "left",
verticalAlign: "middle",
};
key = dir.LEFT;
break;
}
if (val[key] === undefined) {
return {}
}
return {
fontSize: this.base_size/7,
fontFamily: 'Calibri',
fill: 'black',
text: +val[key].toPrecision(3)+'',
width: this.base_size-20,
height: this.base_size-34,
5 years ago
...off,
offset: {
x: (this.base_size-20)/2,
y: (this.base_size-34)/2,
5 years ago
}
}
},
get_triangle_config: function(value, d) {
var rot = 0;
switch (d) {
case dir.UP:
rot = -90;
break;
case dir.RIGHT:
rot = 0;
break;
case dir.DOWN:
rot = 90;
break;
case dir.LEFT:
rot = 180;
break;
}
var $this = this;
var norma_value = value>0?(value+1000)/(2000):(value+30)/60;
5 years ago
return {
sceneFunc: function(context, shape) {
context.beginPath();
var width = $this.base_size / 5;
var arrow_w = $this.base_size / 2;
var stumpf = $this.base_size / 6;
var arrow_l = $this.base_size / 5;
context.moveTo($this.base_size/2-stumpf-arrow_l, width/2);
context.lineTo($this.base_size/2-stumpf, width/2);
context.lineTo($this.base_size/2-stumpf, arrow_w/2);
context.lineTo($this.base_size/2-2, 0);
context.lineTo($this.base_size/2-stumpf, -arrow_w/2);
context.lineTo($this.base_size/2-stumpf, -width/2);
context.lineTo($this.base_size/2-stumpf-arrow_l, -width/2);
context.lineTo($this.base_size/2-stumpf-arrow_l, width/2);
// context.moveTo(0, 0);
// context.lineTo($this.base_size / 2, $this.base_size / 2);
// context.lineTo($this.base_size / 2, -$this.base_size / 2);
// context.lineTo(0, 0);
5 years ago
context.closePath();
// (!) Konva specific method, it is very important
context.fillStrokeShape(shape);
},
fill: palette[Math.round(norma_value*100)],
5 years ago
stroke: 'black',
strokeWidth: 1,
5 years ago
rotation: rot,
}
},
get_tile_config: function(i, t_type, local = false) {
5 years ago
var pos = this.s2p(i);
var over = {};
// not in plus
if (local) {
5 years ago
if (!this.in_plus(this.s2p(i), {
x: Math.round(this.state.x),
y: Math.round(this.state.y)
})) {
5 years ago
over = {
opacity: 0,
fill: "#eee"
};
5 years ago
} else if (i != this.p2s(Math.round(this.state.x), Math.round(this.state.y))) {
5 years ago
over = {
opacity: 1,
fill: "#eee"
};
}
}
const layout = {
width: this.base_size,
height: this.base_size,
stroke: '#ddd',
strokeWidth: this.strokeW,
5 years ago
offset: {
x: this.base_size/2,
y: this.base_size/2,
}
5 years ago
};
switch (t_type) {
case tile.regular:
return {
...layout,
fill: '#fff',
opacity: 1,
...over,
}
case tile.end:
return {
...layout,
fill: '#0eb500',
opacity: 1,
...over,
}
case tile.start:
return {
...layout,
fill: '#ff0008',
opacity: 1,
...over,
}
case tile.dangerous:
return {
...layout,
fill: '#FF7B17',
opacity: 1,
...over,
}
5 years ago
case tile.wall:
return {
...layout,
fill: '#000000',
opacity: 1,
...over,
}
5 years ago
}
}
},
5 years ago
watch: {
learning_rate: function(new_val) {
machine.lr = new_val;
render_latex();
},
discount_factor: function(new_val) {
machine.df = new_val;
render_latex();
},
epsilon: function(new_val) {
machine.epsilon = new_val;
}
}
5 years ago
})
5 years ago
function render_latex() {
// (1-lr) * Q[state, action] + lr * (reward + gamma * np.max(Q[new_state, :])
katex.render(`Q(s,a)\\leftarrow${(1-machine.lr).toFixed(2)}Q(s,a)+${machine.lr.toFixed(2)}(reward + ${machine.df.toFixed(2)}\\max_{a'}(Q(s_{new}, a'))`, document.getElementById('formula'),{displayMode: true,});
5 years ago
}
render_latex();