1
0
forked from s444399/AI

zmiana generowania nowych populacji

This commit is contained in:
tomasz 2020-06-08 22:41:10 +02:00
parent 8af100a754
commit fe3b86d97e
7 changed files with 91 additions and 55 deletions

View File

@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="BookmarkManager">
<bookmark url="file://$PROJECT_DIR$/genetyczne.py" line="21" mnemonic="9" />
</component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="8264ee8d-5217-4cb6-b982-78f67fabc82e" name="Default Changelist" comment=""> <list default="true" id="8264ee8d-5217-4cb6-b982-78f67fabc82e" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
@ -22,8 +25,8 @@
<file pinned="false" current-in-tab="false"> <file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/funkcje.py"> <entry file="file://$PROJECT_DIR$/funkcje.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="326"> <state relative-caret-position="1547">
<caret line="180" column="13" lean-forward="true" selection-start-line="180" selection-start-column="13" selection-end-line="180" selection-end-column="13" /> <caret line="91" column="9" selection-start-line="91" selection-start-column="9" selection-end-line="91" selection-end-column="9" />
<folding> <folding>
<element signature="e#0#43#0" expanded="true" /> <element signature="e#0#43#0" expanded="true" />
</folding> </folding>
@ -39,8 +42,8 @@
<file pinned="false" current-in-tab="false"> <file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/genetyczne.py"> <entry file="file://$PROJECT_DIR$/genetyczne.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="367"> <state relative-caret-position="255">
<caret line="77" column="35" lean-forward="true" selection-start-line="77" selection-start-column="35" selection-end-line="77" selection-end-column="35" /> <caret line="16" column="7" selection-start-line="16" selection-start-column="7" selection-end-line="16" selection-end-column="7" />
</state> </state>
</provider> </provider>
</entry> </entry>
@ -48,8 +51,8 @@
<file pinned="false" current-in-tab="true"> <file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/main.py"> <entry file="file://$PROJECT_DIR$/main.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="98"> <state relative-caret-position="625">
<caret line="6" column="4" lean-forward="true" selection-start-line="6" selection-start-column="4" selection-end-line="6" selection-end-column="4" /> <caret line="46" column="11" selection-start-line="46" selection-start-column="11" selection-end-line="46" selection-end-column="11" />
<folding> <folding>
<element signature="e#0#21#0" expanded="true" /> <element signature="e#0#21#0" expanded="true" />
</folding> </folding>
@ -72,8 +75,8 @@
<file pinned="false" current-in-tab="false"> <file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/program.py"> <entry file="file://$PROJECT_DIR$/program.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="469"> <state relative-caret-position="452">
<caret line="103" column="65" selection-start-line="103" selection-start-column="65" selection-end-line="103" selection-end-column="65" /> <caret line="96" column="52" lean-forward="true" selection-start-line="96" selection-start-column="52" selection-end-line="96" selection-end-column="52" />
<folding> <folding>
<element signature="e#0#13#0" expanded="true" /> <element signature="e#0#13#0" expanded="true" />
</folding> </folding>
@ -101,11 +104,6 @@
</component> </component>
<component name="FindInProjectRecents"> <component name="FindInProjectRecents">
<findStrings> <findStrings>
<find>cop</find>
<find>jakLiczycKoszt</find>
<find>jakLiczyc</find>
<find>eve</find>
<find>doW</find>
<find>self</find> <find>self</find>
<find>gen</find> <find>gen</find>
<find>data.geny</find> <find>data.geny</find>
@ -130,7 +128,12 @@
<find>mutate</find> <find>mutate</find>
<find>od tego mom</find> <find>od tego mom</find>
<find>regalsik</find> <find>regalsik</find>
<find>podajDwaChromosomy</find>
<find>generuj</find>
<find>populacjaF</find>
<find>rys</find>
<find>data</find> <find>data</find>
<find>populacjaFitness</find>
</findStrings> </findStrings>
</component> </component>
<component name="Git.Settings"> <component name="Git.Settings">
@ -165,9 +168,9 @@
<option value="$PROJECT_DIR$/genetyczny/genetyczne.py" /> <option value="$PROJECT_DIR$/genetyczny/genetyczne.py" />
<option value="$PROJECT_DIR$/opis/environment.md" /> <option value="$PROJECT_DIR$/opis/environment.md" />
<option value="$PROJECT_DIR$/LechT.md" /> <option value="$PROJECT_DIR$/LechT.md" />
<option value="$PROJECT_DIR$/genetyczne.py" />
<option value="$PROJECT_DIR$/funkcje.py" /> <option value="$PROJECT_DIR$/funkcje.py" />
<option value="$PROJECT_DIR$/program.py" /> <option value="$PROJECT_DIR$/program.py" />
<option value="$PROJECT_DIR$/genetyczne.py" />
<option value="$PROJECT_DIR$/main.py" /> <option value="$PROJECT_DIR$/main.py" />
</list> </list>
</option> </option>
@ -414,6 +417,13 @@
</component> </component>
<component name="XDebuggerManager"> <component name="XDebuggerManager">
<breakpoint-manager> <breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" suspend="THREAD" type="python-line">
<url>file://$PROJECT_DIR$/genetyczne.py</url>
<line>26</line>
<option name="timeStamp" value="10" />
</line-breakpoint>
</breakpoints>
<default-breakpoints> <default-breakpoints>
<breakpoint type="python-exception"> <breakpoint type="python-exception">
<properties notifyOnTerminate="true" exception="BaseException"> <properties notifyOnTerminate="true" exception="BaseException">
@ -605,16 +615,6 @@
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$PROJECT_DIR$/AStar.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="741">
<caret line="81" column="31" selection-start-line="81" selection-start-column="31" selection-end-line="81" selection-end-column="31" />
<folding>
<element signature="e#0#12#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Data.py"> <entry file="file://$PROJECT_DIR$/Data.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="289"> <state relative-caret-position="289">
@ -635,10 +635,30 @@
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$PROJECT_DIR$/AStar.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="741">
<caret line="81" column="31" selection-start-line="81" selection-start-column="31" selection-end-line="81" selection-end-column="31" />
<folding>
<element signature="e#0#12#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/funkcje.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="1547">
<caret line="91" column="9" selection-start-line="91" selection-start-column="9" selection-end-line="91" selection-end-column="9" />
<folding>
<element signature="e#0#43#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/program.py"> <entry file="file://$PROJECT_DIR$/program.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="469"> <state relative-caret-position="452">
<caret line="103" column="65" selection-start-line="103" selection-start-column="65" selection-end-line="103" selection-end-column="65" /> <caret line="96" column="52" lean-forward="true" selection-start-line="96" selection-start-column="52" selection-end-line="96" selection-end-column="52" />
<folding> <folding>
<element signature="e#0#13#0" expanded="true" /> <element signature="e#0#13#0" expanded="true" />
</folding> </folding>
@ -647,25 +667,15 @@
</entry> </entry>
<entry file="file://$PROJECT_DIR$/genetyczne.py"> <entry file="file://$PROJECT_DIR$/genetyczne.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="367"> <state relative-caret-position="255">
<caret line="77" column="35" lean-forward="true" selection-start-line="77" selection-start-column="35" selection-end-line="77" selection-end-column="35" /> <caret line="16" column="7" selection-start-line="16" selection-start-column="7" selection-end-line="16" selection-end-column="7" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/funkcje.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="326">
<caret line="180" column="13" lean-forward="true" selection-start-line="180" selection-start-column="13" selection-end-line="180" selection-end-column="13" />
<folding>
<element signature="e#0#43#0" expanded="true" />
</folding>
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$PROJECT_DIR$/main.py"> <entry file="file://$PROJECT_DIR$/main.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="98"> <state relative-caret-position="625">
<caret line="6" column="4" lean-forward="true" selection-start-line="6" selection-start-column="4" selection-end-line="6" selection-end-column="4" /> <caret line="46" column="11" selection-start-line="46" selection-start-column="11" selection-end-line="46" selection-end-column="11" />
<folding> <folding>
<element signature="e#0#21#0" expanded="true" /> <element signature="e#0#21#0" expanded="true" />
</folding> </folding>

