# Neural Network genetic algorithm game

**URL:** <https://discourse.processing.org/t/neural-network-genetic-algorithm-game/28146>\
**Category:** Coding Questions\
**Created:** [February 28, 2021, 6:37pm UTC](https://discourse.processing.org/t/neural-network-genetic-algorithm-game/28146 "2021-02-28T18:37:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Farbod](https://avatars.discourse-cdn.com/v4/letter/f/a8b319/32.png) [@Farbod](https://discourse.processing.org/u/Farbod)\
**Post date:** [February 28, 2021, 6:37pm UTC](https://discourse.processing.org/t/neural-network-genetic-algorithm-game/28146/1 "2021-02-28T18:37:42Z")

</div>

I am trying to train a neural network to be able to shoot a bubble accurately. My solution is to have a brain inside the “arrow” and make it determine the shooting “angle” based on the scores it receives. I have followed all the instructions given by Daniel Shiffman. However, it seems my neural network can’t learn any thing for some unknown reasons!

Please let me know if I did something wrong. Here is the code:

[https://editor.p5js.org/ftabaei/sketches/KktHs405n](https://editor.p5js.org/ftabaei/sketches/KktHs405n)

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [March 1, 2021, 5:58pm UTC](https://discourse.processing.org/t/neural-network-genetic-algorithm-game/28146/2 "2021-03-01T17:58:31Z")

</div>

hi! The project looks nice! But your project seems quite huge (for someone to take a look and spot an issue) and it would be helpful if you can elaborate the problem a bit more - for example, why do you think it cannot learn? Is it based on your observation? Which video/material of Dan do you refer to? Genetic algorithm may not be the most efficient way to train a model, so you may need to wait for some iterations.

---

<div class="post-metadata">

**Author:** ![Farbod](https://avatars.discourse-cdn.com/v4/letter/f/a8b319/32.png) [@Farbod](https://discourse.processing.org/u/Farbod)\
**Post date:** [March 1, 2021, 6:39pm UTC](https://discourse.processing.org/t/neural-network-genetic-algorithm-game/28146/3 "2021-03-01T18:39:17Z")

</div>

Hi Micuat,  
Thanks for your reply!  
Regarding this project, I was hoping to find out if the inputs and outputs that I defined for the “arrows” are correct. So the “arrow.js” file is the critical file to look at. I copied all other neural net and genetic algorithm scripts from “[p5.js Web Editor](https://editor.p5js.org/codingtrain/sketches/wb-QHVXBq)” which is developed by Daniel and works perfectly. Based on my observation, after a number of generations there should be an arrow to stay in the scene and hit balloons. My policy is to reposition the arrows that hit the balloons on the top origin point and dispose other unsuccessful arrow. So the arrow with the best performance should be able to hit a balloon and appear in the origin base iteratively 🙂

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [March 2, 2021, 2:24pm UTC](https://discourse.processing.org/t/neural-network-genetic-algorithm-game/28146/4 "2021-03-02T14:24:19Z")

</div>

yes I remember this example! I can only give you a high level comment, which I suggest taking a look at the guideline

> [@Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147):
>
> Summary (TL;DR) Ask complete questions to get better answers! Be specific. For example: I want to load an image. I tried using createImg() , and I expected the image to be on canvas, but what happened instead was the image showing up below the canvas. Isolate your problem and work in small steps. Share only the code directly related to your problem if it is part of a bigger project, and if something isn’t working, try the smallest code that could do the thing you want. Can we run your code to …

What I noticed in you code is that you changed basically two major things - you are using tensorflow.js which doesn’t seem to be included in Dan’s example (or does he have another example that uses tf?), and you changed the “game” itself from the flappy bird to balloon popping. So, first it’s important to separate them to see which is actually causing the issue (the neural net or the game logic, or it can be both of them)

---

<div class="post-metadata">

**Author:** ![Farbod](https://avatars.discourse-cdn.com/v4/letter/f/a8b319/32.png) [@Farbod](https://discourse.processing.org/u/Farbod)\
**Post date:** [March 3, 2021, 4:48am UTC](https://discourse.processing.org/t/neural-network-genetic-algorithm-game/28146/5 "2021-03-03T04:48:51Z")

</div>

Thank you very much for your help!

Yes, admittedly I need to pin point the problem better than this. However, I really don’t know what is the problem! 🙂 Regarding the tensorflow.js, Daniel did this in this example and I followed his guidelines. I think my game mechanics is causing the problem.

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [March 3, 2021, 11:14am UTC](https://discourse.processing.org/t/neural-network-genetic-algorithm-game/28146/6 "2021-03-03T11:14:20Z")

</div>

I see! I don’t know this is helpful, but I noticed that your game actually is dependent on other pins - a pin chases a balloon but it can be popped by another one before it reaches. This doesn’t happen in Dan’s example because the walls are not affected by other birds. This may be making the learning more difficult - what if you make the balloons not going to disappear even though they are popped by other pins, to see if the learning becomes more efficient?

---

<div class="post-metadata">

**Author:** ![Farbod](https://avatars.discourse-cdn.com/v4/letter/f/a8b319/32.png) [@Farbod](https://discourse.processing.org/u/Farbod)\
**Post date:** [March 3, 2021, 4:01pm UTC](https://discourse.processing.org/t/neural-network-genetic-algorithm-game/28146/7 "2021-03-03T16:01:50Z")

</div>

Very good point! Yeah, I didn’t think about this issue. If fact when I increased the number of balloons and decreased the number of pins the learning rate improved! I am going to keep the balloons in the scene and let you know the outcome 🙂 Thank you very much!
