We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Alright. So I've been attempting to create a genetic algorithm to make a ball learn to find a goal. In order to calculate fitness, I made a vector between the ball and the goal, and measured the magnitude. I have tried to store the values of this changing magnitude in an array to find the maximum fitness later. However, I cannot seem to just add values to the array without either just changing the value of the one data point in the array or creating new arrays with the added value. Any suggestions? Similarly, how do you think I can make "chromosomes" and create new, modified versions of the object in the object array?
Answers
https://forum.Processing.org/two/discussion/17524/why-can-t-i-append-a-new-vector-to-an-array-of-vectors
blows off a lot of dust
Tons of irrelevant code there, posted without comment.
The first line is the important one, see ArrayList in the reference. ArrayList is like an array you can extend...
It's not irrelevant! It's showing off chromosome-based movement!
that's the kind of comment that should've been posted alongside the code 8)
Thanks all for the quick replies and helpful comments. What I have gathered from TfGuy44's comments and code is that I should perhaps use dist() instead of the magnitude of a vector to calculate the distance between the goal and the ball, and then use strings instead of arrays to store my values. Similarly, the code helped with my problems with genomes and such. Thanks a lot again and I will post more questions as I inevitably fail miserably.
Just a few questions about your code, TfGuy44. In your program, how does the car move? Similarly, what characteristics are carried over between generations? Thanks and if you would like to clarify any other sections as well, that would be wonderful. @TfGuy44
When I first wrote the Car class, I only had one instance of it, and it was controlled by the arrow keys. The LEFT arrow key would turn it to the left. The RIGHT arrow key would turn it to the right. the UP arrow key would give it some gas and make it accelerate forward in the direction it was pointing. You could also not press any key, and it would gradually slow down.
Essentially, then, there are four "actions" that make the car move differently: LEFT, RIGHT, ACCELERATE, and NOTHING. For short, let's call them L, R, A, and N.
When arrow-key-based control was removed, I switched the Cars over to having a genome-based movement. The "genes" a car possessed were a list of actions to take in the hopes that these actions would move the car closer to the goal. For example, (NNNAARNN) means "Do nothing for a bit, then accelerate some, and turn to the right, then coast for a while." This COULD get the car closer to the goal... if the goal was ahead of the car on the right. But maybe the goal was to the left of the car. In that case (LLA) might be a "better" gene sequence for a car to have.
From there, it was an easy jump to having a generation of cars. Each would have a gene sequence that mapped directly to how the car moved. Each car was then simulated, and the "best" gene sequence would be kept for the next generation. Then you throw in some breading (the first half of one car's sequence and the second half of anothers) and mutation (randomly removing, adding, or changing an action), and suddenly you have cars that get closer and closer to the goal.
Thanks a lot. The code is very impressive. Do you mind if I base sections of my code off of yours if I credit you? Thanks.
You can base your code off it, sure. I would use it as an example and write your own, new version of it, however, since it's sort of a mess...
http://natureofcode.com/book/chapter-9-the-evolution-of-code/
http://www.karlsims.com/papers/siggraph91.html