def resetare_teste(tst): global genom_special global spc import LibrarieSecundara LibrarieSecundara.teste = copy.deepcopy(tst) from LibrarieSecundara import teste teste = copy.deepcopy(tst) nod_output = Gena(1,1) bias = Gena(0, 0) conn_bias = Conexiune(bias, nod_output, random.uniform(0, 0.5)*random.choice([-1, 1])) maxim = 3 gene = [bias, nod_output] conexiuni = [conn_bias] for _ in range(0, len(tst[0][0]) - 1): gena_noua = Gena(maxim,0) maxim += 1 conn_noua = Conexiune(gena_noua, nod_output, random.uniform(0, 0.5)*random.choice([-1, 1])) conexiuni += [conn_noua] gene += [gena_noua] genom_special = Genom(gene, conexiuni) lista1 = [] for _ in range(0, 15): lista1.append(copy.deepcopy(random.choice([ genom_special]))) specie1= Specie(copy.deepcopy(lista1)) sp = [] for _ in range(0, 10): sp = sp + [copy.deepcopy(specie1)] spc = Specii(copy.deepcopy(sp))
def crossover(geno): if len(geno) == 1: return geno[0] genom1, genom2 = geno lista_gene = [] for i in genom1.gene: if i not in genom2.gene: lista_gene += [i] lista_gene += genom2.gene conexiuni = [] for i in genom1.conexiuni: ok = 0 for j in genom2.conexiuni: if i.input == j.input and i.output == j.output: if random.choice([0, 1]) == 0: conexiuni.append(copy.deepcopy(i)) else: conexiuni.append(copy.deepcopy(j)) ok = 1 break if ok == 0: conexiuni.append(i) de_sters = [] for i in lista_gene: if i.nume not in [n.input.nume for n in conexiuni] + [n.output.nume for n in conexiuni]: de_sters += [i] for i in de_sters: lista_gene.remove(i) copil = Genom(lista_gene, conexiuni) return copil
def creare_genom(): nod_output = Gena(1,1) bias = Gena(0, 0) conn_bias = Conexiune(bias, nod_output, random.uniform(0, 0.5)*random.choice([-1, 1])) maxim = 3 gene = [bias, nod_output] conexiuni = [conn_bias] for _ in range(0, len(teste[0][0]) - 1): gena_noua = Gena(maxim,0) maxim += 1 conn_noua = Conexiune(gena_noua, nod_output, random.uniform(0, 0.5)*random.choice([-1, 1])) conexiuni += [conn_noua] gene += [gena_noua] return Genom(gene, conexiuni)
def citire_din_fisier(nume_fisier): gene = [] conexiuni = [] fisier = open(nume_fisier, "r") continut = fisier.read() date = continut.split("\n")[:-1] dictionar = {} for gen in date[0].split(";")[:-1]: extragere = gen.split(":") gene = gene + [Gena(int(extragere[0]), int(extragere[1]))] dictionar[extragere[0]] = Gena(int(extragere[0]), int(extragere[1])) for con in date[1:]: extragere = con.split(",") inp = dictionar[extragere[0].split(":")[0]] out = dictionar[extragere[1].split(":")[0]] wgh = float(extragere[2]) conexiuni = conexiuni + [Conexiune(inp, out, wgh)] fisier.close() return Genom(gene, conexiuni)