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
mod animal_runner;
mod filter_runner;
mod measurement;
mod uav_runner;

pub use self::measurement::Measurement;

use self::animal_runner::AnimalRunner;
use self::measurement::MeasurementModel;
use self::filter_runner::{FilterConfig, FilterRunner};
use self::uav_runner::UavRunner;

use uav::UavState;
use animal::AnimalState;

use SimulationConfig;

/// A snapshot of the simulation at a particular point in time
pub struct SimulationFrame {
    /// The state of each UAV in the simulation
    pub uavs: Vec<UavState>,

    /// A list of all targets (with particle filter information) in the simulation
    pub targets: Vec<FilterFrame>,
}

/// A frame representing a single target (i.e. animal) at a particular point in time
pub struct FilterFrame {
    /// The animal's true state
    pub animal_state: AnimalState,

    /// The current list of particles from the particle filter
    pub particles: Vec<AnimalState>,

    /// An estimate of the animal's true state based on the mean value of all the particles
    pub particles_mean: AnimalState,
}

/// A single target (i.e. animal) in the simulation runner
struct Tracker {
    animal: AnimalRunner,
    // TODO: Allow a different signal model to be used for each UAV
    measurement: MeasurementModel,
    filter: FilterRunner,
}

/// A structure for running a configured simulation
pub struct SimulationRunner {
    pub uavs: Vec<UavRunner>,
    trackers: Vec<Tracker>,
}

impl SimulationRunner {
    /// Create a new simulation runner using the provided config and list of animals
    pub fn new(config: &SimulationConfig) -> SimulationRunner {
        let trackers = config.animals.iter().map(|&animal| {
            let filter_config = FilterConfig {
                num_particles: config.num_particles,
                particle_noise: config.particle_noise,
                map_size: config.map_size,
                animal: animal,
                signal: config.uavs[0].receiver,
            };

            Tracker {
                animal: AnimalRunner::new(animal),
                measurement: MeasurementModel::new(config.uavs[0].receiver),
                filter: FilterRunner::new_initial(filter_config),
            }
        }).collect();

        SimulationRunner {
            uavs: config.uavs.iter().map(|&uav| UavRunner::new(uav)).collect(),
            trackers: trackers,
        }
    }

    /// Get a snapshot of the current simulation frame
    pub fn snapshot(&self) -> SimulationFrame {
        let uavs = self.uavs.iter().map(|uav| uav.snapshot()).collect();

        let targets = self.trackers.iter().map(|tracker| {
            let particles = tracker.filter.snapshot();
            let particles_mean = particles_mean(particles);

            FilterFrame {
                animal_state: tracker.animal.snapshot(),
                particles: particles.into(),
                particles_mean: particles_mean,
            }
        }).collect();

        SimulationFrame {
            uavs: uavs,
            targets: targets,
        }
    }

    /// Checks if the new config is compatible with the old config.
    pub fn config_compatible(&self, config: &SimulationConfig) -> bool {
        self.trackers.len() == config.animals.len() && self.uavs.len() == config.uavs.len()
    }

    /// Performs a single step in the simulation
    pub fn step(&mut self, dt: f32) -> SimulationFrame {
        for uav in &mut self.uavs {
            uav.step(dt);
        }

        for tracker in &mut self.trackers {
            let animal = tracker.animal.step(dt);

            for uav in &self.uavs {
                let measurement = tracker.measurement.generate(uav.snapshot(), animal.position);
                tracker.filter.step(measurement, dt);
            }
        }

        self.snapshot()
    }

    /// Updates the config of all subcomponents (if compabible)
    pub fn update_config(&mut self, config: &SimulationConfig) {
        if !self.config_compatible(config) {
            return;
        }

        for (uav, &uav_config) in self.uavs.iter_mut().zip(&config.uavs) {
            uav.update_config(uav_config);
        }

        for (tracker, &animal_config) in self.trackers.iter_mut().zip(&config.animals) {
            let filter_config = FilterConfig {
                num_particles: config.num_particles,
                particle_noise: config.particle_noise,
                map_size: config.map_size,
                animal: animal_config,
                signal: config.uavs[0].receiver,
            };

            tracker.animal.update_config(animal_config);
            tracker.measurement = MeasurementModel::new(config.uavs[0].receiver);
            tracker.filter.update_config(filter_config);
        }
    }
}

/// Calculates the mean of a list of particle
pub fn particles_mean(particles: &[AnimalState]) -> AnimalState {
    let len = particles.len() as f32;

    let position_sum = particles.iter().map(|x| x.position)
        .fold([0.0, 0.0, 0.0], |acc, x| [acc[0] + x[0], acc[1] + x[1], acc[2] + x[2]]);
    let velocity_sum = particles.iter().map(|x| x.velocity)
        .fold([0.0, 0.0], |acc, x| [acc[0] + x[0], acc[1] + x[1]]);

    AnimalState {
        position: [position_sum[0] / len, position_sum[1] / len, position_sum[2] / len],
        velocity: [velocity_sum[0] / len, velocity_sum[1] / len],
    }
}