Genetic Algorithm
Classes | Macros | Typedefs | Functions
genetic_algorithm_utils.h File Reference

Utility functions for performing different operations on a genome. More...

#include <stdint.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <stdbool.h>
Include dependency graph for genetic_algorithm_utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  genome_t
 The genome structure. It contains genes, the length of those genes and a fitness score. The genome must be initialized with the genome_target_init function before use. More...
 

Macros

#define GENE_POOL   "!@#$^&*()_-=+,.;:'/\\\"{}[]<>? 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
 This is a gene pool. You can add your own characters to this as long as they don't mess with the functions and stuff. More...
 

Typedefs

typedef char gene_t
 A singular gene is just a character. More...
 
typedef struct genome_t genome_t
 The genome structure. It contains genes, the length of those genes and a fitness score. The genome must be initialized with the genome_target_init function before use. More...
 

Functions

genome_t genome_target_init (char *string)
 Initialization of the target genome is slightly different as it already has allocated memory and does not need any mutation. More...
 
genome_t genome_init (uint16_t length)
 Creates a new genome object with allocated memory of the given size. The genome must be manually freed by genome_destroy() after it has been used. More...
 
void genome_destroy (genome_t *p_genome)
 Frees the gene memory allocated to the genome and resets its fitness and length. More...
 
void genome_copy (genome_t *destination, const genome_t *source)
 Performs a deep copy of source to destination while maintaining their original references. More...
 
void genome_print (const genome_t genome)
 Function for printing genomes in a readable format. More...
 
int random_in_pos_range (const int upper_limit, const int lower_limit)
 Provides a pseudo random number between a positive range. More...
 
char get_mutated_gene (void)
 Function for extracting a mutated / random gene from the available gene pool specified in the GENE_POOL macro. More...
 
int genome_calculate_fitness (const char *target, const char *genome, uint16_t length)
 Calculates fitness of the genome based on how close it is to the target. More...
 
void genomes_sort_by_fitness (genome_t genomes[], uint16_t genome_count)
 Sorts the given genome array by ascending fitness. More...
 
void mutate_genome (char *genome, uint16_t length, uint16_t max_mutation, uint16_t min_mutation)
 Provides a mutated genome based on the maximum and minimum possible mutations. More...
 
void genomes_mate (const genome_t *p_target, const genome_t *p_parent_1, const genome_t *p_parent_2, genome_t *p_offspring)
 Mating combines the genomes of two parents over a random crossover point, while the sequence of parents for the crossover is randomly selected. After a crossover, a slight mutation is performed to avoid a local maxima from occurring. Fitness of the new offspring is then calculated, compared to the provided target. More...
 

Detailed Description

Utility functions for performing different operations on a genome.

Author
Usman Mehmood (usman.nosp@m.mehm.nosp@m.ood55.nosp@m.@gma.nosp@m.il.co.nosp@m.m)
Version
0.1
Date
21-09-2023

Copyright (c) 2023, Usman Mehmood

Macro Definition Documentation

◆ GENE_POOL

#define GENE_POOL   "!@#$^&*()_-=+,.;:'/\\\"{}[]<>? 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

This is a gene pool. You can add your own characters to this as long as they don't mess with the functions and stuff.

Typedef Documentation

◆ gene_t

typedef char gene_t

A singular gene is just a character.

◆ genome_t

typedef struct genome_t genome_t

The genome structure. It contains genes, the length of those genes and a fitness score. The genome must be initialized with the genome_target_init function before use.

Function Documentation

◆ genome_calculate_fitness()

int genome_calculate_fitness ( const char *  target,
const char *  genome,
uint16_t  length 
)

Calculates fitness of the genome based on how close it is to the target.

Parameters
[in]targettarget genome string
[in]genomeinput genome string for which fitness has to be calculated
[in]lengthlength of both genomes
Returns
int fitness score
Here is the caller graph for this function:

◆ genome_copy()

void genome_copy ( genome_t destination,
const genome_t source 
)

Performs a deep copy of source to destination while maintaining their original references.

Parameters
[out]destination
[in]source
Here is the caller graph for this function:

◆ genome_destroy()

void genome_destroy ( genome_t p_genome)

Frees the gene memory allocated to the genome and resets its fitness and length.

Parameters
[out]p_genomepointer to genome
Here is the caller graph for this function:

◆ genome_init()

genome_t genome_init ( uint16_t  length)

Creates a new genome object with allocated memory of the given size. The genome must be manually freed by genome_destroy() after it has been used.

Parameters
[in]lengthlength of genome
Returns
genome_t a new genome
Here is the call graph for this function:
Here is the caller graph for this function:

◆ genome_print()

void genome_print ( const genome_t  genome)

Function for printing genomes in a readable format.

Parameters
[in]genomeFunction for printing genomes in a readable format.
[in]genome_1first genome string
[in]genome_2second genome string
Here is the caller graph for this function:

◆ genome_target_init()

genome_t genome_target_init ( char *  string)

Initialization of the target genome is slightly different as it already has allocated memory and does not need any mutation.

Parameters
[in,out]stringtarget string
Returns
genome_t initialized target

◆ genomes_mate()

void genomes_mate ( const genome_t p_target,
const genome_t p_parent_1,
const genome_t p_parent_2,
genome_t p_offspring 
)

Mating combines the genomes of two parents over a random crossover point, while the sequence of parents for the crossover is randomly selected. After a crossover, a slight mutation is performed to avoid a local maxima from occurring. Fitness of the new offspring is then calculated, compared to the provided target.

Parameters
[in]p_targetpointer to target
[in]p_parent_1pointer to parent 1
[in]p_parent_2pointer to parent 2
[out]p_offspringbuffer to store offspring genome in
Here is the call graph for this function:
Here is the caller graph for this function:

◆ genomes_sort_by_fitness()

void genomes_sort_by_fitness ( genome_t  genomes[],
uint16_t  genome_count 
)

Sorts the given genome array by ascending fitness.

Parameters
[in,out]genomesarray of genomes
[in]genome_countnumber of genomes in the array
Here is the caller graph for this function:

◆ get_mutated_gene()

char get_mutated_gene ( void  )

Function for extracting a mutated / random gene from the available gene pool specified in the GENE_POOL macro.

Returns
char mutated gene character
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mutate_genome()

void mutate_genome ( char *  genome,
uint16_t  length,
uint16_t  max_mutation,
uint16_t  min_mutation 
)

Provides a mutated genome based on the maximum and minimum possible mutations.

Parameters
[in,out]genomegenome string to mutate
[in]lengthlength of genome
[in]max_mutationmaximum possible genes to be mutated
[in]min_mutationminimum possible genes to be mutated
Here is the call graph for this function:
Here is the caller graph for this function:

◆ random_in_pos_range()

int random_in_pos_range ( const int  upper_limit,
const int  lower_limit 
)

Provides a pseudo random number between a positive range.

Parameters
[in]upper_limitupper limit number
[in]lower_limitlower limit number
Returns
int pseudo-random number, -ERANGE if upper limit is invalid, -EINVAL if limits are negative
Here is the caller graph for this function: