Skip to content

Commit 3a50e02

Browse files
committed
lint, merge package.json from examples
1 parent 22fb250 commit 3a50e02

13 files changed

Lines changed: 551 additions & 726 deletions

File tree

js/ai/qlearn/examples/dodgeShoot/dodgeShoot.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
export { start };
22

33
import { deepCopy } from "../node_modules/utilsac/utility.js";
4-
import { randomDecide } from "../../source/randomDecide.js"
4+
import { randomDecide } from "../../source/randomDecide.js";
55
import { createIntelligence, decide } from "../../source/qlearn.js";
6-
import { draw, report } from "./draw.js";
6+
import { draw } from "./draw.js";
77
import { initialState } from "./initialState.js";
88
import { scheduleNext } from "../scheduleNext.js";
99

10-
10+
const framesASecond = 60;
1111
const isValidPosition = (w, max) => {
1212
return w >= 0 && w <= max;
1313
};
@@ -43,19 +43,19 @@ const updateGame = (action, state, reward) => {
4343
state.score += reward;
4444
}
4545
});
46-
state.missiles = state.missiles.filter(([dangerX, dangerY]) => {
46+
state.missiles = state.missiles.filter(([, dangerY]) => {
4747
return isValidPosition(dangerY, state.maxY);
4848
});
4949
state.missiles.forEach(missile => {
50-
missile[1] -= 1
50+
missile[1] -= 1;
5151
});
5252

5353
// the enemy emits a missile every n frames
5454
if (state.frame % 4 === 0) {
5555
state.missiles.push(state.positionEnemy.slice());
5656
}
5757
// move enemy from left to right periodically
58-
if (state.frame % 60 > 30) {
58+
if (state.frame % framesASecond > (framesASecond / 2)) {
5959
actions.moveLeft(state, state.positionEnemy);
6060
} else {
6161
actions.moveRight(state, state.positionEnemy);
@@ -67,15 +67,15 @@ const start = (options) => {
6767
reduceStateAndAction,
6868
learn,
6969
useIntelligence = true,
70-
MAX_FRAMES = 20000,
70+
MAX_FRAMES = 2 * 10 ** 4,
7171
display = false,
7272
reward = -1,
7373
} = options;
7474

7575
const state = deepCopy(initialState);
7676
const intelligence = createIntelligence();
7777

78-
return new Promise((resolve, reject) => {
78+
return new Promise((resolve) => {
7979
const step = () => {
8080
const scoreBefore = state.score;
8181
let stateActions;
@@ -99,7 +99,7 @@ const start = (options) => {
9999
const scoreDifference = scoreAfter - scoreBefore;
100100
learn(intelligence, previousStateActions, stateActions, actionName, actionNames, scoreDifference);
101101
}
102-
state.frame++;
102+
state.frame += 1;
103103
if (state.frame < MAX_FRAMES) {
104104
scheduleNext(step);
105105
} else {

js/ai/qlearn/examples/dodgeShoot/draw.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ export { draw, report, compactReport };
33

44
const draw = (state) => {
55
console.log(`-------- ${state.frame} -------- `);
6-
for (let y = state.maxY; y >= 0; y--) {
7-
let line = [`|`];
8-
for (let x = 0; x <= state.maxX; x++) {
6+
for (let y = state.maxY; y >= 0; y -= 1) {
7+
const line = [`|`];
8+
for (let x = 0; x <= state.maxX; x += 1) {
99
if (state.position[0] === x && state.position[1] === y) {
1010
line.push(`*`);
1111
} else if (state.positionEnemy[0] === x && state.positionEnemy[1] === y) {
@@ -33,7 +33,7 @@ const report = ({ state, qualityMap }) => {
3333

3434
const compactReport = ({
3535
state,
36-
qualityMap,
36+
/*qualityMap,*/
3737
reduceStateAndAction,
3838
useIntelligence,
3939
reward,

js/ai/qlearn/examples/dodgeShoot/reduceState.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ export {
22
reduceStateAndActionSeeAll,
33
reduceStateAndActionSeeNearestOnly,
44
reduceStateAndActionSeeAllDistance,
5-
}
5+
};
66

77
const distanceFromVector = (x, y) => {
8-
return (x ** 2 + y ** 2) ** 0.5;
8+
return (x ** 2 + y ** 2) ** (1 / 2);
99
};
1010

1111
const reduceStateAndActionSeeNearestOnly = (state) => {
1212
// we omit actions because they are always the same
13-
let missileInformation = state.missiles[0][1];
13+
const missileInformation = state.missiles[0][1];
1414

1515
return `${state.position}${missileInformation}`;
1616
};
@@ -20,7 +20,7 @@ const reduceStateAndActionSeeAll = (state) => {
2020
};
2121

2222
const reduceStateAndActionSeeAllDistance = (state) => {
23-
const [x, y] = state.position
23+
const [x, y] = state.position;
2424
const distances = state.missiles.map(([xm, ym]) => {
2525
return distanceFromVector(xm - x, ym - y);
2626
});

js/ai/qlearn/examples/dodgeShoot/start.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,19 @@ import {
33
reduceStateAndActionSeeNearestOnly,
44
reduceStateAndActionSeeAllDistance,
55
} from "./reduceState.js";
6-
import { report, compactReport } from "./draw.js";
6+
import { compactReport } from "./draw.js";
77
import { start } from "./dodgeShoot.js";
88
import { learn, learnWithAverage } from "../../source/qlearn.js";
99

1010

1111
const display = false;
1212

1313
const rewardsTrials = [1, -1];
14-
const framesTrials = [2000, 20000, 200000];
14+
const framesTrials = [
15+
2 * 10 ** 3,
16+
2 * 10 ** 4,
17+
2 * 10 ** 5,
18+
];
1519
// debugging
1620
learn.xname = `learn`;
1721
learnWithAverage.xname = `learnWithAverage`;
@@ -24,20 +28,22 @@ const reduceStateTrials = [
2428

2529
framesTrials.forEach(frames => {
2630
rewardsTrials.forEach(reward => {
27-
const options = {
31+
// do not include random in the big loop
32+
// as it will not change based on the algorithm
33+
const randomOptions = {
2834
reduceStateAndAction: undefined,
2935
learn: undefined,
3036
MAX_FRAMES: frames,
3137
useIntelligence: false,
3238
display,
3339
reward,
3440
};
35-
start(options).then(compactReport);
41+
start(randomOptions).then(compactReport);
3642
reduceStateTrials.forEach(reduceStateAction => {
37-
learnTrials.forEach(learn => {
43+
learnTrials.forEach(learnAlgorithm => {
3844
const options = {
3945
reduceStateAndAction: reduceStateAction,
40-
learn,
46+
learn: learnAlgorithm,
4147
MAX_FRAMES: frames,
4248
useIntelligence: true,
4349
display,

js/ai/qlearn/examples/dodgeShoot/visualDemo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
reduceStateAndActionSeeAll,
33
} from "./reduceState.js";
4-
import { report, compactReport } from "./draw.js";
4+
import { report } from "./draw.js";
55
import { start } from "./dodgeShoot.js";
66
import { learn, learnWithAverage } from "../../source/qlearn.js";
77

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ export { draw, report };
33

44
const draw = (state, frame) => {
55
console.log(`----- ${frame} ------ `);
6-
for (let y = state.maxY; y >= 0; y--) {
7-
let line = [`|`];
8-
for (let x = 0; x <= state.maxX; x++) {
6+
for (let y = state.maxY; y >= 0; y -= 1) {
7+
const line = [`|`];
8+
for (let x = 0; x <= state.maxX; x += 1) {
99
if (state.position[0] === x && state.position[1] === y) {
1010
line.push(`*`);
1111
} else if (state.dangers.some(([dx, dy]) => {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ const actions = {
3737
state.position[0] = futureX;
3838
},
3939
moveUp: (state) => {
40-
const [x, y] = state.position;
40+
const [, y] = state.position;
4141
const futureY = y + 1;
4242
if (!isValidPosition(futureY, state.maxY)) {
4343
return;
4444
}
4545
state.position[1] = futureY;
4646
},
4747
moveDown: (state) => {
48-
const [x, y] = state.position;
48+
const [, y] = state.position;
4949
const futureY = y - 1;
5050
if (!isValidPosition(futureY, state.maxY)) {
5151
return;
@@ -61,7 +61,7 @@ const updateGame = (action, state) => {
6161
if (x === dangerX && y === dangerY) {
6262
state.score -= 1;
6363
state.position = [0, 0];
64-
state.dangersTouched.push(frame)
64+
state.dangersTouched.push(frame);
6565
}
6666
});
6767
state.rewards.forEach(([X, Y]) => {
@@ -74,9 +74,9 @@ const updateGame = (action, state) => {
7474
};
7575

7676

77-
const state = initialState;
7877
const actionNames = Object.keys(actions);
7978
const step = () => {
79+
const state = initialState;
8080
const stateActions = reduceStateAndAction(state);
8181
const scoreBefore = state.score;
8282
const actionName = decide(intelligence, stateActions, actionNames);
@@ -89,7 +89,7 @@ const step = () => {
8989
const scoreAfter = state.score;
9090
const reward = scoreAfter - scoreBefore;
9191
learn(intelligence, stateActions, stateActionsAfter, actionName, actionNames, reward);
92-
frame++;
92+
frame += 1;
9393
if (frame < MAX_FRAMES) {
9494
scheduleNext(step);
9595
} else {

0 commit comments

Comments
 (0)