Skip to content

Commit e908a4a

Browse files
committed
fixes #78
1 parent fe219a0 commit e908a4a

5 files changed

Lines changed: 59 additions & 42 deletions

File tree

js/ai/qlearn/examples/eatFoodAvoidDanger/eatFoodAvoidDanger.js

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
1+
export { start };
12
import { createIntelligence, learn, decide } from "../../source/qlearn.js";
23
import { draw, report } from "./draw.js";
34
import { initialState } from "./initialState.js";
45
import { scheduleNext } from "../scheduleNext.js";
56

6-
const MAX_FRAMES = 2000;
7-
const display = false;
8-
9-
let frame = 0;
10-
11-
const intelligence = createIntelligence();
127

138
const reduceStateAndAction = (state) => {
149
// we omit actions because they are always the same
@@ -53,8 +48,9 @@ const actions = {
5348
state.position[1] = futureY;
5449
},
5550
};
51+
const actionNames = Object.keys(actions);
5652

57-
const updateGame = (action, state) => {
53+
const updateGame = (action, state, frame) => {
5854
action(state);
5955
const [x, y] = state.position;
6056
state.dangers.forEach(([dangerX, dangerY]) => {
@@ -74,35 +70,39 @@ const updateGame = (action, state) => {
7470
};
7571

7672

77-
const actionNames = Object.keys(actions);
78-
const step = () => {
79-
const state = initialState;
80-
const stateActions = reduceStateAndAction(state);
81-
const scoreBefore = state.score;
82-
const actionName = decide({ intelligence, stateActions, actionNames });
83-
const action = actions[actionName];
84-
updateGame(action, state); // reward and changes state
85-
if (display) {
86-
draw(state, frame);
87-
}
88-
const stateActionsAfter = reduceStateAndAction(state);
89-
const scoreAfter = state.score;
90-
const reward = scoreAfter - scoreBefore;
91-
learn({
92-
intelligence,
93-
previousStateActions: stateActions,
94-
stateActions: stateActionsAfter,
95-
previousAction: actionName,
96-
actionNames,
97-
reward,
98-
});
99-
frame += 1;
100-
if (frame < MAX_FRAMES) {
101-
scheduleNext(step);
102-
} else {
103-
report(intelligence.qualityMap, state, frame);
104-
}
105-
};
73+
const start = ({ display, MAX_FRAMES }) => {
74+
75+
let frame = 0;
10676

77+
const intelligence = createIntelligence();
78+
const step = () => {
79+
const state = initialState;
80+
const stateActions = reduceStateAndAction(state);
81+
const scoreBefore = state.score;
82+
const actionName = decide({ intelligence, stateActions, actionNames });
83+
const action = actions[actionName];
84+
updateGame(action, state, frame); // reward and changes state
85+
if (display) {
86+
draw(state, frame);
87+
}
88+
const stateActionsAfter = reduceStateAndAction(state);
89+
const scoreAfter = state.score;
90+
const reward = scoreAfter - scoreBefore;
91+
learn({
92+
intelligence,
93+
previousStateActions: stateActions,
94+
stateActions: stateActionsAfter,
95+
previousAction: actionName,
96+
actionNames,
97+
reward,
98+
});
99+
frame += 1;
100+
if (frame < MAX_FRAMES) {
101+
scheduleNext(step);
102+
} else {
103+
report(intelligence.qualityMap, state, frame);
104+
}
105+
};
107106

108-
step();
107+
step();
108+
};

js/ai/qlearn/examples/eatFoodAvoidDanger/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
<p>Open console</p>
1010

1111

12-
<script src="./eatFoodAvoidDanger.js" type="module"></script>
12+
<script src="./visualDemo.js" type="module"></script>
1313
</body>
1414
</html>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { start } from "./eatFoodAvoidDanger.js";
2+
3+
const MAX_FRAMES = 2000;
4+
const display = false;
5+
6+
start({ MAX_FRAMES, display });
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { start } from "./eatFoodAvoidDanger.js";
2+
3+
const MAX_FRAMES = 4000;
4+
const display = true;
5+
6+
start({ MAX_FRAMES, display });

js/ai/qlearn/package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "qlearn",
3-
"version": "5.0.1",
3+
"version": "5.0.2",
44
"type": "module",
55
"module": "./source/qlearn.js",
66
"license": "CC0-1.0",
@@ -18,11 +18,16 @@
1818
"scripts": {
1919
"lint-fix": "eslint --fix source examples",
2020
"lint": "eslint source examples",
21-
"node-1": "node --experimental-modules examples/eatFoodAvoidDanger/eatFoodAvoidDanger.js",
22-
"deno-1": "deno examples/eatFoodAvoidDanger/eatFoodAvoidDanger.js",
21+
22+
"node-1": "node --experimental-modules examples/eatFoodAvoidDanger/start.js",
23+
"node-1-visual": "node --experimental-modules examples/eatFoodAvoidDanger/visualDemo.js",
24+
"deno-1": "deno examples/eatFoodAvoidDanger/start.js",
25+
"deno-1-visual": "deno examples/eatFoodAvoidDanger/visualDemo.js",
26+
2327
"node-2": "node --experimental-modules examples/dodgeShoot/start.js",
28+
"node-2-visual": "node --experimental-modules examples/dodgeShoot/visualDemo.js",
2429
"deno-2": "deno examples/dodgeShoot/start.js",
25-
"visual": "deno examples/dodgeShoot/visualDemo.js",
30+
"deno-2-visual": "deno examples/dodgeShoot/visualDemo.js",
2631
"serve": "serve ."
2732
},
2833
"dependencies": {},

0 commit comments

Comments
 (0)