Skip to content

Commit de6f7d9

Browse files
committed
use named parameters for functions that have 3+
fixes #63
1 parent 3a50e02 commit de6f7d9

6 files changed

Lines changed: 45 additions & 15 deletions

File tree

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ const start = (options) => {
8282
let actionName;
8383
if (useIntelligence) {
8484
stateActions = reduceStateAndAction(state);
85-
actionName = decide(intelligence, stateActions, actionNames);
85+
actionName = decide({ intelligence, stateActions, actionNames });
8686
} else {
87-
actionName = randomDecide(actionNames);
87+
actionName = randomDecide({ actionNames });
8888
}
8989
const action = actions[actionName];
9090
updateGame(action, state, reward); // reward and changes state
@@ -97,7 +97,14 @@ const start = (options) => {
9797
stateActions = reduceStateAndAction(state);
9898
const scoreAfter = state.score;
9999
const scoreDifference = scoreAfter - scoreBefore;
100-
learn(intelligence, previousStateActions, stateActions, actionName, actionNames, scoreDifference);
100+
learn({
101+
intelligence,
102+
previousStateActions,
103+
stateActions,
104+
previousAction: actionName,
105+
actionNames,
106+
reward: scoreDifference,
107+
});
101108
}
102109
state.frame += 1;
103110
if (state.frame < MAX_FRAMES) {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const step = () => {
7979
const state = initialState;
8080
const stateActions = reduceStateAndAction(state);
8181
const scoreBefore = state.score;
82-
const actionName = decide(intelligence, stateActions, actionNames);
82+
const actionName = decide({ intelligence, stateActions, actionNames });
8383
const action = actions[actionName];
8484
updateGame(action, state); // reward and changes state
8585
if (display) {
@@ -88,7 +88,14 @@ const step = () => {
8888
const stateActionsAfter = reduceStateAndAction(state);
8989
const scoreAfter = state.score;
9090
const reward = scoreAfter - scoreBefore;
91-
learn(intelligence, stateActions, stateActionsAfter, actionName, actionNames, reward);
91+
learn({
92+
intelligence,
93+
previousStateActions: stateActions,
94+
stateActions: stateActionsAfter,
95+
previousAction: actionName,
96+
actionNames,
97+
reward,
98+
});
9299
frame += 1;
93100
if (frame < MAX_FRAMES) {
94101
scheduleNext(step);

js/ai/qlearn/readme.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ Will use `.qualityMap`.
6464

6565

6666
```js
67-
const actionName = decide(intelligence, stateAction, actionNames);
67+
const actionName = decide({intelligence, stateAction, actionNames});
6868
```
6969

70-
Alternatively use partial random decide. It is the same function, except it randomly decides 20% of the time
70+
Alternatively use partial random decide. It is the same function, except it randomly decides 20% of the time.
7171

7272
```js
7373
import { partialRandomDecide } from "qlearn/source/partialRandomDecide.js";
@@ -86,11 +86,14 @@ Will update `.qualityMap`.
8686
Note: It is possible to learn even if the previous action did not come from `decide()`, for example: If a human decides, the `learn()` can still be used.
8787

8888
```js
89-
learn(
89+
learn({
9090
intelligence,
91-
previousStateActions, stateActions,
92-
previousAction, actionNames, reward
93-
);
91+
previousStateActions,
92+
stateActions,
93+
previousAction,
94+
actionNames,
95+
reward,
96+
});
9497
```
9598

9699
### `.qualityMap`

js/ai/qlearn/source/partialRandomDecide.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { randomDecide } from "./randomDecide.js";
44

55

66
const defaultRandomRate = 0.2;
7-
const partialRandomDecide = (intelligence, stateActions, actionNames, randomRate = defaultRandomRate, random = Math.random) => {
7+
const partialRandomDecide = ({
8+
intelligence,
9+
stateActions,
10+
actionNames,
11+
randomRate = defaultRandomRate,
12+
random = Math.random,
13+
}) => {
814
if (random() < randomRate) {
915
return decide(intelligence, stateActions, actionNames);
1016
}

js/ai/qlearn/source/qlearn.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const averageQuality = (qualityForState) => {
4141
return average;
4242
};
4343

44-
const decide = (intelligence, stateActions, actionNames) => {
44+
const decide = ({ intelligence, stateActions, actionNames }) => {
4545
const qualityForState = intelligence.qualityMap.get(stateActions);
4646
if (!qualityForState) {
4747
return actionNames[0]; // take first (random)
@@ -51,7 +51,14 @@ const decide = (intelligence, stateActions, actionNames) => {
5151
};
5252

5353
const createLearn = (getNextQualityEstimation) => {
54-
return (intelligence, previousStateActions, stateActions, previousAction, actionNames, reward) => {
54+
return ({
55+
intelligence,
56+
previousStateActions,
57+
stateActions,
58+
previousAction,
59+
actionNames,
60+
reward,
61+
}) => {
5562
let qualityForState = intelligence.qualityMap.get(previousStateActions);
5663
if (!qualityForState) {
5764
// there was no quality map for this set of state and actions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export { randomDecide };
22

33

4-
const randomDecide = (actionNames, random = Math.random) => {
4+
const randomDecide = ({ actionNames, random = Math.random }) => {
55
return actionNames[Math.floor(random() * actionNames.length)];
66
};

0 commit comments

Comments
 (0)