I'm working on a genetic algorithm on neural networks together with some friends for a school project, but we've run into a problem. What we're trying to do is the following: (note that some of this is subject to change and only set up this way for testing purposes)

我正在和一些朋友一起研究神经网络上的遗传算法,但是我们遇到了一个问题。我们要做的是以下内容:(请注意,其中一些可能会发生变化,并且只是为了测试目的而设置这种方式)

We have 10 neural networks per generation. The first of these is the baseNetwork, the rest are slight variations on the baseNetwork. At the end of the generation, the best performing network is selected as the new baseNetwork for the following generation. We store neural networks as arrays, for example nn[1] is the first neural network and in itself it is an array. The problem is that somehow, the baseNetwork is altered after each network rather than at the end of each generation. Here is the genetic algorithm with some notes to make it a bit easier to follow:

我们每代有10个神经网络。其中第一个是baseNetwork,其余的是baseNetwork上的细微变化。在生成结束时,选择性能最佳的网络作为下一代的新baseNetwork。我们将神经网络存储为数组,例如nn [1]是第一个神经网络,它本身就是一个数组。问题是,不知何故,baseNetwork在每个网络之后而不是在每一代结束时被更改。这是遗传算法,有一些注释使它更容易遵循:

function genAlg() {

if (networkNumber < nnPerGen) {
    nn[networkNumber][0][1] = lastfitness; // stores fitness
    nn[networkNumber][0][2] = deathwall; // stores fitness
    networkNumber++; // select new network
    nn[networkNumber] = baseNetwork; // is supposed to replace the currently selected network with the baseNetwork of the generation
    for (i=1; i<=evolveAmt; i++) {
        evolveGate = (Math.floor(gatesAmt * Math.random()) + 1);
        evolve();
    } // make new variation of currently selected network
} else {
    generation++; // start new generation

    for (i=1;i<=nnPerGen;i++) {
        if (nn[i][0][1] > baseNetwork[0][1]) {
            baseNetwork = nn[i];
        } else if (nn[i][0][1] = baseNetwork[0][1]) {
            if (nn[i][0][2] < baseNetwork[0][2]) {
                baseNetwork = nn[i];
            }
        }
    } //Testing fitness, replace baseNetwork if another network is better

    networkNumber = 1; // start working with the first network again
}
}

After very thorough testing we have concluded that the problem lies in this line:

经过非常彻底的测试,我们得出结论,问题在于这一行:

nn[networkNumber] = baseNetwork;

nn [networkNumber] = baseNetwork;

We don't know why, but somehow this very line changes the value of baseNetwork itself. Keep in mind that baseNetwork itself is an array.

我们不知道为什么,但不知何故,这条线路改变了baseNetwork本身的价值。请记住,baseNetwork本身就是一个数组。

1 个解决方案

#1


0

Try

nn[networkNumber] = Array.from(baseNetwork);

Probably you are giving it the same object reference, so by changing one of them through the evolve fnction you change both. Using Array.from(baseArray) you create a new array that is identical to baseArray, but it's a different object

可能你给它提供了相同的对象引用,所以通过改进它们中的一个来改变它们。使用Array.from(baseArray)创建一个与baseArray相同的新数组,但它是一个不同的对象

更多相关文章

  1. javascript 动态数组的使用
  2. 如何使用变量创建数组?
  3. 推json敲出可观察的数组
  4. 从占用转义字符的字符数组创建字符串
  5. 如何获取knockoutjs可观察数组的下一个元素?
  6. 显示json数组中的所有项目
  7. 图表。js数据数组使用PHP, MySQL。如何从JSON数组定义数据源?
  8. 将JavaScript对象转换为要插入关系数据库的数组
  9. javascript数组和对象是否有设置顺序?

随机推荐

  1. 成员函数可以重载吗?
  2. C语言中的三目运算符是什么
  3. c语言是面向什么的语言
  4. C语言中字符串连接函数是什么
  5. C语言中二叉树中序遍历怎么执行?
  6. 一个c语言程序总是从什么开始执行
  7. c++中static关键字的作用是什么?
  8. c语言真假是1和0吗?
  9. 学习asp.net core集成MongoDB的完整步骤
  10. c语言三种基本程序结构是什么?