1+ export { start } ;
12import { createIntelligence , learn , decide } from "../../source/qlearn.js" ;
23import { draw , report } from "./draw.js" ;
34import { initialState } from "./initialState.js" ;
45import { 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
138const 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+ } ;
0 commit comments