-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path02_Learning_Encounter_Models.jl
1709 lines (1353 loc) · 63 KB
/
02_Learning_Encounter_Models.jl
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
### A Pluto.jl notebook ###
# v0.20.4
using Markdown
using InteractiveUtils
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
macro bind(def, element)
#! format: off
quote
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
local el = $(esc(element))
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
el
end
#! format: on
end
# ╔═╡ f2d4b6c0-3f12-11eb-0b8f-abd68dc8ade7
begin
using Pkg
Pkg.activate("../.")
using PlutoUI
end
# ╔═╡ 42224774-2746-4138-ac7e-4e428bed14ea
begin
using DataFrames
using CSV
using Distributions
using Plots
using Discretizers
using Random
using BayesNets
plotlyjs()
if !@isdefined HW2_Support_Code
include("support_code/02_hw_support_code.jl")
end
end
# ╔═╡ 0db17351-97c4-4b2d-95f1-b57a77b45157
begin
using Base64
include_string(@__MODULE__, String(base64decode("IyAqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKgojIERFQ09ESU5HIFRISVMgSVMgQSBWSU9MQVRJT04gT0YgVEhFIEhPTk9SIENPREUKIyAqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKgoKZnVuY3Rpb24gX19leHRyYWN0X2luaXRpYWxfY29uZGl0aW9ucyhlbmNvdW50ZXI6OkVuY291bnRlclN0YXRlKQogICAgCiAgICB5MSA9IGVuY291bnRlci5wbGFuZTEueQogICAgdTEgPSBlbmNvdW50ZXIucGxhbmUxLnUKICAgIHYxID0gZW5jb3VudGVyLnBsYW5lMS52CiAgICAKICAgIM6UeCA9IGVuY291bnRlci5wbGFuZTIueCA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YyID0gRmxvYXQ2NFtdLAogICAgICAgIM6UeCA9IEZsb2F0NjRbXSwgCiAgICAgICAgzpR5ID0gRmxvYXQ2NFtdLCAKICAgICAgICDOlHUgPSBGbG9hdDY0W10sIAogICAgICAgIM6UdiA9IEZsb2F0NjRbXSwgCiAgICApCgoJZm9yIGVuYyBpbiBlbmNvdW50ZXJzCiAgICAgICAgZm9yIGkgaW4gMToobGVuZ3RoKGVuYyktMSkKICAgICAgICAgICAgeDFfdCA9IGVuY1tpXS5wbGFuZTEueAogICAgICAgICAgICB5MV90ID0gZW5jW2ldLnBsYW5lMS55CiAgICAgICAgICAgIHgyX3QgPSBlbmNbaV0ucGxhbmUyLngKICAgICAgICAgICAgeTJfdCA9IGVuY1tpXS5wbGFuZTIueQogICAgICAgICAgICB1MV90ID0gZW5jW2ldLnBsYW5lMS51CiAgICAgICAgICAgIHYxX3QgPSBlbmNbaV0ucGxhbmUxLnYKICAgICAgICAgICAgdTJfdCA9IGVuY1tpXS5wbGFuZTIudQogICAgICAgICAgICB2Ml90ID0gZW5jW2ldLnBsYW5lMi52CiAgICAgICAgICAgIAogICAgICAgICAgICB1MV9uZXh0ID0gZW5jW2krMV0ucGxhbmUxLnUKICAgICAgICAgICAgdjFfbmV4dCA9IGVuY1tpKzFdLnBsYW5lMS52CiAgICAgICAgICAgIHUyX25leHQgPSBlbmNbaSsxXS5wbGFuZTIudQogICAgICAgICAgICB2Ml9uZXh0ID0gZW5jW2krMV0ucGxhbmUyLnYKICAgICAgICAgICAgCiAgICAgICAgICAgIM6UeCA9IHgyX3QgLSB4MV90CiAgICAgICAgICAgIM6UeSA9IHkyX3QgLSB5MV90CiAgICAgICAgICAgIM6UdSA9IHUyX3QgLSB1MV90CiAgICAgICAgICAgIM6UdiA9IHYyX3QgLSB2MV90CiAgICAgICAgICAgIAogICAgICAgICAgICBhdTEgPSB1MV9uZXh0IC0gdTFfdAoJCQlhdjEgPSB2MV9uZXh0IC0gdjFfdAoJCQlhdTIgPSB1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Y29sbGVjdChyYW5nZSgtMzAuMCwgc3RvcCA9IDMwLjAsIGxlbmd0aCA9IDYpKSkKICAgIERbOs6Udl0gID0gTGluZWFyRGlzY3JldGl6ZXIoY29sbGVjdChyYW5nZSgtMTAwLjAsIHN0b3AgPSAxMDAuMCwgbGVuZ3RoID0gNikpKQoJCiAgICBmb3IgKHN5bSwgZGlzYykgaW4gRAogICAgICAgIGRmWyEsIHN5bV0gPSBlbmNvZGUoZGlzYywgZGZbISwgc3ltXSkKICAgIGVuZAoJCiAgICByZXR1cm4gZGYsIEQKZW5kCgpmdW5jdGlvbiBfX2xlYXJuX3RyYW5zaXRpb25fbmV0d29yayhkZjo6RGF0YUZyYW1lLCBEOjpEaWN0e1N5bWJvbCxMaW5lYXJEaXNjcmV0aXplcn0pCgoJYm5fZHluYW1pY3MgPSBEaXNjcmV0ZUJheWVzTmV0KCkKCglwYXJhbXMgPSBHcmVlZHlIaWxsQ2xpbWJpbmcoCiAgICAJU2NvcmVDb21wb25lbnRDYWNoZShkZiksCiAgICAJbWF4X25fcGFyZW50cz0zLAogICAgCXByaW9yPVVuaWZvcm1QcmlvcigpCgkpCgoJbl90cmFuc19jYXRlZ29yaWVzID0gW25sYWJlbHMoRFtzeW1dKSBmb3Igc3ltIGluIHByb3BlcnR5bmFtZXMoZGYpXQoJCglibl9keW5hbWljcyA9IGZpdChEaXNjcmV0ZUJheWVzTmV0LCBkZiwgcGFyYW1zOyBuY2F0ZWdvcmllcz1uX3RyYW5zX2NhdGVnb3JpZXMpCgogICAgcmV0dXJuIGJuX2R5bmFtaWNzCmVuZAo=")))
__extract_initial_conditions = __extract_initial_conditions
__create_discretized_dataset = __create_discretized_dataset
__create_discretized_transitions = __create_discretized_transitions
__extract_transitions = __extract_transitions
__learn_network = __learn_network
__learn_transition_network = __learn_transition_network
md""
end
# ╔═╡ d923dee0-3f12-11eb-0fb8-dffb2e9b3b2a
md"""
# Assignment 2: Learning Aircraft Encounter Models
v2025.0.2
"""
# ╔═╡ a3648e27-ede6-4941-82d4-3698d8be1407
md"## Notebook Setup"
# ╔═╡ 83374e4a-2662-47b0-ae6a-da33e12acccb
md"""
## Additional Readings (optional)
If you want to deepen your understanding of the concepts covered in this assignment, we recommend the following resources:
- Kochenderfer, M. J., Wheeler, T. A., & Wray, K. H. (2022). *Algorithms for Decision Making*
- Chapters 3-5 cover probabilistic modeling, inference, and structure learning
- Available open access at [algorithmsbook.com](https://algorithmsbook.com)
- Kochenderfer, M. J. (2015). *Decision Making Under Uncertainty: Theory and Application*
- Chapter 2: Probabilistic Models
- Available at [http://web.stanford.edu/group/sisl/public/dmu.pdf](http://web.stanford.edu/group/sisl/public/dmu.pdf)
- Koller, D., & Friedman, N. (2009). *Probabilistic Graphical Models: Principles and Techniques*
- Comprehensive coverage of Bayesian networks and parameter learning
- Recommended for deeper theoretical understanding
- [BayesNets.jl Documentation](https://sisl.github.io/BayesNets.jl/dev/)
- Julia package documentation for representation, inference, and learning in Bayesian networks
- Includes examples and API reference
"""
# ╔═╡ 081da510-4fb1-11eb-3334-63534dbae588
md"""
## Problem Description
In collision avoidance system development, we need models that can generate realistic aircraft encounters for testing and validation. Our task is to learn such a model from encounter data. Using provided aircraft trajectories, you will:
1. Learn an initial scene model -- capturing the distribution of how encounters typically begin
2. Learn a dynamics model -- describing how aircraft states evolve over time
Each encounter in our dataset contains 51 timesteps (50 seconds) of trajectory data for two aircraft. The initial scene model will capture the probability distribution over starting positions and velocities. The dynamics model will represent how aircraft states (position, velocity) change from one timestep to the next.
By learning these models, we can:
- Generate new encounter scenarios for testing collision avoidance systems
- Understand common patterns in how encounters develop
- Create more diverse test cases than available in recorded data
You will implement this using Discrete Bayesian Networks, learning both the structure and parameters from data. This assignment will not require any knowledge of the inner workings of Bayesian networks. However, you will be required to implement a function from the `BayesNets.jl` library. The hints should help guide you on how to implement this function.
"""
# ╔═╡ cfff9088-511c-11eb-36f4-936646282096
md"""
## ‼️ What is Turned In
1. Export this notebook as a PDF ([how-to in the documentation](https://plutojl.org/en/docs/export-pdf/))
2. Upload the PDF to [Gradescope](https://www.gradescope.com/)
3. Tag your pages correctly on Gradescope:
- Tag the page containing your checks (with the ✅ or ❌)
**Do not use any external code or Julia packages other than those used in the class materials.**
"""
# ╔═╡ 31d14960-3f13-11eb-3d8a-7531c02990cf
md"""
## Aircraft Encounter Data
The dataset contains records of aircraft encounters -- situations where two aircraft come into close proximity. Each encounter consists of 50 seconds of trajectory data sampled at 1 Hz (51 total timesteps including t=0). The encounters represent scenarios that collision avoidance systems need to handle.
## Data Structure
The data file, `flights.csv` (in the AA120Q/data directory), contains a table with the following columns:
| Variable | Type | Description |
| -------- |:----:| ----------- |
| **id** | Int | encounter id |
| **t** | Int | time in 1s intervals from 0 to 50 |
| **x1** | Float | aircraft 1 position along x-axis |
| **x2** | Float | aircraft 2 position along x-axis |
| **y1** | Float | aircraft 1 position along y-axis |
| **y2** | Float | aircraft 2 position along y-axis |
| **u1** | Float | aircraft 1 horizontal speed [m/s] |
| **u2** | Float | aircraft 2 horizontal speed [m/s] |
| **v1** | Float | aircraft 1 vertical speed [m/s] |
| **v2** | Float | aircraft 2 vertical speed [m/s] |
## Provided Code Structure
To help you work with this encounter data, we provide a few data structures and helper functions. We also load the data to a variable `flights::DataFrame`.
### Data Types
#### `AircraftState`
Represents the state of a single aircraft:
```julia
struct AircraftState
x::Float64 # horizontal position [m]
y::Float64 # vertical position [m]
u::Float64 # horizontal speed [m/s]
v::Float64 # vertical speed [m/s]
end
```
#### `EncounterState`
Contains the information of the state of an encounter:
```julia
mutable struct EncounterState
plane1::AircraftState
plane2::AircraftState
t::Float64
end
```
#### `Encounter`
```julia
const Encounter = Vector{EncounterState}
```
#### `DiscretizedBayesNet`
We will be discretizing our data and learning a Bayesian network. This struct will help us use our model by grouping the Bayesian network with the discretizers we used.
```julia
struct DiscretizedBayesNet
dbn::DiscreteBayesNet
discs::Dict{Symbol, LinearDiscretizer}
end
```
### Helper Function
#### `pull_encounter(flights::DataFrame, id::Int)`
Extracts data for a specific encounter ID from the flights DataFrame and converts it into an Encounter object. The `Encounter` returned is in order of the time progressing of the encounter. For example:
"""
# ╔═╡ eddfed22-c819-46c7-bc9b-f45d4871387f
encounter_1 = pull_encounter(flights, 1); # Get the encounter with ID of 1
# ╔═╡ 10cb1dd1-f60d-4701-8f88-a62e3eef5913
encounter_1[1] # First state of encounter_1 (at the first time step)
# ╔═╡ 746e2966-2e3f-4eb7-914e-91129f39a4a1
md"""
### Vector of all Encounters
Using `pull_encounter`, the provided code also creates a vector of all Encounters. This vector is `encounter_set` is a `Vector` of `Encounter` types.
We know that the ids in the datasets are all unique and range from 1 to 1000. Therefore, it was created from:
```julia
encounter_set = [pull_encounter(flights, id) for id = 1:1000]
```
"""
# ╔═╡ 9a5ee79b-a593-46b2-a9de-732f36069ded
md"""
# 1️⃣ Milestone One: Initial Conditions Model
Our first task is to build a model that captures how aircraft encounters typically begin.
"""
# ╔═╡ 256a1650-a85c-484a-a4d9-384fce5f719e
md"""
## Part 1: Extract Initial Features
First, you'll need to extract the relevant initial features from each encounter. To help us with our modeling, we will define our encounters relative to one aircraft.
For each of our encounters we want:
- Aircraft 1 starts at (0, y1, u1, v1)
- Aircraft 2's position is defined relative to Aircraft 1 (Δx, Δy, Δu, Δv)
- Δx ≥ 0
The following picture should help visualize this.
"""
# ╔═╡ ead1b755-3a62-4843-b327-b107223a1072
PlutoUI.LocalResource("./figures/encounter_def.png")
# ╔═╡ 3623eaa8-0b38-4787-9839-c9aa959d1d3d
md"""
### 💻 **Task**: Implement the function `extract_initial_conditions`
This function has an input of an `EncounterState` and returns a Tuple defining the relative position as defined above. I.e. the function should return `(y1, u1, v1, Δx, Δy, Δu, Δv)`.
"""
# ╔═╡ 4d0a3c12-3410-4b4f-941a-1dfe6c9870bc
function extract_initial_conditions(encounter::EncounterState)
# For our initial conditions model, we need:
y1 = 0.0 # Initial y position of aircraft 1
u1 = 0.0 # Initial horizontal velocity of aircraft 1
v1 = 0.0 # Initial vertical velocity of aircraft 1
Δx = 0.0 # Relative x position (x2 - x1)
Δy = 0.0 # Relative y position (y2 - y1)
Δu = 0.0 # Relative horizontal velocity (u2 - u1)
Δv = 0.0 # Relative vertical velocity (v2 - v1)
# STUDENT CODE START
# 1. Extract initial positions and velocities from encounter state
# 2. Calculate relative positions and velocities
# 3. Ensure Δx ≥ 0
# STUDENT CODE END
return (y1, u1, v1, Δx, Δy, Δu, Δv)
end
# ╔═╡ 151a5840-4f9b-415e-823f-322fd4ca194d
begin
global m1p1_icon = "❌"
global milestone_one_part_1_pass = false
sampled_ids = collect(1:1000)
test_encounters = [pull_encounter(flights, id_i) for id_i in sampled_ids]
try
user_defined_extractions = [extract_initial_conditions(enc_i[1]) for enc_i in test_encounters]
true_extractions = [__extract_initial_conditions(enc_i[1]) for enc_i in test_encounters]
individual_component_match = trues(length(true_extractions[1]))
for ii in 1:length(true_extractions[1])
for jj in 1:length(sampled_ids)
if true_extractions[jj][ii] != user_defined_extractions[jj][ii]
individual_component_match[ii] = false
break
end
end
end
all_match = all(individual_component_match)
if all_match
global m1p1_icon = "✅"
global milestone_one_part_1_pass = true
Markdown.MD(Markdown.Admonition("correct", "🥳", [md"""Your function is correct!"""]))
else
milestone_one_msg = md"""Your function doesn't appear to be correct.
Of $(length(test_encounters)) encounters tested, your function matched on $(sum(all_match)) and differed on the rest."""
delta_x_vals = [user_defined_i[4] for user_defined_i in user_defined_extractions]
if any(delta_x_vals .< 0)
milestone_one_msg = md"""$milestone_one_msg
Check that Δx ≥ 0"""
end
if all(individual_component_match[1:3])
milestone_one_msg = md"""$milestone_one_msg
Components y1, u1, and v1 match. Check the relative values."""
end
Markdown.MD(Markdown.Admonition("warning", "Keep working on it!", [milestone_one_msg]))
end
catch err
Markdown.MD(Markdown.Admonition("danger", "Error", [md"""There is an error with your code: $err
"""]))
end
end
# ╔═╡ 541d34f6-7648-4ab2-96d3-ba3c3e49ce98
md"""
### $(m1p1_icon) Milestone One - Part 1 Check
"""
# ╔═╡ e74cfb06-933d-43d9-8bb2-5f0c40775ce6
md"""
## Part 2: Prepare Data to Learn Initial Conditions Model with Bayesian Networks
Now that we can extract initial features from encounters, we'll learn a Bayesian network to capture relationships between these variables. We'll first convert our continuous data into discrete bins.
### 💻 **Task**: Implement `create_discretized_dataset`
"""
# ╔═╡ d0366eb2-6829-4cce-9e9a-0954c547cb7c
function create_discretized_dataset(encounters::Vector{Encounter})
# Create DataFrame of continuous values
df = DataFrame(
y1 = Float64[],
u1 = Float64[],
v1 = Float64[],
Δx = Float64[],
Δy = Float64[],
Δu = Float64[],
Δv = Float64[]
)
# STUDENT CODE START
# 1. Extract initial features from each encounter
# 2. Add features to DataFrame
# STUDENT CODE END
# Create discretizers and discretize data
if !isempty(df)
D = Dict{Symbol, LinearDiscretizer}(
sym=>LinearDiscretizer(binedges(DiscretizeUniformWidth(6), df[!, sym]))
for sym in propertynames(df)
)
else
D = Dict{Symbol, LinearDiscretizer}()
end
# Encode each column
for (sym, disc) in D
df[!, sym] = encode(disc, df[!, sym])
end
return df, D
end
# ╔═╡ 36abf386-9cd1-4090-b7a0-51db9d2302b5
begin
global m1p21_icon = "❌"
global milestone_one_part_21_pass = false
m1p21_sampled_ids = collect(1:1000)
m1p21_test_encounters = [pull_encounter(flights, id_i) for id_i in m1p21_sampled_ids]
try
user_df, user_D = create_discretized_dataset(m1p21_test_encounters)
true_df, true_D = __create_discretized_dataset(m1p21_test_encounters)
# Check DataFrame dimensions
dims_match = size(user_df) == size(true_df)
disc_match = false
if dims_match
# Check discretization matches
disc_match = true
for sym in propertynames(true_df)
if !all(user_df[!, sym] .== true_df[!, sym])
disc_match = false
break
end
end
end
if dims_match && disc_match
global m1p21_icon = "✅"
global milestone_one_part_21_pass = true
Markdown.MD(Markdown.Admonition("correct", "🎉", [md"""Your discretization is correct!"""]))
else
m1p21_msg = md"""Your discretization doesn't appear to be correct."""
if isempty(user_df)
m1p21_msg = md"""$m1p21_msg
Your DataFrame is empty."""
elseif !dims_match
m1p21_msg = md"""$m1p21_msg
DataFrame dimensions don't match"""
end
if dims_match && !disc_match
m1p21_msg = md"""$m1p21_msg
Discretized values don't match"""
end
Markdown.MD(Markdown.Admonition("warning", "Keep working on it!", [m1p21_msg]))
end
catch err
Markdown.MD(Markdown.Admonition("danger", "Error", [md"""There is an error with your code: $err"""]))
end
end
# ╔═╡ d2d86873-76ff-49fe-a368-de8f3641d310
md"""
### $(m1p21_icon) Milestone One - Part 2 Check
"""
# ╔═╡ 5e9fca68-4016-40ee-b0ba-1db008efee99
md"""
## Part 3: Learn Bayesian Network Structure
Now that we have discretized our data, we can learn a Bayesian network that captures the relationships between variables in our initial conditions.
### 💻 **Task**: Implement `learn_network`
"""
# ╔═╡ a2029e97-7907-456f-baea-7fc4a7ac7feb
function learn_network(df::DataFrame, D::Dict{Symbol,LinearDiscretizer})
bn = DiscreteBayesNet()
# Set up algorithm parameters
params = GreedyHillClimbing(
ScoreComponentCache(df),
max_n_parents=3, # Maximum parents per node
prior=UniformPrior()
)
# Get number of categories for each variable
ncategories = [nlabels(D[sym]) for sym in propertynames(df)]
# STUDENT CODE START
# 1. Learn network structure and parameters
# STUDENT CODE END
# Return the Bayesian network
return bn
end
# ╔═╡ 74e2f715-c62d-4003-87c8-3ab9c89f3813
begin
global m1p3_icon = "❌"
global milestone_one_part_3_pass = false
try
# First get discretized data
local test_df, test_D = __create_discretized_dataset(m1p21_test_encounters)
# Get student's network and solution network
user_bn = learn_network(test_df, test_D)
true_bn = __learn_network(test_df, test_D)
# Compare network structures
struct_match = user_bn.dag == true_bn.dag
# Compare network parameters (CPTs)
local params_match = true
if struct_match # Only check params if structure matches
if length(user_bn.cpds) != length(true_bn.cpds)
params_match = false
end
for ii in 1:length(true_bn.cpds)
if !params_match
break
end
true_cpd = true_bn.cpds[ii]
user_cpd = user_bn.cpds[ii]
if !all(true_cpd.target .== user_cpd.target)
params_match = false
elseif !all(true_cpd.parental_ncategories .== user_cpd.parental_ncategories)
params_match = false
elseif !all(true_cpd.parents .== user_cpd.parents)
params_match = false
elseif !all(true_cpd.distributions .== user_cpd.distributions)
params_match = false
end
end
end
if struct_match && params_match
global m1p3_icon = "✅"
global milestone_one_part_3_pass = true
Markdown.MD(Markdown.Admonition("correct", "🎉", [md"""Your Bayesian network learning is correct!"""]))
else
m1p3_msg = md"""Your learned network doesn't appear to be correct.
Check that you are passing the arguments to `fit` correctly. Expand on the hints for more information on the arguments and how to use `fit`."""
if !struct_match
m1p3_msg = md"""$m1p3_msg
The network structure doesn't match the expected result."""
elseif !params_match
m1p3_msg = md"""$m1p3_msg
The structure is correct, but the network parameters don't match the expected result. Ensure you are using the provided `params` when fitting the network."""
end
Markdown.MD(Markdown.Admonition("warning", "Keep working on it!", [m1p3_msg]))
end
catch err
Markdown.MD(Markdown.Admonition("danger", "Error", [md"""There is an error with your code: $err"""]))
end
end
# ╔═╡ 949de14f-8da1-4983-a2a4-637746b3bd37
md"""
### $(m1p3_icon) Milestone One - Part 3 Check
"""
# ╔═╡ 171b053d-df2a-42be-86ca-3616cbb58512
begin
global milestone_one_complete = (milestone_one_part_1_pass && milestone_one_part_21_pass && milestone_one_part_3_pass)
if milestone_one_complete
md"""
## ✅ Milestone One Complete!
"""
else
md"""
## ❌ Milestone One Incomplete!
"""
end
end
# ╔═╡ b0027ea8-54db-4db5-97dc-c7e961e71637
md"""
# Creating the Initial Conditions Model and Sampling
Now that we have the required functions, we can create our initial conditions model. We also want to create a function to allow us to sample initial conditions from the model.
## Creating the Model
The following two lines use your created functions to discretize the dataset and then learn a Bayesian network that we can sample from.
"""
# ╔═╡ 1f725083-c755-4a28-94b4-0a48aa70edad
begin
if milestone_one_complete
df, D = create_discretized_dataset(encounter_set)
bn = learn_network(df, D)
end
end
# ╔═╡ 42fa595a-c057-4feb-9396-594cbbd4140a
md"""
## Sampling from the Model
To sample from the model, we need both the Bayesian network and the dictionary of discretizers. We will use the `DiscretizedBayesNet` Type defined in the provided code to help us.
"""
# ╔═╡ 2b5fdf91-7a1d-4632-a6fc-d76ff9237179
md"""
Now we can use Julia's multiple dispatch to extend the `rand` function to sample from our model, which will be a `DiscretizedBayesNet`.
"""
# ╔═╡ 6fef1c72-971f-4734-9ed9-bfd99bab46f9
function Base.rand(model::DiscretizedBayesNet)
sample = rand(model.dbn) # Sample from the Bayesian network
# Convert the discrete samples to continuous samples
for (sym, disc) in model.discs
sample[sym] = decode(disc, sample[sym])
end
return sample
end
# ╔═╡ 95790cdf-9d36-4d3e-ac3d-ddcd87d0018d
md"""
### Example: Sampling
Let's create the model using our variables from above (i.e. `bn` and `D`).
"""
# ╔═╡ ea2c6b22-43c7-4b1c-a234-c9a861f6e6dd
if milestone_one_complete
init_cond_model = DiscretizedBayesNet(bn, D)
end
# ╔═╡ 8d0406c5-44f7-4d41-a92b-c9463fd4ce9b
md"""Now let's sample from it."""
# ╔═╡ f6d8023a-c798-4935-9e9b-4f3ab9de099e
md"""The sample we get is not quite an EncounterState. Let's create a helper function that turns the sample into an EncounterState."""
# ╔═╡ c0e16644-bc8c-4193-a341-27864ed38717
function encounter_from_init_sample(sampled_init_cond::Dict{Symbol, Any})
encounter_state = EncounterState(
AircraftState(
0.0,
sampled_init_cond[:y1],
sampled_init_cond[:u1],
sampled_init_cond[:v1]
),
AircraftState(
0.0 + sampled_init_cond[:Δx],
sampled_init_cond[:y1] + sampled_init_cond[:Δy],
sampled_init_cond[:u1] + sampled_init_cond[:Δu],
sampled_init_cond[:v1] + sampled_init_cond[:Δv]
),
0.0 # Time is 0 based on it being an initial condition
)
return encounter_state
end
# ╔═╡ d816892b-e277-47e6-8fb4-a9ec984d2a06
md"""
# 2️⃣ Milestone Two: Dynamics Model
Now that we've successfully implemented the Initial Conditions Model, we'll develop a Bayesian network to capture how aircraft behave during encounters. This Dynamics Model will focus on predicting the accelerations of each aircraft given the current relative state.
## Model Structure
Our dynamics model represents the probability distribution of relative accelerations given the current relative state. Specifically, we want:
$$P(a_{u_1}, a_{v_1}, a_{u_2}, a_{v_2} \mid \Delta x_{t-1}, \Delta y_{t-1}, \Delta u_{t-1}, \Delta v_{t-1})$$
and we can achieve that by learning:
$$P(a_{u_1}, a_{v_1}, a_{u_2}, a_{v_2}, \Delta x_{t-1}, \Delta y_{t-1}, \Delta u_{t-1}, \Delta v_{t-1})$$
and sampling from the network with evidence (given the relative state).
Note:
- ``a_{u_1}, a_{v_1}, a_{u_3}, a_{v_3}`` are the accelerations of the aircraft
- ``\Delta x_{t-1}, \Delta y_{t-1}, \Delta u_{t-1}, \Delta v_{t-1}`` are the previous relative positions and velocities
We'll follow the same steps as before:
1. Extract transition features from our encounters
2. Create a discretized dataset
3. Learn a Bayesian network (structure and parameters)
## Part 1: Extract Transitions
First, let's create a function that takes in all our encounters and returns a DataFrame containing all of the transitions. For each timestep in each encounter, we'll calculate the relative accelerations from the current timestep to the next and also record the relative state variables from the current timestep.
### 💻 **Task**: Implement `extract_transitions`
"""
# ╔═╡ 668cbea8-b56c-491a-9907-513fe37e5861
function extract_transitions(encounters::Vector{Encounter})
# Initialize DataFrame to store transitions
df = DataFrame(
au1 = Float64[], # horizontal acceleration [m/s²] of aircraft 1
av1 = Float64[], # vertical acceleration [m/s²] of aircraft 1
au2 = Float64[], # horizontal acceleration [m/s²] of aircraft 2
av2 = Float64[], # vertical acceleration [m/s²] of aircraft 2
Δx = Float64[], # relative x position [m]
Δy = Float64[], # relative y position [m]
Δu = Float64[], # relative horizontal velocity [m/s]
Δv = Float64[], # relative vertical velocity [m/s]
)
# STUDENT CODE START
# For each encounter:
# For each consecutive pair of timesteps:
# 1. Calculate relative accelerations between t and t+1
# 2. Record relative state at time t
# 3. Add to DataFrame
# STUDENT CODE END
return df
end
# ╔═╡ dea3d3ee-40d3-4098-8c1f-8192ffb4ebd7
begin
global m2p1_icon = "❌"
global milestone_two_part_1_pass = false
try
user_df = extract_transitions(test_encounters)
true_df = __extract_transitions(test_encounters)
# Check DataFrame dimensions
dims_match = size(user_df) == size(true_df)
# Check values match within tolerance
vals_match = true
if dims_match
for col in names(true_df)
if !all(isapprox.(user_df[!, col], true_df[!, col], rtol=1e-10))
vals_match = false
break
end
end
end
if dims_match && vals_match
global m2p1_icon = "✅"
global milestone_two_part_1_pass = true
Markdown.MD(Markdown.Admonition("correct", "🎉", [md"""Your transition extraction is correct!"""]))
else
if !dims_match
Markdown.MD(Markdown.Admonition("warning", "Keep working on it!", [md"""Your transition extraction doesn't appear to be correct.
DataFrame has incorrect dimensions. Check that you're extracting transitions from every valid timestep.
"""]))
elseif !vals_match
# Build column information string
col_matches = []
cols = names(true_df)
for col in names(true_df)
matches = sum(isapprox.(user_df[!, col], true_df[!, col], rtol=1e-10))
push!(col_matches, "$col: $matches/$(size(true_df, 1)) values match")
end
Markdown.MD(Markdown.Admonition("warning", "Keep working on it!", [md"""Your transition extraction doesn't appear to be correct.
Values don't match expected results:
- $(col_matches[1])
- $(col_matches[2])
- $(col_matches[3])
- $(col_matches[4])
- $(col_matches[5])
- $(col_matches[6])
Check your calculations for relative states and accelerations.
"""]))
end
end
catch err
Markdown.MD(Markdown.Admonition("danger", "Error", [md"""There is an error with your code: $err"""]))
end
end
# ╔═╡ 260a8b0f-9620-4c6b-be6e-ada7021ac201
md"""
### $(m2p1_icon) Milestone Two - Part 1 Check
"""
# ╔═╡ 4081909a-c8df-4d44-a3da-24ef1a0c0700
md"""
## Part 2: Learn Transition Model with Bayesian Networks
Now that we can extract transitions from our encounters, we'll learn a Bayesian network to capture how accelerations depend on the current state. Similar to Part 1 of Milestone One, we'll create a discretized dataset using predefined bin edges.
### 💻 **Task**: Implement `create_discretized_transitions`
"""
# ╔═╡ 0c2f2e20-948b-4b45-b83c-db692d028c1d
function create_discretized_transitions(encounters::Vector{Encounter})
# Get raw transition data
df = extract_transitions(encounters)
# Create discretizers with predefined edges
D = Dict{Symbol,LinearDiscretizer}()
D[:au1] = LinearDiscretizer(collect(range(-0.5, stop = 0.5, length = 6)))
D[:av1] = LinearDiscretizer(collect(range(-2.5, stop = 2.5, length = 6)))
D[:au2] = LinearDiscretizer(collect(range(-0.5, stop = 0.5, length = 6)))
D[:av2] = LinearDiscretizer(collect(range(-2.5, stop = 2.5, length = 6)))
D[:Δx] = LinearDiscretizer(collect(range(-1000.0, stop = 1000.0, length = 6)))
D[:Δy] = LinearDiscretizer(collect(range(-1000.0, stop = 1000.0, length = 6)))
D[:Δu] = LinearDiscretizer(collect(range(-30.0, stop = 30.0, length = 6)))
D[:Δv] = LinearDiscretizer(collect(range(-100.0, stop = 100.0, length = 6)))
# STUDENT CODE START
# Encode each column
# STUDENT CODE END
return df, D
end
# ╔═╡ 778f308b-f448-4685-8f11-cdb071784bc1
begin
global m2p2_icon = "❌"
global milestone_two_part_2_pass = false
try
m2p2_user_df, m2p2_user_D = create_discretized_transitions(test_encounters)
m2p2_true_df, m2p2_true_D = __create_discretized_transitions(test_encounters)
# Check DataFrame dimensions
m2p2_dims_match = size(m2p2_user_df) == size(m2p2_true_df)
local m2p2_vals_match = true
if m2p2_dims_match
for col in names(m2p2_true_df)
if !all(m2p2_user_df[!, col] .== m2p2_true_df[!, col])
m2p2_vals_match = false
break
end
end
end
if m2p2_dims_match && m2p2_vals_match
global m2p2_icon = "✅"
global milestone_two_part_2_pass = true
Markdown.MD(Markdown.Admonition("correct", "🎉", [md"""Your discretization of transitions is correct!"""]))
else
m2p2_msg = md"""Your discretization of transitions doesn't appear to be correct."""
if !m2p2_dims_match
m2p2_msg = md"""$m2p2_msg
DataFrame dimensions don't match."""
end
if m2p2_dims_match && !m2p2_vals_match
# Build column mismatch details
col_mismatches = String[]
for col in names(m2p2_true_df)
if !all(m2p2_user_df[!, col] .== m2p2_true_df[!, col])
push!(col_mismatches, "Column $col does not match")
end
end
m2p2_msg = md"""$m2p2_msg
Discretized values don't match:
$(join(col_mismatches, ";\n"))
Check your discretization process carefully."""
end
Markdown.MD(Markdown.Admonition("warning", "Keep working on it!", [m2p2_msg]))
end
catch err
Markdown.MD(Markdown.Admonition("danger", "Error", [md"""There is an error with your code: $err"""]))
end
end
# ╔═╡ aac9e56b-008b-40f0-b168-3e8bfc4adb36
md"""
### $(m2p2_icon) Milestone Two - Part 2 Check
"""
# ╔═╡ 9ebb3a2a-9a74-4084-b570-20edcc5b78af
md"""
## Part 3: Learn Bayesian Network Structure
Similar to before, now that we have discretized our data, we can learn a Bayesian network that captures the relationships between variables in our transition.
### 💻 **Task**: Implement `learn_transition_network`
"""
# ╔═╡ b3bebc6c-a0fb-4ed2-b9e6-c325786ec1fe
function learn_transition_network(df::DataFrame, D::Dict{Symbol,LinearDiscretizer})
bn_dynamics = DiscreteBayesNet()
# Set up algorithm parameters
params = GreedyHillClimbing(
ScoreComponentCache(df),
max_n_parents=3,
prior=UniformPrior()
)
# STUDENT CODE START
# 1. Get the number of categories for each variable for our dynamics model
# 2. Learn network structure and parameters
# STUDENT CODE END
# Return the Bayesian network
return bn_dynamics
end
# ╔═╡ 9290ab5e-6418-4c8a-b017-0db1705cd5cc
begin
global m2p3_icon = "❌"
global milestone_two_part_3_pass = false
try
# First get discretized data
local test_df, test_D = __create_discretized_dataset(m1p21_test_encounters)
# Get student's network and solution network
user_bn = learn_transition_network(test_df, test_D)
true_bn = __learn_transition_network(test_df, test_D)
# Compare network structures
struct_match = user_bn.dag == true_bn.dag
# Compare network parameters (CPTs)
local params_match = true
if struct_match # Only check params if structure matches
if length(user_bn.cpds) != length(true_bn.cpds)
params_match = false
end
for ii in 1:length(true_bn.cpds)
if !params_match
break
end
true_cpd = true_bn.cpds[ii]
user_cpd = user_bn.cpds[ii]
if !all(true_cpd.target .== user_cpd.target)
params_match = false
elseif !all(true_cpd.parental_ncategories .== user_cpd.parental_ncategories)
params_match = false
elseif !all(true_cpd.parents .== user_cpd.parents)
params_match = false
elseif !all(true_cpd.distributions .== user_cpd.distributions)
params_match = false
end
end
end
if struct_match && params_match
global m2p3_icon = "✅"
global milestone_two_part_3_pass = true
Markdown.MD(Markdown.Admonition("correct", "🎉", [md"""Your Bayesian network learning is correct!"""]))
else
m2p3_msg = md"""Your learned network doesn't appear to be correct.
Check that you are passing the arguments to `fit` correctly. Expand on the hints for more information on the arguments and how to use `fit`."""
if !struct_match
m2p3_msg = md"""$m2p3_msg
The network structure doesn't match the expected result."""
elseif !params_match
m2p3_msg = md"""$m2p3_msg
The structure is correct, but the network parameters don't match the expected result. Ensure you are using the provided `params` when fitting the network."""
end
Markdown.MD(Markdown.Admonition("warning", "Keep working on it!", [m2p3_msg]))
end
catch err
Markdown.MD(Markdown.Admonition("danger", "Error", [md"""There is an error with your code: $err"""]))
end
end
# ╔═╡ cca93968-f983-4513-9aa9-2dd93a8dce4c
md"""
### $(m2p3_icon) Milestone Two - Part 3 Check
"""
# ╔═╡ 7320d945-29f6-4356-ad64-5408532645c6
begin
milestone_two_complete = (milestone_two_part_1_pass && milestone_two_part_2_pass && milestone_two_part_3_pass)
if milestone_two_complete
md"""
## ✅ Milestone Two Complete!
"""
else
md"""
## ❌ Milestone Two Incomplete!
"""
end
end
# ╔═╡ d6b0c927-c800-4c90-bde8-f6d33e5334c7
md"""
# Creating and Using the Transition Model
Similar to our initial conditions model, our model is made up of a Bayesian network and discretizers. Therefore, our model is a `DiscretizedBayesNet` type that was defined in the provided code.
"""
# ╔═╡ 006d8885-09d1-4f11-87b7-6e64d308620a
md"""
Let's create our transition model using the encounters data:
"""
# ╔═╡ 0aa04646-e140-4ac5-beac-ac550b76c707
begin
transitions_df, transitions_D = create_discretized_transitions(encounter_set)