-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart_simulation.py
173 lines (128 loc) · 6.39 KB
/
start_simulation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import random
import time
from flyingObject import FlyingObject
from utils import SimulationParameters, determine_sector, calculate_distance
from datetime import timedelta
import threading
import tkinter as tk
import os
# List to store flying objects created during the simulation
simulation_objects = {}
# Function to get the current simulation time
def get_simulation_time(start_time, elapsed_time):
return start_time + timedelta(seconds=elapsed_time)
# Function to update the timer label
def update_timer(timer_label, start_time, elapsed_time, real_to_sim_time_ratio):
current_simulation_time = get_simulation_time(start_time, elapsed_time)
simulated_time_increment = timedelta(seconds=real_to_sim_time_ratio)
current_simulation_time += simulated_time_increment
formatted_time = current_simulation_time.strftime('%Y-%m-%d %H:%M:%S')
timer_label.config(text=f"Timer: {formatted_time}")
timer_label.after(1000, update_timer, timer_label, start_time, elapsed_time + real_to_sim_time_ratio,
real_to_sim_time_ratio)
def clear_json_file(file_path):
if os.path.exists(file_path):
with open(file_path, 'w') as file:
file.truncate()
# List to store thread instances
threads = []
# Function to run the simulation for a single FlyingObject
def run_flying_object(window, canvas, start_timeframe, screen_width, screen_height, start_threads):
# Generate random coordinates for the initial point
x, y = None, None
while x is None or y is None or not (0 <= x <= 1000) or not (
0 <= y <= 1000) or determine_sector(x, y) is None:
x = random.uniform(0, screen_width)
y = random.uniform(0, screen_height)
# Create a point on the canvas to represent the FlyingObject
point = canvas.create_oval(x - 5, y - 5, x + 5, y + 5, fill="red", outline="red")
flying_object = FlyingObject(
canvas, x, y, speed=random.uniform(300, 400),
time_frame=start_timeframe
)
# Store the FlyingObject's ID in the point object's tag
canvas.itemconfig(point, tags=(flying_object.object_id,))
# Generate random coordinates for the way point
way_x, way_y = None, None
while way_x is None or way_y is None or not (
100 <= calculate_distance(x, y, way_x, way_y) <= 150) or determine_sector(
way_x, way_y) is None:
way_x = random.uniform(0, screen_width)
way_y = random.uniform(0, screen_height)
# Create a way point on the canvas
way_point = canvas.create_oval(way_x - 5, way_y - 5, way_x + 5, way_y + 5,
fill="green", outline="green")
# Generate random coordinates for the destination point
dest_x, dest_y = None, None
while dest_x is None or dest_y is None or not (
150 <= calculate_distance(x, y, dest_x, dest_y) <= 400) or determine_sector(
dest_x, dest_y) is None:
dest_x = random.uniform(0, screen_width)
dest_y = random.uniform(0, screen_height)
# Create a destination point on the canvas
dest_point = canvas.create_oval(dest_x - 5, dest_y - 5, dest_x + 5, dest_y + 5,
fill="blue", outline="blue")
bezier_curve = (x, y, way_x, way_y, dest_x, dest_y)
canvas.create_line(bezier_curve, smooth="true", tags=flying_object.object_id)
flying_object.move(dest_x=dest_x, dest_y=dest_y, bezier_curve=bezier_curve, counter_json=start_threads)
# Object has reached the destination, delete it from the canvas
canvas.delete(flying_object.object_id)
canvas.delete(dest_point)
canvas.delete(way_point)
# Create a function to start the simulation
def startSimulation(canvas, window, screen_width, screen_height, start_button, setting_button, parameters):
start_button['state'] = 'disabled'
setting_button['state'] = 'disabled'
clear_json_file("data.json")
# Create a threading.Event to signal threads to stop when the time expires
stop_event = threading.Event()
# Create a threading.Lock to synchronize access to the stop event
stop_event_lock = threading.Lock()
# Set the start date and time
parameters.start_timeframe
# Generate flying objects randomly for 3 minutes
end_time = time.time() + 60 # 1 minute
# Set the ratio of real-time to simulated time (adjust as needed)
real_to_sim_time_ratio = 10 * 6 # 10 minutes of real-time corresponds to 10 hours of simulated time
# Create a label for the timer
timer_label = tk.Label(window, text="Current Time: 00:00:00", font=("Helvetica", 14))
timer_label.pack(side=tk.RIGHT, anchor=tk.SE, padx=10, pady=10)
timer_label.place(x=screen_width - 280, y=screen_height - 75)
elapsed_time = 0
end_time = time.time() + (parameters.simulation_duration / 60) # convert hours to minutes
# Set the number of threads to be created within a minute
threads_to_create = parameters.numbers_of_objects
threads_created = 0
print(threads_to_create)
print(parameters.simulation_duration)
# Start updating the timer label
update_timer(timer_label, parameters.simulation_start_time, 0, real_to_sim_time_ratio)
while time.time() < end_time:
# Calculate the current simulation time
current_simulation_time = get_simulation_time(parameters.simulation_start_time, elapsed_time)
# Randomly decide whether to create a thread or not
if threads_created < threads_to_create and random.choice([True, True, False, False, False, False]):
# Create a thread for each FlyingObject
thread = threading.Thread(target=run_flying_object, args=(window, canvas, parameters, screen_width, screen_height, threads_created))
threads.append(thread)
# Start the thread
thread.start()
# Increment the count of threads created
threads_created += 1
# Increase elapsed time
elapsed_time += 1
# Update the canvas
window.update_idletasks()
window.update()
# Sleep for a short duration to control the rate of object creation
time.sleep(1)
# Acquire the lock before setting the stop event
with stop_event_lock:
stop_event.set()
# Clear the list of threads
threads.clear()
# Destroy the timer label after simulation ends
timer_label.destroy()
# Enable the button after simulation ends
start_button['state'] = 'normal'
setting_button['state'] = 'normal'