test commit

master
Thomas Martin 2025-01-28 15:06:20 +01:00
parent 609b681a44
commit 990216c536
5 changed files with 116 additions and 16 deletions

View File

@ -94,10 +94,10 @@ def __main__():
board = copy.deepcopy(newBoard) board = copy.deepcopy(newBoard)
else: else:
p = math.exp(-(abs(newFitness - lastFitness) / temp)) p = math.exp(-(abs(newFitness - lastFitness) / temp))
print(f"Probability: {p*100}%")
if(p > random.random()): if(p > random.random()):
print(f"Returned to less fit state: {lastFitness}") # print(f"Returned to less fit state: {lastFitness}")
print(f"Probability: {p*100}%")
lastFitness = newFitness lastFitness = newFitness
board = copy.deepcopy(newBoard) board = copy.deepcopy(newBoard)
else: else:

View File

@ -21,7 +21,6 @@ def gray_to_binary(gray_array):
return binary_array return binary_array
def binary_array_to_int(binary_array): def binary_array_to_int(binary_array):
# Convert binary array to integer
binary_string = ''.join(map(str, binary_array)) binary_string = ''.join(map(str, binary_array))
return int(binary_string, 2) return int(binary_string, 2)
@ -49,10 +48,6 @@ def fitnessFunction(gen):
quadratic_error = np.mean(squared_differences) quadratic_error = np.mean(squared_differences)
return -quadratic_error 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): def tournament_selection(generation):
selected = [generation[np.random.randint(0, POPULATION_SIZE)] for _ in range(TOURNAMENT_SIZE)] selected = [generation[np.random.randint(0, POPULATION_SIZE)] for _ in range(TOURNAMENT_SIZE)]
selected = sorted(selected, key=fitnessFunction, reverse=True) selected = sorted(selected, key=fitnessFunction, reverse=True)
@ -66,8 +61,8 @@ def crossover(gene1, gene2):
def mutateL(gene): def mutateL(gene):
if np.random.rand() < mut_rate: if np.random.rand() < mut_rate:
bit = np.random.randint(0, GENE_LENGTH) # irgendein bit = np.random.randint(0, GENE_LENGTH)
gene[bit] = 1 - gene[bit] # Bit kippen gene[bit] = 1 - gene[bit]
return gene return gene
def mutate(gene): def mutate(gene):

View File

@ -119,11 +119,9 @@ def calcState(pacman, ghost, labyrinth):
ROW = len(labyrinth) ROW = len(labyrinth)
COL = len(labyrinth[0]) COL = len(labyrinth[0])
# Unpack Pacman and ghost positions
p_x, p_y = pacman.x, pacman.y p_x, p_y = pacman.x, pacman.y
g_x, g_y = ghost.x, ghost.y g_x, g_y = ghost.x, ghost.y
# Calculate the Pacman and ghost indices
pacman_index = p_y * COL + p_x pacman_index = p_y * COL + p_x
ghost_index = g_y * COL + g_x ghost_index = g_y * COL + g_x
position_state = pacman_index * (ROW * COL) + ghost_index position_state = pacman_index * (ROW * COL) + ghost_index
@ -134,10 +132,8 @@ def calcState(pacman, ghost, labyrinth):
cookie_up = 1 if p_x > 0 and labyrinth[p_y][p_x - 1] == '.' 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 cookie_down = 1 if p_x < ROW - 1 and labyrinth[p_y][p_x + 1] == '.' else 0
# Encode the cookie presence into a 4-bit binary number
cookie_state = (cookie_left << 3) + (cookie_right << 2) + (cookie_up << 1) + cookie_down cookie_state = (cookie_left << 3) + (cookie_right << 2) + (cookie_up << 1) + cookie_down
# Combine position_state and cookie_state into a single state number
state = position_state * 16 + cookie_state state = position_state * 16 + cookie_state
return state return state

View File

