master
Thomas Martin 2024-10-30 11:01:50 +01:00
commit ee34024fc8
4 changed files with 506 additions and 0 deletions

85
Aufgabe_1.py 100644
View File

@ -0,0 +1,85 @@
import sys
from copy import deepcopy
from random import randint, shuffle
import numpy as np
def calcFitness(board):
return -np.sum([len(set(board[:, i])) != 9 for i in range(9)])
def initSodoku():
row = np.arange(1,10)
board = np.tile(row, (9, 1))
printBoard(board)
return board
def shuffleBoard(board):
for i in range(9):
shuffle(board[i])
def step(board):
newBoard = deepcopy(board)
x = randint(0, 8)
y1 = randint(0,8)
y2 = randint(0,8)
while y2 == y1:
y2 = randint(0, 8)
newBoard[x][y1], newBoard[x][y2] = board[x][y2], board[x][y1]
return newBoard
def printRed(text):
return format(f"\033[1;31m{text}\033[0;0m")
def printBoard(board):
print("\n")
for i in range(len(board)):
if (i % 3 == 0 and i != 0):
print("------+------+------")
for j in range(len(board[i])):
if (j % 3 == 0 and j != 0):
sys.stdout.write("|")
if(isWrong(board,i,j)):
sys.stdout.write(printRed(board[i][j]) + " ")
else:
sys.stdout.write(str(board[i][j]) + " ")
print("")
def isWrong(board, i, j):
x = board[i][j]
for y in range(9):
if y == i:
continue
if x == board[y][j]:
return True
return False
def __main__():
iterations = 0
board = initSodoku()
fitness = calcFitness(board)
while fitness < 0:
newBoard = step(board)
newFitness = calcFitness(newBoard)
if(newFitness >= fitness):
board = deepcopy(newBoard)
if(newFitness > fitness):
printBoard(board)
fitness = newFitness
print(f"Fitness: {fitness}")
iterations += 1
print(f"Total iterations: {iterations}")
printBoard(board)
__main__()

114
Aufgabe_2.py 100644
View File

@ -0,0 +1,114 @@
import copy
import math
import random
import sys
from random import randint, shuffle
import numpy as np
def printBoard(board):
for i in range(len(board)):
if (i % 3 == 0 and i != 0):
print("------+------+------")
for j in range(len(board[i])):
if (j % 3 == 0 and j != 0):
sys.stdout.write("|")
if(isWrong(board,i,j)):
sys.stdout.write(printRed(board[i][j]) + " ")
else:
sys.stdout.write(str(board[i][j]) + " ")
print("")
print("\n")
def isWrong(board, i, j):
x = board[i][j]
for y in range(9):
if y == i:
continue
if x == board[y][j]:
return True
return False
def fitness(board):
fitness = 0
for i in range(0, 9, 3):
for j in range(0, 9, 3):
square = np.array([board[i][j], board[i+1][j], board[i+2][j], board[i][j+1], board[i][j+2], board[i+1][j+1], board[i+1][j+2], board[i+2][j+1], board[i+2][j+2]])
fitness -= np.sum([len(set(square)) != 9])
#for i in range(9):
# print(board[i, :])
return fitness + -np.sum([len(set(board[:, i])) != 9 for i in range(9)])
def initSodoku():
row = np.arange(1,10)
board = np.tile(row, (9, 1))
return board
def shuffleBoard(board):
for i in range(9):
shuffle(board[i])
def step(board):
x = randint(0, 8)
y1 = randint(0,8)
y2 = randint(0,8)
while y2 == y1:
y2 = randint(0, 8)
board[x][y1], board[x][y2] = board[x][y2], board[x][y1]
def printRed(text):
return format(f"\033[1;31m{text}\033[0;0m")
def __main__():
board = initSodoku()
iteration = 0
initialTemp = np.float64(100)
temp = initialTemp
# shuffleBoard(board)
newBoard = copy.deepcopy(board)
lastFitness = fitness(board)
print(f"Fitness: {lastFitness}")
printBoard(board)
while lastFitness < 0 and temp > 0:
step(newBoard)
newFitness = fitness(newBoard)
if(newFitness == 0):
break
if(newFitness >= lastFitness):
lastFitness = newFitness
board = copy.deepcopy(newBoard)
else:
p = math.exp(-(abs(newFitness - lastFitness) / temp))
if(p > random.random()):
print(f"Returned to less fit state: {lastFitness}")
print(f"Probability: {p*100}%")
lastFitness = newFitness
board = copy.deepcopy(newBoard)
else:
newBoard = copy.deepcopy(board)
iteration += 1
temp *= 0.9999
print(f"Iteration {iteration}")
printBoard(newBoard)
print(fitness(newBoard))
__main__()

113
Aufgabe_3.py 100644
View File

