Trying to combine elements of 2 lists into 1
<p>I've written a function that should randomly pick 2 lists from the first 9 items of a bigger list and randomly pick values from each list and create a new list of the same length. When testing it on it's own the function seems to work properly, but when called as a part of the program it doesn't seem to do anything. Each generation returns the exact same values as the last. I'm really not sure what's wrong here and I would love your help!</p>

<pre><code>    w, h = 9, 10
network = [[0 for x in range(w)] for y in range(h)]

def sigmoid(sigin):
  return 1 / (1 + math.exp(-sigin))
def netcal(x):
    network[x].append(sigmoid((sigmoid(i1*network[x][0]+i2*network[x][1])*network[x][6])+(sigmoid(i1*network[x][2]+i2*network[x][3])*network[x][7])+(sigmoid(i1*network[x][4]+i2*network[x][5])*network[x][8])))
def seed():
    b = 0
    while b &lt; 10:
        y = 0
        while y &lt; 9:
            network[b][y] = random.random()
            y += 1
        b += 1
def calall():
    c = 0
    while c &lt; 9:
        netcal(c)
        c += 1
def cost():
    d = 0
    while d &lt; 9:
        network[d].append(1 - network[d][9])
        print(network[d][10])
        d += 1
def sort():
    network.sort(key=lambda x: x[-1])
#not working
def evol():
    num = random.sample(range(0,10), 2)
    evol1 = network[num[0]]
    evol2 = network[num[1]]
    evol3 = network[9]
    i = 0
    while i &lt; 9 :
        j = random.randrange(0,2)
        if j == 0 :
            evol3[i] = evol1[i]
        else:
            evol3[i] = evol2[i]
        i += 1
    network[9] = evol3

i1=0
i2=1

seed()

t = 0
while t &lt; 1000:
    calall()
    cost()
    sort()
    evol()
    print('break')
    t += 1
</code></pre>

<p>Here's the code I used to test the function:</p>

<pre><code>w, h = 9, 10
network = [[0 for x in range(w)] for y in range(h)]

def seed():
    b = 0
    while b &lt; 10:
        y = 0
        while y &lt; 9:
            network[b][y] = random.random()
            y += 1
        b += 1

def sort():
    network.sort(key=lambda x: x[-1])

def evol():
    num = random.sample(range(0,9), 2)
    print(num)
    evol1 = network[num[0]]
    evol2 = network[num[1]]
    evol3 = network[9]
    i = 0
    while i &lt; 9 :
        j = random.randrange(0,2)
        if j == 0 :
            evol3[i] = evol1[i]
        else:
            evol3[i] = evol2[i]
        i += 1
    network[9] = evol3

seed()
sort()
print(network[9])
evol()
print(network[9])
</code></pre>

<p>I just figured it out and I feel incredibly stupid. The output from the code is the cost function for every net. This cost value is output explicitly as network[x][10]. However, when calculating the cost function it is appended to the end of the list, rather than replacing the value. The function was working perfectly fine, but because the cost is appended instead of changing the code outputs a static value. </p>

<p>Scratch that. That definitely was an issue, but after fixing that I'm still faced with the exact same problem. Here's my updated code(edit: I've also made the evol() function output network[9] at the beginning and end of the function and it is showing the weights are changing as they should be. This leads me to believe the issue is with either the calculation of the nets or their output):</p>

<pre><code>    w, h = 11, 10
network = [[0 for x in range(w)] for y in range(h)]

def sigmoid(sigin):
  return 1 / (1 + math.exp(-sigin))
def netcal(x):
    network[x][9] = (sigmoid((sigmoid(i1*network[x][0]+i2*network[x][1])*network[x][6])+(sigmoid(i1*network[x][2]+i2*network[x][3])*network[x][7])+(sigmoid(i1*network[x][4]+i2*network[x][5])*network[x][8])))
def seed():
    b = 0
    while b &lt; 10:
        y = 0
        while y &lt; 9:
            network[b][y] = random.random()
            y += 1
        b += 1
def calall():
    c = 0
    while c &lt; 9:
        netcal(c)
        c += 1
def cost():
    d = 0
    while d &lt; 9:
        network[d][10] = (1 - network[d][9])
        print(network[d][10])
        d += 1
def sort():
    network.sort(key=lambda x: x[-1])
#not working
def evol():
    num = random.sample(range(0,9), 2)
    evol1 = network[num[0]]
    evol2 = network[num[1]]
    evol3 = network[9]
    print('before', network[9])
    i = 0
    while i &lt; 9 :
        j = random.randrange(0,2)
        if j == 0 :
            evol3[i] = evol1[i]
        else:
            evol3[i] = evol2[i]
        i += 1
    network[9] = evol3
    print('after', network[9])
i1=0
i2=1

seed()


t = 0
while t &lt; 10:
    calall()
    cost()
    sort()
    evol()
    print('break')
    t += 1
</code></pre>

<p>There were 2 very stupid issues in the original code. </p>

<ol>
<li>The output of the cost function was reading network[x][10], but new cost values were appended rather than replacing the old value, so the output was just putting out the same variable on a loop.</li>
<li>The calculate and cost loops were both set to end before they would get to the last item in the list, so while the weights were changing those values were never calculated nor displayed.</li>
</ol>

python neural-network genetic-algorithm evolutionary-algorithm