import numpy as np
from itertools import product  # To generate all binary combinations and weight combinations

# Initialisierung der Schwellenwerte
lower_threshold = 0.8
upper_threshold = 1.2

# Trainingsdaten (Inputs für das XOR-Problem und andere)
inputs = [[0, 0], [0, 1], [1, 0], [1, 1]]

# Alle möglichen Zieltabellen (16 Kombinationen)
all_possible_targets = list(product([0, 1], repeat=4))

# Mögliche Werte für die Gewichte (in Schritten von 0.1)
weight_values = np.arange(-1.0, 1.1, 0.1)

# Trainingsloop für jede mögliche Zieltabelle
for table_index, targets in enumerate(all_possible_targets, start=1):
    print(f"\n=== Wahrheitstabelle {table_index}: Targets = {targets} ===")
    
    # Initialisieren der Startwerte
    bias_list = [0.0, 0.7, 0.9]  # Bias nur mit den Werten 0.0, 0.7, 0.9
    network_trained = False
    final_weights = None

    # Iterate over bias values
    for bias in bias_list:
        print(f"Versuch mit Bias {bias}:")
        
        # Teste alle Kombinationen der Gewichte
        for weight_combination in product(weight_values, repeat=2):
            current_weights = np.array(weight_combination)
            all_correct = True

            for input_vector, target in zip(inputs, targets):
                # Berechnung der gewichteten Summe inkl. Bias
                weighted_sum = np.dot(input_vector, current_weights) + bias

                # Aktivierungsfunktion (Schwellenwertfunktion mit zwei Schwellenwerten)
                output = 1 if lower_threshold < weighted_sum < upper_threshold else 0

                # Überprüfe, ob die Ausgabe korrekt ist
                if target != output:
                    all_correct = False
                    break  # Kein Erfolg mit diesen Gewichten; abbrechen

            # Wenn alle Ausgaben korrekt sind, speichere die Gewichte und Bias
            if all_correct:
                network_trained = True
                final_weights = current_weights
                break

        if network_trained:
            print(f"Das Netzwerk hat Wahrheitstabelle {table_index} erfolgreich gelernt.")
            print(f"Gewählte Gewichte: {final_weights}")
            print(f"Gewählter Bias: {bias}")
            break  # Weiter zur nächsten Wahrheitstabelle

    if not network_trained:
        print(f"Das Netzwerk hat Wahrheitstabelle {table_index} nicht gelernt.")
