treelearn

This commit is contained in:
Tomasz Adamczyk 2021-05-16 15:36:53 +02:00
parent dea6adc796
commit 4b73064f4e
5 changed files with 31 additions and 21 deletions

View File

@ -3,7 +3,7 @@
<component name="ChangeListManager">
<list default="true" id="56453584-72bd-49f4-a39c-fccf91ab20c6" 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$/mydecisiontree.png" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/py.py" beforeDir="false" afterPath="$PROJECT_DIR$/py.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/treelearn.py" beforeDir="false" afterPath="$PROJECT_DIR$/treelearn.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
@ -132,6 +132,7 @@
<workItem from="1621110688412" duration="148000" />
<workItem from="1621110850717" duration="377000" />
<workItem from="1621111315400" duration="72000" />
<workItem from="1621171225695" duration="880000" />
</task>
<servers />
</component>
@ -147,22 +148,22 @@
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state x="690" y="287" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog/0.0.1920.1080@0.0.1920.1080" timestamp="1617994393023" />
<state width="1879" height="295" key="GridCell.Tab.0.bottom" timestamp="1621111387013">
<state width="1879" height="295" key="GridCell.Tab.0.bottom" timestamp="1621172112876">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="295" key="GridCell.Tab.0.bottom/0.0.1920.1080@0.0.1920.1080" timestamp="1621111387013" />
<state width="1879" height="295" key="GridCell.Tab.0.center" timestamp="1621111387012">
<state width="1879" height="295" key="GridCell.Tab.0.bottom/0.0.1920.1080@0.0.1920.1080" timestamp="1621172112876" />
<state width="1879" height="295" key="GridCell.Tab.0.center" timestamp="1621172112876">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="295" key="GridCell.Tab.0.center/0.0.1920.1080@0.0.1920.1080" timestamp="1621111387012" />
<state width="1879" height="295" key="GridCell.Tab.0.left" timestamp="1621111387012">
<state width="1879" height="295" key="GridCell.Tab.0.center/0.0.1920.1080@0.0.1920.1080" timestamp="1621172112876" />
<state width="1879" height="295" key="GridCell.Tab.0.left" timestamp="1621172112876">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="295" key="GridCell.Tab.0.left/0.0.1920.1080@0.0.1920.1080" timestamp="1621111387012" />
<state width="1879" height="295" key="GridCell.Tab.0.right" timestamp="1621111387013">
<state width="1879" height="295" key="GridCell.Tab.0.left/0.0.1920.1080@0.0.1920.1080" timestamp="1621172112876" />
<state width="1879" height="295" key="GridCell.Tab.0.right" timestamp="1621172112876">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="295" key="GridCell.Tab.0.right/0.0.1920.1080@0.0.1920.1080" timestamp="1621111387013" />
<state width="1879" height="295" key="GridCell.Tab.0.right/0.0.1920.1080@0.0.1920.1080" timestamp="1621172112876" />
<state width="1879" height="364" key="GridCell.Tab.1.bottom" timestamp="1619289752045">
<screen x="0" y="0" width="1920" height="1080" />
</state>
@ -179,10 +180,10 @@
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="364" key="GridCell.Tab.1.right/0.0.1920.1080@0.0.1920.1080" timestamp="1619289752045" />
<state x="0" y="0" key="com.intellij.ide.util.TipDialog" timestamp="1621111315179">
<state x="0" y="0" key="com.intellij.ide.util.TipDialog" timestamp="1621171227060">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state x="0" y="0" key="com.intellij.ide.util.TipDialog/0.0.1920.1080@0.0.1920.1080" timestamp="1621111315179" />
<state x="0" y="0" key="com.intellij.ide.util.TipDialog/0.0.1920.1080@0.0.1920.1080" timestamp="1621171227060" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
@ -196,6 +197,6 @@
</breakpoint-manager>
</component>
<component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/SmartTractor$py.coverage" NAME="py Coverage Results" MODIFIED="1621111375675" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
<SUITE FILE_PATH="coverage/SmartTractor$py.coverage" NAME="py Coverage Results" MODIFIED="1621172058000" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
</component>
</project>

Binary file not shown.

9
py.py
View File

