Turry Posted January 8, 2018 Report Posted January 8, 2018 (edited) Primul program facut in "domeniul" machine learning. Sunt niste cerculete care evolueaza sa treaca peste un obstacol si sa ajunga la un target. Link Video link Edited January 9, 2018 by Turry 5 Quote
Active Members MrGrj Posted January 8, 2018 Active Members Report Posted January 8, 2018 Cateva sugestii, majoritatea PEP8 related: - constantele ar trebui sa fie UPPERCASEd: (e.g: targetRadius ar trebui redenumit ca TARGET_RADIUS) - ar trebui sa lasi doua linii noi intre functiile din afara unei clase sau intre functii si clase Asta: .. particleColor = sf.Color(255, 255, 255, 50) def distance(x1, y1, x2, y2): """ Calculate the distance from 2 points. (X1, Y1) and (X2, Y2) Formula: ( (X1-X2)**2 + (Y1-Y2)**2 )**1/2 """ return math.sqrt((x1 - x2)**2 + (y1 - y2)**2) class DNA(): ... Ar trebui sa arate asa: .. particleColor = sf.Color(255, 255, 255, 50) def distance(x1, y1, x2, y2): """ Calculate the distance from 2 points. (X1, Y1) and (X2, Y2) Formula: ( (X1-X2)**2 + (Y1-Y2)**2 )**1/2. """ return math.sqrt((x1 - x2)**2 + (y1 - y2)**2) class DNA(): ... - denumirea variabilelor tre' sa fie snake_cased (la fel si numele metodelor): e.g: def applyForce -> def apply_force etc - cand iterezi printr-o lista, si ai nevoie si de index si de valoare, e recomandat sa faci asa: lista_mea = ['malai', 'faina', 'pui', 'clatite'] for i, value in enumerate(lista_mea): print(i, value) """ output: (0, 'malai') (1, 'faina') (2, 'pui') (3, 'clatite') """ - cand creezi o clasa care nu are nici-un argument poti renunta la paranteze: class DNA() -> class DNA: - urmatoarea functie: def crossover(self, partner): """ Take the genes from 2 parents and create 1 offspring """ newDNA = [] # Pick one random point from the genes randomPoint = random.randint(0, len(self.genes)) for x in range(len(self.genes)): if x < randomPoint: newDNA.append(self.genes[x]) else: newDNA.append(partner.genes[x]) return DNA(newDNA) Poate fi rescrisa cu recomandarea de mai sus si folosind list comprehensions astfel: def crossover(self, partner): """ Take the genes from 2 parents and create 1 offspring. """ random_point = random.randint(0, len(self.genes)) new_dna = [ value if i < random_point else partner.genes[i] for i, value in enumerate(self.genes) ] return DNA(new_dna) Probabil ca se pot face mai multe schimbari insa nu am timp sa ma uit peste tot codu'. Felicitari oricum pentru efort si keep it goin' 5 Quote
yoyois Posted January 8, 2018 Report Posted January 8, 2018 (edited) Faina idee de algoritm! Nu pare cam "eficient" in practica pt ca necesita o populatie fmare pt convergenta. In general, in practica costul generatii*indivizi se vrea mic. (si toate celelalte dezavantave genetice: random walk, local optimum etc.) E frumos programul ca exemplu de solutie pur genetica. Felicitari. Edited January 8, 2018 by yoyois 1 Quote
Turry Posted January 8, 2018 Author Report Posted January 8, 2018 4 hours ago, MrGrj said: Cateva sugestii, majoritatea PEP8 related: - constantele ar trebui sa fie UPPERCASEd: (e.g: targetRadius ar trebui redenumit ca TARGET_RADIUS) - ar trebui sa lasi doua linii noi intre functiile din afara unei clase sau intre functii si clase Asta: .. particleColor = sf.Color(255, 255, 255, 50) def distance(x1, y1, x2, y2): """ Calculate the distance from 2 points. (X1, Y1) and (X2, Y2) Formula: ( (X1-X2)**2 + (Y1-Y2)**2 )**1/2 """ return math.sqrt((x1 - x2)**2 + (y1 - y2)**2) class DNA(): ... Ar trebui sa arate asa: .. particleColor = sf.Color(255, 255, 255, 50) def distance(x1, y1, x2, y2): """ Calculate the distance from 2 points. (X1, Y1) and (X2, Y2) Formula: ( (X1-X2)**2 + (Y1-Y2)**2 )**1/2. """ return math.sqrt((x1 - x2)**2 + (y1 - y2)**2) class DNA(): ... - denumirea variabilelor tre' sa fie snake_cased (la fel si numele metodelor): e.g: def applyForce -> def apply_force etc - cand iterezi printr-o lista, si ai nevoie si de index si de valoare, e recomandat sa faci asa: lista_mea = ['malai', 'faina', 'pui', 'clatite'] for i, value in enumerate(lista_mea): print(i, value) """ output: (0, 'malai') (1, 'faina') (2, 'pui') (3, 'clatite') """ - cand creezi o clasa care nu are nici-un argument poti renunta la paranteze: class DNA() -> class DNA: - urmatoarea functie: def crossover(self, partner): """ Take the genes from 2 parents and create 1 offspring """ newDNA = [] # Pick one random point from the genes randomPoint = random.randint(0, len(self.genes)) for x in range(len(self.genes)): if x < randomPoint: newDNA.append(self.genes[x]) else: newDNA.append(partner.genes[x]) return DNA(newDNA) Poate fi rescrisa cu recomandarea de mai sus si folosind list comprehensions astfel: def crossover(self, partner): """ Take the genes from 2 parents and create 1 offspring. """ random_point = random.randint(0, len(self.genes)) new_dna = [ value if i < random_point else partner.genes[i] for i, value in enumerate(self.genes) ] return DNA(new_dna) Probabil ca se pot face mai multe schimbari insa nu am timp sa ma uit peste tot codu'. Felicitari oricum pentru efort si keep it goin' Multumesc mult pentru sfaturi. Am sa corectez greselile si am sa incerc sa respect PEP8. 2 hours ago, yoyois said: Faina idee de algoritm! Nu pare cam "eficient" in practica pt ca necesita o populatie fmare pt convergenta. In general, in practica costul generatii*indivizi se vrea mic. (si toate celelalte dezavantave genetice: random walk, local optimum etc.) E frumos programul ca exemplu de solutie pur genetica. Felicitari. Stiu ca nu este cel mai eficient dar mi se pare extrem de interesanta ideea de a nu programa explicit cum trebuie sa rezolve problema iar calculatorul sa "descopere" o solutie cat de cat optima. Quote
Turry Posted January 9, 2018 Author Report Posted January 9, 2018 Pentru cei ce nu vor sa treaca prin procesul de training / nu vor sa instaleze Python doar pentru un simplu program, am adaugat un link catre video. Link catre video Quote