355 lines
9.6 KiB
Python
355 lines
9.6 KiB
Python
![]() |
import pandas as pd
|
||
|
import pygame
|
||
|
from sklearn.model_selection import train_test_split
|
||
|
from sklearn.preprocessing import LabelEncoder
|
||
|
from sklearn.tree import DecisionTreeClassifier
|
||
|
from sklearn.metrics import accuracy_score
|
||
|
from sklearn.metrics import confusion_matrix
|
||
|
from pygame.locals import *
|
||
|
import numpy as np
|
||
|
import matplotlib.pyplot as plt
|
||
|
from sklearn import tree
|
||
|
import random
|
||
|
from textpygame import get_key, ask, display_box
|
||
|
|
||
|
|
||
|
|
||
|
#read csv file
|
||
|
dataset= pd.read_csv("veganism.csv")
|
||
|
|
||
|
#show shape of dataset
|
||
|
print(dataset.shape)
|
||
|
|
||
|
#create a new dataset
|
||
|
newdataset = pd.DataFrame(dataset, columns=['ethnicity', 'gender', 'appearence', 'vegan'])
|
||
|
|
||
|
# creating instance of labelencoder
|
||
|
labelencoder = LabelEncoder()
|
||
|
# Assigning numerical values and storing in another column
|
||
|
newdataset['ethnicity_no'] = labelencoder.fit_transform(newdataset['ethnicity'])
|
||
|
newdataset['gender_no']= labelencoder.fit_transform(newdataset['gender'])
|
||
|
newdataset['appearence_no']= labelencoder.fit_transform(newdataset['appearence'])
|
||
|
print(newdataset)
|
||
|
|
||
|
# for x values drop unimportant columns, axis=1 specifies that we want the columns not rows
|
||
|
Y=newdataset['vegan']
|
||
|
X=newdataset.drop(newdataset.columns[0:4], axis=1)
|
||
|
print(X,Y)
|
||
|
|
||
|
#test 14% of the data
|
||
|
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.15)
|
||
|
classifier = DecisionTreeClassifier()
|
||
|
classifier.fit(X_train, Y_train)
|
||
|
y_pred = classifier.predict(X_test)
|
||
|
|
||
|
#ignores minority groups which results in 0 for prediction. to solve this i have to use smthing else
|
||
|
print(pd.DataFrame(confusion_matrix(Y_test, y_pred),columns=['Predicted not vegan', 'Predicted vegan'], index=['Actually not vegan', 'Actually vegan']))
|
||
|
|
||
|
#accuracy score is high as expected
|
||
|
print(accuracy_score(Y_test, y_pred))
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
fn=['ethnicity','gender','appearence']
|
||
|
cn=['yes', 'no']
|
||
|
|
||
|
# Setting dpi = 300 to make image clearer than default
|
||
|
fig, axes = plt.subplots(nrows = 1,ncols = 1,figsize = (4,4), dpi=300)
|
||
|
|
||
|
tree.plot_tree(classifier,
|
||
|
feature_names = fn,
|
||
|
class_names=cn,
|
||
|
filled = True);
|
||
|
|
||
|
fig.savefig('imagenamenew.png')
|
||
|
|
||
|
|
||
|
|
||
|
#using the my_grid3 program
|
||
|
|
||
|
# Colors:
|
||
|
# Define some colors
|
||
|
BLACK = (0, 0, 0)
|
||
|
WHITE = (255, 255, 255)
|
||
|
GREEN = (0, 255, 0)
|
||
|
RED = (255, 0, 0)
|
||
|
BLUE = (0, 0, 240)
|
||
|
MAGENTA=(255, 0, 255)
|
||
|
# Width and Height of each square:
|
||
|
WIDTH = 20
|
||
|
HEIGHT = 20
|
||
|
|
||
|
# Margin:
|
||
|
MARGIN = 5
|
||
|
grid = [[0 for x in range(16)] for y in range(16)]
|
||
|
|
||
|
|
||
|
def change_value(i, j, width, n):
|
||
|
for r in range(i, i + width):
|
||
|
for c in range(j, j + width):
|
||
|
grid[r][c] = n
|
||
|
if grid[r][c]==5:
|
||
|
break
|
||
|
|
||
|
|
||
|
class Table:
|
||
|
def __init__(self, coordinate_i, coordinate_j):
|
||
|
self.coordinate_i = coordinate_i
|
||
|
self.coordinate_j = coordinate_j
|
||
|
change_value(coordinate_i, coordinate_j, 2, 1)
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
class Kitchen:
|
||
|
def __init__(self, coordinate_i, coordinate_j):
|
||
|
self.coordinate_i = coordinate_i
|
||
|
self.coordinate_j = coordinate_j
|
||
|
change_value(coordinate_i, coordinate_j, 3, 2)
|
||
|
|
||
|
|
||
|
class Agent:
|
||
|
def __init__(self, orig_coordinate_i, orig_coordinate_j):
|
||
|
self.orig_coordinate_i = orig_coordinate_i
|
||
|
self.orig_coordinate_j = orig_coordinate_j
|
||
|
self.state = np.array([1, 2])
|
||
|
change_value(orig_coordinate_j, orig_coordinate_j, 1, 3)
|
||
|
self.state_update(orig_coordinate_i, orig_coordinate_j)
|
||
|
|
||
|
def state_update(self, c1, c2):
|
||
|
self.state[0] = c1
|
||
|
self.state[1] = c2
|
||
|
print(self.state)
|
||
|
|
||
|
def leave(self):
|
||
|
change_value(self.state[0], self.state[1], 1, 0)
|
||
|
|
||
|
def move_up(self):
|
||
|
self.leave()
|
||
|
self.state_update(x - 1, y)
|
||
|
change_value(self.state[0], self.state[1], 1, 3)
|
||
|
|
||
|
def move_down(self):
|
||
|
self.leave()
|
||
|
change_value(self.state[0], self.state[1], 1, 0)
|
||
|
self.state_update(x + 1, y)
|
||
|
change_value(self.state[0], self.state[1], 1, 3)
|
||
|
|
||
|
def move_right(self):
|
||
|
self.leave()
|
||
|
self.state_update(x, y + 1)
|
||
|
change_value(self.state[0], self.state[1], 1, 3)
|
||
|
|
||
|
def move_left(self):
|
||
|
self.leave()
|
||
|
self.state_update(x, y - 1)
|
||
|
change_value(self.state[0], self.state[1], 1, 3)
|
||
|
class Customer:
|
||
|
def __init__(self, coordinate_i, coordinate_j):
|
||
|
self.coordinate_i = coordinate_i
|
||
|
self.coordinate_j = coordinate_j
|
||
|
change_value(coordinate_i, coordinate_j,1 , 4)
|
||
|
class CustomerPlace:
|
||
|
def __init__(self, coordinate_i, coordinate_j):
|
||
|
self.coordinate_i = coordinate_i
|
||
|
self.coordinate_j = coordinate_j
|
||
|
change_value(coordinate_i, coordinate_j,1 , 5)
|
||
|
|
||
|
|
||
|
## default positions of the agent:
|
||
|
x = 11
|
||
|
y = 11
|
||
|
agent = Agent(x, y)
|
||
|
|
||
|
table1 = Table(2, 2)
|
||
|
table2 = Table(2, 7)
|
||
|
table3 = Table(2, 12)
|
||
|
table4 = Table(7, 2)
|
||
|
table5 = Table(7, 7)
|
||
|
table6 = Table(7, 12)
|
||
|
table7 = Table(12, 2)
|
||
|
table8 = Table(12, 7)
|
||
|
|
||
|
|
||
|
pygame.init()
|
||
|
# create a font object.
|
||
|
# 1st parameter is the font file
|
||
|
# which is present in pygame.
|
||
|
# 2nd parameter is size of the font
|
||
|
font = pygame.font.Font('freesansbold.ttf', 14)
|
||
|
X = 400
|
||
|
Y = 400
|
||
|
# create a text suface object,
|
||
|
# on which text is drawn on it.
|
||
|
text = font.render('waiter: hello, let me help you with your order.', True, WHITE, BLACK)
|
||
|
userText=font.render('user: ', True, BLUE, BLACK)
|
||
|
# create a rectangular object for the
|
||
|
# text surface object
|
||
|
textRect = text.get_rect()
|
||
|
inputRect = userText.get_rect()
|
||
|
# set the center of the rectangular object.
|
||
|
textRect.center= (200, 340)
|
||
|
inputRect.center=(200,370)
|
||
|
# class Kitchen:
|
||
|
kitchen = Kitchen(13, 13)
|
||
|
|
||
|
x=[2,7,12]
|
||
|
y=[2,7]
|
||
|
|
||
|
random_customer_seat_x=random.choice(x)
|
||
|
random_customer_seat_y=random.choice(y)
|
||
|
print(random_customer_seat_x,random_customer_seat_y)
|
||
|
seat=Customer(random_customer_seat_x,random_customer_seat_y)
|
||
|
|
||
|
next_to=CustomerPlace(random_customer_seat_x,random_customer_seat_y-1)
|
||
|
|
||
|
WINDOW_SIZE = [405, 405]
|
||
|
screen = pygame.display.set_mode(WINDOW_SIZE)
|
||
|
|
||
|
pygame.display.set_caption("Waiter_Grid3")
|
||
|
|
||
|
done = False
|
||
|
print(random_customer_seat_x,random_customer_seat_y-1)
|
||
|
clock = pygame.time.Clock()
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
#updating the drawing
|
||
|
def updateDraw():
|
||
|
x = agent.state[0]
|
||
|
y = agent.state[1]
|
||
|
screen.fill(BLACK) # Background color
|
||
|
for row in range(16): # Drawing the grid
|
||
|
for column in range(16):
|
||
|
color = WHITE
|
||
|
if grid[row][column] == 1:
|
||
|
color = GREEN
|
||
|
if grid[row][column] == 2:
|
||
|
color = RED
|
||
|
if grid[row][column] == 3:
|
||
|
color = BLUE
|
||
|
if grid[row][column] == 4:
|
||
|
color = MAGENTA
|
||
|
surface = pygame.draw.rect(screen,
|
||
|
color,
|
||
|
|
||
|
[(MARGIN + WIDTH) * column + MARGIN,
|
||
|
(MARGIN + HEIGHT) * row + MARGIN,
|
||
|
WIDTH,
|
||
|
HEIGHT])
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
def customer():
|
||
|
if x == random_customer_seat_x and y == random_customer_seat_y - 1:
|
||
|
screen.blit(text, textRect)
|
||
|
ethnicity3= ask(screen, question="ethnicity ")
|
||
|
gender3= ask(screen,question="gender ")
|
||
|
appearence3=ask(screen, question="appearence ")
|
||
|
if ethnicity3 == "black":
|
||
|
ethnicity3 = 1
|
||
|
elif ethnicity3 == "asian":
|
||
|
ethnicity3 = 0
|
||
|
else:
|
||
|
ethnicity3 = 2
|
||
|
|
||
|
if gender3 == "male":
|
||
|
gender3 = 0
|
||
|
else:
|
||
|
gender3 = 1
|
||
|
|
||
|
if appearence3 == "hippie":
|
||
|
appearence3= 0
|
||
|
else:
|
||
|
appearence3 = 1
|
||
|
prediction = classifier.predict([[ethnicity3, gender3, appearence3]])
|
||
|
pygame.quit()
|
||
|
|
||
|
if prediction == [0]:
|
||
|
print("You're probably not vegan. Would you like a regular menu?")
|
||
|
else:
|
||
|
print("It seems like youre vegan. Would you like a vegan menu?")
|
||
|
exit()
|
||
|
|
||
|
|
||
|
|
||
|
running=True
|
||
|
|
||
|
# pick a font you have and set its size
|
||
|
text2 = 'this text is editable'
|
||
|
font2 = pygame.font.SysFont(None, 48)
|
||
|
img2 = font.render(text2, True, RED)
|
||
|
|
||
|
rect2 = img2.get_rect()
|
||
|
rect2.topleft = (20, 20)
|
||
|
|
||
|
|
||
|
background = BLACK
|
||
|
# put the label object on the screen at point x=100, y=100
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
cursor = Rect(rect2.topright, (3, rect2.height))
|
||
|
while running:
|
||
|
|
||
|
x = agent.state[0]
|
||
|
y = agent.state[1]
|
||
|
pygame.time.delay(100)
|
||
|
for event in pygame.event.get(): # Checking for the event
|
||
|
if event.type == pygame.QUIT: # If the program is closed:
|
||
|
done = True # To exit the loop
|
||
|
|
||
|
keys = pygame.key.get_pressed() # Moving the agent:
|
||
|
|
||
|
if keys[pygame.K_LEFT]: # Left:
|
||
|
# Checking if not a table, a kitchen, or a wall:
|
||
|
if y - 1 < 0 or grid[x][y - 1] == 1:
|
||
|
continue
|
||
|
else:
|
||
|
agent.move_left() # If okay, the move
|
||
|
if keys[pygame.K_RIGHT]: # The same procedure with right:
|
||
|
if y + 1 > 15 or grid[x][y + 1] == 1 or grid[x][y + 1] == 2:
|
||
|
continue
|
||
|
else:
|
||
|
agent.move_right()
|
||
|
if keys[pygame.K_UP]: # The same procedure with up:
|
||
|
if x - 1 < 0 or grid[x - 1][y] == 1 or grid[x - 1][y] == 2:
|
||
|
continue
|
||
|
|
||
|
else:
|
||
|
agent.move_up()
|
||
|
if keys[pygame.K_DOWN]: # The same procedure with down
|
||
|
if x + 1 > 15 or grid[x + 1][y] == 1 or grid[x + 1][y] == 2:
|
||
|
continue
|
||
|
else:
|
||
|
agent.move_down()
|
||
|
|
||
|
updateDraw()
|
||
|
customer()
|
||
|
clock.tick(60) # Limit to 60 frames per second
|
||
|
pygame.display.flip() # Updating the screen
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|