Skip to content

Conversation

@ypt
Copy link

@ypt ypt commented May 27, 2015

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.

…issue when running using node and trying to load modules.
@glebec
Copy link

glebec commented May 4, 2016

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

@ypt
Copy link
Author

ypt commented May 4, 2016

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

@SomeKittens
Copy link

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

@richardklafter
Copy link

Useful PR. Thanks.

@z-hao-wang
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants