added Aufgabe5

master
Thomas Martin 2024-11-16 15:35:33 +01:00
parent e00f915c73
commit 609b681a44
6 changed files with 125 additions and 16 deletions

View File

@ -1,12 +1,11 @@
import sys
from copy import deepcopy
from random import random
import numpy as np
import pygame
import random
import math
from pygame import QUIT, KEYDOWN
# Initialize pygame
pygame.init()
@ -26,7 +25,7 @@ BLACK = (0, 0, 0)
# Labyrinth as a string
labyrinth_origin = [
"##########",
"#... ....#",
"#........#",
"#.##..##.#",
"#........#",
"##########"
@ -124,21 +123,21 @@ def calcState(pacman, ghost, labyrinth):
p_x, p_y = pacman.x, pacman.y
g_x, g_y = ghost.x, ghost.y
# Step 1: Calculate the Pacman and ghost indices
# Calculate the Pacman and ghost indices
pacman_index = p_y * COL + p_x
ghost_index = g_y * COL + g_x
position_state = pacman_index * (ROW * COL) + ghost_index
# Step 2: Check for cookies in the four directions relative to Pacman
cookie_left = 1 if p_y > 0 and labyrinth[p_y - 1][p_x] == 'C' else 0
cookie_right = 1 if p_y < COL - 1 and labyrinth[p_y + 1][p_x] == 'C' else 0
cookie_up = 1 if p_x > 0 and labyrinth[p_y][p_x - 1] == 'C' else 0
cookie_down = 1 if p_x < ROW - 1 and labyrinth[p_y][p_x + 1] == 'C' else 0
# Check for cookies in the four directions relative to Pacman
cookie_left = 1 if p_y > 0 and labyrinth[p_y - 1][p_x] == '.' else 0
cookie_right = 1 if p_y < COL - 1 and labyrinth[p_y + 1][p_x] == '.' else 0
cookie_up = 1 if p_x > 0 and labyrinth[p_y][p_x - 1] == '.' else 0
cookie_down = 1 if p_x < ROW - 1 and labyrinth[p_y][p_x + 1] == '.' else 0
# Step 3: Encode the cookie presence into a 4-bit binary number
# Encode the cookie presence into a 4-bit binary number
cookie_state = (cookie_left << 3) + (cookie_right << 2) + (cookie_up << 1) + cookie_down
# Step 4: Combine position_state and cookie_state into a single state number
# Combine position_state and cookie_state into a single state number
state = position_state * 16 + cookie_state
return state
@ -166,17 +165,32 @@ while True:
print("Round: ", round)
print("Won: ", win, " Lose: ", lose)
# Initialize Pacman and Ghost positions
pacman = Pacman(1, 1)
ghost = Ghost(COLS - 2, ROWS - 2)
labyrinth = deepcopy(labyrinth_origin)
# Game loop # reward = 1
newPacPos = False
x = 0
y = 0
while not newPacPos:
x = np.random.randint(COLS)
y = np.random.randint(ROWS)
if labyrinth[y][x] != "#":
newPacPos = True
pacman = Pacman(x, y)
newGhostPos = False
while not newGhostPos:
x = np.random.randint(COLS)
y = np.random.randint(ROWS)
if labyrinth[y][x] != "#" and not (pacman.x == x and pacman.y == y):
newGhostPos = True
ghost = Ghost(x,y)
done = False
if iter > max_iter:
max_iter = iter
print(max_iter)
iter = 0
while not done:
epsion_happned = False
# eindimensionaler state
@ -232,7 +246,7 @@ while True:
q[s][a] += alpha * (reward + gamma * np.max(q[new_s]) - q[s][a])
if(round > 100000):
# epsilon = 0
epsilon = 0
draw_labyrinth()
pacman.draw()
ghost.draw()

View File

@ -0,0 +1,95 @@
import numpy as np
from collections import Counter
trainingDataFile = 'data/t10k-images.idx3-ubyte'
trainingLabelFile = 'data/t10k-labels.idx1-ubyte'
def euclidean_distance(img1, img2):
return np.sqrt(np.sum((img1 - img2) ** 2))
def getData():
with open(trainingDataFile, mode='rb') as file:
magicNumber = file.read(4)
numOfImages = int.from_bytes(file.read(4), 'big')
height, width = int.from_bytes(file.read(4), 'big'), int.from_bytes(file.read(4), 'big')
print(height, width)
data = []
for i in range(numOfImages):
image = []
for j in range(width):
for k in range(height):
pixel = int.from_bytes(file.read(1), 'big')
image.append(pixel)
data.append(np.array(image))
return data
def getLabels():
with open(trainingLabelFile, mode='rb') as file:
magicNumber = file.read(4)
numOfImages = int.from_bytes(file.read(4), 'big')
data = []
for i in range(numOfImages):
data.append(int.from_bytes(file.read(1), 'big'))
return data
def euclidean_distance2(img1, img2):
distance = 0
for i in range(img1.__len__()):
distance += (img1[i] - img2[i])**2
return distance
def nearestNeighbor(image, reference, referenceTags, k=11):
nearestDistance = []
for i in range(reference.__len__()):
distance = euclidean_distance2(image, reference[i])
nearestDistance.append((distance, referenceTags[i]))
nearestDistance.sort(key=lambda x: x[0])
if nearestDistance.__len__() > k:
nearestDistance.pop()
labels = {}
for i in nearestDistance:
if labels.get(i[1]) is None:
labels[i[1]] = 1
else:
labels[i[1]] += 1
sorted(labels.items(), key=lambda item: item[1])
return list(labels.keys())[0]
labels = getLabels()
data = getData()
baseVectorsCount = 1000
realLabels = labels[:baseVectorsCount]
realData = data[:baseVectorsCount]
trainingLabels = labels[7000:]
trainingData = data[7000:]
wrong = 0
for i in range(baseVectorsCount):
nearestDistance = nearestNeighbor(realData[i], trainingData, trainingLabels)
if nearestDistance != realLabels[i]:
print("Identified as " + str(nearestDistance) + ", in reality: " + str(realLabels[i]))
wrong += 1
if i % 100 == 0:
if i == 0:
continue
print("Step: " + str(i) + " Error rate: " + str((wrong/i)*100) + "%")
print("Final error rate: " + str((wrong/baseVectorsCount)*100) + "%")

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.