mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-12-01 02:33:54 +03:00
fixing sim serialization after TurnID became a tuple struct
This commit is contained in:
parent
cce85994f6
commit
af6b7c08ac
@ -7,6 +7,7 @@ use geom::{Angle, Pt2D};
|
|||||||
use intersections::{IntersectionPolicy, StopSign, TrafficSignal};
|
use intersections::{IntersectionPolicy, StopSign, TrafficSignal};
|
||||||
use map_model::{LaneID, LaneType, Map, TurnID};
|
use map_model::{LaneID, LaneType, Map, TurnID};
|
||||||
use multimap::MultiMap;
|
use multimap::MultiMap;
|
||||||
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||||
use std;
|
use std;
|
||||||
use std::collections::{BTreeMap, HashSet, VecDeque};
|
use std::collections::{BTreeMap, HashSet, VecDeque};
|
||||||
use std::f64;
|
use std::f64;
|
||||||
@ -214,10 +215,33 @@ pub struct DrivingSimState {
|
|||||||
// Using BTreeMap instead of HashMap so iteration is deterministic.
|
// Using BTreeMap instead of HashMap so iteration is deterministic.
|
||||||
pub(crate) cars: BTreeMap<CarID, Car>,
|
pub(crate) cars: BTreeMap<CarID, Car>,
|
||||||
lanes: Vec<SimQueue>,
|
lanes: Vec<SimQueue>,
|
||||||
|
// See https://github.com/serde-rs/json/issues/402.
|
||||||
|
#[serde(serialize_with = "serialize_turn_map")]
|
||||||
|
#[serde(deserialize_with = "deserialize_turn_map")]
|
||||||
turns: BTreeMap<TurnID, SimQueue>,
|
turns: BTreeMap<TurnID, SimQueue>,
|
||||||
intersections: Vec<IntersectionPolicy>,
|
intersections: Vec<IntersectionPolicy>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn serialize_turn_map<S: Serializer>(
|
||||||
|
map: &BTreeMap<TurnID, SimQueue>,
|
||||||
|
s: S,
|
||||||
|
) -> Result<S::Ok, S::Error> {
|
||||||
|
map.iter()
|
||||||
|
.map(|(a, b)| (a.clone(), b.clone()))
|
||||||
|
.collect::<Vec<(_, _)>>()
|
||||||
|
.serialize(s)
|
||||||
|
}
|
||||||
|
fn deserialize_turn_map<'de, D: Deserializer<'de>>(
|
||||||
|
d: D,
|
||||||
|
) -> Result<BTreeMap<TurnID, SimQueue>, D::Error> {
|
||||||
|
let vec = <Vec<(TurnID, SimQueue)>>::deserialize(d)?;
|
||||||
|
let mut map = BTreeMap::new();
|
||||||
|
for (k, v) in vec {
|
||||||
|
map.insert(k, v);
|
||||||
|
}
|
||||||
|
Ok(map)
|
||||||
|
}
|
||||||
|
|
||||||
impl DrivingSimState {
|
impl DrivingSimState {
|
||||||
pub fn new(map: &Map) -> DrivingSimState {
|
pub fn new(map: &Map) -> DrivingSimState {
|
||||||
let mut intersections: Vec<IntersectionPolicy> = Vec::new();
|
let mut intersections: Vec<IntersectionPolicy> = Vec::new();
|
||||||
|
@ -3,6 +3,7 @@ use dimensioned::si;
|
|||||||
use draw_ped::DrawPedestrian;
|
use draw_ped::DrawPedestrian;
|
||||||
use map_model::{Lane, LaneID, Map, Turn, TurnID};
|
use map_model::{Lane, LaneID, Map, Turn, TurnID};
|
||||||
use multimap::MultiMap;
|
use multimap::MultiMap;
|
||||||
|
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||||
use std;
|
use std;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use {On, PedestrianID};
|
use {On, PedestrianID};
|
||||||
@ -105,11 +106,34 @@ impl Pedestrian {
|
|||||||
pub struct WalkingSimState {
|
pub struct WalkingSimState {
|
||||||
// Trying a different style than driving for storing things
|
// Trying a different style than driving for storing things
|
||||||
peds_per_sidewalk: MultiMap<LaneID, Pedestrian>,
|
peds_per_sidewalk: MultiMap<LaneID, Pedestrian>,
|
||||||
|
#[serde(serialize_with = "serialize_turn_map")]
|
||||||
|
#[serde(deserialize_with = "deserialize_turn_map")]
|
||||||
peds_per_turn: MultiMap<TurnID, Pedestrian>,
|
peds_per_turn: MultiMap<TurnID, Pedestrian>,
|
||||||
|
|
||||||
id_counter: usize,
|
id_counter: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn serialize_turn_map<S: Serializer>(
|
||||||
|
map: &MultiMap<TurnID, Pedestrian>,
|
||||||
|
s: S,
|
||||||
|
) -> Result<S::Ok, S::Error> {
|
||||||
|
// TODO maybe need to sort by TurnID to have deterministic output
|
||||||
|
map.iter()
|
||||||
|
.map(|(a, b)| (a.clone(), b.clone()))
|
||||||
|
.collect::<Vec<(_, _)>>()
|
||||||
|
.serialize(s)
|
||||||
|
}
|
||||||
|
fn deserialize_turn_map<'de, D: Deserializer<'de>>(
|
||||||
|
d: D,
|
||||||
|
) -> Result<MultiMap<TurnID, Pedestrian>, D::Error> {
|
||||||
|
let vec = <Vec<(TurnID, Pedestrian)>>::deserialize(d)?;
|
||||||
|
let mut map = MultiMap::new();
|
||||||
|
for (k, v) in vec {
|
||||||
|
map.insert(k, v);
|
||||||
|
}
|
||||||
|
Ok(map)
|
||||||
|
}
|
||||||
|
|
||||||
impl WalkingSimState {
|
impl WalkingSimState {
|
||||||
pub fn new() -> WalkingSimState {
|
pub fn new() -> WalkingSimState {
|
||||||
WalkingSimState {
|
WalkingSimState {
|
||||||
|
@ -3,6 +3,16 @@ extern crate control;
|
|||||||
extern crate map_model;
|
extern crate map_model;
|
||||||
extern crate sim;
|
extern crate sim;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serialization() {
|
||||||
|
// This assumes this map has been built
|
||||||
|
let input = "../data/small.abst";
|
||||||
|
|
||||||
|
let map = map_model::Map::new(input, &map_model::Edits::new()).expect("Couldn't load map");
|
||||||
|
let sim = sim::Sim::new(&map, Some(42));
|
||||||
|
abstutil::write_json("/tmp/sim_state.json", &sim).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_scratch() {
|
fn from_scratch() {
|
||||||
// This assumes this map has been built
|
// This assumes this map has been built
|
||||||
@ -11,8 +21,6 @@ fn from_scratch() {
|
|||||||
let spawn_count = 1000;
|
let spawn_count = 1000;
|
||||||
|
|
||||||
println!("Creating two simulations");
|
println!("Creating two simulations");
|
||||||
// TODO bundle all of the layers of the map together in some super-struct, so this
|
|
||||||
// initialization and plumbing is easier
|
|
||||||
let map = map_model::Map::new(input, &map_model::Edits::new()).expect("Couldn't load map");
|
let map = map_model::Map::new(input, &map_model::Edits::new()).expect("Couldn't load map");
|
||||||
let control_map = control::ControlMap::new(&map);
|
let control_map = control::ControlMap::new(&map);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user