genetic

package
v0.0.0-...-a4a72b7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 23, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package genetic provides functions to work with strings using genetic algorithm. https://en.wikipedia.org/wiki/Genetic_algorithm

Author: D4rkia

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conf

type Conf struct {
	// Maximum size of the population.
	// Bigger could be faster but more memory expensive.
	PopulationNum int

	// Number of elements selected in every generation for evolution
	// the selection takes. Place from the best to the worst of that
	// generation must be smaller than PopulationNum.
	SelectionNum int

	// Probability that an element of a generation can mutate changing one of
	// its genes this guarantees that all genes will be used during evolution.
	MutationProb float64

	// Enables debugging output to the console.
	Debug bool
}

Conf stands for configurations set provided to GeneticString function.

type PopulationItem

type PopulationItem struct {
	Key   string
	Value float64
}

Population item represent a single step in the evolution process. One can think of population item as a single species. Key stands for the actual data entity of the species, which is a string in current implementation. Key can be interpreted as species DNA. Value shows how close this species to the desired target, where 1 means, that species DNA equals to the targeted one, 0 for no matchings in the DNA.

**Note** In the current implementation species DNA length is suppose to be equal to the target length for algorithm to work.

type Result

type Result struct {
	// Number of generations steps performed.
	Generation int

	// Number of generated population items.
	Analyzed int

	// Result of generation with the best Value.
	Best PopulationItem
}

Result structure contains generation process statistics, as well as the best resulted population item.

func GeneticString

func GeneticString(target string, charmap []rune, conf *Conf) (*Result, error)

GeneticString generates PopulationItem based on the imputed target string, and a set of possible runes to build a string with. In order to optimise string generation additional configurations can be provided with Conf instance. Empty instance of Conf (&Conf{}) can be provided, then default values would be set.

Link to the same algorithm implemented in python: https://github.com/TheAlgorithms/Python/blob/master/genetic_algorithm/basic_string.py

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL