genetic_evolution 0.3.0 genetic_evolution: ^0.3.0 copied to clipboard
A package that simulates a Genetic Evolutionary Algorithm among its generations.
genetic_evolution #
A package that simulates a Genetic Evolutionary Algorithm among its generations.
Table of Contents #
Basic Usage #
First, you must define your own GeneService
, describing the Genes within a DNA strand. Notice that we are defining a Gene to be of type <String>
.
class MyGeneService extends GeneService<String> {
...
}
Then, you must define your own FitnessService
, describing how well an Entity's DNA performs.
class MyFitnessService extends FitnessService<String> {
...
}
Next, you can initialize a GeneticEvolution
object.
GeneticEvolution geneticEvolution = GeneticEvolution(
geneticEvolutionConfig: GeneticEvolutionConfig(
numGenes: 10,
mutationRate: 0.25
),
fitnessService: MyFitnessService(),
geneService: MyGeneService(),
);
And finally, you can get the next generation using nextGeneration()
.
Generation generation = await geneticEvolution.nextGeneration();
Examples #
Simple Word Example #
The code for this Word example can be found in the /example/word_example
folder (here).
Intermediate Color Contrast Example #
The code for this Color Contrast example can be found in the /example/color_contrast_example
folder (here).
File Parsing #
You can write specific generations to a file, and similarly read in specific generations stored on a file.
First, define your own JsonConverter
. (This class describes how to convert objects of Type <T>
to and from JSON)
// Type <T> is Type <String> in this example
class GeneJsonConverter extends JsonConverter<String, Map<String, dynamic>> {
@override
fromJson(Map<String, dynamic> json) {
return json['text'];
}
@override
toJson(String object) {
return {'text': object};
}
}
Next, define your FileParser
object based on your above JsonConverter
.
final fileParser = FileParser<String>(
geneJsonConverter: GeneJsonConverter(),
);
Include this fileParser
within the GeneticEvolution
constructor.
final geneticEvolution = GeneticEvolution(
fileParser: fileParser,
... // other params go here
)
Now, you can write the current Generation
to a file.
geneticEvolution.writeGenerationToFile();
Lastly, you can load in a specific Generation
read from a file.
geneticEvolution.loadGenerationFromFile(wave: 10);