1
0
forked from s444399/AI

dzialajacy algorytm oraz zbieranie boxow

This commit is contained in:
tomasz 2020-05-19 13:01:21 +02:00
parent 9c48010c38
commit 33726c29d5
9 changed files with 173 additions and 124 deletions

View File

@ -2,12 +2,10 @@
<project version="4"> <project version="4">
<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 afterPath="$PROJECT_DIR$/genetyczny/eee.py" afterDir="false" />
<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" />
<change beforePath="$PROJECT_DIR$/Mapa/generate.py" beforeDir="false" afterPath="$PROJECT_DIR$/Mapa/generate.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/genetyczny/Gene.py" beforeDir="false" afterPath="$PROJECT_DIR$/genetyczny/Gene.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/genetyczny/Data.py" beforeDir="false" afterPath="$PROJECT_DIR$/genetyczny/Data.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/genetyczny/eee.py" beforeDir="false" afterPath="$PROJECT_DIR$/genetyczny/eee.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/genetyczny/funkcje.py" beforeDir="false" afterPath="$PROJECT_DIR$/genetyczny/funkcje.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/genetyczny/funkcje.py" beforeDir="false" afterPath="$PROJECT_DIR$/genetyczny/funkcje.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/genetyczny/genetyczny.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/program.py" beforeDir="false" afterPath="$PROJECT_DIR$/program.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/program.py" beforeDir="false" afterPath="$PROJECT_DIR$/program.py" afterDir="false" />
</list> </list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
@ -21,11 +19,11 @@
</component> </component>
<component name="FileEditorManager"> <component name="FileEditorManager">
<leaf SIDE_TABS_SIZE_LIMIT_KEY="300"> <leaf SIDE_TABS_SIZE_LIMIT_KEY="300">
<file pinned="false" current-in-tab="false"> <file pinned="false" current-in-tab="true">
<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="815"> <state relative-caret-position="536">
<caret line="94" selection-start-line="94" selection-end-line="94" /> <caret line="94" column="47" lean-forward="true" selection-start-line="94" selection-start-column="47" selection-end-line="94" selection-end-column="47" />
<folding> <folding>
<element signature="e#0#13#0" expanded="true" /> <element signature="e#0#13#0" expanded="true" />
</folding> </folding>
@ -36,8 +34,8 @@
<file pinned="false" current-in-tab="false"> <file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/genetyczny/Gene.py"> <entry file="file://$PROJECT_DIR$/genetyczny/Gene.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="102"> <state relative-caret-position="119">
<caret line="6" column="16" selection-start-line="6" selection-start-column="16" selection-end-line="6" selection-end-column="16" /> <caret line="7" column="36" selection-start-line="7" selection-start-column="13" selection-end-line="7" selection-end-column="36" />
</state> </state>
</provider> </provider>
</entry> </entry>
@ -54,8 +52,8 @@
<file pinned="false" current-in-tab="false"> <file pinned="false" current-in-tab="false">
<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="731"> <state relative-caret-position="229">
<caret line="43" selection-start-line="43" selection-end-line="43" /> <caret line="41" selection-start-line="41" selection-end-line="41" />
<folding> <folding>
<element signature="e#0#21#0" expanded="true" /> <element signature="e#0#21#0" expanded="true" />
</folding> </folding>
@ -64,10 +62,13 @@
</entry> </entry>
</file> </file>
<file pinned="false" current-in-tab="false"> <file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/Mapa/box.py"> <entry file="file://$PROJECT_DIR$/AStar.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="68"> <state relative-caret-position="117">
<caret line="4" column="24" selection-start-line="4" selection-start-column="24" selection-end-line="4" selection-end-column="24" /> <caret line="19" column="31" selection-start-line="19" selection-start-column="28" selection-end-line="19" selection-end-column="31" />
<folding>
<element signature="e#0#12#0" expanded="true" />
</folding>
</state> </state>
</provider> </provider>
</entry> </entry>
@ -81,11 +82,11 @@
</provider> </provider>
</entry> </entry>
</file> </file>
<file pinned="false" current-in-tab="true"> <file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/genetyczny/funkcje.py"> <entry file="file://$PROJECT_DIR$/genetyczny/funkcje.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="424"> <state relative-caret-position="519">
<caret line="54" selection-start-line="54" selection-end-line="54" /> <caret line="217" column="33" selection-start-line="217" selection-start-column="33" selection-end-line="217" selection-end-column="33" />
<folding> <folding>
<element signature="e#0#48#0" expanded="true" /> <element signature="e#0#48#0" expanded="true" />
</folding> </folding>
@ -96,8 +97,8 @@
<file pinned="false" current-in-tab="false"> <file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/genetyczny/eee.py"> <entry file="file://$PROJECT_DIR$/genetyczny/eee.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="408"> <state relative-caret-position="308">
<caret line="24" selection-start-line="24" selection-end-line="24" /> <caret line="35" column="13" selection-start-line="35" selection-start-column="13" selection-end-line="35" selection-end-column="13" />
<folding> <folding>
<element signature="e#0#32#0" expanded="true" /> <element signature="e#0#32#0" expanded="true" />
</folding> </folding>
@ -116,23 +117,10 @@
</component> </component>
<component name="FindInProjectRecents"> <component name="FindInProjectRecents">
<findStrings> <findStrings>
<find>10</find>
<find>regalsik</find>
<find>#</find>
<find>put</find>
<find>randomB</find>
<find>search</find>
<find>sear</find>
<find>rando</find>
<find>chi</find>
<find>genera</find>
<find>return</find>
<find>occupied</find>
<find>astarstate</find> <find>astarstate</find>
<find>grid</find> <find>grid</find>
<find>whereDecision</find> <find>whereDecision</find>
<find>genMa</find> <find>genMa</find>
<find>random</find>
<find>mapfor</find> <find>mapfor</find>
<find>tmp</find> <find>tmp</find>
<find>global</find> <find>global</find>
@ -142,10 +130,23 @@
<find>eve</find> <find>eve</find>
<find>doW</find> <find>doW</find>
<find>self</find> <find>self</find>
<find>moves</find>
<find>gen</find> <find>gen</find>
<find>wybierz</find> <find>data.gen</find>
<find>data.geny</find>
<find>crosso</find>
<find>fitness</find> <find>fitness</find>
<find>wybierzUnbox</find>
<find>genRandomPop</find>
<find>cro</find>
<find>moves</find>
<find>genPop</find>
<find>wybierzUn</find>
<find>cros</find>
<find>cross</find>
<find>cr</find>
<find>genpo</find>
<find>random</find>
<find>wybierz</find>
</findStrings> </findStrings>
</component> </component>
<component name="Git.Settings"> <component name="Git.Settings">
@ -171,22 +172,22 @@
<option value="$PROJECT_DIR$/genetyczny/randomBox.py" /> <option value="$PROJECT_DIR$/genetyczny/randomBox.py" />
<option value="$PROJECT_DIR$/AStar.py" /> <option value="$PROJECT_DIR$/AStar.py" />
<option value="$PROJECT_DIR$/genetyczny/Gene1.py" /> <option value="$PROJECT_DIR$/genetyczny/Gene1.py" />
<option value="$PROJECT_DIR$/genetyczny/Gene.py" />
<option value="$PROJECT_DIR$/main.py" /> <option value="$PROJECT_DIR$/main.py" />
<option value="$PROJECT_DIR$/genetyczny/genetyczny.py" /> <option value="$PROJECT_DIR$/genetyczny/genetyczny.py" />
<option value="$PROJECT_DIR$/genetyczny/Data.py" /> <option value="$PROJECT_DIR$/genetyczny/Data.py" />
<option value="$PROJECT_DIR$/Mapa/generate.py" /> <option value="$PROJECT_DIR$/Mapa/generate.py" />
<option value="$PROJECT_DIR$/program.py" /> <option value="$PROJECT_DIR$/genetyczny/Gene.py" />
<option value="$PROJECT_DIR$/genetyczny/funkcje.py" /> <option value="$PROJECT_DIR$/genetyczny/funkcje.py" />
<option value="$PROJECT_DIR$/genetyczny/eee.py" /> <option value="$PROJECT_DIR$/genetyczny/eee.py" />
<option value="$PROJECT_DIR$/program.py" />
</list> </list>
</option> </option>
</component> </component>
<component name="ProjectFrameBounds" extendedState="7"> <component name="ProjectFrameBounds" extendedState="7">
<option name="x" value="1897" /> <option name="x" value="2203" />
<option name="y" value="-9" /> <option name="y" value="-9" />
<option name="width" value="1551" /> <option name="width" value="606" />
<option name="height" value="879" /> <option name="height" value="838" />
</component> </component>
<component name="ProjectLevelVcsManager" settingsEditedManually="true" /> <component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectView"> <component name="ProjectView">
@ -383,16 +384,17 @@
</component> </component>
<component name="ToolWindowManager"> <component name="ToolWindowManager">
<frame x="1912" y="-8" width="1216" height="1576" extended-state="6" /> <frame x="1912" y="-8" width="1216" height="1576" extended-state="6" />
<editor active="true" />
<layout> <layout>
<window_info content_ui="combo" id="Project" order="0" sideWeight="0.4995206" visible="true" weight="0.2733564" /> <window_info content_ui="combo" id="Project" order="0" sideWeight="0.4995206" visible="true" weight="0.2733564" />
<window_info id="Structure" order="1" sideWeight="0.5004794" side_tool="true" weight="0.3382353" /> <window_info id="Structure" order="1" sideWeight="0.5004794" side_tool="true" weight="0.3382353" />
<window_info id="Favorites" order="2" sideWeight="0.5011338" side_tool="true" weight="0.27768165" /> <window_info id="Favorites" order="2" sideWeight="0.5011338" side_tool="true" weight="0.27768165" />
<window_info anchor="bottom" id="Message" order="0" /> <window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" weight="0.32640332" /> <window_info active="true" anchor="bottom" id="Find" order="1" visible="true" weight="0.32640332" />
<window_info anchor="bottom" id="Run" order="2" sideWeight="0.49932885" weight="0.22037423" /> <window_info anchor="bottom" id="Run" order="2" sideWeight="0.49932885" weight="0.21136521" />
<window_info anchor="bottom" id="Cvs" order="3" weight="0.25" /> <window_info anchor="bottom" id="Cvs" order="3" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="4" weight="0.4" /> <window_info anchor="bottom" id="Inspection" order="4" weight="0.4" />
<window_info active="true" anchor="bottom" x="1942" y="641" width="1156" height="277" id="Debug" order="5" sideWeight="0.49932885" visible="true" weight="0.13305613" /> <window_info anchor="bottom" x="1942" y="641" width="1156" height="277" id="Debug" order="5" sideWeight="0.49932885" weight="0.4144144" />
<window_info anchor="bottom" id="TODO" order="6" weight="0.32848233" /> <window_info anchor="bottom" id="TODO" order="6" weight="0.32848233" />
<window_info anchor="bottom" id="Version Control" order="7" weight="0.32848233" /> <window_info anchor="bottom" id="Version Control" order="7" weight="0.32848233" />
<window_info anchor="bottom" id="Terminal" order="8" weight="0.32848233" /> <window_info anchor="bottom" id="Terminal" order="8" weight="0.32848233" />
@ -431,18 +433,8 @@
<breakpoints> <breakpoints>
<line-breakpoint enabled="true" suspend="THREAD" type="python-line"> <line-breakpoint enabled="true" suspend="THREAD" type="python-line">
<url>file://$PROJECT_DIR$/genetyczny/eee.py</url> <url>file://$PROJECT_DIR$/genetyczny/eee.py</url>
<line>24</line> <line>31</line>
<option name="timeStamp" value="190" /> <option name="timeStamp" value="229" />
</line-breakpoint>
<line-breakpoint enabled="true" suspend="THREAD" type="python-line">
<url>file://$PROJECT_DIR$/genetyczny/funkcje.py</url>
<line>197</line>
<option name="timeStamp" value="192" />
</line-breakpoint>
<line-breakpoint enabled="true" suspend="THREAD" type="python-line">
<url>file://$PROJECT_DIR$/genetyczny/funkcje.py</url>
<line>40</line>
<option name="timeStamp" value="195" />
</line-breakpoint> </line-breakpoint>
</breakpoints> </breakpoints>
<default-breakpoints> <default-breakpoints>
@ -585,6 +577,13 @@
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$PROJECT_DIR$/Mapa/box.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="68">
<caret line="4" column="24" selection-start-line="4" selection-start-column="24" selection-end-line="4" selection-end-column="24" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/AStarState.py"> <entry file="file://$PROJECT_DIR$/AStarState.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="153"> <state relative-caret-position="153">
@ -592,54 +591,34 @@
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$PROJECT_DIR$/genetyczny/Gene.py"> <entry file="file://$USER_HOME$/AppData/Local/Programs/Python/Python37/Lib/codecs.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="102"> <state relative-caret-position="155">
<caret line="6" column="16" selection-start-line="6" selection-start-column="16" selection-end-line="6" selection-end-column="16" /> <caret line="310" column="64" lean-forward="true" selection-start-line="310" selection-start-column="10" selection-end-line="310" selection-end-column="64" />
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$USER_HOME$/AppData/Local/Programs/Python/Python37/Lib/codecs.py"> <entry file="file://$APPLICATION_HOME_DIR$/helpers/pydev/pydevd.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="172"> <state relative-caret-position="255">
<caret line="311" selection-start-line="311" selection-end-line="311" /> <caret line="1740" selection-start-line="1740" selection-end-line="1740" />
</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="731"> <state relative-caret-position="229">
<caret line="43" selection-start-line="43" selection-end-line="43" /> <caret line="41" selection-start-line="41" selection-end-line="41" />
<folding> <folding>
<element signature="e#0#21#0" expanded="true" /> <element signature="e#0#21#0" expanded="true" />
</folding> </folding>
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$PROJECT_DIR$/program.py"> <entry file="file://$PROJECT_DIR$/genetyczny/Gene.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="815"> <state relative-caret-position="119">
<caret line="94" selection-start-line="94" selection-end-line="94" /> <caret line="7" column="36" selection-start-line="7" selection-start-column="13" selection-end-line="7" selection-end-column="36" />
<folding>
<element signature="e#0#13#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/AStar.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="595">
<caret line="35" selection-start-line="35" selection-end-line="35" />
<folding>
<element signature="e#0#12#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/Mapa/box.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="68">
<caret line="4" column="24" selection-start-line="4" selection-start-column="24" selection-end-line="4" selection-end-column="24" />
</state> </state>
</provider> </provider>
</entry> </entry>
@ -650,10 +629,20 @@
</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="117">
<caret line="19" column="31" selection-start-line="19" selection-start-column="28" selection-end-line="19" selection-end-column="31" />
<folding>
<element signature="e#0#12#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/genetyczny/eee.py"> <entry file="file://$PROJECT_DIR$/genetyczny/eee.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="408"> <state relative-caret-position="308">
<caret line="24" selection-start-line="24" selection-end-line="24" /> <caret line="35" column="13" selection-start-line="35" selection-start-column="13" selection-end-line="35" selection-end-column="13" />
<folding> <folding>
<element signature="e#0#32#0" expanded="true" /> <element signature="e#0#32#0" expanded="true" />
</folding> </folding>
@ -662,13 +651,23 @@
</entry> </entry>
<entry file="file://$PROJECT_DIR$/genetyczny/funkcje.py"> <entry file="file://$PROJECT_DIR$/genetyczny/funkcje.py">
<provider selected="true" editor-type-id="text-editor"> <provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="424"> <state relative-caret-position="519">
<caret line="54" selection-start-line="54" selection-end-line="54" /> <caret line="217" column="33" selection-start-line="217" selection-start-column="33" selection-end-line="217" selection-end-column="33" />
<folding> <folding>
<element signature="e#0#48#0" expanded="true" /> <element signature="e#0#48#0" expanded="true" />
</folding> </folding>
</state> </state>
</provider> </provider>
</entry> </entry>
<entry file="file://$PROJECT_DIR$/program.py">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="536">
<caret line="94" column="47" lean-forward="true" selection-start-line="94" selection-start-column="47" selection-end-line="94" selection-end-column="47" />
<folding>
<element signature="e#0#13#0" expanded="true" />
</folding>
</state>
</provider>
</entry>
</component> </component>
</project> </project>

