`
-})
+}
+
+const PopupLibrary = {
+ install(Vue, options = {}) {
+ const root = new Vue(light_box)
+
+ // Mount root Vue instance on new div element added to body
+ root.$mount(document.body.appendChild(document.createElement('div')))
+
+ Vue.prototype.$lightbox = root;
+ }
+}
+
+window.Vue.use(PopupLibrary)
// ----------------------------------------------------------------------------
// -------------------------------- Main --------------------------------------
// ----------------------------------------------------------------------------
-
-function make_machine_reactive(th, machine){
+function makeMachineReactive(th, machine){
var $this = th;
+ $this.machine.s2p = function(state) {
+ return {
+ x: (state % $this.maze.width),
+ y: Math.floor(state / $this.maze.width),
+ }
+ };
+ $this.machine.p2s = function(x, y) {
+ return x + y * $this.maze.width;
+ };
+
// Score wrapper
var s = machine.score;
$this.machine.score = s;
@@ -565,7 +541,7 @@ function make_machine_reactive(th, machine){
// State wrapper
var s = machine.state;
- $this.machine.state = $this.s2p(s);
+ $this.machine.state = $this.machine.s2p(s);
Object.defineProperty(machine, 'state', {
get: function() {
return this._state
@@ -577,21 +553,7 @@ function make_machine_reactive(th, machine){
});
machine.state = s;
- $this.machine.s2p = $this.s2p;
- $this.machine.p2s = $this.p2s;
-
- window.addEventListener("episode", (ev) => {
- if (ev.detail == "failed"){
- $this.lightText = "Out of battery. The robot will be resetted.";
- } else if (ev.detail == "success"){
- $this.lightText = "You reached the goal. The robot will be resetted.";
- }
- $this.lightOptions = {
- "ok": () => {$this.lightpopup = false;},
- }
- $this.lightpopup = true;
- })
-
+ $this.machine.object.setCallback($this.onNewEpisode);
}
app = new Vue({
@@ -600,7 +562,7 @@ app = new Vue({
VueSlider: window['vue-slider-component'],
},
data: {
- state: 0,
+ state: null,
maze: maze,
machine: {
object: machine,
@@ -620,21 +582,15 @@ app = new Vue({
},
width: 0,
height: 0,
- state: null,
components: [],
- lightText: "",
- lightOptions: "",
- lightpopup: false,
navigation: {},
- onEnterState: function(){},
- onLeaveState: function(){},
},
created() {
// Resize handler
window.addEventListener('resize', this.handleResize)
this.handleResize();
- make_machine_reactive(this, machine);
+ makeMachineReactive(this, machine);
this.state = "init";
},
destroyed() {
@@ -643,14 +599,8 @@ app = new Vue({
computed: {
stage_config: function() {
return {
- width: this.width,
- height: this.height,
- }
- },
- map_config: function() {
- return {
- x: this.width*0.25,
- y: this.height*0.1,
+ x: 0,
+ y: 0,
width: this.width*0.5,
height: this.height*0.8,
}
@@ -707,23 +657,16 @@ app = new Vue({
},
},
methods: {
- s2p: function(state) {
- return {
- x: (state % this.maze.width),
- y: Math.floor(state / this.maze.width),
- }
- },
- p2s: function(x, y) {
- return x + y * this.maze.width;
- },
+ onEnterState: function(){},
+ onLeaveState: function(){},
handleState: function(s) {
if (!this.machine.object.running) {
this.machine.state_tween.to(this.machine.state, 0.2, {
- x: this.s2p(s).x,
- y: this.s2p(s).y
+ x: this.machine.s2p(s).x,
+ y: this.machine.s2p(s).y
});
} else {
- this.machine.state = this.s2p(s);
+ this.machine.state = this.machine.s2p(s);
}
},
handleResize: function() {
@@ -735,23 +678,29 @@ app = new Vue({
},
changeState: function(state){
this.components = [];
- this.lightText = "";
- this.lightOptions = "";
- this.lightpopup = false;
this.navigation = {};
this.onEnterState = function(){};
this.onLeaveState = function(){};
this.state = state;
+ },
+ onNewEpisode: function(result){
+ var text;
+ if (result == "failed"){
+ text = "Out of battery. The robot will be resetted.";
+ } else if (result == "success"){
+ text = "You reached the goal. The robot will be resetted.";
+ }
+ return this.$lightbox.popup(text, ["ok"]);
}
},
watch: {
'machine.learning_rate': function(new_val) {
machine.lr = new_val;
- render_latex();
+ renderLatex();
},
'machine.discount_factor': function(new_val) {
machine.df = new_val;
- render_latex();
+ renderLatex();
},
'machine.epsilon': function(new_val) {
machine.epsilon = new_val;
@@ -764,11 +713,11 @@ app = new Vue({
}
})
-function render_latex() {
+function renderLatex() {
// (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,});
}
-render_latex();
+renderLatex();
@@ -778,34 +727,23 @@ render_latex();
var StateMgr = {
init: {
- lightText: `Reinforcement learning (RL) is an area of machine learning concerned with how software agents ought to take actions in an environment so as to maximize some notion of cumulative reward. Reinforcement learning is one of three basic machine learning paradigms, alongside supervised learning and unsupervised learning. (wikipedia)
- This exhibit explains how a robot can learn to navigate through a maze in order to reach its destination, before running out of power. At first the robot knows nothing, and learns from each new action (movement) and state (location reached). Slowly it starts to develop an understanding of the maze that will allow it to reach the charging station before it runs out of power. Eventually, it should learn to avoid any detour and reach the charging station in the optimal number of steps.`,
- lightOptions: {
- "next": () => app.changeState("local"),
- },
onEnterState: function () {
- this.lightpopup = true;
+ var lightText = `Reinforcement learning (RL) is an area of machine learning concerned with how software agents ought to take actions in an environment so as to maximize some notion of cumulative reward. Reinforcement learning is one of three basic machine learning paradigms, alongside supervised learning and unsupervised learning. (wikipedia)
+ This exhibit explains how a robot can learn to navigate through a maze in order to reach its destination, before running out of power. At first the robot knows nothing, and learns from each new action (movement) and state (location reached). Slowly it starts to develop an understanding of the maze that will allow it to reach the charging station before it runs out of power. Eventually, it should learn to avoid any detour and reach the charging station in the optimal number of steps.`
+ this.$lightbox.popup(lightText, ["next"]).then((r) => this.changeState("local"));
},
- onLeaveState: function () {
- this.lightpopup = false;
- }
},
local: {
components: ["local", "navi", "score"],
navigation: {
"reset robot": () => machine.reset_machine(),
- "continue": () => app.changeState("global"),
- },
- lightText: "But there is a problem! The robot cannot see the whole maze, it only knows where it is and in which direction it can move. Can you reach the charging station in those conditions? Use the arrows to move",
- lightOptions: {
- "next": () => {app.lightpopup = false;},
+ "continue": null,
},
onEnterState: function () {
- this.lightpopup = true;
+ this.navigation.continue = () => this.changeState("global");
+ var lightText = "But there is a problem! The robot cannot see the whole maze, it only knows where it is and in which direction it can move. Can you reach the charging station in those conditions? Use the arrows to move";
+ this.$lightbox.popup(lightText, ["next"]);
},
- onLeaveState: function () {
- this.lightpopup = false;
- }
},
global: {
components: ["global", "sliders", "plot", "navi", "score"],
@@ -816,44 +754,11 @@ var StateMgr = {
"greedy step!": () => machine.greedy_step(),
"reset machine": () => machine.reset_machine(),
},
- lightText: `As a human, you keep track of where you are and how you got there without thinking, which helps you think about what actions you should take next to reach your destination. And you can also just look around! How can then the robot 'think' of the maze, to know which action is the best at every moment? And how can it learn that? It must somehow keep track of where it is, and remember how good or bad was each action at each place in the maze, try new things, and update it's "mental image" of what was a good decision and what not.
-
- Reinforcement Learning uses the concept of a "Q-function", which keeps track of how "good" it expects it to be to take a specific action 'a' from a specific location 's'. This is written as Q(s, a). It also uses a "policy", which determines the best action to take in a given state, and is written as π(s). The robot must learn those functions while it navigates the maze. With each step, the functions are modified by a little bit, until eventually they give it the best strategy to solve the maze.`,
- lightOptions: {
- "continue": () => {app.lightpopup = false;},
- },
onEnterState: function () {
- this.lightpopup = true;
+ var lightText = `As a human, you keep track of where you are and how you got there without thinking, which helps you think about what actions you should take next to reach your destination. And you can also just look around! How can then the robot 'think' of the maze, to know which action is the best at every moment? And how can it learn that? It must somehow keep track of where it is, and remember how good or bad was each action at each place in the maze, try new things, and update it's "mental image" of what was a good decision and what not.
+
+ Reinforcement Learning uses the concept of a "Q-function", which keeps track of how "good" it expects it to be to take a specific action 'a' from a specific location 's'. This is written as Q(s, a). It also uses a "policy", which determines the best action to take in a given state, and is written as π(s). The robot must learn those functions while it navigates the maze. With each step, the functions are modified by a little bit, until eventually they give it the best strategy to solve the maze.`;
+ this.$lightbox.popup(lightText, ["continue"]);
},
}
};
-
-
-// ----------------------------------------------------------------------------
-// ------------------------------- Dummy --------------------------------------
-// ----------------------------------------------------------------------------
-
-// Vue.component('dummy', {
-// props: ['config'],
-// data: function () {
-// return {
-// robot_image: null,
-// }
-// },
-// created() {
-//
-// },
-// mounted() {
-//
-// },
-// computed: {
-// fun: function() {
-// return
-// },
-// },
-// methods: {
-// fun: function(arg) {
-// },
-// },
-// template: ``
-// })