如何在对象数组上顺序/单独应用相同的函数?

时间:2015-10-12 15:08:36

标签: javascript algorithm iteration

在单个项目上成功应用this遗传算法后,我想优化对象数组的值。

为了做到这一点,我尝试以下列方式使用forEach循环:

function getOptimalValues(req, res){
mdl.getItems({limit: 5}, function(err, items){//this function retrieves 5 items from database
    items.forEach(function (elem) {
        var queryObj = {
            properties: elem,
            targetEnergy: req.targetEnergy
        };

        genetic.getOptimalQuantities(queryObj, function (err, optimalValues) {
            geneticResults.push(optimalValues);
            if (geneticResults.length == items.length) {
                res(null, geneticResults);
            }
        });
    });
});
}

genetic.getOptimalQuantities定义如下:

function getOptimalQuantities(req, res){
var properties = req.properties;
var targetEnergy = req.targetEnergy;

var targetValues = {energy: targetEnergy, properties: properties};

var Task = require('genetic').Task;
var options = { getRandomSolution : getCandidateQuantities  // previously described to produce random solution
    , popSize : 100  // population size
    , stopCriteria : stopCriteria  // previously described to act as stopping criteria for entire process (set to 100 generations)
    , fitness : getFitnessValue  // 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 : crossoverFunction // previously described to produce child solution by combining two parents
};
var t = new Task(options);
t.targetValues = targetValues;
//t.on('mutate', function () { console.log('MUTATION!') });
t.on('statistics', function (statistics) {
    console.log('statistics',statistics.maxScore);
});
t.on('iteration start', function (generation) {
    console.log('iteration start - ',generation)
});
t.run(function (stats) {
    var dataObj = {quantities: stats.max, items: t.bestCombination}
    res(null, dataObj);
});
}

在运行此过程时,我得到以下输出:

iteration start -  1
statistics 0.008126878121533886
iteration start -  1
iteration start -  1
statistics 0.007777620410591467
statistics 0.007777620410591467
iteration start -  1
iteration start -  1
iteration start -  1
statistics 0.008133385505205764
statistics 0.008133385505205764
statistics 0.008133385505205764
iteration start -  1
iteration start -  1
iteration start -  1
iteration start -  1
statistics 0.0093968469349952
statistics 0.0093968469349952
statistics 0.0093968469349952
statistics 0.0093968469349952
iteration start -  1
iteration start -  1
iteration start -  1
iteration start -  1
iteration start -  1
statistics 0.008431076204956763
statistics 0.008431076204956763
statistics 0.008431076204956763
statistics 0.008431076204956763
statistics 0.008431076204956763
iteration start -  2
iteration start -  2
iteration start -  2
iteration start -  2
iteration start -  2
...

对于传递给函数getOptimalQuantities的五个项目中的每一个,应该有100次迭代,并且每次迭代的每个项目的适应度值(作为统计数据)应该是不同的(所有元素的相等的机会接近于零)。因此,通过观察输出,我猜想遗传算法并不是针对本例中传递的5个项目中的每一个单独运行。

有谁知道如何确保功能(本例中的遗传算法)不会混淆多个输入的数据?直观地,更容易想象每个调用的顺序执行(这就是为什么要求顺序执行的原因),但通常任何建议的方法,顺序或并行都会非常有用。

感谢您的帮助。

尼科

1 个答案:

答案 0 :(得分:0)

我相信这是你正在使用的库中的一个错误。它的Task类继承自节点的EventEmitter类,该类包含一个名为init的方法,用于设置每个对象的侦听器哈希。但随后Task会使用自己的init方法覆盖此方法:

Task.prototype.init = function (callback) {
  var self = this
  self.emit('init start')
  async.until(function () { return (self.population.length == self.popSize)}
    , function (callback) { self.getRandomSolution(function (solution) { self.population.push(solution); callback() }) }
    , function (err) { 
        if (err!=null) { self.emit('error', err) }
        else { 
          self.emit('init end', self.population)
      callback()
        }
      }
  )
}

这意味着EventEmitter.init()不会被调用,这意味着Task的每个实例都没有自己的侦听器数据结构,而是使用相同的全局实例。当你只有一个Task实例时,这样可以正常工作,但只要你同时处理多个实例,它们就会被添加到同一个事件监听器哈希中,并且只要它们中的任何一个发出事件,它们都会得到通知