Binary file not shown.

Binary file not shown.

View File

@ -89,7 +89,7 @@ def rysujWykres(data, x, yStart, yEnd):
y = data.doWykresu[i] y = data.doWykresu[i]
x = i x = i
plt.scatter(x, y) plt.scatter(x, y)
plt.pause(0.0001) #plt.pause(0.0001)
plt.show() plt.show()

View File

@ -8,18 +8,25 @@ def start(data, wheel, dane):
fragment = float(dane[1]) fragment = float(dane[1])
mutacja = float(dane[2]) mutacja = float(dane[2])
unbox = int(dane[3]) unbox = int(dane[3])
"""
ileGeneracji = 1000
ileWPopulacji = 16
fragment = 0.5
mutacja = 0.05
unbox = 3
"""
data.kordyWozka = (wheel.ns, wheel.we) data.kordyWozka = (wheel.ns, wheel.we)
data.jakLiczycKoszt = unbox data.jakLiczycKoszt = unbox
randomPopulation = genRandomPopulation(data, ileWPopulacji) randomPopulation = genRandomPopulation(data, ileWPopulacji)
for i in range(ileGeneracji): for i in range(ileGeneracji):
if i == 0: if i == 0:
best2 = dwieNajlepsze(randomPopulation, data) populacjaFitness = fitnessDlaPopulacji(randomPopulation, data)
else: else:
x = genPopulacje(data,best2[0], best2[1], ileWPopulacji, fragment, mutacja) #chrom2 = podajDwaChromosomy(populacjaFitness[0], populacjaFitness[1])
best2 = dwieNajlepsze(x, data) x = genPopulacje(data,populacjaFitness[0], populacjaFitness[1], ileWPopulacji, fragment, mutacja)
del x populacjaFitness = fitnessDlaPopulacji(x, data)
#del x
data.histZmian.append(data.best[1]) data.histZmian.append(data.best[1])
@ -61,6 +68,21 @@ def genRandomPopulation(data, ileWPopulacji):
populacja.append(genRandomChromosome(data)) populacja.append(genRandomChromosome(data))
return populacja 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): def fitness(chromosome, data):
koszt = 0 koszt = 0
@ -87,7 +109,7 @@ def fitness(chromosome, data):
return koszt return koszt
def dwieNajlepsze(populacja, data): def fitnessDlaPopulacji(populacja, data):
tmpPopulacja = populacja[:] tmpPopulacja = populacja[:]
chromFitness = [] chromFitness = []
@ -102,7 +124,7 @@ def dwieNajlepsze(populacja, data):
elif(data.best[1] > bestValue): elif(data.best[1] > bestValue):
data.best = (pierwsza[:],bestValue) data.best = (pierwsza[:],bestValue)
data.doWykresu.append(bestValue) data.doWykresu.append(bestValue)
"""
tmpPopulacja.pop(bestChromIndex) tmpPopulacja.pop(bestChromIndex)
chromFitness.pop(bestChromIndex) chromFitness.pop(bestChromIndex)
@ -111,9 +133,9 @@ def dwieNajlepsze(populacja, data):
druga = tmpPopulacja[bestChromIndex] druga = tmpPopulacja[bestChromIndex]
tmpPopulacja.pop(bestChromIndex) tmpPopulacja.pop(bestChromIndex)
chromFitness.pop(bestChromIndex) chromFitness.pop(bestChromIndex)
"""
return (tmpPopulacja, chromFitness)
return (pierwsza, druga)
def crossover(data,pierwszy, drugi, fragmentLiczba, wspMutacji): def crossover(data,pierwszy, drugi, fragmentLiczba, wspMutacji):
ileWChrom = len(pierwszy) ileWChrom = len(pierwszy)
@ -158,15 +180,17 @@ def crossover(data,pierwszy, drugi, fragmentLiczba, wspMutacji):
return nowyChrom return nowyChrom
def genPopulacje(data,pierwszy, drugi, ileWPopulacji, fragmentLiczba, wspMutacji): def genPopulacje(data,populacja, chromFitness, ileWPopulacji, fragmentLiczba, wspMutacji):
ileWChrom = len(pierwszy) ileWChrom = len(populacja[0])
fragment = round(fragmentLiczba*ileWChrom) fragment = round(fragmentLiczba*ileWChrom)
if(fragment == 1): if(fragment == 1):
fragment +=1 fragment +=1
nowaPopulacja = [] nowaPopulacja = []
for i in range(ileWPopulacji): for i,index in enumerate(range(ileWPopulacji)):
nowaPopulacja.append(crossover(data,pierwszy,drugi,fragment, wspMutacji)) if index % 2 == 0:
dwaChrom = podajDwaChromosomy(populacja,chromFitness)
nowaPopulacja.append(crossover(data,dwaChrom[0],dwaChrom[1],fragment, wspMutacji))
return nowaPopulacja return nowaPopulacja

View File

@ -89,9 +89,11 @@ class MainWindow:
elif(event.type==pygame.KEYDOWN): elif(event.type==pygame.KEYDOWN):
if event.key == pygame.K_g: if event.key == pygame.K_g:
updateMap(self.data, self.map, self.mapForAStar, self.regals) updateMap(self.data, self.map, self.mapForAStar, self.regals)
dane = okno() dane = okno()
if(dane == 0): if(dane == 0):
continue continue
start(self.data,self.wheel, dane) start(self.data,self.wheel, dane)
for gen in self.data.best[0]: for gen in self.data.best[0]:
if(gen.unboxWczesniejszegoGenu == None): if(gen.unboxWczesniejszegoGenu == None):
@ -101,7 +103,7 @@ class MainWindow:
zbierzBox(gen,self.data, self.moves, kordStartowy) zbierzBox(gen,self.data, self.moves, kordStartowy)
elif(event.key== pygame.K_r): 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) updateMap(self.data, self.map, self.mapForAStar, self.regals)
elif len(self.moves)==0: elif len(self.moves)==0:
self.wheel.move(event, self.map) self.wheel.move(event, self.map)