0

Chrome keeps killing the page in the middle of my connect-four browser game when it is running properly. The game is a player vs computer setup and the game itself runs properly and never crashes. The page crashes when I set the number of iterations too high for training the computer opponent. The programs trains the ai using a qLearning algorithm where it plays itself and stores a value for each encountered state. If I set the number of iterations to about 125,000 or less, then everything works fine (except the opponent is not so great). I cannot tell if it is the running time of the loop (would take about 30 minutes to run) that kills the program or something else such as memory constraints for recording states and their corresponding q-values.

How can I get the program to run for more training iterations without chrome killing the page?

Matt S
  • 1,434
  • 1
  • 15
  • 16

1 Answers1

2

You've got a couple of options on how to handle your code.

Option 1: setInterval / setTimeout

As others have suggested, using setInterval or setTimeout can run your code in "chunks" and no one chunk will cause a timeout.

Option 2: setInterval + generators

With deeply nested code, using setTimeout is very difficult to properly re-enter the code.

Read up on generators -- that makes running code in chunks much nicer, but it may take some redesign.

Option 3: webworkers

Webworkers provide another way, depending on what you are calculating. They run in the background and don't have access to the DOM or anything else, but they are great at calcuation.

Option 4: nodejs

Your last option is to move away from the browser and run in another environment such as node.JS. If you are running under Windows, HTA files may be another option.

Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74
  • I would say the starting point should definitely be webworkers as 1) the calculation is not UI related, so it should not be on the main thread, and 2) the method takes more than 0.5-1 seconds, so it shouldn't be on the UI thread for a second reason. 3) I'm imagining chrome is killing the calc because it's on the main thread where it has heavy restrictions, whereas if it were on a background thread, chrome would be more lenient. –  Jun 22 '16 at 19:12