@ -20,7 +20,8 @@ def main():
tractor1 = tractor.Tractor(amount_of_seeds_dict, collected_plants_dict, definitions.TRACTOR_DIRECTION_NORTH, fertilizer_dict, definitions.TRACTOR_FUEL, definitions.TRACTOR_WATER_LEVEL, 0, 0)
tractor1_rect = pygame.Rect(tractor1.get_x(), tractor1.get_y(), definitions.BLOCK_SIZE, definitions.BLOCK_SIZE)
clock = pygame.time.Clock()
treelearn.treelearn()
tree = treelearn.treelearn() #tworzenie drzewa decyzyjnego
decision = [0] #początkowa decyzja o braku powrotu do stacji (0)
run = True
while run: #pętla główna programu
clock.tick(definitions.FPS)
@ -31,12 +32,16 @@ def main():
if not move_list and plant.Plant.if_any_mature_plant(map1) is True: #jeżeli są jakieś ruchy do wykonania w move_list oraz istnieje jakaś dojrzała roślina
istate = graph.Istate(tractor1.get_direction(), tractor1.get_x() / definitions.BLOCK_SIZE, tractor1.get_y() / definitions.BLOCK_SIZE) #stan początkowy traktora (jego orientacja oraz jego aktualne współrzędne)
#move_list = (graph.graphsearch([], [], istate, graph.succ, plant.Plant.get_closest_mature_plant(map1, tractor1))) #lista z ruchami, które należy po kolei wykonać, graph
move_list = (astar.graphsearch([], [], istate, graph.succ, plant.Plant.get_closest_mature_plant(map1, istate), astar.f, map1)) #lista z ruchami, które należy po kolei wykonać, astar
if decision == [0]: #jeżeli decyzja jest 0 (brak powrotu do stacji) to uprawiaj pole
move_list = (astar.graphsearch([], [], istate, graph.succ, plant.Plant.get_closest_mature_plant(map1, istate), astar.f, map1)) #lista z ruchami, które należy po kolei wykonać, astar
else: #jeżeli decyzja jest 1 (powrót do stacji) to wróć do stacji uzupełnić zapasy
move_list = (graph.graphsearch([], [], istate, graph.succ, (0, 0))) #lista z ruchami, które należy po kolei wykonać, graphsearch
elif move_list: #jeżeli move_list nie jest pusta
tractor1.handle_movement(move_list.pop(0), tractor1_rect) #wykonaj kolejny ruch oraz zdejmij ten ruch z początku listy
else:
tractor1.handle_random_movement(tractor1_rect) #wykonuj losowe ruchy
tractor1.do_work(map1, station1, tractor1_rect) #wykonaj pracę na danym polu
decision = treelearn.make_decision(tree, tractor1.get_all_amount_of_seeds(), tractor1.get_all_collected_plants(), tractor1.get_all_fertilizer(), tractor1.get_fuel(), tractor1.get_water_level()) #podejmij decyzję czy wracać do stacji (0 : NIE, 1 : TAK)
plant.Plant.grow_plants(map1) #zwiększ poziom dojrzałości roślin
pygame.quit()
if __name__ == "__main__":

View File

@ -5,16 +5,20 @@ import pandas
import pydotplus
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
def treelearn():
df = pandas.read_csv(os.path.join('resources', 'data.csv'))
def treelearn(): #zwraca utworzone drzewo decyzyjne
df = pandas.read_csv(os.path.join('resources', 'data.csv')) #czytanie danych do nauki drzewa z pliku .csv
features = ['amount of seeds', 'collected plants', 'fertilizer', 'fuel', 'water level']
x = df[features]
y = df['back to station']
dtree = DecisionTreeClassifier()
dtree = dtree.fit(x, y)
x = df[features] #wczytanie atrybutów, z których ma się uczyć drzewo
y = df['back to station'] #podjęte decyzje
dtree = DecisionTreeClassifier() #tworzy obiekt drzewa
dtree = dtree.fit(x, y) #uczy drzewo
data = tree.export_graphviz(dtree, out_file=None, feature_names=features)
graph = pydotplus.graph_from_dot_data(data)
graph.write_png(os.path.join('resources', 'mydecisiontree.png'))
img = pltimg.imread(os.path.join('resources', 'mydecisiontree.png'))
imgplot = plt.imshow(img)
plt.show()
plt.show() #wyświetl drzewo decyzyjne
return dtree
def make_decision(tree, amount_of_seeds, collected_plants, fertilizer, fuel, water_level): #zwraca decyzję o powrocie do stacji (0 : NIE, 1 : TAK)
decision = tree.predict([[amount_of_seeds, collected_plants, fertilizer, fuel, water_level]]) #podejmij decyzję na podstawie aktualnych parametrów traktora o powrocie do stacji lub nie
return decision