genetic-js icon indicating copy to clipboard operation
genetic-js copied to clipboard

Don't simulate webworker when running evolve without webworker.

Open ypt opened this issue 10 years ago • 5 comments

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.

ypt avatar May 27 '15 02:05 ypt

@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 avatar May 04 '16 04:05 glebec

@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.

ypt avatar May 04 '16 22:05 ypt

Thanks, was trying to run on node and this fixed my problem!

SomeKittens avatar Jan 16 '17 02:01 SomeKittens

Useful PR. Thanks.

richardklafter avatar Feb 05 '17 23:02 richardklafter

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.

z-hao-wang avatar Aug 05 '18 06:08 z-hao-wang