%% Track Simulated Vehicles Using GNN and JPDA Trackers in Simulink
% This example shows how to configure and utilize GNN and JPDA trackers in
% a simulated highway scenario in Simulink(R) with Sensor Fusion and
% Tracking Toolbox(TM). It closely follows the
% <docid:driving_ug#mw_8ae0eb21-efca-4a6d-9a4a-0415a638c554 Sensor
% Fusion Using Synthetic Radar and Vision Data in Simulink example>. A main
% benefit of modeling the system in Simulink is the simplicity of
% performing "what-if" analysis and choosing a tracker that results in the
% best performance based on the requirements.

% Copyright 2019-2020 The MathWorks, Inc.

%% Introduction
% Synthetic radar and vision data help us in evaluating the target tracking
% algorithms. This example primarily focuses on the tracking abilities of
% |trackerGNN| and |trackerJPDA| in Simulink.
%
%% Setup and Overview of the Model
% The setup of this example is the same as the setup of
% <docid:driving_ug#mw_8ae0eb21-efca-4a6d-9a4a-0415a638c554 Sensor
% Fusion Using Synthetic Radar and Vision Data in Simulink> example, except
% that the simulated detections are concatenated as the input to the
% |Trackers| block.
load_system('TrackSimulatedVehiclesUsingGNNandJPDAExample');
set_param('TrackSimulatedVehiclesUsingGNNandJPDAExample','SimulationCommand','update');
open_system('TrackSimulatedVehiclesUsingGNNandJPDAExample');
%%
% *Trackers*
%
% The |Trackers| block is a variant subsystem, which you can use to switch
% between the GNN tracker and the JPDA tracker.
%
% The first variant of the Trackers block, |trackerGNN|, assumes a constant
% velocity motion model and an extended Kalman filter by setting the Filter
% initialization function as the default |initcvekf|.
%
% <<../GNNMask.PNG>>
%
% With GNN tracker, you can choose your own customized filter which fits
% the motion of the simulated objects. You can choose the assignment
% algorithm among MatchPairs, Munkres, Jonker-Volgenant, Auction and  your
% own customized assignment algorithm. You can also specify the track
% maintenance logic as |History| or |Score|.
%
% The second variant of the Trackers block, |trackerJPDA|, also assumes a
% constant velocity motion model and an extended Kalman filter by the
% default filter initialization function |initcvekf|.
%
% <<../JPDAMask.PNG>>
%
% With |trackerJPDA|, you can customize your own filter initialization
% function and choose between the |History| and the |Integrated| track logic.
%
% You can select your preferable subsystem by setting the value of
% conditional variable |Tracker| in the base workspace. The following table
% shows the |Tracker| values corresponding  to their configurations.
%
% <<../TrackerConfig.png>>
%
% You can also use the <docid:simulink_ug#bso5b65 Model Explorer> to
% change the value of |Tracker|.
% 
% <<../ModelExplorer.png>>
%
% *Track Metrics*
%
% The Track Metrics is implemented using a <docid:simulink_ref#bs9e91t-1
% MATLAB System> block. The code for the block is defined by a helper
% class, HelperTrackMetrics.
%
%% Results
% To visualize the results, use the Bird's-Eye Scope. The
% Bird's-Eye Scope is a model-level visualization tool  via a menu provided
% on the Simulink model toolbar. After opening the scope, click *Find
% Signals* to set up the signals. Then run the simulation to display the
% actors, vision and radar detections, tracks, and road boundaries. The
% following image shows the bird's-eye scope for this example.
%
% In Simulink, you can run this example via interpreted execution or code
% generation. With interpreted execution, the model simulates the block
% using the MATLAB(R) execution engine which allows faster startup time but
% longer execution time. With code generation, the model simulates the
% block using the subset of MATLAB code supported for code generation which
% allows better performance than the interpreted execution.
%
% After running the model, you can visualize the results on the figures below.
% 
% <<../TrackerGNN_Results.gif>>
%
% The above figure shows the tracking results using synthetic radar and
% vision data with |trackerGNN|. You can see that the tracker maintained
% tracks throughout the tracking region.
% 
% <<../TrackerJPDA_Results.gif>>
%
% The above figure shows the tracking results using synthetic radar and
% vision data with |trackerJPDA|. You can see that the tracker maintained
% tracks throughout the tracking region and the tracking performance is
% better than that of |trackerGNN| as there are fewer false tracks
% generated.
%
% *Evaluate Tracking Performance*
%
% You can use the |Track Metrics| block to evaluate the tracking performance
% of each tracker using quantitative metrics. In this example, you view the
% number of target tracks, the number of redundant tracks, and the number
% of false tracks. A target track is a track that is associated with unique
% targets. A redundant track is a track that is associated with a ground
% truth object that has been associated to another track. A false track is
% a track that is not associated with any ground truth objects. Below the
% first figure shows the tracking performance results of GNN tracker and
% the second figure shows the results of JPDA tracker.
% 
% <<../Assign_Results.png>>
%
% The assignment metrics illustrate that one redundant track was
% initialized and confirmed by |trackerGNN| whereas |trackerJPDA| does not
% create any redundant tracks. The redundant tracks were generated due to
% imperfect clustering, where detections belonging to the same target were
% clustered into more than one cluster. Also, the |trackerGNN| created and
% confirmed three false tracks whereas |trackerJPDA| confirmed only one false
% track. These metrics show that |trackerJPDA| provides better tracking
% performance than |trackerGNN|.
%% Summary
% This example shows how to generate a scenario, simulate sensor
% detections, and use these detections to track moving vehicles around the
% ego vehicle using the |trackerGNN| and |trackerJPDA| blocks in Simulink. You
% have also seen the simplicity of exchanging between the two trackers, the
% flexibility of customizing these trackers to suit your own target
% tracking requirements, and the ability to evaluate the tracking results
% using track metrics.