# Using Chromosomes to Go from Input to Output in Genetic Algorithms

Lately I've developed an interest in AI and genetic algorithms. Specifically, AI that learns to do something completely on its own. For example, play a video game. I've seen AIs that start out playing the game with no knowledge of what any of the buttons do, the only information they are given is their position, velocity, and the solid tiles and entities near them. Eventually the AI learns to run and jump. Later on, it learns to avoid enemies.

I mostly understand how genetic algorithms work, but there is one thing I really don't get. The chromosome is basically the instructions for the AI, right? My question is, how do you use the chromosome of the AI to go from the inputs(the AI's position, velocity, nearby tiles/enemies) to the outputs(button presses)? I think this is called neural networks, but after researching for an hour or so I didn't really understand the concept. Is the answer to my question just a bunch of "if" statements?

if(enemyNear)
{
if(chromosomeContents[0]==1)
jump();
else
keepRunning();
}


That seems like it would be redundant, since you would have to manually program in all of the best solutions, right? I'm not sure; I feel like I'm missing something really obvious here. If anyone could explain this to me, that would be fantastic. :D (Sorry if my explanation was confusing. I tried to explain the best I could.)

• Just so you know, these topics are generally covered in master's degree in universities. And they are a single subject class worth of 45 hours of teaching. You'll have to spend a great deal of time to understand and master it. – Vaillancourt Mar 16 '15 at 1:00

You wouldn't have to hardcode the best answers. You'd make chromosomes out of genes that indicate what to compare and how to compare them.

Comparison DNAs: < > == !=
Modifying DNAs: + - * /
Value DNAs: player.position.x, 9, 3.18, etc
Condition Extension DNAs: && ||

If you make a bunch of genes out of some combination of the above DNAs you can get meaningful behaviour after a number of generations. You could create rules that immediately abort any AI with a gene with a bad mutation that won't compile. You could do a lot of things, but a real bare bones example might look something like:

class Gene{
DNA[] sequence;
Output action;
bool Evaluate(){
//turn sequence into a true/false comparison & return
}
}
foreach ( gene; chromosome ){
if( gene.Evaluate ){
AI.do(gene.action);
}
}


The important factor in your DNA learning is how you select and recombine traits each generation. For example, you don't want to breed the AI that sat in the corner all level with the one that got 1/3 of the way though the level, you want to combine the one that got halfway thought the level with one that got 3/4 of the way though.

• Interesting... I never thought about it that way. Thank you! :) – TheBeocro Mar 16 '15 at 10:52