Guest User

Untitled

a guest
Jan 24th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. function handleReset(Dinos) {
  2. // Checks if this is the first time it is called and set first time as false
  3. if (firstTime) {
  4. firstTime = false;
  5. // Initialize the random Model for each Dino.
  6. Dinos.forEach((dino) => {
  7. dino.model = new RandomModel();
  8. dino.model.init();
  9. });
  10. }
  11. else {
  12. /* If this is not the first time it is called, then we need to train
  13. our model to form next generation.*/
  14. // Train model before restarting
  15. console.info('Training');
  16.  
  17. /* We create an array called chromosomes which uses the getChromosome
  18. function of the randomModel class which converts weights ans bias
  19. into an array of size 4 [w1,w2,w3,b1].so each element of chromosomes
  20. array is another array of size 4 */
  21.  
  22. const chromosomes = rankList.map((dino) => dino.model.getChromosome());
  23. // console.info(chromosomes)
  24. // Clear rankList
  25. // We empty the ranklist so that we can store the next gen in it.
  26. rankList.splice(0);
  27.  
  28. /* We call the fit function of the genetic model on chromosomes array
  29. which then calls our genetic operators and creates the new chromosomes
  30. array and sets them in the models of the dinos.
  31. */
  32.  
  33. chromosomes = geneticModel.fit(chromosomes);
  34.  
  35. Dinos.forEach((dino, i) => {
  36. dino.model.setChromosome(chromosomes[i]);
  37. });
  38. }
  39. }
Add Comment
Please, Sign In to add comment