Genetics is a genetic algorithm implementation for node;
Genetics is written in CoffeeScript (it can be used in JavaScript as well).
npm install Genetics
See test/test.coffee
for a quick start.
solType must be an implementation of class Solution()
totalPop is the total number of solutions
keepPop is the number of solutions kept after selection (by default : totalPop / 2)
crossover is the crossover rate. The number of solutions generated by cross-over will be (totalPop - keepPop) * crossover
.
This class must implement the folowing method :
after implementation, Genetic.init() and Genetic.run() must be called
return true if end Conditions is reached else return false
Select Solutions to be kept from @pop After the Selection, @pop.length must equal @totalPop
This class must implement the following methods :
Generate a random solution.
Generate a solution from sol1 and sol2
Return a mutated solution
Set Solution.fit to a number representing the solution's fitness
contain all solutions
current generation
- set @solType, @totalPop and @keepPop
- if @keepPop == 0, then @keepPop = @totalPop / 2
- create an empty array @pop
- generate @totalPop solutions calling Solution.random()
- solutions are pushed into @pop
- sort @pop by fitness
- solutions must have been evaluated before calling @sort()
- return bestfit after calling @sort()
- call Solution.eval() for each solution in @pop
- return a randomly chose solution from @pop
- run genetic algorithm
- initial pop is eval
- initialize @gen to 0
- @end() is checked
- newPop is generated with Solution.crossOver() and Solution.mutate()
- newPop is eval by calling Solution.eval() for each new solution
- newPop is added to @pop
- if @select() is not implemented, the @totalPop best Solution are selected
- else @select() is called, an exception is threw if @pop.length != @totalPop
- @gen is incremented
- return total Fitness of @pop
- return a random solution selected by roulette wheel
- f is a random number, auto-generated if f == null
- select by roulette wheel sampling
- using this selection method, Solution.eval must return a value in [0,1]
- implementation may looks like :
select: -> @selectRWS()
- select by roulette wheel sampling, with higher probability for bests fit
- using this selection method, Solution.eval must return a value in [0,1]
- implementation may looks like :
select: -> @selectRWS2()
- select by Stochastic universal sampling
- using this selection method, Solution.eval must return a value in [0,1]
- implementation may looks like :
select: -> @selectSUS()