To show how this package can be used, we use some of the predefined fitness functions and use the API. First it is important to use the package to get access to the function API.
using GeneticAlgorithmSimple examples of using the genAlgo function.
- Find maximum of binarystring function:
# with default valuesgenAlgo(binarystring)
# with set named optional variablesgenAlgo(binarystring, genNum=100)
# with own provided optional crossover functiongenAlgo(binarystring, crossover=(x,y) ->k_point_crossover(x,y,10))- Find maximum of griewank function:
# with default valuesgenAlgo(griewank)
# with set named optional variablesgenAlgo(griewank, unitValues=-1000.0:1000.0, genNum=500, mutRate=0.25)
# with own provided optional selection functiongenAlgo(griewank, selection=(a,b,c)->GeneticAlgorithm.weighted_selection(a,b,c))- Find maximum of rastrigin function:
# with default valuesgenAlgo(rastrigin)
# with set named optional variablesgenAlgo(rastrigin, mutRate=0.75, crossRate=1, genNum=1000)- Find maximum of rosenbrock function:
# with set named optional variablessolveRosenbrock(a=4,b=100,genNum=100, mutRate=0.1, unitShape=[3], popSize=11)
# with set named optional variablessolveRosenbrock(a=4,b=100,genNum=250, mutRate=0.25, unitShape=[2], popSize=500, unitValues=-1000.0:1000.0)- Solve Sudoku:
s = [000820090;057609013;084031000;078060450;009100006;560300980;830406000;005018000;106750200]
solveSudoku(s, genNum=250)One can also use own fitness functions:
- Maximize trace of a matrix between the values 0 and 723:
genAlgo(x ->tr(x), unitValues=0:723, popSize=10, mutRate=0.5, unitShape=[2,2], crossRate=0.8)- Find matrix with det = 0 <=> flip fitness score so desired output has highest value:
functionf(x)
returndet(x) >=1000?0.0001:1000-abs(det(x))
endgenAlgo(x ->f(x), unitValues=0:723, popSize=10, mutRate=0.5, unitShape=[2,2], crossRate=0.8, genNum=1000)