8
8
from flow .controllers .routing_controllers import ContinuousRouter
9
9
from flow .controllers .car_following_models import IDMController , \
10
10
OVMController , BCMController , LinearOVM , CFMController
11
+ from flow .controllers import FollowerStopper , PISaturation
11
12
from tests .setup_scripts import ring_road_exp_setup
12
13
import os
13
14
import numpy as np
@@ -23,7 +24,6 @@ class TestCFMController(unittest.TestCase):
23
24
def setUp (self ):
24
25
# add a few vehicles to the network using the requested model
25
26
# also make sure that the input params are what is expected
26
-
27
27
contr_params = {
28
28
"time_delay" : 0 ,
29
29
"k_d" : 1 ,
@@ -456,5 +456,131 @@ def test_static_lane_changer(self):
456
456
self .assertEqual (sum (np .array (lanes )), 0 )
457
457
458
458
459
+ class TestFollowerStopper (unittest .TestCase ):
460
+
461
+ """
462
+ Makes sure that vehicles with a static lane-changing controller do not
463
+ change lanes.
464
+ """
465
+
466
+ def setUp (self ):
467
+ # add a few vehicles to the network using the requested model
468
+ # also make sure that the input params are what is expected
469
+ contr_params = {"v_des" : 7.5 }
470
+
471
+ vehicles = VehicleParams ()
472
+ vehicles .add (
473
+ veh_id = "test_0" ,
474
+ acceleration_controller = (FollowerStopper , contr_params ),
475
+ routing_controller = (ContinuousRouter , {}),
476
+ car_following_params = SumoCarFollowingParams (
477
+ accel = 20 , decel = 5 ),
478
+ num_vehicles = 5 )
479
+
480
+ # create the environment and scenario classes for a ring road
481
+ self .env , scenario = ring_road_exp_setup (vehicles = vehicles )
482
+
483
+ def tearDown (self ):
484
+ # terminate the traci instance
485
+ self .env .terminate ()
486
+
487
+ # free data used by the class
488
+ self .env = None
489
+
490
+ def test_get_action (self ):
491
+ self .env .reset ()
492
+ ids = self .env .k .vehicle .get_ids ()
493
+
494
+ test_headways = [5 , 10 , 15 , 20 , 25 ]
495
+ test_speeds = [5 , 7.5 , 7.5 , 8 , 7 ]
496
+ for i , veh_id in enumerate (ids ):
497
+ self .env .k .vehicle .set_headway (veh_id , test_headways [i ])
498
+ self .env .k .vehicle .test_set_speed (veh_id , test_speeds [i ])
499
+
500
+ requested_accel = [
501
+ self .env .k .vehicle .get_acc_controller (veh_id ).get_action (self .env )
502
+ for veh_id in ids
503
+ ]
504
+
505
+ expected_accel = [- 16.666667 , 0 , 0. , - 5. , 5. ]
506
+
507
+ np .testing .assert_array_almost_equal (requested_accel , expected_accel )
508
+
509
+ def test_find_intersection_dist (self ):
510
+ self .env .reset ()
511
+ ids = self .env .k .vehicle .get_ids ()
512
+
513
+ test_edges = ["" , "center" ]
514
+ for i , veh_id in enumerate (ids ):
515
+ if i < 2 :
516
+ self .env .k .vehicle .test_set_edge (veh_id , test_edges [i ])
517
+
518
+ requested = [
519
+ self .env .k .vehicle .get_acc_controller (
520
+ veh_id ).find_intersection_dist (self .env )
521
+ for veh_id in ids
522
+ ]
523
+
524
+ expected = [- 10 , 0 , 23. , 34.5 , 46. ]
525
+
526
+ np .testing .assert_array_almost_equal (requested , expected )
527
+
528
+ # we also check that the accel value is None when this value is
529
+ # negative
530
+ self .assertIsNone (self .env .k .vehicle .get_acc_controller (
531
+ ids [0 ]).get_action (self .env ))
532
+
533
+
534
+ class TestPISaturation (unittest .TestCase ):
535
+
536
+ """
537
+ Makes sure that vehicles with a static lane-changing controller do not
538
+ change lanes.
539
+ """
540
+
541
+ def setUp (self ):
542
+ # add a few vehicles to the network using the requested model
543
+ # also make sure that the input params are what is expected
544
+ contr_params = {}
545
+
546
+ vehicles = VehicleParams ()
547
+ vehicles .add (
548
+ veh_id = "test_0" ,
549
+ acceleration_controller = (PISaturation , contr_params ),
550
+ routing_controller = (ContinuousRouter , {}),
551
+ car_following_params = SumoCarFollowingParams (
552
+ accel = 20 , decel = 5 ),
553
+ num_vehicles = 5 )
554
+
555
+ # create the environment and scenario classes for a ring road
556
+ self .env , scenario = ring_road_exp_setup (vehicles = vehicles )
557
+
558
+ def tearDown (self ):
559
+ # terminate the traci instance
560
+ self .env .terminate ()
561
+
562
+ # free data used by the class
563
+ self .env = None
564
+
565
+ def test_get_action (self ):
566
+ self .env .reset ()
567
+ ids = self .env .k .vehicle .get_ids ()
568
+
569
+ test_headways = [5 , 10 , 15 , 20 , 25 ]
570
+ test_speeds = [5 , 7.5 , 7.5 , 8 , 7 ]
571
+ for i , veh_id in enumerate (ids ):
572
+ self .env .k .vehicle .set_headway (veh_id , test_headways [i ])
573
+ self .env .k .vehicle .test_set_speed (veh_id , test_speeds [i ])
574
+
575
+ requested_accel = [
576
+ self .env .k .vehicle .get_acc_controller (veh_id ).get_action (self .env )
577
+ for veh_id in ids
578
+ ]
579
+
580
+ expected_accel = [20. , - 36.847826 , - 35.76087 , - 37.173913 , - 31.086957 ]
581
+
582
+ np .testing .assert_array_almost_equal (requested_accel , expected_accel )
583
+
584
+
459
585
if __name__ == '__main__' :
460
586
unittest .main ()
0 commit comments