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
//Import source modules
use crate::building::Building;
use crate::floors::Floors;
use crate::people::People;

//Implement standard/imported modules
use rand::{Rng, SeedableRng};
use rand::rngs::StdRng;
use rand::distributions::{Distribution, Uniform, Bernoulli};

/// # `ElevatorController` trait
///
/// An `ElevatorController` implementation controls the elevators of a building.
pub trait ElevatorController {
    fn get_building(&mut self) -> &Building;

    fn get_building_mut(&mut self) -> &mut Building;

    fn clone_building(&mut self) -> Building;

    fn can_be_upgraded(&self) -> bool;

    fn upgrade(&mut self, incrementation: f64);

    fn update_elevators(&mut self);
}

/// # `RandomController` struct
///
/// A `RandomController` implements the `ElevatorController` trait.  It randomly
/// generates destination floors for each of a building's elevators once the elevator
/// reaches its destination floor.
 pub struct RandomController {
    pub building: Building,
    num_floors: usize,
    floors_to: Vec<Option<usize>>,
    dst_to: Uniform<usize>,
    p_rational: f64,
    dst_rational: Bernoulli,
    upgradable: bool,
    rng: StdRng
}

//Implement the RandomController interface
impl RandomController {
    /// Initialize a new RandomController given a `Building`, an `StdRng` (from
    /// the rand library), and an `f64` representing the probability that the
    /// RandomController behaves rationally.
    ///
    /// ## Example
    ///
    /// ```
    /// let my_rng = rand::thread_rng();
    /// let my_building: Building = Building::from(
    ///     4_usize,
    ///     2_usize,
    ///     0.5_f64,
    ///     5.0_f64,
    ///     2.5_f64,
    ///     0.5_f64
    /// );
    /// let my_controller: RandomController = RandomController::from(
    ///     my_building,
    ///     my_rng,
    ///     0.5_f64
    /// );
    /// ```
    pub fn from(building: Building, rng: StdRng, p_rational: f64) -> RandomController {
        //Get the number of floors and elevators in the building
        let num_floors: usize = building.floors.len();
        let num_elevators: usize = building.elevators.len();

        //Initialize the destination floors for the elevators
        let floors_to: Vec<Option<usize>> = {
            let mut tmp_floors_to: Vec<Option<usize>> = Vec::new();
            for _ in 0..num_elevators {
                tmp_floors_to.push(None);
            }
            tmp_floors_to
        };

        //Initialize the distribution for randomizing dest floors
        let dst_to: Uniform<usize> = Uniform::new(0_usize, num_floors);

        //Initialize the controller
        RandomController {
            building: building,
            num_floors: num_floors,
            floors_to: floors_to,
            dst_to: dst_to,
            p_rational: p_rational,
            dst_rational: Bernoulli::new(p_rational).unwrap(),
            upgradable: true,
            rng: rng
        }
    }

    /// Initialize a new RandomController from just a building.  The rng is
    /// created on the fly, and the rational probability is defaulted to 0.
    ///
    /// ## Example
    ///
    /// ```
    /// let my_building: Building = Building::from(
    ///     4_usize,
    ///     2_usize,
    ///     0.5_f64,
    ///     5.0_f64,
    ///     2.5_f64,
    ///     0.5_f64
    /// );
    /// let my_controller: RandomController = RandomController::from(my_building);
    /// ```
    pub fn from_building(building: Building) -> RandomController {
        //Initialize default values for the additional properties for this controller
        let rng = StdRng::from_seed(rand::thread_rng().gen());
        let p_rational = 0.0_f64;

        //Initialize and return the RandomController
        RandomController::from(building, rng, p_rational)
    }

