learn, graph, read csv; decision making #23

Merged
s459307 merged 1 commits from decisiontree into master 2022-05-13 08:49:12 +02:00
2 changed files with 82 additions and 49 deletions

100
main.py
View File

@ -7,28 +7,30 @@ import astar
import common
import field
import settings
import tree
import shutup
possible_fields = {
possibleFields = {
'dirt': field.Dirt(),
'grass': field.Grass(),
'cobble': field.Cobble(),
'sand': field.Sand(),
'station': field.Station(),
'carrot_empty': field.Carrot('carrot_empty'),
'carrot_sow': field.Carrot('carrot_sow'),
'carrot_watered': field.Carrot('carrot_watered'),
'carrot_feritized': field.Carrot('carrot_feritized'),
'potato_empty': field.Potato('potato_empty'),
'potato_sow': field.Potato('potato_sow'),
'potato_watered': field.Potato('potato_watered'),
'potato_feritized': field.Potato('potato_feritized'),
'wheat_empty': field.Wheat('wheat_empty'),
'wheat_sow': field.Wheat('wheat_sow'),
'wheat_watered': field.Wheat('wheat_watered'),
'wheat_feritized': field.Wheat('wheat_feritized')
'carrot_empty': field.Carrot_empty(),
'carrot_sow': field.Carrot_sow(),
'carrot_watered': field.Carrot_watered(),
'carrot_feritized': field.Carrot_feritized(),
'potato_empty': field.Potato_empty(),
'potato_sow': field.Potato_sow(),
'potato_watered': field.Potato_watered(),
'potato_feritized': field.Potato_feritized(),
'wheat_empty': field.Wheat_empty(),
'wheat_sow': field.Wheat_sow(),
'wheat_watered': field.Wheat_watered(),
'wheat_feritized': field.Wheat_feritized()
}
possible_fields_with_plants = [
possibleFieldsWithPlants = [
'carrot_empty',
'carrot_sow',
'carrot_watered',
@ -43,11 +45,12 @@ possible_fields_with_plants = [
'wheat_feritized'
]
shutup.please()
def randomize_map():
fields_array = []
for i in possible_fields:
fields_array.append(possible_fields[i].tile.object)
for i in possibleFields:
fields_array.append(possibleFields[i].tile.object)
field_array_big = []
field_array_small = []
field_array_big_2 = []
@ -61,16 +64,16 @@ def randomize_map():
# k = random.choice(list(possibleFields.keys()))
x = random.uniform(0, 100)
if x <= 80:
plant = random.choice(possible_fields_with_plants)
field_array_small.append(possible_fields[plant].tile.object)
plant = random.choice(possibleFieldsWithPlants)
field_array_small.append(possibleFields[plant].tile.object)
field_array_small_2.append('dirt')
field_array_small_3.append(plant)
elif 80 < x <= 90:
field_array_small.append(possible_fields['sand'].tile.object)
field_array_small.append(possibleFields['sand'].tile.object)
field_array_small_2.append('sand')
field_array_small_3.append('sand')
elif 90 < x <= 100:
field_array_small.append(possible_fields['grass'].tile.object)
field_array_small.append(possibleFields['grass'].tile.object)
field_array_small_2.append('grass')
field_array_small_3.append('grass')
field_array_big.append(field_array_small)
@ -81,14 +84,14 @@ def randomize_map():
field_array_small_3 = []
for i in range(height):
field_array_big[math.floor(width / 2)][i] = possible_fields['cobble'].tile.object
field_array_big[math.floor(width / 2)][i] = possibleFields['cobble'].tile.object
field_array_big_2[math.floor(width / 2)][i] = 'cobble'
field_array_big_3[math.floor(width / 2)][i] = 'cobble'
for i in range(width):
field_array_big[i][math.floor(height / 2)] = possible_fields['cobble'].tile.object
field_array_big[i][math.floor(height / 2)] = possibleFields['cobble'].tile.object
field_array_big_2[i][math.floor(height / 2)] = 'cobble'
field_array_big_3[i][math.floor(height / 2)] = 'cobble'
field_array_big[0][0] = possible_fields['station'].tile.object
field_array_big[0][0] = possibleFields['station'].tile.object
field_array_big_2[0][0] = 'station'
field_array_big_3[0][0] = 'station'
return field_array_big, field_array_big_2, field_array_big_3
@ -97,16 +100,16 @@ def randomize_map():
def read_img(agent, fields):
window = common.get('window')
current_field = fields[agent.x()][agent.y()]
if current_field == possible_fields['grass'].tile.object:
window.blit(possible_fields['grass'].block.object, (0, 0))
elif current_field == possible_fields['dirt'].tile.object:
window.blit(possible_fields['dirt'].block.object, (0, 0))
elif current_field == possible_fields['sand'].tile.object:
window.blit(possible_fields['sand'].block.object, (0, 0))
elif current_field == possible_fields['cobble'].tile.object:
window.blit(possible_fields['cobble'].block.object, (0, 0))
elif current_field == possible_fields['station'].tile.object:
window.blit(possible_fields['station'].block.object, (0, 0))
if current_field == possibleFields['grass'].tile.object:
window.blit(possibleFields['grass'].block.object, (0, 0))
elif current_field == possibleFields['dirt'].tile.object:
window.blit(possibleFields['dirt'].block.object, (0, 0))
elif current_field == possibleFields['sand'].tile.object:
window.blit(possibleFields['sand'].block.object, (0, 0))
elif current_field == possibleFields['cobble'].tile.object:
window.blit(possibleFields['cobble'].block.object, (0, 0))
elif current_field == possibleFields['station'].tile.object:
window.blit(possibleFields['station'].block.object, (0, 0))
pygame.display.update()
pygame.time.delay(2000)
@ -143,7 +146,7 @@ def draw_window(agent, fields):
pygame.display.update()
def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement, agent):
def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement, agent, t):
fields_to_sow = []
fields_to_water = []
fields_to_feritize = []
@ -170,7 +173,6 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
tab = [i, j]
fields_to_harvest.append(tab)
while True:
decision = False # tu będzie wywołanie drzewa decyzyjnego (powrot do bazy)
cords = agent.coordinates()
x = cords['x']
y = cords['y']
@ -182,6 +184,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
carrots = agent.get_carrots()
potatoes = agent.get_potatoes()
wheat = agent.get_wheat()
decision = tree.make_decision(t, fuel, water, feritizer, carrots, potatoes, wheat, x, y, seeds)
print("fuel:", fuel, "water:", water, "feritizer:", feritizer, "seeds:", seeds, "carrots:", carrots,
"potatoes:", potatoes, "wheat:", wheat)
if k == 0 and len(fields_to_harvest) > 0 and decision == False: # harvest
@ -196,7 +199,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_sow.append(field_to_visit)
fields_with_plants[x1][y1] = 'wheat_empty'
fields_for_movement[x1][y1] = possible_fields['wheat_empty'].tile.object
fields_for_movement[x1][y1] = possibleFields['wheat_empty'].tile.object
draw_window(agent, fields_for_movement)
wheat += 1
agent.set_wheat(wheat)
@ -208,7 +211,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_sow.append(field_to_visit)
fields_with_plants[x1][y1] = 'carrot_empty'
fields_for_movement[x1][y1] = possible_fields['carrot_empty'].tile.object
fields_for_movement[x1][y1] = possibleFields['carrot_empty'].tile.object
draw_window(agent, fields_for_movement)
carrots += 1
agent.set_carrots(carrots)
@ -220,7 +223,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_sow.append(field_to_visit)
fields_with_plants[x1][y1] = 'potato_empty'
fields_for_movement[x1][y1] = possible_fields['potato_empty'].tile.object
fields_for_movement[x1][y1] = possibleFields['potato_empty'].tile.object
draw_window(agent, fields_for_movement)
potatoes += 1
agent.set_potatoes(potatoes)
@ -238,7 +241,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_feritize.append(field_to_visit)
fields_with_plants[x1][y1] = 'wheat_watered'
fields_for_movement[x1][y1] = possible_fields['wheat_watered'].tile.object
fields_for_movement[x1][y1] = possibleFields['wheat_watered'].tile.object
draw_window(agent, fields_for_movement)
water -= 1
agent.set_water(water)
@ -250,7 +253,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_feritize.append(field_to_visit)
fields_with_plants[x1][y1] = 'carrot_watered'
fields_for_movement[x1][y1] = possible_fields['carrot_watered'].tile.object
fields_for_movement[x1][y1] = possibleFields['carrot_watered'].tile.object
draw_window(agent, fields_for_movement)
water -= 1
agent.set_water(water)
@ -262,7 +265,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_feritize.append(field_to_visit)
fields_with_plants[x1][y1] = 'potato_watered'
fields_for_movement[x1][y1] = possible_fields['potato_watered'].tile.object
fields_for_movement[x1][y1] = possibleFields['potato_watered'].tile.object
draw_window(agent, fields_for_movement)
water -= 1
agent.set_water(water)
@ -280,7 +283,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_harvest.append(field_to_visit)
fields_with_plants[x1][y1] = 'wheat_feritized'
fields_for_movement[x1][y1] = possible_fields['wheat_feritized'].tile.object
fields_for_movement[x1][y1] = possibleFields['wheat_feritized'].tile.object
draw_window(agent, fields_for_movement)
feritizer -= 1
agent.set_feritizer(feritizer)
@ -292,7 +295,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_harvest.append(field_to_visit)
fields_with_plants[x1][y1] = 'carrot_feritized'
fields_for_movement[x1][y1] = possible_fields['carrot_feritized'].tile.object
fields_for_movement[x1][y1] = possibleFields['carrot_feritized'].tile.object
draw_window(agent, fields_for_movement)
feritizer -= 1
agent.set_feritizer(feritizer)
@ -304,7 +307,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_harvest.append(field_to_visit)
fields_with_plants[x1][y1] = 'potato_feritized'
fields_for_movement[x1][y1] = possible_fields['potato_feritized'].tile.object
fields_for_movement[x1][y1] = possibleFields['potato_feritized'].tile.object
draw_window(agent, fields_for_movement)
feritizer -= 1
agent.set_feritizer(feritizer)
@ -322,7 +325,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_water.append(field_to_visit)
fields_with_plants[x1][y1] = 'wheat_sow'
fields_for_movement[x1][y1] = possible_fields['wheat_sow'].tile.object
fields_for_movement[x1][y1] = possibleFields['wheat_sow'].tile.object
draw_window(agent, fields_for_movement)
seeds -= 1
agent.set_seeds(seeds)
@ -334,7 +337,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_water.append(field_to_visit)
fields_with_plants[x1][y1] = 'carrot_sow'
fields_for_movement[x1][y1] = possible_fields['carrot_sow'].tile.object
fields_for_movement[x1][y1] = possibleFields['carrot_sow'].tile.object
draw_window(agent, fields_for_movement)
seeds -= 1
agent.set_seeds(seeds)
@ -346,7 +349,7 @@ def generate_movement(fields_for_astar, fields_with_plants, fields_for_movement,
agent_movement(move_list, agent, fields_for_movement, fields_for_astar)
fields_to_water.append(field_to_visit)
fields_with_plants[x1][y1] = 'potato_sow'
fields_for_movement[x1][y1] = possible_fields['potato_sow'].tile.object
fields_for_movement[x1][y1] = possibleFields['potato_sow'].tile.object
draw_window(agent, fields_for_movement)
seeds -= 1
agent.set_seeds(seeds)
@ -429,6 +432,7 @@ def main():
pygame.display.set_caption(settings.Pygame.display_name())
fields, fields_2, fields_3 = randomize_map()
x = True
t = tree.treelearn()
while common.get('game_running'):
pygame.time.Clock().tick(settings.Pygame.fps())
for event in pygame.event.get():
@ -439,7 +443,7 @@ def main():
else:
draw_window(agent, fields)
if x:
generate_movement(fields_2, fields_3, fields, agent)
generate_movement(fields_2, fields_3, fields, agent, t)
x = False
agent_action(agent.rotate(None))

29
tree.py Normal file
View File

@ -0,0 +1,29 @@
import matplotlib.image as pltimg
import matplotlib.pyplot as plt
import os
import pandas
import pickle
import pydotplus
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
def treelearn():
if os.path.exists("assets/tree.pkl"):
dtree = pickle.load(open(os.path.join('assets', "tree.pkl"), "rb"))
else:
df = pandas.read_csv(os.path.join('assets/data', 'data.csv'))
columns = ['Fuel','Water','Fertalizer','Carrots','Potatoes','Wheat','X','Y','seeds']
x = df[columns]
y = df['back to station']
dtree = DecisionTreeClassifier()
dtree = dtree.fit(x, y)
pickle.dump(dtree, open(os.path.join('assets', "tree.pkl"), "wb"))
data = tree.export_graphviz(dtree, out_file=None, feature_names=columns)
graph = pydotplus.graph_from_dot_data(data)
graph.write_png(os.path.join('assets', 'mytree.png'))
img = pltimg.imread(os.path.join('assets', 'mytree.png'))
imgplot = plt.imshow(img)
plt.show()
return dtree
def make_decision(tree, Fuel,Water,Fertalizer,Carrots,Potatoes,Wheat,X,Y,seeds):
decision = tree.predict([[Fuel, Water, Fertalizer, Carrots, Potatoes, Wheat, X, Y, seeds]])
return decision