Skip to content

Commit

Permalink
sim-rs: improve topology generation
Browse files Browse the repository at this point in the history
  • Loading branch information
SupernaviX committed Dec 19, 2024
1 parent 4ee0c3f commit 5bcd542
Show file tree
Hide file tree
Showing 6 changed files with 18,661 additions and 13,112 deletions.
8 changes: 4 additions & 4 deletions sim-rs/sim-cli/src/bin/gen-test-data/strategy/globe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ pub fn globe(args: &GlobeArgs) -> Result<RawConfig> {
}

println!("generating edges...");
let alpha = 0.15;
let beta = 0.2;
let max_distance = distance((0.0, 90.0), (90.0, 180.0));
let alpha = 0.05;
let beta = 0.2 * (500.0 / args.node_count as f64).min(1.0);
let max_distance = distance((-90.0, 90.0), (90.0, 180.0));
for from in 0..args.node_count {
// stake pools don't connect directly to each other
let first_candidate_connection = if from < args.stake_pool_count {
Expand All @@ -125,7 +125,7 @@ pub fn globe(args: &GlobeArgs) -> Result<RawConfig> {
}
// nodes are connected probabilistically, based on how far apart they are
let dist = distance(nodes[from].location, nodes[to].location);
let probability = alpha * (-dist / (beta * max_distance)).exp();
let probability = beta * (-dist / (alpha * max_distance)).exp();
if rng.gen_bool(probability) {
links.add(from, to, None);
}
Expand Down
15 changes: 4 additions & 11 deletions sim-rs/sim-cli/src/bin/gen-test-data/strategy/random_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,14 @@ use clap::Parser;
use rand::{seq::SliceRandom as _, thread_rng, Rng as _};
use sim_core::config::{RawConfig, RawNodeConfig};

use crate::strategy::utils::{distribute_stake, generate_full_config, LinkTracker};
use crate::strategy::utils::{distance, distribute_stake, generate_full_config, LinkTracker};

#[derive(Debug, Parser)]
pub struct RandomGraphArgs {
node_count: usize,
stake_pool_count: usize,
}

fn distance((lat1, long1): (f64, f64), (lat2, long2): (f64, f64)) -> f64 {
// euclidean distance probably good enough
let dist_x = (lat2 - lat1).rem_euclid(180.0);
let dist_y = (long2 - long1).rem_euclid(180.0);
(dist_x.powi(2) + dist_y.powi(2)).sqrt()
}

pub fn random_graph(args: &RandomGraphArgs) -> Result<RawConfig> {
if args.stake_pool_count >= args.node_count {
bail!("At least one node must not be a stake pool");
Expand All @@ -43,8 +36,8 @@ pub fn random_graph(args: &RandomGraphArgs) -> Result<RawConfig> {

println!("generating edges...");
let alpha = 0.15;
let beta = 0.2;
let max_distance = distance((0.0, 90.0), (90.0, 180.0));
let beta = 0.25 * (100.0 / args.node_count as f64).min(1.0);
let max_distance = distance((-90.0, 90.0), (90.0, 180.0));
for from in 0..args.node_count {
// stake pools don't connect directly to each other
let first_candidate_connection = if from < args.stake_pool_count {
Expand All @@ -59,7 +52,7 @@ pub fn random_graph(args: &RandomGraphArgs) -> Result<RawConfig> {
}
// nodes are connected probabilistically, based on how far apart they are
let dist = distance(nodes[from].location, nodes[to].location);
let probability = alpha * (-dist / (beta * max_distance)).exp();
let probability = beta * (-dist / (alpha * max_distance)).exp();
if rng.gen_bool(probability) {
links.add(from, to, None);
}
Expand Down
6 changes: 3 additions & 3 deletions sim-rs/sim-cli/src/bin/gen-test-data/strategy/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ pub fn distribute_stake(stake_pool_count: usize) -> Result<Vec<u64>> {

pub fn distance((lat1, long1): (f64, f64), (lat2, long2): (f64, f64)) -> f64 {
// euclidean distance probably good enough
let dist_x = (lat2 - lat1).rem_euclid(180.0);
let dist_y = (long2 - long1).rem_euclid(180.0);
(dist_x.powi(2) + dist_y.powi(2)).sqrt()
let dist_lat = (lat2 - lat1).abs();
let dist_long = (long2 - long1).abs().min((long2 - long1 + 180.0).abs());
(dist_lat.powi(2) + dist_long.powi(2)).sqrt()
}

pub fn generate_full_config(nodes: Vec<RawNodeConfig>, links: Vec<RawLinkConfig>) -> RawConfig {
Expand Down
Loading

0 comments on commit 5bcd542

Please sign in to comment.