initial commit
This commit is contained in:
56
src/random_generator.rs
Normal file
56
src/random_generator.rs
Normal file
@@ -0,0 +1,56 @@
|
||||
use petgraph::stable_graph::{StableGraph, NodeIndex};
|
||||
use petgraph::{Directed};
|
||||
use egui_graphs::{to_graph, Graph};
|
||||
use egui::Pos2;
|
||||
use rand::thread_rng;
|
||||
use rand::Rng;
|
||||
use rand::seq::SliceRandom;
|
||||
|
||||
pub struct MaxflowProblem {
|
||||
pub g: Graph<(), u64, Directed, u32, egui_graphs::DefaultNodeShape, egui_graphs::DefaultEdgeShape>,
|
||||
}
|
||||
|
||||
impl MaxflowProblem {
|
||||
pub fn new(num_nodes: u64, max_capacity: u64) -> Self {
|
||||
let mut graph: Graph<_, u64> = Graph::new(StableGraph::default());
|
||||
let mut nodes = Vec::<NodeIndex>::new();
|
||||
let mut rng = thread_rng();
|
||||
|
||||
// generate nodes
|
||||
for _i in 0..num_nodes {
|
||||
let x: f32 = rng.gen_range(0.0..=500.0);
|
||||
let y: f32 = rng.gen_range(0.0..=500.0);
|
||||
let pos = Pos2::new(x,y);
|
||||
let n = graph.add_node_with_label_and_location((), String::new(), pos);
|
||||
nodes.push(n);
|
||||
|
||||
// to achieve a strongly connected graph, every node
|
||||
// should have at least one edge pointing from and to
|
||||
// a node TODO
|
||||
let node1 = nodes.choose(&mut rng).unwrap();
|
||||
let capacity1: u64 = rng.gen_range(1..=max_capacity);
|
||||
// if node1.position() overlaps do sth
|
||||
graph.add_edge_with_label(n, *node1, capacity1, capacity1.to_string());
|
||||
graph.add_edge_with_label(*node1, n, capacity1, capacity1.to_string());
|
||||
}
|
||||
|
||||
// sort node pairs by distance
|
||||
|
||||
// randomly add edges
|
||||
for _i in 0..num_nodes {
|
||||
let node1 = nodes.choose(&mut rng).unwrap();
|
||||
let node2 = nodes.choose(&mut rng).unwrap();
|
||||
let capacity: u64 = rng.gen_range(1..=max_capacity);
|
||||
//graph.add_edge_with_label(*node1, *node2, capacity, capacity.to_string());
|
||||
}
|
||||
|
||||
//let widget_graph: Graph<_, _, _, _, egui_graphs::DefaultNodeShape, egui_graphs::DefaultEdgeShape> = to_graph(&graph);
|
||||
Self { g: graph }
|
||||
}
|
||||
|
||||
fn distance(a: Pos2, b: Pos2) -> f32 {
|
||||
let delta_x = a.x - b.x;
|
||||
let delta_y = a.y - b.y;
|
||||
return f32::sqrt(delta_x.powf(2.) + delta_y.powf(2.));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user