3
How do I do nested functions in Python? Another question, because I always need to use the self parameter, and I’ve already put parameters in the function?
For example, it is considered that the function gerar_individuo() is already implemented and that I need to create a function called gerar_populacao(self, individual) as below:
def gerar_populacao(self, individuo):
tamanho_populacao = 100
while(tamanho_populacao > 0):
individuo = gerar_individuo(linha_do_tempo, linha_do_tempo, disciplinas)
When I execute my code gives error:
Undefined variable 'generat_individual'
Undefined variable 'line_do_tempo'
Undefined variable 'disciplines'
From what I can tell, he is not recognizing the function gerar_individuo() as being an external reference. How do I resolve this?
# aqui está todo o código:
import time
import random
import copy
class AG():
cs = []
gav = ["A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08"]
icc = ["A02", "A08"]
lc = ["A09", "A10", "A11", "A12", "A13", "A01", "A02"]
oc = ["A14", "A15", "A16", "A17", "A18", "A19", "A20",
"A21", "A22", "A23", "A24", "A25", "A26", "A27", "A28", "A29", "A30",
"A31", "A32", "A33", "A34", "A35", "A36", "A37", "A38", "A39", "A40" ,
"A01", "A02", "A04", "A08"]
pcal = ["A40", "A41", "A42", "A43", "A44", "A45", "A46", "A47", "A48",
"A49", "A50", "A51", "A52", "A53", "A54", "A55", "A56", "A57", "A58",
"A59", "A60", "A61", "A62", "A63", "A64", "A65", "A66", "A67", "A68",
"A01", "A02", "A03", "A05", "A08"]
pc1 = ["A69", "A70", "A71", "A72", "A73", "A74", "A01", "A02", "A08"]
#codigos das disciplinas
_cs = 1101
_gav = 1102
_icc = 1103
_lc = 1104
_oc = 1105
_pcal = 1106
_pc1 = 1107
disciplinas = [_cs, _gav, _icc, _lc, _oc, _pcal, _pc1]
primeiro_ano = [1101, 1102, 1103, 1104, 1105, 1106, 1107]
segundo_ano = [2101, 2102, 2103, 2104, 2105, 2106, 2107, 2108]
terceiro_ano = [3101, 3102, 3103, 3104, 3105, 3106, 3107, 3108]
quarto_ano = [4101, 4102, 4103, 4104, 4105, 4106, 4107]
linha_do_tempo = []
TAM = 80
def __init__(self):
self.linha_do_tempo = self.zero_list_maker(self.TAM)
print ("Inicializando a linha do tempo...")
print ("Linha do tempo: ", self.linha_do_tempo)
def zero_list_maker(self, length):
listofzeros = [0] * length
return listofzeros
def gerar_populacao(self):
populacao = []
tamanho_populacao = 100
lCount = len(self.linha_do_tempo)
while(tamanho_populacao > 0):
# para nao alterar as listas originais fazemos um copy delas para cada individuo
discis = copy.deepcopy(self.disciplinas)
linha_tempo = copy.deepcopy(self.linha_do_tempo)
randDisc = []
next1 = False
for i in range(0, lCount-1):
if(len(discis) > 0):
position = random.randint(0,lCount-1)
disc = random.choice(discis)
if(linha_tempo[i] == 0 and linha_tempo[i+1] == 0):
linha_tempo[position] = disc
randDisc.append(disc)
discis.remove(disc)
next1 = True
elif(next1):
linha_tempo[position] = disc
randDisc.append(disc)
discis.remove(disc)
next1 = False
else:
break
populacao.append(linha_tempo)
tamanho_populacao -= 1
return populacao
ag = AG() population = ag.gerar_population() print(population) first individuo = population[0] print(' n nFirst person: n', first individual)
The problem is not the function, it is the variable
linha_tempo
that is not defined. And why does it repeat? Self is if these functions belong to an object/class. They are within a class?– Miguel
I understood friend =) yes, everything is within one class only. It’s like this: I made a function generate individual with the code you made, as it is above, where I step by parameter the line_do_tempothat was initialized with 80 zero positions:
– Allan
line_do_time = [0] * 80
– Allan
this function generate individual returns something? if print individual after that will have worked
– Miguel
but it should declare time line within the function to generate population, before the while
– Miguel
and the list of disciplines, which contains all disciplines. I randomized them, according to their code, in the timeline. Now I need to generate a list of individuals, e.g. 100 different timelines and store in a list.
– Allan
Maybe it would be easier if you put in all the relevant code so we can help you
– Miguel
Jewel, I’ll do it then in another question =)
– Allan
Okay, buddy, I edited my question up there, and I added all the code. I tried to do a while() with population size and print the result, but did not generate anything xD
– Allan
Why are you calling
gerar_populacao
with twolinha_do_tempo
?– Miguel
If I remove the first time string parameter does not work, it generates an error saying that the self parameter has not been initialized. The following error appears: No value for argument 'disciplines' in method call
– Allan
I am doing the project in C9.io because it is tcc I preferred to do in the cloud. How do I reference you in my tcc? I will thank you for the help!
– Allan
There’s no need @Allan. I’m from Portugal, here it’s late, bedtime. Tomorrow at work I will come back here and if there is no solution we have solved this problem. It can be?
– Miguel
Yes friend, you can leave! As for the fact that you are from Portugal, there is no problem, because my theoretical background is in an article by a Professor. from Portugal =) Profª Paula Amaral from the University of Lisbon :D good rest friend, thanks even for the help!
– Allan
Let’s go continue this discussion in chat.
– Miguel