use alloc::vec::Vec;
use crate::graph::{Graph, ReverseGraph};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BenchmarkGraph<const N: usize> {
out: Vec<Vec<(u32, [u32; N])>>,
into: Vec<Vec<(u32, [u32; N])>>,
}
pub type Query = (u32, u32);
impl<const N: usize> BenchmarkGraph<N> {
#[must_use]
pub fn from_arcs(nodes: u32, arcs: &[(u32, u32, [u32; N])]) -> Self {
let n = usize::try_from(nodes).expect("node counts fit in usize");
let mut graph = Self {
out: alloc::vec![Vec::new(); n],
into: alloc::vec![Vec::new(); n],
};
for &(from, to, cost) in arcs {
graph.out[index(from)].push((to, cost));
graph.into[index(to)].push((from, cost));
}
graph
}
#[must_use]
pub fn node_count(&self) -> u32 {
u32::try_from(self.out.len()).expect("built from a u32 count")
}
#[must_use]
pub fn arc_count(&self) -> usize {
self.out.iter().map(Vec::len).sum()
}
pub fn arcs(&self) -> impl Iterator<Item = (u32, u32, [u32; N])> + '_ {
self.out.iter().enumerate().flat_map(|(u, edges)| {
let u = u32::try_from(u).expect("built from a u32 count");
edges.iter().map(move |&(v, c)| (u, v, c))
})
}
#[must_use]
pub fn project<const M: usize>(&self, objectives: [usize; M]) -> BenchmarkGraph<M> {
let pick = |c: [u32; N]| objectives.map(|i| c[i]);
let arcs: Vec<_> = self.arcs().map(|(u, v, c)| (u, v, pick(c))).collect();
BenchmarkGraph::from_arcs(self.node_count(), &arcs)
}
}
impl<const N: usize> Graph for BenchmarkGraph<N> {
type State = u32;
type Weight = [u32; N];
fn successors(&self, state: &u32, out: &mut Vec<(u32, [u32; N])>) {
out.extend_from_slice(&self.out[index(*state)]);
}
}
impl<const N: usize> ReverseGraph for BenchmarkGraph<N> {
fn predecessors(&self, state: &u32, out: &mut Vec<(u32, [u32; N])>) {
out.extend_from_slice(&self.into[index(*state)]);
}
}
fn index(state: u32) -> usize {
usize::try_from(state).expect("u32 fits in usize")
}
#[derive(Debug, Clone)]
struct Rng(u64);
impl Rng {
fn next(&mut self) -> u64 {
self.0 = self.0.wrapping_add(0x9e37_79b9_7f4a_7c15);
let mut z = self.0;
z = (z ^ (z >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
z = (z ^ (z >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
z ^ (z >> 31)
}
fn range(&mut self, low: u32, high: u32) -> u32 {
let span = u64::from(high - low) + 1;
low + u32::try_from(self.next() % span).expect("below the span")
}
}
#[must_use]
pub fn random_grid<const N: usize>(
width: u32,
height: u32,
seed: u64,
) -> (BenchmarkGraph<N>, Query) {
assert!(width > 0 && height > 0, "a grid needs at least one cell");
let cells = width.checked_mul(height).expect("the grid fits u32 states");
let (source, target) = (cells, cells + 1);
let mut rng = Rng(seed);
let mut cost = || core::array::from_fn(|_| rng.range(1, 10));
let mut arcs = Vec::new();
for y in 0..height {
for x in 0..width {
let cell = y * width + x;
if x + 1 < width {
arcs.push((cell, cell + 1, cost()));
arcs.push((cell + 1, cell, cost()));
}
if y + 1 < height {
arcs.push((cell, cell + width, cost()));
arcs.push((cell + width, cell, cost()));
}
}
arcs.push((source, y * width, cost()));
arcs.push((y * width + width - 1, target, cost()));
}
(
BenchmarkGraph::from_arcs(cells + 2, &arcs),
(source, target),
)
}
#[must_use]
pub fn netmaker(
nodes: u32,
out_min: u32,
out_max: u32,
locality: u32,
seed: u64,
) -> BenchmarkGraph<3> {
assert!(
nodes >= 2 && out_min < out_max,
"`NetMaker` needs two states and out_min < out_max"
);
let mut rng = Rng(seed);
let mut order: Vec<u32> = (0..nodes).collect();
for i in (1..order.len()).rev() {
let j = index(rng.range(0, u32::try_from(i).expect("below nodes")));
order.swap(i, j);
}
let mut out: Vec<Vec<(u32, [u32; 3])>> = alloc::vec![Vec::new(); index(nodes)];
for k in 0..order.len() {
let (u, v) = (order[k], order[(k + 1) % order.len()]);
let mut bands = [(1, 333), (334, 666), (667, 1000)];
for i in (1..3).rev() {
let j = index(rng.range(0, u32::try_from(i).expect("below 3")));
bands.swap(i, j);
}
out[index(u)].push((v, bands.map(|(low, high)| rng.range(low, high))));
}
for u in 0..nodes {
let degree = rng.range(out_min + 1, out_max);
let low = u.saturating_sub(locality / 2);
let high = (u + locality / 2).min(nodes - 1);
let mut free: Vec<u32> = (low..=high)
.filter(|&v| v != u && out[index(u)].iter().all(|e| e.0 != v))
.collect();
while out[index(u)].len() < index(degree) && !free.is_empty() {
let pick = index(rng.range(0, u32::try_from(free.len() - 1).expect("small")));
let v = free.swap_remove(pick);
out[index(u)].push((v, core::array::from_fn(|_| rng.range(1, 99))));
}
}
let arcs: Vec<_> = out
.iter()
.enumerate()
.flat_map(|(u, edges)| {
edges
.iter()
.map(move |&(v, c)| (u32::try_from(u).expect("below nodes"), v, c))
})
.collect();
BenchmarkGraph::from_arcs(nodes, &arcs)
}
#[must_use]
pub fn netmaker_queries(nodes: u32, count: usize, seed: u64) -> Vec<Query> {
assert!(nodes >= 10, "queries need at least ten states");
let tenth = nodes / 10;
let mut rng = Rng(seed);
(0..count)
.map(|_| (rng.range(0, tenth - 1), rng.range(nodes - tenth, nodes - 1)))
.collect()
}
#[must_use]
pub fn guards_grid(map: &str, guards: &[(u32, u32)]) -> Option<BenchmarkGraph<2>> {
let (width, height, open) = parse_map(map)?;
let passable = |x: i64, y: i64| {
x >= 0 && y >= 0 && x < i64::from(width) && y < i64::from(height) && open[cell(width, x, y)]
};
let exposure: Vec<u32> = (0..height)
.flat_map(|y| (0..width).map(move |x| (x, y)))
.map(|(x, y)| {
let seen = guards
.iter()
.filter(|&&(gx, gy)| {
sees(
&passable,
(i64::from(gx), i64::from(gy)),
(i64::from(x), i64::from(y)),
)
})
.count();
u32::try_from(seen).expect("guard counts fit")
})
.collect();
let mut arcs = Vec::new();
for y in 0..i64::from(height) {
for x in 0..i64::from(width) {
if !passable(x, y) {
continue;
}
for (dx, dy) in [
(1, 0),
(-1, 0),
(0, 1),
(0, -1),
(1, 1),
(1, -1),
(-1, 1),
(-1, -1),
] {
let (tx, ty) = (x + dx, y + dy);
if !passable(tx, ty) {
continue;
}
let diagonal = dx != 0 && dy != 0;
let risk = if diagonal {
if !passable(x + dx, y) || !passable(x, y + dy) {
continue;
}
exposure[cell(width, tx, ty)]
.max(exposure[cell(width, x + dx, y)])
.max(exposure[cell(width, x, y + dy)])
} else {
exposure[cell(width, tx, ty)]
};
let id = |x: i64, y: i64| u32::try_from(cell(width, x, y)).expect("cells fit u32");
arcs.push((id(x, y), id(tx, ty), [if diagonal { 14 } else { 10 }, risk]));
}
}
}
Some(BenchmarkGraph::from_arcs(width.checked_mul(height)?, &arcs))
}
fn cell(width: u32, x: i64, y: i64) -> usize {
usize::try_from(y * i64::from(width) + x).expect("a cell on the map")
}
fn parse_map(map: &str) -> Option<(u32, u32, Vec<bool>)> {
let mut lines = map.lines();
let (mut width, mut height) = (None, None);
for line in lines.by_ref() {
let mut words = line.split_whitespace();
match (words.next(), words.next()) {
(Some("height"), Some(h)) => height = h.parse().ok(),
(Some("width"), Some(w)) => width = w.parse().ok(),
(Some("map"), None) => break,
_ => {}
}
}
let (width, height): (u32, u32) = (width?, height?);
let mut open = Vec::new();
for _ in 0..height {
let row = lines.next()?;
if row.chars().count() != index(width) {
return None;
}
open.extend(row.chars().map(|c| matches!(c, '.' | 'G' | 'S')));
}
Some((width, height, open))
}
fn sees(passable: &impl Fn(i64, i64) -> bool, from: (i64, i64), to: (i64, i64)) -> bool {
let (mut x, mut y) = from;
let (dx, dy) = ((to.0 - x).abs(), -(to.1 - y).abs());
let (sx, sy) = (if x < to.0 { 1 } else { -1 }, if y < to.1 { 1 } else { -1 });
let mut err = dx + dy;
loop {
if !passable(x, y) {
return false;
}
if (x, y) == to {
return true;
}
let e2 = 2 * err;
if e2 >= dy {
err += dy;
x += sx;
}
if e2 <= dx {
err += dx;
y += sy;
}
}
}
#[must_use]
pub fn road_extended(
nodes: u32,
arcs: &[(u32, u32, [u32; 2])],
elevation: &[i32],
) -> BenchmarkGraph<5> {
let mut degree = alloc::vec![0_u32; index(nodes)];
for &(u, _, _) in arcs {
degree[index(u)] += 1;
}
let extended: Vec<_> = arcs
.iter()
.map(|&(u, v, [d, t])| {
let rise = elevation[index(u)].abs_diff(elevation[index(v)]);
(u, v, [d, t, rise, degree[index(u)] + degree[index(v)], 1])
})
.collect();
BenchmarkGraph::from_arcs(nodes, &extended)
}