From 990216c536101abf8061001fc100c8518a685114 Mon Sep 17 00:00:00 2001 From: Thomas Martin <2121321@stud.hs-mannheim.de> Date: Tue, 28 Jan 2025 15:06:20 +0100 Subject: [PATCH] test commit --- Aufgabe_2.py | 4 +- Aufgabe_3.py | 9 +--- Aufgabe_4.py | 4 -- Aufgabe_5/Aufgabe_5.py | 6 +-- Aufgabe_6.py | 109 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 116 insertions(+), 16 deletions(-) create mode 100644 Aufgabe_6.py diff --git a/Aufgabe_2.py b/Aufgabe_2.py index 3020c42..01288d3 100644 --- a/Aufgabe_2.py +++ b/Aufgabe_2.py @@ -94,10 +94,10 @@ def __main__(): board = copy.deepcopy(newBoard) else: p = math.exp(-(abs(newFitness - lastFitness) / temp)) + print(f"Probability: {p*100}%") if(p > random.random()): - print(f"Returned to less fit state: {lastFitness}") - print(f"Probability: {p*100}%") + # print(f"Returned to less fit state: {lastFitness}") lastFitness = newFitness board = copy.deepcopy(newBoard) else: diff --git a/Aufgabe_3.py b/Aufgabe_3.py index 6b2e198..5e6ffb0 100644 --- a/Aufgabe_3.py +++ b/Aufgabe_3.py @@ -21,7 +21,6 @@ def gray_to_binary(gray_array): 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) @@ -49,10 +48,6 @@ def fitnessFunction(gen): 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) @@ -66,8 +61,8 @@ def crossover(gene1, gene2): def mutateL(gene): if np.random.rand() < mut_rate: - bit = np.random.randint(0, GENE_LENGTH) # irgendein - gene[bit] = 1 - gene[bit] # Bit kippen + bit = np.random.randint(0, GENE_LENGTH) + gene[bit] = 1 - gene[bit] return gene def mutate(gene): diff --git a/Aufgabe_4.py b/Aufgabe_4.py index 3f8c738..90c7189 100644 --- a/Aufgabe_4.py +++ b/Aufgabe_4.py @@ -119,11 +119,9 @@ def calcState(pacman, ghost, labyrinth): ROW = len(labyrinth) COL = len(labyrinth[0]) - # Unpack Pacman and ghost positions p_x, p_y = pacman.x, pacman.y g_x, g_y = ghost.x, ghost.y - # 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 @@ -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_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 - # Combine position_state and cookie_state into a single state number state = position_state * 16 + cookie_state return state diff --git a/Aufgabe_5/Aufgabe_5.py b/Aufgabe_5/Aufgabe_5.py index 12e87b8..921405d 100644 --- a/Aufgabe_5/Aufgabe_5.py +++ b/Aufgabe_5/Aufgabe_5.py @@ -1,12 +1,11 @@ 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 euclidean_distance(img1, img2): +# return np.sqrt(np.sum((img1 - img2) ** 2)) def getData(): with open(trainingDataFile, mode='rb') as file: @@ -41,6 +40,7 @@ def getLabels(): return data +#np.lin.alg def euclidean_distance2(img1, img2): distance = 0 for i in range(img1.__len__()): diff --git a/Aufgabe_6.py b/Aufgabe_6.py new file mode 100644 index 0000000..5fb8775 --- /dev/null +++ b/Aufgabe_6.py @@ -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() \ No newline at end of file