我正在按照Daniel Shiffman的代码性质来检查一些代码。
在关于G.A的特定部分中,我无法弄清楚 map()函数的工作方式。
我了解代码的作用,但不知道如何执行。 我以为 map()函数只需要2个参数;一个函数和另一个可选参数。但是在这种情况下,似乎要花5点?
for (let i = 0; i < this.population.length; i++) {
let fitness = map(this.population[i].fitness, 0, maxFitness, 0, 1);
let n = floor(fitness * 100);
for (let j = 0; j < n; j++) {
this.matingPool.push(this.population[i]);
}
}
//full source code -> https://github.com/nature-of-code/noc-examples-p5.js/blob/master/chp09_ga/NOC_9_01_GA_Shakespeare/Population.js
答案 0 :(得分:2)
通过代码段中链接的github存储库,我们可以在this file中找到map
函数的定义(这是一个大文件,因此加载可能需要一些时间)。
如果我们搜索查询map = function
,则可以在第24361行找到定义。可以在下面找到map函数的代码
p5.prototype.map = function(n, start1, stop1, start2, stop2) {
return ((n-start1)/(stop1-start1))*(stop2-start2)+start2;
};