@ -0,0 +1,113 @@
import numpy as np
from matplotlib import pyplot as plt
qmin = -4
qmax = 4
GENE_LENGTH = 16
POPULATION_SIZE = 250
GENERATION_COUNT = 250
TOURNAMENT_SIZE = 5
x_values = np.linspace(-1, 1, 100)
mut_rate = 0.05 # Mutationsrate
def initGen():
return [np.random.randint(2, size=4 * GENE_LENGTH) for _ in range(POPULATION_SIZE)]
def gray_to_binary(gray_array):
binary_array = [gray_array[0]]
for i in range(1, len(gray_array)):
binary_array.append(binary_array[i - 1] ^ gray_array[i])
return binary_array
def binary_array_to_int(binary_array):
# Convert binary array to integer
binary_string = ''.join(map(str, binary_array))
return int(binary_string, 2)
def grayToInt(gray_array):
return binary_array_to_int(gray_to_binary(gray_array))
def getCoefficents(gene):
params = []
for i in range(4):
dezimahl = grayToInt(gene[i * GENE_LENGTH: (i+1) * GENE_LENGTH])
q = qmin + (qmax - qmin) / (2**GENE_LENGTH - 1) * dezimahl
params.append(q)
return params
def goalFunction(x):
return np.exp(x)
def approxFunction(x, gen):
a, b, c, d = getCoefficents(gen)
return a*x**3 + b*x**2 + c*x + d
def fitnessFunction(gen):
squared_differences = (approxFunction(x_values, gen) - goalFunction(x_values)) ** 2
quadratic_error = np.mean(squared_differences)
return -quadratic_error
def tournament_selection(generation):
gene1 = generation[np.random.randint(0, POPULATION_SIZE)]
gene2 = generation[np.random.randint(0, POPULATION_SIZE)]
def tournament_selection(generation):
selected = [generation[np.random.randint(0, POPULATION_SIZE)] for _ in range(TOURNAMENT_SIZE)]
selected = sorted(selected, key=fitnessFunction, reverse=True)
return selected[0]
def crossover(gene1, gene2):
split = np.random.randint(1, len(gene1) - 1)
newGene1 = np.append(gene1[:split], gene2[split:])
newGene2 = np.append(gene2[:split], gene1[split:])
return newGene1, newGene2
def mutateL(gene):
if np.random.rand() < mut_rate:
bit = np.random.randint(0, GENE_LENGTH) # irgendein
gene[bit] = 1 - gene[bit] # Bit kippen
return gene
def mutate(gene):
for bit in range(len(gene)):
if np.random.rand() < mut_rate:
gene[bit] = 1 - gene[bit]
return gene
def main():
oldGen = initGen()
allTimeGene = oldGen[0]
alltimeFitness = -1000
for i in range(GENERATION_COUNT):
newGen = []
while len(newGen) < POPULATION_SIZE:
gene1 = tournament_selection(oldGen)
gene2 = tournament_selection(oldGen)
newgene1, newgene2 = crossover(gene1, gene2)
newGen.append(mutate(newgene1))
newGen.append(mutate(newgene2))
oldGen = np.array(newGen)
oldGen = sorted(oldGen, key=fitnessFunction, reverse=True)
if fitnessFunction(oldGen[0]) > alltimeFitness:
alltimeFitness = fitnessFunction(oldGen[0])
allTimeGene = oldGen[0]
print("Generation", i, "Best fitness:", alltimeFitness)
print("Final fitness:", alltimeFitness)
plt.plot(x_values, approxFunction(x_values, allTimeGene), label='Approximated f(x)')
plt.plot(x_values, goalFunction(x_values), label='Target g(x)', linestyle='dashed')
plt.title('Genetic Algorithm')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
if __name__ == "__main__":
main()

194
Aufgabe_4.py 100644
View File

