# Genetic Algorithm - Fit max circles inside box

I am using a genetic algorithm to find the best way to pack circles inside a box without each touching the others and filling as much space as possible. My doubt is if an individual from a generation must be a circle or all the circles.

Can anyone help me on how would I go from here? Thank you

• @Juho but then in the crossover two fit individuals (not touching) they would get closer each time. What would be the genome of each? Mar 26, 2018 at 17:30
• Sorry, I meant that each individual should be a candidate solution. It is up to you to pick an encoding of a solution (i.e., how to specify the location/sizes of individual circles).
– Juho
Mar 26, 2018 at 17:40
• @Juho my problem is that if I have two parents and merge cross them over, the child is likely to be inbetween them which is not ideal. Converging problems make sense for me, but diverging like this on doesnt Mar 26, 2018 at 17:43
• I might not be understanding you, but it's actually good that you also get solutions that are less fit. This is a built-in mechanism whose point is to explore the search space and avoid local optimums.
– Juho
Mar 26, 2018 at 17:48

In genetic algorithms, each individual should be a candidate solution to the problem. You're trying to find a packing of circles into the box, so each individual should be a complete packing that specifies the location of all the circles.

• That way how would the genome of each individual be? Mar 26, 2018 at 17:37
• @scottbear, that's a separate question. There are many possibilities; there is no one right answer, and it's up to you to pick something. If you have a new question, please post it separately using the 'Ask Question' button, but I suggest you first think about it, come up with some possible approaches, try them, and then tell us in the question what you have already tried and why you rejected those approaches.
– D.W.
Mar 26, 2018 at 17:41

A few people hear are pointing out that in a GA, individuals should be complete solutions to the problem. That's generally true, but there are evolutionary methods that do the opposite. Within the field of Learning Classifier Systems, the two approaches corresponding to your question are called the Michigan approach (each individual is only one rule or circle) and the Pittsburgh approach (each individual encodes the entire set of rules or circles). Currently, Michigan approaches (namely XCS and its variants) dominate the field, so there is evidence that you can craft such algorithms successfully. More generally, there are coevolutionary methods that have similar ideas.

The only restrictions you really have on an evolutionary method is that you need to be able to go from genotype to fitness value in some way, and you need some ability to perform transformations on the genotype like mutation and/or recombination. Mutation and crossover don't necessarily give you trouble whether or not you have one circle per individual or one solution per individual. The only issue is how do you assign fitness to one circle? If you solve that problem, then you can build algorithms that use one individual per circle.

It's certainly easier to write a fitness function that takes the entire solution as one individual, but you can do it the other way. If this is something you want to pursue, look at how XCS works and look into cooperative coevolutionary algorithms.

• What I did in terms of fitness was calculating the area that was overlapping and outside the box of each circle. But I get to the point where all have the same fitness and then child's are equal to parents Mar 26, 2018 at 18:19
• If you're using the model where each individual represents all the circles, then that's fine. Note that Genetic Algorithms will generally converge to something. Once you've converged, you see the behavior you mention. You can do things to reduce selection pressure, increase mutation, etc. to try to slow that process down, but you generally will converge at some point. Mar 26, 2018 at 20:15