Binary file not shown.

View File

@ -1,8 +1,9 @@
class Gene: class Gene:
def __init__(self, kordydnaty): def __init__(self):
self.kordy = kordydnaty self.kordy = None
self.unbox1 = None self.unbox1 = None
self.unbox2 = None self.unbox2 = None
self.ktoryUnbox = None self.unboxWczesniejszegoGenu = None
self.kordyUnboxa = None

View File

@ -7,11 +7,11 @@ import matplotlib.animation as animation
def start(data, wheel): def start(data, wheel):
ileGeneracji = 200 ileGeneracji = 500
ileWPopulacji = 16 ileWPopulacji = 16
fragment = 0.2 fragment = 0.5
mutacja = 0.05 mutacja = 0.05
unbox = 3 unbox = 1
data.kordyWozka = (wheel.ns, wheel.we) data.kordyWozka = (wheel.ns, wheel.we)
@ -22,24 +22,27 @@ def start(data, wheel):
if i == 0: if i == 0:
best2 = dwieNajlepsze(randomPopulation, data) best2 = dwieNajlepsze(randomPopulation, data)
else: else:
x = genPopulacje(best2[0], best2[1], ileWPopulacji, fragment, mutacja) x = genPopulacje(data,best2[0], best2[1], ileWPopulacji, fragment, mutacja)
best2 = dwieNajlepsze(x, data) best2 = dwieNajlepsze(x, data)
del x
data.histZmian.append(data.best[1]) data.histZmian.append(data.best[1])
break
#rysujWykres(data, ileGeneracji, 1000, 2000)
rysujWykres(data, ileGeneracji, 0, 2000)
def ruszWozek(gen,data, moves, wheel): def zbierzBox(gen,data, moves, kordStartowy):
regalKordy = gen.kordy regalKordy = gen.kordy
unbox = data.unbox[gen.ktoryUnbox]
star = AStar() star = AStar()
mapForAStar = data.astarMap[:] mapForAStar = data.astarMap[:]
mapForAStar[regalKordy[0]][regalKordy[1]] = 0 mapForAStar[regalKordy[0]][regalKordy[1]] = 0
path = star.search([wheel.ns, wheel.we], regalKordy, mapForAStar, 1, 1) path = star.search([kordStartowy[0], kordStartowy[1]], regalKordy, mapForAStar, 1, 1)
cns = wheel.ns cns = kordStartowy[0]
cwe = wheel.we cwe = kordStartowy[1]
value = path[cns][cwe] value = path[cns][cwe]
while True: while True:
if cns > 0 and path[cns - 1][cwe] == (value + 1): if cns > 0 and path[cns - 1][cwe] == (value + 1):
cns = cns - 1 cns = cns - 1
@ -57,7 +60,7 @@ def ruszWozek(gen,data, moves, wheel):
value = value + 1 value = value + 1
continue continue
if cwe < (len(mapForAStar[0]) - 1) and path[cns][cwe + 1] == (value + 1): if cwe < (len(mapForAStar[0]) - 1) and path[cns][cwe + 1] == (value + 1):
cns = cns + 1 cwe = cwe + 1
moves.append(4) moves.append(4)
value = value + 1 value = value + 1
continue continue
@ -65,10 +68,21 @@ def ruszWozek(gen,data, moves, wheel):
mapForAStar[regalKordy[0]][regalKordy[1]] = 1 mapForAStar[regalKordy[0]][regalKordy[1]] = 1
# wyszukiwanie ścieżki z miejsca podjęcia paczki do regału # wyszukiwanie ścieżki z miejsca podjęcia paczki do regału
# zmienna path posiada macierz oraz kroki podjęte przez wózek # zmienna path posiada macierz oraz kroki podjęte przez wózek
path = star.search([regalKordy[0], regalKordy[1]], unbox, mapForAStar, 1, 1) path = star.search([regalKordy[0], regalKordy[1]], gen.kordyUnboxa, mapForAStar, 1, 1)
#mapForAStar[where[0]][where[1]] = 1 #mapForAStar[where[0]][where[1]] = 1
value = path[cns][cwe] value = path[cns][cwe]
while True: while True:
if(value == 0):
if(path[cns - 1][cwe] == 1):
cns = cns - 1
elif(path[cns + 1][cwe] == 1):
cns = cns + 1
elif(path[cns][cwe - 1] == 1):
cwe = cwe - 1
elif(path[cns][cwe + 1] == 1):
cwe = cwe + 1
value = path[cns][cwe]
continue
if cns > 0 and path[cns - 1][cwe] == (value + 1): if cns > 0 and path[cns - 1][cwe] == (value + 1):
cns = cns - 1 cns = cns - 1
moves.append(1) moves.append(1)
@ -140,3 +154,11 @@ def okno():
good = False good = False
if(good == True): if(good == True):
return [fieldValues[0], fieldValues[1], fieldValues[2],fieldValues[3]] return [fieldValues[0], fieldValues[1], fieldValues[2],fieldValues[3]]
def zwolnijPamiec(generacja, dwieNajlepsze):
for i in generacja:
if( i != dwieNajlepsze[0] and i != dwieNajlepsze[1]):
generacja.remove(i)
return generacja

