use alloc::format;
use alloc::vec::Vec;
use crate::{Axiom, Effect, Fact, Operator, State, Task, Variable};
#[derive(Clone, Debug)]
pub struct Rng(u64);
impl Rng {
#[must_use]
pub fn new(seed: u64) -> Self {
Self(seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) | 1)
}
pub fn next_u64(&mut self) -> u64 {
self.0 ^= self.0 >> 12;
self.0 ^= self.0 << 25;
self.0 ^= self.0 >> 27;
self.0.wrapping_mul(0x2545_F491_4F6C_DD1D)
}
pub fn range(&mut self, lo: u64, hi: u64) -> u64 {
assert!(lo <= hi, "empty range {lo}..={hi}");
let x = self.next_u64();
(hi - lo).checked_add(1).map_or(x, |n| lo + x % n)
}
fn range_u32(&mut self, lo: u32, hi: u32) -> u32 {
let x = self.range(u64::from(lo), u64::from(hi));
u32::try_from(x).expect("a draw in u32 bounds fits in u32")
}
fn range_usize(&mut self, lo: usize, hi: usize) -> usize {
let x = self.range(lo as u64, hi as u64);
usize::try_from(x).expect("a draw in usize bounds fits in usize")
}
fn distinct_vars(&mut self, n: usize, k: usize) -> Vec<usize> {
let mut vars: Vec<usize> = (0..n).collect();
for i in 0..k.min(n) {
let j = self.range_usize(i, n - 1);
vars.swap(i, j);
}
vars.truncate(k.min(n));
vars
}
fn fact(&mut self, variables: &[Variable], var: usize) -> Fact {
Fact::new(var, self.range_u32(0, variables[var].domain_size - 1))
}
fn facts(&mut self, variables: &[Variable], n: usize, k: usize) -> Vec<Fact> {
self.distinct_vars(n, k)
.into_iter()
.map(|v| self.fact(variables, v))
.collect()
}
}
#[derive(Clone, Copy, Debug)]
pub struct RandomTaskParams {
pub num_vars: usize,
pub max_domain: u32,
pub num_ops: usize,
pub max_cost: u32,
pub max_conditions: usize,
pub num_derived: usize,
}
impl Default for RandomTaskParams {
fn default() -> Self {
Self {
num_vars: 4,
max_domain: 3,
num_ops: 10,
max_cost: 5,
max_conditions: 0,
num_derived: 0,
}
}
}
#[must_use]
pub fn random_task(seed: u64, p: RandomTaskParams) -> Task {
let mut rng = Rng::new(seed);
let variables: Vec<Variable> = (0..p.num_vars)
.map(|i| Variable::new(format!("v{i}"), rng.range_u32(2, p.max_domain.max(2))))
.collect();
let operators = (0..p.num_ops)
.map(|i| random_operator(&mut rng, &p, &variables, i))
.collect();
let initial = State(
(0..p.num_vars)
.map(|v| rng.fact(&variables, v).value)
.collect(),
);
let n_goal = rng.range_usize(1, 2);
let goal = rng.facts(&variables, p.num_vars, n_goal);
let mut task = Task {
variables,
operators,
initial,
goal,
axioms: Vec::new(),
};
for layer in 0..p.num_derived {
add_derived_variable(&mut rng, &mut task, layer, p.num_ops);
}
task
}
fn random_operator(
rng: &mut Rng,
p: &RandomTaskParams,
variables: &[Variable],
index: usize,
) -> Operator {
let n_pre = rng.range_usize(0, 2);
let n_eff = rng.range_usize(1, 2);
let pre = rng.facts(variables, p.num_vars, n_pre);
let eff = rng
.distinct_vars(p.num_vars, n_eff)
.into_iter()
.map(|v| {
let fact = rng.fact(variables, v);
let conditions = if p.max_conditions == 0 {
Vec::new()
} else {
let n = rng.range_usize(0, p.max_conditions);
rng.facts(variables, p.num_vars, n)
};
Effect { conditions, fact }
})
.collect();
Operator {
name: format!("op{index}"),
pre,
eff,
cost: rng.range_u32(1, p.max_cost.max(1)),
}
}
fn add_derived_variable(rng: &mut Rng, task: &mut Task, layer: usize, num_ops: usize) {
let var = task.variables.len();
for _ in 0..rng.range(1, 2) {
let n = rng.range_usize(1, 2);
let conditions = rng.facts(&task.variables, var, n);
task.axioms.push(Axiom {
conditions,
head: Fact::new(var, 1),
});
}
task.variables.push(Variable {
axiom_layer: Some(u32::try_from(layer).expect("at most u32::MAX axiom layers")),
..Variable::new(format!("d{layer}"), 2)
});
task.initial.0.push(0);
let derived = Fact::new(var, rng.range_u32(0, 1));
if rng.range(0, 1) == 0 {
task.goal.push(derived);
} else {
let op = rng.range_usize(0, num_ops.max(1) - 1);
if let Some(op) = task.operators.get_mut(op) {
op.pre.push(derived);
}
}
}