    /// Set the destination floors of the elevators randomly according to
    /// random or rational logic, depending on the p_rational
    pub fn update_floors_to(&mut self) {
        //If the number of elevators in the building is greater than the number
        //of destination floors in the controller, then add new destination
        //floors
        while self.building.elevators.len() > self.floors_to.len() {
            self.floors_to.push(None);
        }

        //If the numer of floors in the building is greater than the number of
        //floors tracked by the controller, then update the number of floors
        //tracked by the controller and re-instantiate the dest distribution
        if self.building.floors.len() != self.num_floors {
            self.num_floors = self.building.floors.len();
            self.dst_to = Uniform::new(0, self.num_floors);
        }

        //Loop through the elevators in the building
        for (i, elevator) in self.building.elevators.iter().enumerate() {
            //If the destination floor for the elevator is None, then update it
            match self.floors_to[i] {
                Some(_) => {},
                None => {
                    if self.dst_rational.sample(&mut self.rng) {
                        if elevator.stopped {
                            //Find the nearest destination floor among people on the elevator
                            let (nearest_dest_floor, min_dest_floor_dist): (usize, usize) = elevator.get_nearest_dest_floor();
                
                            //If the nearest dest floor is identified, then set as the dest floor
                            if min_dest_floor_dist != 0_usize {
                                self.floors_to[i] = Some(nearest_dest_floor);
                                continue;
                            }
                
                            //Find the nearest waiting floor among people throughout the building
                            let (nearest_wait_floor, min_wait_floor_dist): (usize, usize) = self.building.get_nearest_wait_floor(elevator.floor_on);
                
                            //If the nearest wait floor is identified, then set as the dest floor
                            if min_wait_floor_dist != 0_usize {
                                self.floors_to[i] = Some(nearest_wait_floor);
                                continue;
                            }
                        }
                    } else {
                        self.floors_to[i] = Some(self.dst_to.sample(&mut self.rng));
                        continue;
                    }
                    self.floors_to[i] = Some(elevator.floor_on);
                }
            }
        }
    }

    /// If any elevators are at their destination floor, then set that floor
    /// to None so that it can be re-randomized next time step.
    pub fn clear_floors_to(&mut self) {
        //Loop through the elevators in the building
        for (i, elevator) in self.building.elevators.iter().enumerate() {
            let dest_floor = self.floors_to[i].unwrap();
            if dest_floor == elevator.floor_on {
                self.floors_to[i] = None;
            }
        }
    }
}

//Implement the ElevatorController trait for the RandomController
impl ElevatorController for RandomController {
    /// Immutably borrow the building belonging to the controller
    fn get_building(&mut self) -> &Building {
        &self.building
    }

    /// Mutably borrow the building belonging to the controller
    fn get_building_mut(&mut self) -> &mut Building {
        &mut self.building
    }

    /// Clone the building belonging to the controller.  Generally used when
    /// swapping controllers.
    fn clone_building(&mut self) -> Building {
        self.building.clone()
    }

    /// Return a boolean signifying whether the controller can be upgraded or
    /// not.
    fn can_be_upgraded(&self) -> bool {
        //If the controller is 100% rational, then no further upgrades are
        //possible
        if self.p_rational >= 1.0_f64 {
            return false;
        }

        //Otherwise, the elevator controller can be upgraded
        self.upgradable
    }

    /// Upgrade the controller given an incrementation float
    fn upgrade(&mut self, incrementation: f64) {
        //Add the current rationality probability to the incrementation and
        //check to see if it exceeds 1, if so then ceiling it at 1.0
        let mut new_p_rational: f64 = self.p_rational + incrementation;
        if new_p_rational > 1.0_f64 {
            new_p_rational = 1.0_f64;
        }

        //Update the rationality probability and distribution of the controller
        self.p_rational = new_p_rational;
        self.dst_rational = Bernoulli::new(self.p_rational).unwrap();
    }

    /// If the destination floor is None, then randomize a new destination floor.
    /// If the elevator is not on its destination floor then move toward it.  If the
    /// elevator is on its destination floor then stop it and set its destination
    /// floor to None for randomization during the next step.
    fn update_elevators(&mut self) {
        //Update the destination floors
        self.update_floors_to();
        
        //Loop through the dest floors and update the building's elevators accordingly
        for (i, floor_to) in self.floors_to.iter().enumerate() {
            //Unwrap the destination floor
            let dest_floor: usize = floor_to.unwrap();

            //Update the elevator's direction based on its destination floor
            self.building.elevators[i].update_direction(dest_floor);

            //Update the elevator
            let _new_floor_index = self.building.elevators[i].update_floor();
        }

        //Clear the destination floors if any elevators arrived at their destinations
        self.clear_floors_to();
    }
}

/// # `NearestController` struct
///
/// A `NearestController` implements the `ElevatorController` trait.  It decides each
/// elevator's direction based on the nearest destination floor among people on the
/// elevator, then the nearest floor with people waiting.
pub struct NearestController {
    pub building: Building,
    upgradable: bool
}