@ -0,0 +1,194 @@
import pygame
import random
import math
# Initialize pygame
pygame.init()
# Define constants
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 400
CELL_SIZE = 40
# Define colors
YELLOW = (255, 255, 0)
RED = (255, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
# Labyrinth as a string
labyrinth = [
"##########",
"#........#",
"#.##..##.#",
"#........#",
"##########"
]
# Get labyrinth dimensions
ROWS = len(labyrinth)
COLS = len(labyrinth[0])
# Initialize game screen
screen = pygame.display.set_mode((COLS * CELL_SIZE, ROWS * CELL_SIZE))
pygame.display.set_caption("Micro-Pacman")
# Pacman class
class Pacman:
def __init__(self, x, y):
self.x = x
self.y = y
self.count = 0
def move(self, dx, dy):
new_x, new_y = self.x + dx, self.y + dy
if labyrinth[new_y][new_x] != "#":
self.x = new_x
self.y = new_y
def draw(self):
radius = CELL_SIZE // 2 - 4
start_angle = math.pi / 6
end_angle = -math.pi / 6
pygame.draw.circle(screen, YELLOW, (self.x * CELL_SIZE + CELL_SIZE // 2, self.y * CELL_SIZE + CELL_SIZE // 2), CELL_SIZE // 2 - 4)
# Calculate the points for the mouth
start_pos = (self.x* CELL_SIZE + CELL_SIZE // 2 + int(radius*1.3 * math.cos(start_angle)),
self.y* CELL_SIZE + CELL_SIZE // 2 - int(radius*1.3 * math.sin(start_angle)))
end_pos = (self.x* CELL_SIZE + CELL_SIZE // 2 + int(radius*1.3 * math.cos(end_angle)),
self.y* CELL_SIZE + CELL_SIZE // 2 - int(radius*1.3 * math.sin(end_angle)))
self.count += 1
if self.count%2==0:
# Draw the mouth by filling a polygon
pygame.draw.polygon(screen, BLACK, [(self.x* CELL_SIZE + CELL_SIZE // 2, self.y* CELL_SIZE + CELL_SIZE // 2), start_pos, end_pos])
# Ghost class with pixel art
class Ghost:
# Define the pixel art for the ghost using strings
ghost_pixels = [
" #### ",
"######",
"## # #",
"######",
"######",
"# # # "
]
def __init__(self, x, y):
self.x = x
self.y = y
def move_towards_pacman(self, pacman):
if self.x < pacman.x and labyrinth[self.y][self.x + 1] != "#":
self.x += 1
elif self.x > pacman.x and labyrinth[self.y][self.x - 1] != "#":
self.x -= 1
elif self.y < pacman.y and labyrinth[self.y + 1][self.x] != "#":
self.y += 1
elif self.y > pacman.y and labyrinth[self.y - 1][self.x] != "#":
self.y -= 1
def draw(self):
pixel_size = CELL_SIZE // len(self.ghost_pixels) # Size of each pixel in the ghost art
for row_idx, row in enumerate(self.ghost_pixels):
for col_idx, pixel in enumerate(row):
if pixel == "#":
pixel_x = self.x * CELL_SIZE + col_idx * pixel_size
pixel_y = self.y * CELL_SIZE + row_idx * pixel_size
pygame.draw.rect(screen, RED, (pixel_x, pixel_y, pixel_size, pixel_size))
# Draw walls and cookies
def draw_labyrinth():
for y, row in enumerate(labyrinth):
for x, cell in enumerate(row):
if cell == "#":
pygame.draw.rect(screen, BLUE, (x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE))
elif cell == ".":
pygame.draw.circle(screen, WHITE, (x * CELL_SIZE + CELL_SIZE // 2, y * CELL_SIZE + CELL_SIZE // 2), 5)
# Main game function
def main():
clock = pygame.time.Clock()
# Initialize Pacman and Ghost positions
pacman = Pacman(1, 1)
ghost = Ghost(COLS - 2, ROWS - 2)
# Game loop
running = True
iter = 0
while running:
screen.fill(BLACK)
iter = iter + 1
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Handle Pacman movement
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
pacman.move(-1, 0)
if keys[pygame.K_RIGHT]:
pacman.move(1, 0)
if keys[pygame.K_UP]:
pacman.move(0, -1)
if keys[pygame.K_DOWN]:
pacman.move(0, 1)
if iter%3==0:
# Ghost moves towards Pacman
ghost.move_towards_pacman(pacman)
# Check for collisions (game over if ghost catches pacman)
if pacman.x == ghost.x and pacman.y == ghost.y:
print("Game Over! The ghost caught Pacman.")
running = False
# Eat cookies
if labyrinth[pacman.y][pacman.x] == ".":
labyrinth[pacman.y] = labyrinth[pacman.y][:pacman.x] + " " + labyrinth[pacman.y][pacman.x+1:]
# Check if all cookies are eaten (game over)
if all("." not in row for row in labyrinth):
print("You Win! Pacman ate all the cookies.")
running = False
# Draw the labyrinth, pacman, and ghost
draw_labyrinth()
pacman.draw()
ghost.draw()
# Update display
pygame.display.flip()
# Cap the frame rate
clock.tick(5)
pygame.quit()
if __name__ == "__main__":
main()
'''
Write a pacman game using pygame. The pacman should be a yellow circle, the ghost is a red square. The labyrinth is written as a string as follows:
"##########
#........#
#.##..##.#
#........#
##########"
The "." are cookies the pacman can eat (as Graphics small white circles). The "#" are walls (as graphics blue squares on a black background ). The ghost always tries to catch the pacman. Pacman as well as the ghost go one step in each game loop iteration. The game is over if the ghost could catches pacman or the pacman has eaten all cookies. Start your answer with "Shure, here is the full pacman code.
Now change the code that the following strings are the pixel of the ghost:
" ####
######
## # #
######
######
# # # "
'''