Skip to content

Commit

Permalink
Merge pull request #127 from Janekdererste/node-weight
Browse files Browse the repository at this point in the history
Node weight
  • Loading branch information
Janekdererste authored Mar 21, 2024
2 parents 43efaf8 + 9b3e99e commit d82ce88
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 31 deletions.
33 changes: 32 additions & 1 deletion src/bin/convert_to_binary.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::path::PathBuf;

use ahash::HashMapExt;
use clap::Parser;
use nohash_hasher::IntMap;
use tracing::info;

use rust_q_sim::simulation::config::PartitionMethod;
use rust_q_sim::simulation::network::global_network::Network;
Expand All @@ -25,10 +28,13 @@ fn main() {
rust_q_sim::simulation::logging::init_std_out_logging();
let args = InputArgs::parse();

let net = Network::from_file_path(&args.network, 1, PartitionMethod::None);
let mut net = Network::from_file_path(&args.network, 1, PartitionMethod::None);
let mut veh = Garage::from_file(&args.vehicles);
let pop = Population::from_file(&args.population, &mut veh);

let cmp_weights = compute_computational_weights(&pop);
assign_computational_weights(&mut net, cmp_weights);

rust_q_sim::simulation::id::store_to_file(&create_file_path(&args, "ids"));
net.to_file(&create_file_path(&args, "network"));
veh.to_file(&create_file_path(&args, "vehicles"));
Expand All @@ -39,3 +45,28 @@ fn create_file_path(args: &InputArgs, extension: &str) -> PathBuf {
args.output_dir
.join(format!("{}.{}.binpb", args.run_id, extension))
}

fn compute_computational_weights(pop: &Population) -> IntMap<u64, u32> {
info!("Computing computational weights based on routes in plans file");
let result: IntMap<u64, u32> = pop
.persons
.values()
.flat_map(|p| p.plan.as_ref().unwrap().legs.iter())
.flat_map(|leg| leg.route.as_ref().unwrap().route.iter())
.fold(IntMap::new(), |mut map, link_id| {
map.entry(*link_id)
.and_modify(|counter| *counter += 1)
.or_insert(1u32);
map
});
info!("Finished computing computational weights");
result
}

fn assign_computational_weights(net: &mut Network, cmp_weights: IntMap<u64, u32>) {
for (link_id, weight) in cmp_weights {
let link = &net.links[link_id as usize];
let node = &mut net.nodes[link.to.internal() as usize];
node.cmp_weight = weight;
}
}
3 changes: 2 additions & 1 deletion src/simulation/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ pub enum VertexWeight {
InLinkCapacity,
InLinkCount,
Constant,
PreComputed,
}

#[derive(PartialEq, Debug, ValueEnum, Clone, Copy, Serialize, Deserialize)]
Expand Down Expand Up @@ -317,7 +318,7 @@ impl MetisOptions {

pub fn ufactor(&self) -> usize {
let val = (self.imbalance_factor * 1000.) as usize;
if val <= 0 {
if val == 0 {
return 1;
};
val
Expand Down
5 changes: 5 additions & 0 deletions src/simulation/id/id_store.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fs;
use std::fs::File;
use std::io::{BufReader, BufWriter, Cursor, Read, Seek, Write};
use std::path::Path;
Expand Down Expand Up @@ -25,7 +26,11 @@ enum IdCompression {

fn serialize_to_file(store: &IdStore, file_path: &Path, compression: IdCompression) {
info!("Starting writing IdStore to file {file_path:?}");
// Create the file and all necessary directories
let prefix = file_path.parent().unwrap();
fs::create_dir_all(prefix).unwrap();
let file = File::create(file_path).unwrap();

let mut file_writer = BufWriter::new(file);
serialize(store, &mut file_writer, compression);
info!("Finished writing IdStore to file {file_path:?}");
Expand Down
2 changes: 1 addition & 1 deletion src/simulation/messaging/communication/message_broker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ mod tests {
}

fn create_node(id: u64, partition: u32) -> Node {
let node = Node::new(Id::new_internal(id), 0., 0., partition);
let node = Node::new(Id::new_internal(id), 0., 0., partition, 1);
node
}

Expand Down
22 changes: 12 additions & 10 deletions src/simulation/network/global_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Node {
pub in_links: Vec<Id<Link>>,
pub out_links: Vec<Id<Link>>,
pub partition: u32,
pub cmp_weight: u32,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -159,14 +160,15 @@ impl Network {
}

impl Node {
pub fn new(id: Id<Node>, x: f64, y: f64, part: u32) -> Self {
pub fn new(id: Id<Node>, x: f64, y: f64, part: u32, cmp_weight: u32) -> Self {
Node {
id,
x,
y,
in_links: Vec::new(),
out_links: Vec::new(),
partition: part,
cmp_weight,
}
}
}
Expand Down Expand Up @@ -229,7 +231,7 @@ mod tests {
fn add_node() {
let mut network = Network::new();
let id = Id::create("node-id");
let node = Node::new(id.clone(), 1., 1., 0);
let node = Node::new(id.clone(), 1., 1., 0, 1);

assert_eq!(0, network.nodes.len());
network.add_node(node);
Expand All @@ -242,8 +244,8 @@ mod tests {
fn add_node_reject_duplicate() {
let mut network = Network::new();
let id = Id::create("node-id");
let node = Node::new(id.clone(), 1., 1., 0);
let duplicate = Node::new(id.clone(), 2., 2., 0);
let node = Node::new(id.clone(), 1., 1., 0, 1);
let duplicate = Node::new(id.clone(), 2., 2., 0, 1);

assert_eq!(0, network.nodes.len());
network.add_node(node);
Expand All @@ -253,8 +255,8 @@ mod tests {
#[test]
fn add_link() {
let mut network = Network::new();
let from = Node::new(Id::create("from"), 0., 0., 0);
let to = Node::new(Id::create("to"), 3., 4., 0);
let from = Node::new(Id::create("from"), 0., 0., 0, 1);
let to = Node::new(Id::create("to"), 3., 4., 0, 1);
let id = Id::create("link-id");
let link = Link::new_with_default(id.clone(), &from, &to);

Expand Down Expand Up @@ -283,8 +285,8 @@ mod tests {
#[should_panic]
fn add_link_reject_duplicate() {
let mut network = Network::new();
let from = Node::new(Id::create("from"), 0., 0., 0);
let to = Node::new(Id::create("to"), 3., 4., 0);
let from = Node::new(Id::create("from"), 0., 0., 0, 1);
let to = Node::new(Id::create("to"), 3., 4., 0, 1);
let id = Id::create("link-id");
let link = Link::new_with_default(id.clone(), &from, &to);
let duplicate = Link::new_with_default(id.clone(), &from, &to);
Expand Down Expand Up @@ -340,8 +342,8 @@ mod tests {

#[test]
fn link_new_with_default() {
let from = Node::new(Id::create("from"), 0., 0., 0);
let to = Node::new(Id::create("to"), 3., 4., 0);
let from = Node::new(Id::create("from"), 0., 0., 0, 1);
let to = Node::new(Id::create("to"), 3., 4., 0, 1);
let id = Id::create("link-id");
let link = Link::new_with_default(id.clone(), &from, &to);

Expand Down
24 changes: 17 additions & 7 deletions src/simulation/network/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,18 @@ fn write_to_xml(network: &Network, path: &Path) {

for node in &network.nodes {
let attributes = Attrs {
attributes: vec![Attr {
name: String::from("partition"),
value: node.partition.to_string(),
class: String::from("java.lang.Integer"),
}],
attributes: vec![
Attr {
name: "partition".to_string(),
value: node.partition.to_string(),
class: "java.lang.Integer".to_string(),
},
Attr {
name: "cmp_weight".to_string(),
class: "java.lang.Integer".to_string(),
value: node.cmp_weight.to_string(),
},
],
};
let io_node = IONode {
id: node.id.external().to_string(),
Expand Down Expand Up @@ -108,7 +115,7 @@ fn load_from_proto(path: &Path) -> Network {
let mut result = Network::new();
result.effective_cell_size = wire_net.effective_cell_size;
for wn in &wire_net.nodes {
let node = Node::new(Id::get(wn.id), wn.x, wn.y, wn.partition);
let node = Node::new(Id::get(wn.id), wn.x, wn.y, wn.partition, wn.cmp_weight);
result.add_node(node);
}
for wl in &wire_net.links {
Expand Down Expand Up @@ -141,6 +148,7 @@ fn write_to_proto(network: &Network, path: &Path) {
x: n.x,
y: n.y,
partition: n.partition,
cmp_weight: n.cmp_weight,
})
.collect();
let links: Vec<_> = network
Expand Down Expand Up @@ -277,9 +285,11 @@ impl IONetwork {
fn add_io_node(network: &mut Network, io_node: &IONode) {
let id = Id::create(&io_node.id);
let part_attr = Attrs::find_or_else_opt(&io_node.attributes, "partition", || "0");
let cmp_weight_attr = Attrs::find_or_else_opt(&io_node.attributes, "cmp_weight", || "1");
let partition = u32::from_str(part_attr).unwrap();
let cmp_weight = u32::from_str(cmp_weight_attr).unwrap();

let mut node = Node::new(id, io_node.x, io_node.y, partition);
let mut node = Node::new(id, io_node.x, io_node.y, partition, cmp_weight);
node.partition = partition;
network.add_node(node);
}
Expand Down
7 changes: 5 additions & 2 deletions src/simulation/network/metis_partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ fn add_vwgt(network: &Network, options: &MetisOptions, vwgt: &mut Vec<Idx>, node
VertexWeight::Constant => {
vwgt.push(1);
}
VertexWeight::PreComputed => {
vwgt.push(node.cmp_weight as Idx);
}
}
}
}
Expand All @@ -102,8 +105,8 @@ mod tests {
let mut net = Network::new();
let from_id = Id::create("from");
let to_id = Id::create("to");
net.add_node(Node::new(from_id.clone(), 0., 0., 0));
net.add_node(Node::new(to_id.clone(), 100., 0., 0));
net.add_node(Node::new(from_id.clone(), 0., 0., 0, 1));
net.add_node(Node::new(to_id.clone(), 100., 0., 0, 1));
let link_id = Id::create("link");
net.add_link(Link::new_with_default(
link_id,
Expand Down
19 changes: 10 additions & 9 deletions src/simulation/network/sim_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ mod tests {
in_links: vec![],
out_links: vec![],
partition: 0,
cmp_weight: 1,
};
let node2 = Node {
id: Id::new_internal(1),
Expand Down Expand Up @@ -837,13 +838,13 @@ mod tests {
#[test]
fn neighbors() {
let mut net = Network::new();
let node = Node::new(Id::create("node-1"), -0., 0., 0);
let node_1_1 = Node::new(Id::create("node-1-1"), -0., 0., 1);
let node_1_2 = Node::new(Id::create("node-1-2"), -0., 0., 1);
let node = Node::new(Id::create("node-1"), -0., 0., 0, 1);
let node_1_1 = Node::new(Id::create("node-1-1"), -0., 0., 1, 1);
let node_1_2 = Node::new(Id::create("node-1-2"), -0., 0., 1, 1);

let node_2_1 = Node::new(Id::create("node-2-1"), -0., 0., 2);
let node_3_1 = Node::new(Id::create("node-3-1"), -0., 0., 3);
let node_4_1 = Node::new(Id::create("not-a-neighbor"), 0., 0., 4);
let node_2_1 = Node::new(Id::create("node-2-1"), -0., 0., 2, 1);
let node_3_1 = Node::new(Id::create("node-3-1"), -0., 0., 3, 1);
let node_4_1 = Node::new(Id::create("not-a-neighbor"), 0., 0., 4, 1);

// create in links from partitions 1 and 2. 2 incoming links from partition 1, one incoming from
// partition 2
Expand Down Expand Up @@ -880,9 +881,9 @@ mod tests {
}

fn init_three_node_network(network: &mut Network) {
let node1 = Node::new(Id::create("node-1"), -100., 0., 0);
let node2 = Node::new(Id::create("node-2"), 0., 0., 0);
let node3 = Node::new(Id::create("node-3"), 100., 0., 0);
let node1 = Node::new(Id::create("node-1"), -100., 0., 0, 1);
let node2 = Node::new(Id::create("node-2"), 0., 0., 0, 1);
let node3 = Node::new(Id::create("node-3"), 100., 0., 0, 1);
let mut link1 = Link::new_with_default(Id::create("link-1"), &node1, &node2);
link1.capacity = 3600.;
link1.freespeed = 10.;
Expand Down
1 change: 1 addition & 0 deletions src/simulation/wire_types/network.proto
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ message Node {
double x = 2;
double y = 3;
uint32 partition = 4;
uint32 cmpWeight = 5;
}

message Link {
Expand Down

0 comments on commit d82ce88

Please sign in to comment.