//Implement the NearestController interface
impl NearestController {
    /// Initialize a new NearestController given a `Building`.
    ///
    /// ## Example
    ///
    /// ```
    /// let my_building: Building = Building::from(
    ///     4_usize,
    ///     2_usize,
    ///     0.5_f64,
    ///     5.0_f64,
    ///     2.5_f64,
    ///     0.5_f64
    /// );
    /// let my_controller: NearestController = NearestController::from(my_building);
    /// ```
    pub fn from(building: Building) -> NearestController {
        //Initialize the controller
        NearestController {
            building: building,
            upgradable: false
        }
    }

    /// Initialize a new NearestController from just a building
    ///
    /// ## Example
    ///
    /// ```
    /// let my_building: Building = Building::from(
    ///     4_usize,
    ///     2_usize,
    ///     0.5_f64,
    ///     5.0_f64,
    ///     2.5_f64,
    ///     0.5_f64
    /// );
    /// let my_controller: NearestController = NearestController::from(my_building);
    /// ```
    pub fn from_building(building: Building) -> NearestController {
        //Initialize the controller
        NearestController {
            building: building,
            upgradable: false
        }
    }
}

//Implement the ElevatorController trait for the NearestController
impl ElevatorController for NearestController {
    /// Get the building belonging to the controller
    fn get_building(&mut self) -> &Building {
        &self.building
    }

    /// Mutably borrow the building belonging to the controller
    fn get_building_mut(&mut self) -> &mut Building {
        &mut self.building
    }

    /// Clone the building belonging to the controller.  Generally used when
    /// swapping controllers.
    fn clone_building(&mut self) -> Building {
        self.building.clone()
    }

    /// Return a boolean signifying whether the controller can be upgraded or
    /// not.  Always returns false, since the NearestController cannot be
    /// upgraded.
    fn can_be_upgraded(&self) -> bool {
        self.upgradable
    }

    /// Upgrade the controller given an incrementation float.  Does nothing for
    /// the NearestController since it cannot be upgraded.
    fn upgrade(&mut self, _incrementation: f64) {}

    /// Decide each elevator's direction based on the nearest destination floor among
    /// people on the elevator, then the nearest floor with people waiting.
    fn update_elevators(&mut self) {
        //Initialize a vector of decisions for the elevators
        let mut elevator_decisions: Vec<usize> = Vec::new();

        //Loop through the elevators in the building
        for elevator in self.building.elevators.iter() {
            //If stopped, check where to go next
            if elevator.stopped {
                //Find the nearest destination floor among people on the elevator
                let (nearest_dest_floor, min_dest_floor_dist): (usize, usize) = elevator.get_nearest_dest_floor();

                //If the nearest dest floor is identified, then update the elevator
                if min_dest_floor_dist != 0_usize {
                    elevator_decisions.push(nearest_dest_floor);
                    continue;
                }

                //Find the nearest waiting floor among people throughout the building
                let (nearest_wait_floor, min_wait_floor_dist): (usize, usize) = self.building.get_nearest_wait_floor(elevator.floor_on);

                //If the nearest wait floor is identified, then update the elevator
                if min_wait_floor_dist != 0_usize {
                    elevator_decisions.push(nearest_wait_floor);
                    continue;
                }
            } else {
                //If moving down and on the bottom floor, then stop
                if !elevator.moving_up && elevator.floor_on == 0_usize {
                    elevator_decisions.push(elevator.floor_on);
                    continue;
                }

                //If moving up and on the top floor, then stop
                if elevator.moving_up && elevator.floor_on == (self.building.floors.len() - 1_usize) {
                    elevator_decisions.push(elevator.floor_on);
                    continue;
                }

                //If there are people waiting on the elevator for the current floor, then stop
                if elevator.are_people_going_to_floor(elevator.floor_on) {
                    elevator_decisions.push(elevator.floor_on);
                    continue;
                }

                //If there are people waiting on the current floor, then stop
                if self.building.are_people_waiting_on_floor(elevator.floor_on) {
                    elevator_decisions.push(elevator.floor_on);
                    continue;
                }
            }

            //If we make it this far without returning, then return the current state
            elevator_decisions.push(elevator.floor_on);
        }

        //Loop through the elevator decisions and update the elevators
        for (i, decision) in elevator_decisions.iter().enumerate() {
            //Update the elevator direction
            self.building.elevators[i].update_direction(*decision);

            //Update the elevator
            let _new_floor_index = self.building.elevators[i].update_floor();
        }
    }
}