@ -1,12 +1,11 @@
import numpy as np import numpy as np
from collections import Counter
trainingDataFile = 'data/t10k-images.idx3-ubyte' trainingDataFile = 'data/t10k-images.idx3-ubyte'
trainingLabelFile = 'data/t10k-labels.idx1-ubyte' trainingLabelFile = 'data/t10k-labels.idx1-ubyte'
def euclidean_distance(img1, img2): # def euclidean_distance(img1, img2):
return np.sqrt(np.sum((img1 - img2) ** 2)) # return np.sqrt(np.sum((img1 - img2) ** 2))
def getData(): def getData():
with open(trainingDataFile, mode='rb') as file: with open(trainingDataFile, mode='rb') as file:
@ -41,6 +40,7 @@ def getLabels():
return data return data
#np.lin.alg
def euclidean_distance2(img1, img2): def euclidean_distance2(img1, img2):
distance = 0 distance = 0
for i in range(img1.__len__()): for i in range(img1.__len__()):

109
Aufgabe_6.py 100644
View File

@ -0,0 +1,109 @@
import numpy as np
import random
from matplotlib import pyplot as plt
# Sigmoid-Aktivierungsfunktion und deren Ableitung
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
return x * (1 - x)
# Trainingsdaten erzeugen und normalisieren
inputs = []
targets = []
for i in range(10000): # Mehr Trainingsdaten
x = round(random.uniform(0, 1), 3)
y = round(random.uniform(0, 1), 3)
xy = x * y
inputs.append([x, y])
targets.append([xy])
inputs = np.array(inputs)
targets = np.array(targets)
# Hyperparameter
learning_rate = 0.00001 # Reduzierte Lernrate
epochs = 1000
# Initialisierung der Gewichte und Biases
np.random.seed(42)
input_layer_neurons = 2
hidden_layer_neurons = 4
output_neurons = 1
weights_input_hidden = np.random.uniform(-0.5, 0.5, (input_layer_neurons, hidden_layer_neurons))
weights_hidden_output = np.random.uniform(-0.5, 0.5, (hidden_layer_neurons, output_neurons))
bias_hidden = np.random.uniform(-0.5, 0.5, (1, hidden_layer_neurons))
bias_output = np.random.uniform(-0.5, 0.5, (1, output_neurons))
# Training des Netzwerks
for epoch in range(epochs):
# Vorwärtspropagation
hidden_layer_input = np.dot(inputs, weights_input_hidden) + bias_hidden
hidden_layer_output = sigmoid(hidden_layer_input)
output_layer_input = np.dot(hidden_layer_output, weights_hidden_output) + bias_output
predicted_output = sigmoid(output_layer_input)
# Fehlerberechnung
error = targets - predicted_output
# Backpropagation
d_predicted_output = error * sigmoid_derivative(predicted_output)
error_hidden_layer = d_predicted_output.dot(weights_hidden_output.T)
d_hidden_layer = error_hidden_layer * sigmoid_derivative(hidden_layer_output)
# Gewichte und Biases aktualisieren
weights_hidden_output += hidden_layer_output.T.dot(d_predicted_output) * learning_rate
weights_input_hidden += inputs.T.dot(d_hidden_layer) * learning_rate
bias_output += np.sum(d_predicted_output, axis=0, keepdims=True) * learning_rate
bias_hidden += np.sum(d_hidden_layer, axis=0, keepdims=True) * learning_rate
# Optional: Fortschritt anzeigen
if epoch % 1000 == 0:
loss = np.mean(np.square(error))
print(f"Epoch {epoch}, Loss: {loss}")
test_inputs = []
for i in range(10): # Mehr Trainingsdaten
x = round(random.uniform(0, 1), 3)
y = round(random.uniform(0, 1), 3)
test_inputs.append([x, y])
hidden_layer_input = np.dot(test_inputs, weights_input_hidden) + bias_hidden
hidden_layer_output = sigmoid(hidden_layer_input)
output_layer_input = np.dot(hidden_layer_output, weights_hidden_output) + bias_output
predicted_output = sigmoid(output_layer_input)
print("\nTest Results:")
for i, test_input in enumerate(test_inputs):
print(f"Input: {test_input}, Predicted Output: {predicted_output[i][0]:.3f}, Actual Output: {test_input[0]*test_input[1]:.3f}")
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
X = []
Y = []
Z = []
# Grab some test data.
for test_input in test_inputs:
X.append(test_input[0])
Y.append(test_input[1])
Z.append(0)
Z = np.array(Z)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
plt.show()