końcowa wersja #2

Merged
s444425 merged 1 commits from s444425/AI:master into master 2020-06-08 22:44:19 +02:00
7 changed files with 91 additions and 55 deletions

View File

@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="BookmarkManager">
<bookmark url="file://$PROJECT_DIR$/genetyczne.py" line="21" mnemonic="9" />
</component>
<component name="ChangeListManager">
<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" />
@ -22,8 +25,8 @@
<file pinned="false" current-in-tab="false">
<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" />
<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>
@ -39,8 +42,8 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/genetyczne.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="367">
<caret line="77" column="35" lean-forward="true" selection-start-line="77" selection-start-column="35" selection-end-line="77" selection-end-column="35" />
<state relative-caret-position="255">
<caret line="16" column="7" selection-start-line="16" selection-start-column="7" selection-end-line="16" selection-end-column="7" />
</state>
</provider>
</entry>
@ -48,8 +51,8 @@
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/main.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="98">
<caret line="6" column="4" lean-forward="true" selection-start-line="6" selection-start-column="4" selection-end-line="6" selection-end-column="4" />
<state relative-caret-position="625">
<caret line="46" column="11" selection-start-line="46" selection-start-column="11" selection-end-line="46" selection-end-column="11" />
<folding>
<element signature="e#0#21#0" expanded="true" />
</folding>
@ -72,8 +75,8 @@
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/program.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="469">
<caret line="103" column="65" selection-start-line="103" selection-start-column="65" selection-end-line="103" selection-end-column="65" />
<state relative-caret-position="452">
<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>
<element signature="e#0#13#0" expanded="true" />
</folding>
@ -101,11 +104,6 @@
</component>
<component name="FindInProjectRecents">
<findStrings>
<find>cop</find>
<find>jakLiczycKoszt</find>
<find>jakLiczyc</find>
<find>eve</find>
<find>doW</find>
<find>self</find>
<find>gen</find>
<find>data.geny</find>
@ -130,7 +128,12 @@
<find>mutate</find>
<find>od tego mom</find>
<find>regalsik</find>
<find>podajDwaChromosomy</find>
<find>generuj</find>
<find>populacjaF</find>
<find>rys</find>
<find>data</find>
<find>populacjaFitness</find>
</findStrings>
</component>
<component name="Git.Settings">
@ -165,9 +168,9 @@
<option value="$PROJECT_DIR$/genetyczny/genetyczne.py" />
<option value="$PROJECT_DIR$/opis/environment.md" />
<option value="$PROJECT_DIR$/LechT.md" />
<option value="$PROJECT_DIR$/genetyczne.py" />
<option value="$PROJECT_DIR$/funkcje.py" />
<option value="$PROJECT_DIR$/program.py" />
<option value="$PROJECT_DIR$/genetyczne.py" />
<option value="$PROJECT_DIR$/main.py" />
</list>
</option>
@ -414,6 +417,13 @@
</component>
<component name="XDebuggerManager">
<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>
<breakpoint type="python-exception">
<properties notifyOnTerminate="true" exception="BaseException">
@ -605,16 +615,6 @@
</state>
</provider>
</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">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="289">
@ -635,10 +635,30 @@
</state>
</provider>
</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">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="469">
<caret line="103" column="65" selection-start-line="103" selection-start-column="65" selection-end-line="103" selection-end-column="65" />
<state relative-caret-position="452">
<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>
<element signature="e#0#13#0" expanded="true" />
</folding>
@ -647,25 +667,15 @@
</entry>
<entry file="file://$PROJECT_DIR$/genetyczne.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="367">
<caret line="77" column="35" lean-forward="true" selection-start-line="77" selection-start-column="35" selection-end-line="77" selection-end-column="35" />
</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 relative-caret-position="255">
<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$/main.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="98">
<caret line="6" column="4" lean-forward="true" selection-start-line="6" selection-start-column="4" selection-end-line="6" selection-end-column="4" />
<state relative-caret-position="625">
<caret line="46" column="11" selection-start-line="46" selection-start-column="11" selection-end-line="46" selection-end-column="11" />
<folding>
<element signature="e#0#21#0" expanded="true" />
</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]
x = i
plt.scatter(x, y)
plt.pause(0.0001)
#plt.pause(0.0001)
plt.show()

View File

@ -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

View File

@ -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)