diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 42c9970..a3c438b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,5 +1,8 @@ + + + @@ -22,8 +25,8 @@ - - + + @@ -39,8 +42,8 @@ - - + + @@ -48,8 +51,8 @@ - - + + @@ -72,8 +75,8 @@ - - + + @@ -101,11 +104,6 @@ - cop - jakLiczycKoszt - jakLiczyc - eve - doW self gen data.geny @@ -130,7 +128,12 @@ mutate od tego mom regalsik + podajDwaChromosomy + generuj + populacjaF + rys data + populacjaFitness @@ -165,9 +168,9 @@ @@ -414,6 +417,13 @@ + + + file://$PROJECT_DIR$/genetyczne.py + 26 + + @@ -605,16 +615,6 @@ - - - - - - - - - - @@ -635,10 +635,30 @@ + + + + + + + + + + + + + + + + + + + + - - + + @@ -647,25 +667,15 @@ - - - - - - - - - - - - + + - - + + diff --git a/__pycache__/funkcje.cpython-37.pyc b/__pycache__/funkcje.cpython-37.pyc index 0188593..d134230 100644 Binary files a/__pycache__/funkcje.cpython-37.pyc and b/__pycache__/funkcje.cpython-37.pyc differ diff --git a/__pycache__/genetyczne.cpython-37.pyc b/__pycache__/genetyczne.cpython-37.pyc index 3df080b..483b4f9 100644 Binary files a/__pycache__/genetyczne.cpython-37.pyc and b/__pycache__/genetyczne.cpython-37.pyc differ diff --git a/__pycache__/program.cpython-37.pyc b/__pycache__/program.cpython-37.pyc index 9432cbc..c549a5b 100644 Binary files a/__pycache__/program.cpython-37.pyc and b/__pycache__/program.cpython-37.pyc differ diff --git a/funkcje.py b/funkcje.py index 69cd5ba..aa3d960 100644 --- a/funkcje.py +++ b/funkcje.py @@ -89,7 +89,7 @@ def rysujWykres(data, x, yStart, yEnd): y = data.doWykresu[i] x = i plt.scatter(x, y) - plt.pause(0.0001) + #plt.pause(0.0001) plt.show() diff --git a/genetyczne.py b/genetyczne.py index 7557af6..29b5b31 100644 --- a/genetyczne.py +++ b/genetyczne.py @@ -8,18 +8,25 @@ def start(data, wheel, dane): fragment = float(dane[1]) mutacja = float(dane[2]) unbox = int(dane[3]) - + """ + ileGeneracji = 1000 + ileWPopulacji = 16 + fragment = 0.5 + mutacja = 0.05 + unbox = 3 + """ data.kordyWozka = (wheel.ns, wheel.we) data.jakLiczycKoszt = unbox randomPopulation = genRandomPopulation(data, ileWPopulacji) for i in range(ileGeneracji): if i == 0: - best2 = dwieNajlepsze(randomPopulation, data) + populacjaFitness = fitnessDlaPopulacji(randomPopulation, data) else: - x = genPopulacje(data,best2[0], best2[1], ileWPopulacji, fragment, mutacja) - best2 = dwieNajlepsze(x, data) - del x + #chrom2 = podajDwaChromosomy(populacjaFitness[0], populacjaFitness[1]) + x = genPopulacje(data,populacjaFitness[0], populacjaFitness[1], ileWPopulacji, fragment, mutacja) + populacjaFitness = fitnessDlaPopulacji(x, data) + #del x data.histZmian.append(data.best[1]) @@ -61,6 +68,21 @@ def genRandomPopulation(data, ileWPopulacji): populacja.append(genRandomChromosome(data)) return populacja +def podajDwaChromosomy(populacja, chromFitness): + + bestValue = min(chromFitness) + bestChromIndex = chromFitness.index(bestValue) + chrom1 = populacja[bestChromIndex] + populacja.pop(bestChromIndex) + chromFitness.pop(bestChromIndex) + + bestValue = min(chromFitness) + bestChromIndex = chromFitness.index(bestValue) + chrom2 = populacja[bestChromIndex] + populacja.pop(bestChromIndex) + chromFitness.pop(bestChromIndex) + + return (chrom1, chrom2) def fitness(chromosome, data): koszt = 0 @@ -87,7 +109,7 @@ def fitness(chromosome, data): return koszt -def dwieNajlepsze(populacja, data): +def fitnessDlaPopulacji(populacja, data): tmpPopulacja = populacja[:] chromFitness = [] @@ -102,7 +124,7 @@ def dwieNajlepsze(populacja, data): elif(data.best[1] > bestValue): data.best = (pierwsza[:],bestValue) data.doWykresu.append(bestValue) - + """ tmpPopulacja.pop(bestChromIndex) chromFitness.pop(bestChromIndex) @@ -111,9 +133,9 @@ def dwieNajlepsze(populacja, data): druga = tmpPopulacja[bestChromIndex] tmpPopulacja.pop(bestChromIndex) chromFitness.pop(bestChromIndex) + """ - - return (pierwsza, druga) + return (tmpPopulacja, chromFitness) def crossover(data,pierwszy, drugi, fragmentLiczba, wspMutacji): ileWChrom = len(pierwszy) @@ -158,15 +180,17 @@ def crossover(data,pierwszy, drugi, fragmentLiczba, wspMutacji): return nowyChrom -def genPopulacje(data,pierwszy, drugi, ileWPopulacji, fragmentLiczba, wspMutacji): - ileWChrom = len(pierwszy) +def genPopulacje(data,populacja, chromFitness, ileWPopulacji, fragmentLiczba, wspMutacji): + ileWChrom = len(populacja[0]) fragment = round(fragmentLiczba*ileWChrom) if(fragment == 1): fragment +=1 nowaPopulacja = [] - for i in range(ileWPopulacji): - nowaPopulacja.append(crossover(data,pierwszy,drugi,fragment, wspMutacji)) + for i,index in enumerate(range(ileWPopulacji)): + if index % 2 == 0: + dwaChrom = podajDwaChromosomy(populacja,chromFitness) + nowaPopulacja.append(crossover(data,dwaChrom[0],dwaChrom[1],fragment, wspMutacji)) return nowaPopulacja diff --git a/program.py b/program.py index 856ccc1..2d0b9b0 100644 --- a/program.py +++ b/program.py @@ -89,9 +89,11 @@ class MainWindow: elif(event.type==pygame.KEYDOWN): if event.key == pygame.K_g: updateMap(self.data, self.map, self.mapForAStar, self.regals) + dane = okno() if(dane == 0): continue + start(self.data,self.wheel, dane) for gen in self.data.best[0]: if(gen.unboxWczesniejszegoGenu == None): @@ -101,7 +103,7 @@ class MainWindow: zbierzBox(gen,self.data, self.moves, kordStartowy) elif(event.key== pygame.K_r): - self.map = randomBox(self.map, self.regals, 5) + self.map = randomBox(self.map, self.regals, 20) updateMap(self.data, self.map, self.mapForAStar, self.regals) elif len(self.moves)==0: self.wheel.move(event, self.map)