Skip to content

Commit

Permalink
Refactor code: Improve file handling and variable initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
AIIrondev committed Jul 27, 2024
1 parent 45a219a commit da8922e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
39 changes: 39 additions & 0 deletions Code/ai.fll
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Bsp: Datensatz (Bitte bearbeiten für die von Ihnen gemessene Werte)
# Für die kalibrierung bitte einfach die eingebaute Funktion calibrate aus und lassen sie sich das Ergebnis ausgeben.
# Die Reifennutzung verändert sich für jeden Run um -0.05
# Der Batterieladestand muss jedesmal gesondert abgelesen werden
# Wenn sie diese Schritte einhalten kann die KI / das KNN richtig funktionieren
data = [
{'Kalibrierung': 1.0, 'Batterieladestand': 100, 'Reifennutzung': 1.0, 'Multiplikation': 1.00},
{'Kalibrierung': 1.0, 'Batterieladestand': 90, 'Reifennutzung': 0.9, 'Multiplikation': 1.10},
{'Kalibrierung': 1.0, 'Batterieladestand': 80, 'Reifennutzung': 0.8, 'Multiplikation': 1.25},
{'Kalibrierung': 0.9, 'Batterieladestand': 100, 'Reifennutzung': 1.0, 'Multiplikation': 1.10},
{'Kalibrierung': 1.1, 'Batterieladestand': 100, 'Reifennutzung': 1.0, 'Multiplikation': 0.95}
]

def euclidean_distance(point1, point2):
distance = 0.0
for key in point1:
if key != 'Multiplikation':
distance += (point1[key] - point2[key]) ** 2
return math.sqrt(distance)

def knn_predict(data, new_data_point, k=3):
# Berechne die Distanz zwischen dem neuen Punkt und allen anderen Punkten
distances = []
for item in data:
dist = euclidean_distance(new_data_point, item)
distances.append((dist, item['Multiplikation']))

distances.sort(key=lambda x: x[0])
neighbors = distances[:k]

total_distance = sum(neighbor[1] for neighbor in neighbors)
predicted_distance = total_distance / k

return predicted_distance

# Beispielhafte Vorhersage
new_data_point = {'Kalibrierung': 0.84, 'Batterieladestand': 85, 'Reifennutzung': 0.95}
predicted_distance = knn_predict(data, new_data_point, k=3)
print(f'Vorhergesagte Multiplikation: {predicted_distance}')
6 changes: 3 additions & 3 deletions Code/compiler_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def write_function(function,file,value=False):
sleep_out = f"\ntime.sleep({str(value)})"
f.write(sleep_out)
case "init":
init_out = f"\nimport force_sensor, distance_sensor, motor, motor_pair\nfrom hub import port\nimport time\nfrom app import linegraph as ln\nimport runloop\nfrom math import *\nimport random\n"
init_out = f"\nimport force_sensor, distance_sensor, motor, motor_pair\nfrom hub import port\nimport time\nfrom app import linegraph as ln\nimport runloop\nfrom math import *\nimport random\nimport math\n"
f.write(init_out)
case "ai.chose":
ai_chose_out = f"ai_chose = '{str(value)}'\n"
Expand Down Expand Up @@ -285,8 +285,8 @@ def compile_llsp3(file, directory, project_name):
for filename in filenames:
file_path = os.path.join(foldername, filename)
arcname = os.path.relpath(file_path, directory)
zip_ref.write(file_path, arcname)
zip_ref.write(file_path, arcname)

if os.path.exists(llsp3_file_path):
os.remove(manifest_path)
os.remove(icon_svg_path)
Expand Down

0 comments on commit da8922e

Please sign in to comment.