-
Notifications
You must be signed in to change notification settings - Fork 0
GA Polygons
Relevant namespaces: Genetic_Algorithm.GA.Generics, Polygons.GA, Polygons.GA.FitnessCalculators
This is an overview of the classes implemented based on GA's generic templates to introduce polygons into the genetic algorithm.
Notable principles of this class are its fitness and mutation system.
The individual's fitness has an indicator of being invalid. This indicator is assigned on initialisation (individual doesn't know its fitness) and after any mutation.
If a fitness calculator encounters an already valid fitness, it doesn't have to run any new calculations and simply retrieves the present value. Otherwise, the fitness for the individual is calculated and assigned.
The mutation method makes a decision whether a gene should mutate (for every gene separately), lets the chosen genes mutate and then has to let every gene know that it needs to update its relative angle and distance representations, since those change whenever a single gene changes its "location" (because any change also moves the centroid of the shape).
Being an IGene encoding of a System.Drawing.Point, these genes contain X and Y coordinates and additionally an angle and distance of the encoded point relative to the whole individual's centroid. This alternative representation is introduced as it might be more practical in some cases or could be more sensible as a crossover parameter should an adapter decide that it wants to perform crossover by making averages of individuals' genes.
This class is a concrete implementation of the abstract Genetic_Algorithm.GA.Generics.GeneticAlgorithmAdapter.
The only method that is required for implementation is CrossOver, where the PolygonAdapter zips genomes of two PolygonIndividuals (similar to creating an enumeration of tuples with one gene from parent1 and second gene from parent2) and then with 0.5 probability chooses the gene of one or the other parent.
The adapter also creates an offspring's name and outline color based on its parents, but that is merely a cosmetic feature.
Currently, there are two fitness calculators implemented for polygons:
BasicSymmetryFitnessCalculatorSymmetryIntersectionPenaltyFitnessCalculator
This calculator assigns a highest possible fitness to every individual and then iterates through every gene in the individual's genome, performing the following actions on each gene (note that genes in this context are synonymous to vertex locations, since that is what is being encoded):
- determines the location of a "perfectly mirrored relative" for that gene (a point which would be the exact mirrored image against the specified symmetry axis)
- finds an actual gene whose position is closest to the "perfect location"
- subtracts the distance between the ideal and actual position of the closest mirrored relative from the total fitness The result fitness is thus the lower the more deviation from ideal symmetry there is for each vertex of the polygon.
This fitness calculator inherits from Basic Symmetry Fitness Calculator, so the first part of an individual's fitness evaluation is the same.
Afterwards, it checks every pair of the polygon's edges for intersection and divides the basic fitness by the amount of intersections found, so every intersection has a great negative impact on the shape's fitness.