View File

@ -10,14 +10,16 @@ import numpy
def generateGeny(data): def generateGeny(data):
geny = []
zajeteRegaly = data.zajeteRegaly[:] zajeteRegaly = data.zajeteRegaly[:]
for r in zajeteRegaly: for r in zajeteRegaly:
g = Gene(r) g = Gene()
g.kordy = r
g.unbox1 = policzCost(data.astarMap,r,data.unbox[0]) g.unbox1 = policzCost(data.astarMap,r,data.unbox[0])
if(len(data.unbox) > 1): if(len(data.unbox) > 1):
g.unbox2 = policzCost(data.astarMap,r,data.unbox[1]) g.unbox2 = policzCost(data.astarMap,r,data.unbox[1])
data.geny.append(g) geny.append(g)
return geny
def znajdzUnbox(data,mapa): def znajdzUnbox(data,mapa):
unboxy = [] unboxy = []
@ -38,15 +40,15 @@ def policzCost(mapaBoxy, poczatek, koniec):
return koszt return koszt
def genRandomChromosome(data): def genRandomChromosome(data):
chromosome = data.geny[:] chromosome = generateGeny(data)
random.shuffle(chromosome) random.shuffle(chromosome)
krotkaKosztJakiUnbox = None
unboxLastGen = None unboxLastGen = None
for gen in chromosome: for gen in chromosome:
gen.ktoryUnbox = unboxLastGen gen.unboxWczesniejszegoGenu = unboxLastGen
krotkaKosztJakiUnbox = wybierzUnbox(gen, data.jakLiczycKoszt) krotkaKosztJakiUnbox = wybierzUnbox(gen, data.jakLiczycKoszt)
unboxLastGen = krotkaKosztJakiUnbox[1] unboxLastGen = krotkaKosztJakiUnbox[1]
gen.kordyUnboxa = data.unbox[krotkaKosztJakiUnbox[1]]
return chromosome return chromosome
def genRandomPopulation(data, ileWPopulacji): def genRandomPopulation(data, ileWPopulacji):
@ -80,7 +82,7 @@ def fitness(chromosome, data):
return koszt return koszt
def wybierzUnbox(gen, jakLiczycKoszt): def wybierzUnbox(gen, jakLiczycKoszt): #funkcja ustawiajaca jaki unbox
if(jakLiczycKoszt == 0): if(jakLiczycKoszt == 0):
x = random.choice([gen.unbox1, gen.unbox2]) x = random.choice([gen.unbox1, gen.unbox2])
if(x == gen.unbox1): if(x == gen.unbox1):
@ -136,6 +138,7 @@ def znajdzBox(mapa, regals):
zajeteRegaly.append((x,y)) zajeteRegaly.append((x,y))
return zajeteRegaly return zajeteRegaly
#wybiera z populacji dwa najlepsze chromosomy
def dwieNajlepsze(populacja, data): def dwieNajlepsze(populacja, data):
tmpPopulacja = populacja[:] tmpPopulacja = populacja[:]
chromFitness = [] chromFitness = []
@ -164,37 +167,56 @@ def dwieNajlepsze(populacja, data):
return (pierwsza, druga) return (pierwsza, druga)
def genPopulacje(pierwszy, drugi, ileWPopulacji, fragmentLiczba, wspMutacji): def genPopulacje(data,pierwszy, drugi, ileWPopulacji, fragmentLiczba, wspMutacji):
ileWChrom = len(pierwszy) ileWChrom = len(pierwszy)
fragment = round(fragmentLiczba*ileWChrom) fragment = round(fragmentLiczba*ileWChrom)
if(fragment == 1):
fragment +=1
nowaPopulacja = [] nowaPopulacja = []
for i in range(ileWPopulacji): for i in range(ileWPopulacji):
nowaPopulacja.append(crossover(pierwszy,drugi,fragment, wspMutacji)) nowaPopulacja.append(crossover(data,pierwszy,drugi,fragment, wspMutacji))
return nowaPopulacja return nowaPopulacja
def crossover(pierwszy, drugi, fragmentLiczba, wspMutacji): def crossover(data,pierwszy, drugi, fragmentLiczba, wspMutacji):
ileWChrom = len(pierwszy) ileWChrom = len(pierwszy)
tmp = random.randint(0, ileWChrom-fragmentLiczba) tmp = random.randint(0, ileWChrom-fragmentLiczba)
kordyFragment = (tmp,tmp+fragmentLiczba) kordyFragment = (tmp,tmp+fragmentLiczba)
nowyChrom = [None] * ileWChrom nowyChrom = [Gene() for q in range(ileWChrom)]
iterator = kordyFragment[1] iterator = kordyFragment[1]
pomIterator = kordyFragment[1] pomIterator = kordyFragment[1]
nowyChrom[kordyFragment[0]:kordyFragment[1]] = pierwszy[kordyFragment[0]:kordyFragment[1]] usedKordy = []
for i in range(kordyFragment[0],kordyFragment[1]):
nowyChrom[i].kordy = pierwszy[i].kordy
nowyChrom[i].unbox1 = pierwszy[i].unbox1
nowyChrom[i].unbox2 = pierwszy[i].unbox2
usedKordy.append(pierwszy[i].kordy)
while(None in nowyChrom): for x in range(ileWChrom):
if(iterator > ileWChrom - 1): if(iterator > ileWChrom - 1):
iterator = 0 iterator = 0
if(pomIterator > ileWChrom - 1): if(pomIterator > ileWChrom - 1):
pomIterator = 0 pomIterator = 0
if(nowyChrom[iterator] == None and drugi[pomIterator] not in nowyChrom): if(nowyChrom[iterator].kordy == None and drugi[pomIterator].kordy not in usedKordy):
nowyChrom[iterator] = drugi[pomIterator] nowyChrom[iterator].kordy = drugi[pomIterator].kordy
nowyChrom[iterator].kordy = drugi[pomIterator].kordy
nowyChrom[iterator].unbox1 = drugi[pomIterator].unbox1
nowyChrom[iterator].unbox2 = drugi[pomIterator].unbox2
iterator += 1 iterator += 1
pomIterator += 1 pomIterator += 1
elif(nowyChrom[iterator] == None and drugi[pomIterator] in nowyChrom): else:
pomIterator +=1 pomIterator +=1
nowyChrom = mutate(wspMutacji, nowyChrom) nowyChrom = mutate(wspMutacji, nowyChrom)
unboxLastGen = None
for gen in nowyChrom:
gen.unboxWczesniejszegoGenu = unboxLastGen
krotkaKosztJakiUnbox = wybierzUnbox(gen, data.jakLiczycKoszt)
unboxLastGen = krotkaKosztJakiUnbox[1]
gen.kordyUnboxa = data.unbox[krotkaKosztJakiUnbox[1]]
return nowyChrom return nowyChrom
@ -205,7 +227,7 @@ def updateMap(data, map, mapForAstar, regals):
data.zajeteRegaly = znajdzBox(map, regals) data.zajeteRegaly = znajdzBox(map, regals)
data.astarMap = data.genMap(mapForAstar) data.astarMap = data.genMap(mapForAstar)
def mutate(wspMutacji, chrom): def mutate(wspMutacji, chrom): #w zaleznosci od tego jak wiele mutwac wybierz pary i zamien miejscami
ileWChrom = len(chrom) ileWChrom = len(chrom)
ileZmian = round(ileWChrom * wspMutacji) ileZmian = round(ileWChrom * wspMutacji)
for i in range(ileZmian): for i in range(ileZmian):

