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

Hello! So I am working on a genetic algorithm in python, and I am wondering how

ID: 3578964 • Letter: H

Question

Hello! So I am working on a genetic algorithm in python, and I am wondering how to loop between functions.

I have def initial() which defines the initial population, it returns "generation"

I have def fitness() which defines the fitness, it receives "generation" from initial(), it returns "scores"

I have def pairing() which defines the pairs, it receives "scores" from fitness(), it also does the mating, and creates a new generation, it returns "newgeneration".

Now, the problem is, I want this new generation to go to fitness() however, I am not sure how to have it do the initial() generation, then afterward do the "newgeneration." I am thinking there should be some of loop and conditional, like if it is the 1st iteration, then generation = initial(), else generation = pairing(), (this is the fitness() function). I'm also wondering how I would end the code in general. Thanks for your help!

Here is some of the code, not all of it, but to give you an idea of how flat-footed I'm doing this:

def initial():
global organisms, organisms1
organisms = []
organisms1 = []

i = 0
for i in range(2*h):
o = [[0 for j in range(h)]for j in range(h)]
for k in range(h):
if rand()>.5:
o[k]=1
else:
o[k]=0
organisms.append(o)

for i in range(h):
no = organisms[0+i]+organisms[2*h+i]
organisms1.append(no)
return(organisms1)

def fitness():
d1 = np.delete(d,(0),axis =0)
d1 = np.delete(d1,(0),axis = 0)
d1= np.delete(d1,(0),axis = 1)
d1 = np.delete(d1,(0),axis = 1)
global scores, distances, lengs
scores = []
distances = []
generation = initial()
scores = ''Haven't quite figured this out yet..."
return(scores)

def pairing():
global scores, generation, newgeneration, choseran
generation = initial()
newgeneration = []
choseran = []
scores = fitness()
scores1 = scores.copy()
scores = [int(x) for x in scores]
scores.sort()
w = scores1.index(scores[0])
w1 = scores1.index(scores[1])
newgeneration.append(generation[w])
newgeneration.append(generation[w1])
g = [[0 for j in range(h)]for j in range(h)]
a = [[0 for j in range(h)]for j in range(h)]
o = [[0 for j in range(h)]for j in range(h)]
v = [[0 for j in range(h)]for j in range(h)]
for i in range(h):
if generation[w][i]!=generation[w1][i]:
if rand()<.5:
g[i] = 0
a[i] = 1
else:
g[i] = 1
a[i] = 0
else:
g[i]=generation[w][i]
a[i]=generation[w][i]
for i in range(h):
if g[i]==1:
o[i]=0
else:
o[i]=1
for i in range(h):
if a[i]==1:
v[i]=0
else:
v[i]=1

q = v+a
n = g+o
newgeneration.append(q)
newgeneration.append(n)
# print(newgeneration)
i1 = 1
count = 0
count1 = 0
while i1<h:
if rand()<1*i1/(h):
ran1 = generation[count]
choseran.append(ran1)
break
else:
count+=1
i1+=1
  
u=1
e1=1
while u<h:
if rand()<1*u/(h):
ran2 = generation[count1]
choseran.append(ran2)
break
else:
count1+=1
u+=1
while e1<h:
if rand()<1*e1/(h):
ran3 = generation[count1]
choseran.append(ran3)
break
else:
count1+=1
e1+=1
  

for v2 in range(h):
if ran1[v2]!=ran2[v2]:
if rand()<.5:
t1[v2] = 0
else:
t1[v2] = 1
else:
t1[v2]=ran1[v2]

for ui in range(h):
if t1[ui]==1:
bn1[ui]=0
else:
bn1[ui]=1
newgeneration.append(t1+bn1)

newgeneration.append(q1)
newgeneration.append(n1)

return newgeneration

initial()
pairing()
fitness()

Explanation / Answer

Since, you have to call fitness function on initial() generation and newgeneration (one after the other), you can pass a flag to the fitness function which will help you determine the generation which you want to use.

You just have to modify the structure of your fitness function to:

def fitness(flag):
   if flag == 1:
   generation = initial()
   else:
   generation = newgeneration

Since, pairing returns newgeneration, you should store it when you call it so that it can be passed to fitness. So, the end part of your code becomes:

generation = initial()
newgeneration = pairing()
fitness(1)

where argument 1 in fitness function is the value of the flag. You have to change the flag's value from 1 to 0 as soon as it processes initial generation.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote