Artificial intelligence that evolves in Python

25,476

Solution 1

Interestingly, I've created what you're describing, in Python.

Here's a pic:

enter image description here

The green dots are food, and the red ones are organisms that need to evolve and learn how to eat the food blocks.

The best source of inspiration and guidance for something like this is definitely Polyworld: youtube.com/watch?v=_m97_kL4ox0.

Another great example which helped me learn about backpropagation, which I suggest you should learn too, is here: arctrix.com/nas/python/bpnn.py.

Although this question is simply too vague to answer correctly, the general tip is that you first get a solid ground on how ANN's function, and then construct a program with this general algorithm:

  • Check for user input (should the camera move? should the program quit?)
  • For each organism:
    1. Check if it is alive, etc.
    2. Take input from the world
    3. Evaluate its neural network
    4. Apply the results to the physical body
    5. Apply positive or negative feedback (as you'll see, it all comes down to this)
  • Update the world (you can use a physics engine)
  • Optionally, draw the world

For example, your input neurons could correspond with physical or visual stimulation. Is there something in front of me? Then set neuron X to 1, otherwise to 0.

And your output neurons could map into forces. Is neuron Y active? If so, apply an impulse to this organism's in this world cycle.

I also recommend that you don't use other people's libraries for your neural network computation, because this is a great way to actually learn how to implement them yourself. You could you Pygame for rendering, and PyBox2D for physics, though.

Now, about "seeing" other organisms... These problems are best solved when isolated. You need a good software design, that will enable you to divide the whole problem into many subproblems, which are easier to solve.

In general, seeing can be done by raycasting. Create a directional, normalized vector (where the length is 1 unit, that is, sqrt(x*x + y*y) == 1), and then scale it (x *= factor; y *= factor;). That way, you get a line that's stretching outwards. Before you scale it, you can then loop through all of the organisms in the world (you'll have to optimize this), and check if the vector is inside of them. If it is, calculate its length, and your organism has some information about its surroundings.

There are many simpler ways. For example, divide space around each organism into four quadrants, which four represent eyes (X - up, right, down, left of the organism). Then calculate the length from the organism to every other organism (optimize!). Find the closest organism. Find out in which quadrant it is. Voila, an active eye!

I hope I was helpful, but you'll have to be more precise and come back when you have an actual problem. Read the FAQ as well.

Solution 2

http://scikit-learn.org/ is a great resource (and framework) for you to learn about machine learning in python.

If you want to build a neural network, http://pybrain.org/ is a good framework.

Share:
25,476
user1689935
Author by

user1689935

Updated on July 18, 2022

Comments

  • user1689935
    user1689935 almost 2 years

    So I've been thinking of creating a program similar to this in Python. The problem is that I know next to nothing about AI. Right now I'm reading up on genetic algorithms and neural networks and such, but there's a lot of things to absorb.

    My largest problem now is I don't know how the entire thing is put together. So if anyone can tell me the general framework for a program like this, I'll be really grateful. For example, how the creatures can "see" things in their surroundings, the kind of "chromosomes" they have and how the environment is created.

    Also, can anyone suggest suitable libraries for AIs, any engines, etc? I've been thinking of using neurolab, will that be a good idea?

    Thanks!