gen-alg
The library of genetic algorithm
Quick start
const Population = ; const population = schema: a: type: 'float' min: 0 max: 1 digits: 2 1 - entitya - 05 ** 2; let stop = false;for let i = 0; i < 1000 && !stop; i++ stop = populationnext; // next generation const a = population; console;
Schema example
const schema = field1: type: 'int' min: 0 max: 100 field2: type: 'float' min: 0 max: 1 digits: 3 field3: type: 'object' fields: field1: type: 'int' min: 0 max: 50 ;
Fitness helper
/** * Gets fitness value * @param * @param * @param * @return */ { if x === value return 1; const dev = Math / se; return Math;}
for example:
const Population fitnessHelper = ; const population = schema: a: type: 'int' min: 0 max: 100 // size: 20, fitnessHelper;let stop = false;for let i = 0; i < 1000 && !stop; i++ stop = populationnext; const a = population; console;
Multiple fields
const Population fitnessHelper = ; const population = schema: a: type: 'int' min: 0 max: 100 b: type: 'float' min: 0 max: 1 digits: 2 size: 40 // you can performance manipulate fitnessHelper * fitnessHelper;let stop = false;for let i = 0; i < 1000 && !stop; i++ stop = populationnext; const a b = population; console;
Array hack
The array type doesn't support yet. If you want use array you can write little hack, like that:
const Population fitnessHelper = ; // ------ Array hack ------const length = 9;const arraySchema = {};for let i = 0; i < length; i++ arraySchemai = type: 'int' min: 0 max: 100 ; { const arr = ; for let i = 0; i < length; i++ arr; return arr;}// ----------------------- const population = schema: arraySchema { let fitness = 1; const arr = ; for let i = 0; i < arrlength; i++ const x = arri; const targetValue = i + 1 * 10; fitness *= fitnessHelper; return fitness; }; let stop = false;for let i = 0; i < 1000 && !stop; i++ stop = populationnext; const arr = ; console;