genetic-js
genetic-js copied to clipboard
Don't simulate webworker when running evolve without webworker.
Using eval(blobScript) (here) was causing issues for me with module loading when running genetic-js in a node.js app.
An example node.js app:
// in my-fitness.js
var stats = require('some-stats-package');
module.exports = function(entity){
var newFitness = stats.doSomething(entity); // an error happens here
return newFitness;
};
// in app.js
var Genetic = require('genetic-js');
var fitness = require('./my-fitness');
var genetic = Genetic.create();
genetic.optimize = Genetic.Optimize.Maximize;
genetic.select1 = Genetic.Select1.Tournament2;
genetic.select2 = Genetic.Select2.Tournament2;
genetic.fitness = fitness;
...
genetic.evolve(config, userData);
Running the app like this:
$ node app.js
Results in an error like this
ReferenceError: stats is not defined
at Object.eval (eval at <anonymous> (eval at <anonymous> (/project_path/node_modules/genetic-js/lib/genetic.js:261:10)), <anonymous>:3:19)
My quick-fix was to stop using eval when running without webworkers.
Also, I haven't tried running this in the browser yet, so I'm not totally sure about the impact of this change in browsers, unfortunately.
@subprotocol FWIW, I agree with this PR. In my use case I wanted to disable web workers to avoid the frustration of having to refactor my existing codebase to work around eval and its lack of closure.
It will have to be updated to merge in the latest changes first, of course (@ypt).
@glebec @subprotocol, looking again at this PR, the main change was here in these lines:
https://github.com/subprotocol/genetic-js/pull/3/files#diff-a13ce942436f5d0cca406cdd1548c38bL263
If this looks like something that is useful for this project, I can clean up and resubmit this PR (rebasing off current master, and undoing all those whitespace changes that my editor automatically applied). Let me know. Thanks.
Thanks, was trying to run on node and this fixed my problem!
Useful PR. Thanks.
this is great. it also fixed a problem for me in nodejs. I was passing a class variable and it won't work with eval. but works with this PR.