-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.py
More file actions
601 lines (464 loc) · 17.7 KB
/
Copy pathsimulation.py
File metadata and controls
601 lines (464 loc) · 17.7 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
"""
LunarLander-v3 öğrenci görev ortamı.
Öğrenciler yalnızca STUDENT AREA bölümündeki choose_action(observation)
fonksiyonunu değiştirecektir. Ortam kontrolü, metrikler, iniş değerlendirmesi,
panel çizimi ve simülasyon akışı değiştirilmemelidir.
Kontroller:
- SPACE: Simülasyon çalışırken pause/resume yapar.
- SPACE: Episode bittiyse yeni simülasyon başlatır.
- Pencere kapatılırsa program kapanır.
"""
import random
import time
import importlib
import gymnasium as gym
import pygame
# ============================================================
# STUDENT AREA
# Öğrenciler sadece bu fonksiyonu değiştirecek.
# ============================================================
def choose_action(observation):
"""
Öğrencilerin görevi:
observation değerlerine bakarak 0, 1, 2 veya 3 aksiyonlarından
birini döndürmek.
Aksiyonlar:
0 = hiçbir şey yapma
1 = sol yön motoru
2 = ana motor
3 = sağ yön motoru
Observation:
observation[0] = x konumu
observation[1] = y konumu
observation[2] = x hızı
observation[3] = y hızı
observation[4] = açı
observation[5] = açısal hız
observation[6] = sol bacak temas durumu
observation[7] = sağ bacak temas durumu
"""
# Başlangıç kontrolcüsü: rastgele hareket eder.
# Öğrenciler bu satırı kendi karar algoritmalarıyla değiştirecek.
return random.randint(0, 3)
# ============================================================
# TEACHER / ENVIRONMENT AREA
# Öğrenciler bu bölümün altını değiştirmemeli.
# ============================================================
ACTION_MEANINGS = {
0: "No-op",
1: "Left engine",
2: "Main engine",
3: "Right engine",
}
# Göreve özel fiziksel iniş kalite eşikleri
LANDING_ZONE_LIMIT = 0.20
MAX_VERTICAL_SPEED = 0.35
MAX_HORIZONTAL_SPEED = 0.25
MAX_ANGLE = 0.20
MAX_ANGULAR_VELOCITY = 1.00
# Reward tabanlı başarı eşikleri
MIN_TOTAL_REWARD_FOR_CORRECT_LANDING = 0.0
MIN_TOTAL_REWARD_FOR_EXCELLENT_LANDING = 200.0
# Final görev skoru ayarları
# Fiziksel olarak düzgün inse bile total_reward negatifse öğrenciye 100/100 verilmez.
LOW_REWARD_SCORE_CAP = 70
# Simülasyon ayarları
FPS_DELAY = 0.02
PAUSED_DELAY = 0.05
# Takla + güvenli iniş görevi için ayarlanabilir ortam parametreleri.
# Bu değerler değiştirilerek görev zorluğu hızlıca ayarlanabilir.
CUSTOM_START_ENABLED = True
START_HEIGHT_OFFSET = 4.0
INITIAL_ANGLE = 0.0
INITIAL_ANGULAR_VELOCITY = 5.0
MAIN_ENGINE_POWER_SCALE = 1.15
SIDE_ENGINE_POWER_SCALE = 1.00
# Panel ayarları
PANEL_WIDTH = 285
PANEL_HEIGHT = 145
PANEL_MARGIN = 10
PANEL_ALPHA = 210
FONT_SIZE = 14
def sanitize_action(action):
"""
Öğrenci fonksiyonundan gelen aksiyonu güvenli hale getirir.
Geçersiz aksiyon verilirse 0 aksiyonuna çevrilir.
"""
if isinstance(action, bool):
return 0
try:
action = int(action)
except (TypeError, ValueError):
return 0
if action not in ACTION_MEANINGS:
return 0
return action
def extract_lander_state(observation):
"""
Observation dizisini isimlendirilmiş değerlere dönüştürür.
"""
return {
"x_pos": float(observation[0]),
"y_pos": float(observation[1]),
"x_vel": float(observation[2]),
"y_vel": float(observation[3]),
"angle": float(observation[4]),
"angular_vel": float(observation[5]),
"left_leg": float(observation[6]),
"right_leg": float(observation[7]),
}
def calculate_landing_score(state):
"""
İniş kalitesini 0-100 arasında puanlar.
Kritik kural:
İki bacak temas etmiyorsa skor doğrudan 0'dır.
Böylece çakılma veya tek bacaklı temas durumunda kısmi puan verilmez.
"""
both_legs_contact = int(state["left_leg"]) == 1 and int(state["right_leg"]) == 1
checks = {
"both_legs_contact": both_legs_contact,
"in_landing_zone": abs(state["x_pos"]) <= LANDING_ZONE_LIMIT,
"slow_vertical_landing": abs(state["y_vel"]) <= MAX_VERTICAL_SPEED,
"slow_horizontal_motion": abs(state["x_vel"]) <= MAX_HORIZONTAL_SPEED,
"stable_angle": abs(state["angle"]) <= MAX_ANGLE,
"stable_angular_velocity": abs(state["angular_vel"]) <= MAX_ANGULAR_VELOCITY,
}
if not checks["both_legs_contact"]:
return 0, checks
score = 0
if checks["in_landing_zone"]:
score += 30
if checks["slow_vertical_landing"]:
score += 25
if checks["slow_horizontal_motion"]:
score += 15
if checks["stable_angle"]:
score += 15
if checks["stable_angular_velocity"]:
score += 10
# İki bacak teması zorunlu ön koşuldur, küçük tamamlayıcı puan verilir.
score += 5
return score, checks
def calculate_task_score(physical_score, total_reward, physically_correct_landing):
"""
Öğrenci görev puanını hesaplar.
Physical Score sadece son iniş geometrisini ve hızını ölçer.
Task Score ise görevin nihai puanıdır ve Total Reward değerini de dikkate alır.
Mantık:
- Fiziksel iniş doğru değilse task score fiziksel puanı aşamaz.
- Fiziksel iniş doğru ama total_reward negatifse 100/100 verilmez.
- total_reward negatif olduğunda puan ciddi biçimde düşürülür.
- total_reward >= 0 ise fiziksel iniş puanı korunur.
"""
if not physically_correct_landing:
return physical_score
if total_reward < MIN_TOTAL_REWARD_FOR_CORRECT_LANDING:
penalized_score = physical_score + total_reward
penalized_score = max(0, int(round(penalized_score)))
return min(LOW_REWARD_SCORE_CAP, penalized_score)
return physical_score
def evaluate_landing(observation, total_reward, terminated, truncated):
"""
Episode sonucunu göreve göre değerlendirir.
Başarı mantığı:
- Çakılma / tek bacak / temassız bitiş: 0 puan ve başarısız.
- İki bayrak arası, yavaş, dengeli ve düzgün açılı iniş: fiziksel olarak doğru iniş.
- Correct Landing için fiziksel doğru iniş + total_reward >= 0 gerekir.
- Excellent Landing için fiziksel doğru iniş + total_reward >= 200 gerekir.
"""
if truncated:
return "TIME LIMIT", False, 0, 0
if not terminated:
return "RUNNING", False, 0, 0
state = extract_lander_state(observation)
physical_score, checks = calculate_landing_score(state)
if not checks["both_legs_contact"]:
return "FAILED / CRASHED", False, 0, 0
physically_correct_landing = (
checks["in_landing_zone"]
and checks["slow_vertical_landing"]
and checks["slow_horizontal_motion"]
and checks["stable_angle"]
and checks["stable_angular_velocity"]
)
task_score = calculate_task_score(
physical_score=physical_score,
total_reward=total_reward,
physically_correct_landing=physically_correct_landing,
)
reward_is_acceptable = total_reward >= MIN_TOTAL_REWARD_FOR_CORRECT_LANDING
correct_landing = physically_correct_landing and reward_is_acceptable
if physically_correct_landing and total_reward >= MIN_TOTAL_REWARD_FOR_EXCELLENT_LANDING:
status = "EXCELLENT LANDING"
elif correct_landing:
status = "CORRECT LANDING"
elif physically_correct_landing and not reward_is_acceptable:
status = "LANDING OK / LOW REWARD"
elif not checks["in_landing_zone"]:
status = "OUTSIDE LANDING ZONE"
elif not checks["slow_vertical_landing"]:
status = "TOO FAST LANDING"
elif not checks["slow_horizontal_motion"]:
status = "TOO MUCH HORIZONTAL SPEED"
elif not checks["stable_angle"]:
status = "BAD ANGLE"
elif not checks["stable_angular_velocity"]:
status = "UNSTABLE ROTATION"
elif physical_score >= 75:
status = "ROUGH BUT ACCEPTABLE"
else:
status = "FAILED / LOW SCORE"
return status, correct_landing, task_score, physical_score
def get_status_color(status, success):
"""
Duruma göre panel yazı rengini belirler.
"""
if success:
return 80, 255, 120
if status in {"FAILED / CRASHED", "TIME LIMIT", "FAILED / LOW SCORE"}:
return 255, 90, 90
if status in {
"OUTSIDE LANDING ZONE",
"TOO FAST LANDING",
"TOO MUCH HORIZONTAL SPEED",
"BAD ANGLE",
"UNSTABLE ROTATION",
"LANDING OK / LOW REWARD",
"ROUGH BUT ACCEPTABLE",
}:
return 255, 190, 80
return 255, 255, 255
def get_simulation_label(waiting_for_space, paused):
"""
Simülasyon durumunu panel etiketi olarak döndürür.
"""
if waiting_for_space:
return "FINISHED"
if paused:
return "PAUSED"
return "RUNNING"
def configure_lunar_lander_engine_power(env):
"""
LunarLander motor güçlerini ölçekler.
Gymnasium LunarLander, ana ve yan motor güçlerini ortam modülündeki
sabitlerden okur. Sabitler bulunamazsa simülasyon varsayılan güçlerle
çalışmaya devam eder.
"""
try:
env_module = importlib.import_module(env.unwrapped.__class__.__module__)
except (AttributeError, ImportError):
return
if hasattr(env_module, "MAIN_ENGINE_POWER"):
env_module.MAIN_ENGINE_POWER = 13.0 * MAIN_ENGINE_POWER_SCALE
if hasattr(env_module, "SIDE_ENGINE_POWER"):
env_module.SIDE_ENGINE_POWER = 0.6 * SIDE_ENGINE_POWER_SCALE
def configure_lander_start(env):
"""
Her reset sonrası lander başlangıcını takla görevi için ayarlar.
START_HEIGHT_OFFSET roketi daha yukarı taşır.
INITIAL_ANGULAR_VELOCITY pozitif/negatif verilerek takla yönü değiştirilebilir.
"""
if not CUSTOM_START_ENABLED:
return False
lander = getattr(env.unwrapped, "lander", None)
legs = getattr(env.unwrapped, "legs", [])
if lander is None:
return False
lander.position = (lander.position.x, lander.position.y + START_HEIGHT_OFFSET)
lander.angle = INITIAL_ANGLE
lander.angularVelocity = INITIAL_ANGULAR_VELOCITY
for leg in legs:
leg.position = (leg.position.x, leg.position.y + START_HEIGHT_OFFSET)
leg.angle = INITIAL_ANGLE
leg.angularVelocity = INITIAL_ANGULAR_VELOCITY
return True
def sync_observation_with_custom_start(observation, start_was_configured):
"""
İlk aksiyonun da güncel başlangıç ayarlarını görmesi için observation'ı günceller.
"""
if not start_was_configured:
return observation
observation = observation.copy()
observation[1] = float(observation[1]) + START_HEIGHT_OFFSET / (400 / 30 / 2)
observation[4] = INITIAL_ANGLE
observation[5] = INITIAL_ANGULAR_VELOCITY * 20 / 50
return observation
def print_environment_settings():
"""
Ayarlanabilir görev parametrelerini terminale basar.
"""
print("-" * 50)
print("Custom flip landing settings")
print(f"Custom Start Enabled: {CUSTOM_START_ENABLED}")
print(f"Start Height Offset: {START_HEIGHT_OFFSET}")
print(f"Initial Angle: {INITIAL_ANGLE}")
print(f"Initial Angular Velocity: {INITIAL_ANGULAR_VELOCITY}")
print(f"Main Engine Power Scale: {MAIN_ENGINE_POWER_SCALE}")
print(f"Side Engine Power Scale: {SIDE_ENGINE_POWER_SCALE}")
print("-" * 50)
def draw_info_panel(env, font, total_reward, task_score, physical_score, status,
success, waiting_for_space, paused):
"""
Sağ üst köşeye sade sonuç paneli çizer.
"""
screen = getattr(env.unwrapped, "screen", None)
if screen is None:
return
width, _ = screen.get_size()
panel_x = width - PANEL_WIDTH - PANEL_MARGIN
panel_y = PANEL_MARGIN
panel_surface = pygame.Surface((PANEL_WIDTH, PANEL_HEIGHT))
panel_surface.set_alpha(PANEL_ALPHA)
panel_surface.fill((20, 20, 20))
screen.blit(panel_surface, (panel_x, panel_y))
result_text = "YES" if success else "NO"
simulation_text = get_simulation_label(waiting_for_space, paused)
lines = [
f"Simulation: {simulation_text}",
f"Task Score: {task_score}/100",
f"Physical Score: {physical_score}/100",
f"Total Reward: {total_reward:.2f}",
f"Status: {status}",
f"Correct Landing: {result_text}",
]
y_offset = panel_y + 10
status_color = get_status_color(status, success)
for line in lines:
color = status_color if line.startswith("Status") else (255, 255, 255)
text_surface = font.render(line, True, color)
screen.blit(text_surface, (panel_x + 10, y_offset))
y_offset += 18
if waiting_for_space:
message = "SPACE: next simulation"
elif paused:
message = "SPACE: resume"
else:
message = "SPACE: pause"
message_surface = font.render(message, True, (255, 220, 80))
screen.blit(message_surface, (panel_x + 10, panel_y + PANEL_HEIGHT - 25))
pygame.display.flip()
def reset_episode(env, episode):
"""
Yeni episode başlatır ve sayaçları sıfırlar.
"""
observation, info = env.reset()
start_was_configured = configure_lander_start(env)
observation = sync_observation_with_custom_start(observation, start_was_configured)
episode_data = {
"episode": episode,
"step_count": 0,
"total_reward": 0.0,
"last_reward": 0.0,
"last_action": None,
"status": "RUNNING",
"success": False,
"task_score": 0,
"physical_score": 0,
"waiting_for_space": False,
"paused": False,
}
return observation, info, episode_data
def print_episode_result(episode_data):
"""
Episode sonucunu terminale düzenli biçimde yazdırır.
Panel sade tutulduğu için teknik sayaçlar terminalde bırakılır.
"""
print("-" * 50)
print(f"Episode: {episode_data['episode']}")
print(f"Step: {episode_data['step_count']}")
print(f"Total Reward: {episode_data['total_reward']:.2f}")
print(f"Task Score: {episode_data['task_score']}/100")
print(f"Physical Score: {episode_data['physical_score']}/100")
print(f"Status: {episode_data['status']}")
print(f"Correct Landing: {'YES' if episode_data['success'] else 'NO'}")
print(f"Min Reward Required: {MIN_TOTAL_REWARD_FOR_CORRECT_LANDING:.0f}")
print("Yeni simülasyon için SPACE tuşuna bas.")
print("-" * 50)
def draw_current_panel(env, font, episode_data):
"""
Aktif episode verilerinden paneli çizer.
"""
draw_info_panel(
env=env,
font=font,
total_reward=float(episode_data["total_reward"]),
task_score=int(episode_data["task_score"]),
physical_score=int(episode_data["physical_score"]),
status=str(episode_data["status"]),
success=bool(episode_data["success"]),
waiting_for_space=bool(episode_data["waiting_for_space"]),
paused=bool(episode_data["paused"]),
)
def handle_keydown(event, env, observation, info, episode_data):
"""
Klavye girdilerini yönetir.
SPACE:
- Episode bitmişse yeni episode başlatır.
- Episode devam ediyorsa pause/resume yapar.
"""
if event.key != pygame.K_SPACE:
return observation, info, episode_data
if episode_data["waiting_for_space"]:
next_episode = int(episode_data["episode"]) + 1
return reset_episode(env, next_episode)
episode_data["paused"] = not bool(episode_data["paused"])
return observation, info, episode_data
def main():
pygame.init()
env = gym.make("LunarLander-v3", render_mode="human")
configure_lunar_lander_engine_power(env)
print_environment_settings()
observation, info, episode_data = reset_episode(env, episode=1)
font = pygame.font.SysFont("Arial", FONT_SIZE)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
observation, info, episode_data = handle_keydown(
event=event,
env=env,
observation=observation,
info=info,
episode_data=episode_data,
)
if not running:
break
if episode_data["waiting_for_space"] or episode_data["paused"]:
draw_current_panel(env, font, episode_data)
time.sleep(PAUSED_DELAY)
continue
raw_action = choose_action(observation)
action = sanitize_action(raw_action)
observation, reward, terminated, truncated, info = env.step(action)
episode_data["last_action"] = action
episode_data["last_reward"] = float(reward)
episode_data["total_reward"] = float(episode_data["total_reward"]) + float(reward)
episode_data["step_count"] = int(episode_data["step_count"]) + 1
if terminated or truncated:
status, success, task_score, physical_score = evaluate_landing(
observation=observation,
total_reward=float(episode_data["total_reward"]),
terminated=bool(terminated),
truncated=bool(truncated),
)
episode_data["status"] = status
episode_data["success"] = success
episode_data["task_score"] = task_score
episode_data["physical_score"] = physical_score
episode_data["waiting_for_space"] = True
episode_data["paused"] = False
print_episode_result(episode_data)
else:
episode_data["status"] = "RUNNING"
episode_data["success"] = False
episode_data["task_score"] = 0
episode_data["physical_score"] = 0
draw_current_panel(env, font, episode_data)
time.sleep(FPS_DELAY)
env.close()
pygame.quit()
print("Program kapatıldı.")
if __name__ == "__main__":
main()