dolphin278 / genetic Goto Github PK
View Code? Open in Web Editor NEWImplementation of genetic algorithms for nodejs
Implementation of genetic algorithms for nodejs
May you guide how to use your library to solve the tsp ? Thanks a lot
Someone got this problem before? I cannot run more than 100 generations.
/Users/michaelmalura/Development/bot/node_modules/genetic/lib/genetic/Task.js:210
level += self.parents[position].score
^
TypeError: Cannot read property 'score' of undefined
at /Users/michaelmalura/Development/bot/node_modules/genetic/lib/genetic/Task.js:210:44
at Object.async.until (/Users/michaelmalura/Development/bot/node_modules/genetic/node_modules/async/lib/async.js:568:13)
at /Users/michaelmalura/Development/bot/node_modules/genetic/lib/genetic/Task.js:198:13
at /Users/michaelmalura/Development/bot/node_modules/genetic/node_modules/async/lib/async.js:486:21
at /Users/michaelmalura/Development/bot/node_modules/genetic/node_modules/async/lib/async.js:185:13
at iterate (/Users/michaelmalura/Development/bot/node_modules/genetic/node_modules/async/lib/async.js:108:13)
at /Users/michaelmalura/Development/bot/node_modules/genetic/node_modules/async/lib/async.js:119:25
at /Users/michaelmalura/Development/bot/node_modules/genetic/node_modules/async/lib/async.js:187:17
at /Users/michaelmalura/Development/bot/node_modules/genetic/node_modules/async/lib/async.js:491:34
at /Users/michaelmalura/Development/bot/node_modules/genetic/lib/genetic/Task.js:192:7
function random(start, end) {
return Math.floor(Math.random() * end) + start;
};
function getRandomSolution(callback) {
// let threshhold = 0.03;
// let buyCut = 3;
// let sellCut = 8;
callback({
threshhold: random(0.02, 0.3), // 2% - 30%
buyCut: random(1, 100), // 1/1 - 1/100
sellCut: random(1, 100) // 1/1 - 1/100
})
}
function fitness(solution, callback) {
const { profit, sells, buys } = simulate(stockData, Object.assign({}, solution));
callback(profit)
}
function mutate(solution, callback) {
let s = Object.assign({}, solution);
if (Math.random() < 0.3) {
s.threshhold = random(0.02, 0.3);
}
if (Math.random() < 0.3) {
s.buyCut = random(1, 100);
}
if (Math.random() < 0.3) {
s.sellCut = random(1, 100);
}
callback(s)
}
function crossover(father, mother, callback) {
var child = {};
assert(!!father, `No father`);
assert(!!mother, `No mother`);
if (Math.random() >= 0.5) {
child.threshhold = father.threshhold;
} else {
child.threshhold = mother.threshhold;
}
if (Math.random() >= 0.5) {
child.sellCut = father.sellCut;
} else {
child.sellCut = mother.sellCut;
}
if (Math.random() >= 0.5) {
child.buyCut = father.buyCut;
} else {
child.buyCut = mother.buyCut;
}
callback(child);
}
function stopCriteria() {
return (this.generation === 300)
}
const geneticOptions = {
getRandomSolution: getRandomSolution, // previously described to produce random solution
popSize: 100, // population size
stopCriteria: stopCriteria, // previously described to act as stopping criteria for entire process
fitness: fitness, // previously described to measure how good your solution is
minimize: false, // whether you want to minimize fitness function. default is `false`, so you can omit it
mutateProbability: 0.1, // mutation chance per single child generation
mutate: mutate, // previously described to implement mutation
crossoverProbability: 0.3, // crossover chance per single child generation
crossover: crossover // previously described to produce child solution by combining two parents
}
const runEvolution = function () {
const taskInstance = new Task(geneticOptions);
taskInstance.on('error', function (error) { console.log('ERROR - ', error) })
taskInstance.run(function (stats) {
console.log('results', stats);
});
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.