Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HELP. Hello! Another genetic algorithm python 3.5 code question. I am really in

ID: 3581248 • Letter: H

Question

HELP. Hello! Another genetic algorithm python 3.5 code question. I am really in deep trouble becuase I am at a loss for what to do for the fitness part of this genetic algorithm. Say I have a distance matrix:

[0,2,6,4,3,2,1,4,5],
[2,0,3,4,5,10,2,3,1],
[6,3,0,2,3,10,2,4,8],
[4,4,2,0,12,7,3,2,6],
[3,5,3,12,0,2,8,4,1],
[2,10,10,7,2,0,9,2,5],
[1,2,2,3,8,9,0,10,2],
[4,3,4,2,4,2,10,0,4],
[5,1,8,6,1,5,2,4,0]]

Where the diagonals are 0 because the distance between node1 and node1 =0, node2 and node2 =0, etc. In this matrix, there are total of 7 nodes and 2 origins, the first two rows and columns represent the distance between origin 1,2 and the rest of the nodes. The genetic algorithm is supposed to determine which nodes should be assigned to which origin in order to minimize distance. I know that python is probably not the best choice, and I have done similar things in programs such as Julia, but for the course I am taking, the Professor wants us to get down to the nitty-gritty details of coding to do this. So genetic algorithm that does clustering.

Now to the actual question. So I have defined my organisms as binary numbers, there are a total of 7 different organisms (no particular reason for 7). Say one of them is defined as [1,0,0,1,1,0,1,0,1,1,0,0,1,0] notice that the first part is [1,0,0,1,1,0,1], and the second part is [0,1,1,0,0,1,0], if add them you get all 1s. This is because the first part of this [1,0,0,1,1,0,1], represents the nodes covered by origin1, so in this case, origin 1 covers nodes 1,4,5,and 7, and origin 2 covers 2,3, and 6. So, finally, the fitness would be the total distance this would take. I have tried different things that do not work at all. But essentially what I want the code to do is look through [1,0,0,1,1,0,1], and where it equals 1, that corresponds a value in the distance matrix. I would like it to pick the minimum distance for that organism if possible.

I am going to attempt to be transparent and show exactly what I want the code to do with this given example.

organism1 = [1,0,0,1,1,0,1,0,1,1,0,0,1,0]

for i in range(7):

if organism(i) ==1:

** find the minimum route in d**

**add both for origin 1 and origin2**

and that is fitness.

Sorry this is such a long question. I really appreciate any help. I hope I made my question clear. Thanks!

Explanation / Answer

import numpy
m1 = numpy.array([[0,2,6,4,3,2,1,4,5],
[2,0,3,4,5,10,2,3,1],
[6,3,0,2,3,10,2,4,8],
[4,4,2,0,12,7,3,2,6],
[3,5,3,12,0,2,8,4,1],
[2,10,10,7,2,0,9,2,5],
[1,2,2,3,8,9,0,10,2],
[4,3,4,2,4,2,10,0,4],
[5,1,8,6,1,5,2,4,0]])

m2 = numpy.array([[0,2,3,10,2,4,8],
[2,0,12,7,3,2,6],
[3,12,0,2,8,4,1],
[10,7,2,0,9,2,5],
[2,3,8,9,0,10,2],
[4,2,4,2,10,0,4],
[8,6,1,5,2,4,0]])
organism1 = [1,0,0,1,1,0,1,0,1,1,0,0,1,0]
s1 = organism1[:7]
s2 = organism1[7:]
s1_l1 = []
s2_l2 = []
s1_d1 = []
s2_d2 = []

count2 = 0
for i in range(1,8):
if s1[i-1] ==1:
s1_d1.append(i)
if s2[i-1]==1:
s2_d2.append(i)
for i in range(7):
count1 = 0
count2 = 0
for j in s1_d1:
count1 += m2[i][j-1]
s1_l1.append((count1,i))
for j in s2_d2:
count2 += m2[i][j-1]
s2_l2.append((count2,i))
s1_l1 = sorted(s1_l1)
s2_l2 = sorted(s2_l2)
print("fitness",s1_l1[0][0]+s2_l2[0][0])