View File

@ -59,7 +59,7 @@ class MainWindow:
self.map[i][j] = Shelf(self.screen, self.cell, i, j, (self.map[i][j]-3)%4, (self.map[i][j]-3)//4) self.map[i][j] = Shelf(self.screen, self.cell, i, j, (self.map[i][j]-3)%4, (self.map[i][j]-3)//4)
self.mapForAStar[i][j] = 1 self.mapForAStar[i][j] = 1
self.map = randomBox(self.map, self.regals, 5) self.map = randomBox(self.map, self.regals, 15)
updateMap(self.data, self.map, self.mapForAStar, self.regals) updateMap(self.data, self.map, self.mapForAStar, self.regals)
generateGeny(self.data) generateGeny(self.data)
@ -94,7 +94,12 @@ class MainWindow:
if event.key == pygame.K_g: if event.key == pygame.K_g:
start(self.data,self.wheel) start(self.data,self.wheel)
for gen in self.data.best[0]: for gen in self.data.best[0]:
ruszWozek(gen,self.data, self.moves, self.wheel) if(gen.unboxWczesniejszegoGenu == None):
kordStartowy = (self.wheel.ns, self.wheel.we)
else:
kordStartowy = self.data.unbox[gen.unboxWczesniejszegoGenu]
zbierzBox(gen,self.data, self.moves, kordStartowy)
elif len(self.moves)==0: elif len(self.moves)==0:
self.wheel.move(event, self.map) self.wheel.move(event, self.map)