Nested Functions in Python

3

How do I do nested functions in Python? Another question, because I always need to use the self parameter, and have I already put parameters in the function?

For example, it is considered that the function generate_individual () is already implemented and that I need to create a function called generate_population (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 run my code it gives the error:

  

Undefined variable 'generate_individual'

     

Undefined variable 'timeline'

     

Undefined variable 'disciplines'

As far as I can tell, it is not recognizing the function generate_individual () because it is an external reference. How do I solve 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_populacao () print (population) first_individual = population [0] print ('\ n \ n \ nFirst person: \ n', first_individual)

    
asked by anonymous 19.06.2016 / 21:52

1 answer

3

@Allan here goes. The problem is that when we are accessing properties / methods within the class we have to use the prefix / argument self which is to mention that the method / property belongs to that class. When I say:

metodo = função (def my_func():) ex: zero_list_maker

propriedade = variaveis de instancia ex: timeline, which then within the methods refer to it as: self.linha_do_tempo :

I started from the principle that every 1 of 100 tamanho_populacao should store what result of each loop of the loop is:

import time
import random
import copy

class AG():

    cs = []
    gav = []                              
    icc = []
    lc = []
    oc = []
    pcal = []
    pc1 = []

    disciplinas = []

    primeiro_ano = []
    segundo_ano = []
    terceiro_ano = []
    quarto_ano = []

    linha_do_tempo = []
    TAM =  80

    def __init__(self):
        self.icc = ["A02", "A08"]
        self.lc = ["A09", "A10", "A11", "A12", "A13", "A01", "A02"]
        self.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"]
        self.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"]
        self.pc1 = ["A69", "A70", "A71", "A72", "A73", "A74", "A01", "A02", "A08"]
        self.gav = ["A01", "A02", "A03", "A04", "A05", "A06", "A07", "A08"]

        self.primeiro_ano = [1101, 1102, 1103, 1104, 1105, 1106, 1107]
        self.segundo_ano = [2101, 2102, 2103, 2104, 2105, 2106, 2107, 2108]
        self.terceiro_ano = [3101, 3102, 3103, 3104, 3105, 3106, 3107, 3108]
        self.quarto_ano = [4101, 4102, 4103, 4104, 4105, 4106, 4107]

        _cs = 1101
        _gav = 1102
        _icc = 1103
        _lc = 1104
        _oc = 1105
        _pcal = 1106
        _pc1 = 1107

        self.disciplinas = [_cs, _gav, _icc, _lc, _oc, _pcal, _pc1]

        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 = []
            positions = []
            for i in range(0, lCount-1):
                if(len(discis) > 0):
                    position = random.randint(0,lCount-2)
                    disc = random.choice(discis)
                    if(linha_tempo[position] == 0 and linha_tempo[position+1] == 0):
                        linha_tempo[position] = disc
                        linha_tempo[position+1] = disc
                        randDisc.append(disc)
                        discis.remove(disc)
                else:
                    break
            populacao.append(linha_tempo)
            tamanho_populacao -= 1
        return populacao


ag = AG()
populacao = ag.gerar_populacao()
print(populacao)
primeiro_individuo = populacao[0]
print('\n\n\nPrimeira pessoa:\n', primeiro_individuo)

I deleted some of the imports I was not using and the comments for the code are not very extensive here.

    
20.06.2016 / 11:07