first steps towards linkplanning

This commit is contained in:
Alain Emilia Anna Zscheile 2024-07-23 23:02:42 +02:00
parent 1ee85e4523
commit 783d60b117
8 changed files with 498 additions and 11 deletions

204
Cargo.lock generated
View file

@ -2,6 +2,55 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "anstream"
version = "0.6.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b"
[[package]]
name = "anstyle-parse"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19"
dependencies = [
"anstyle",
"windows-sys",
]
[[package]]
name = "approx"
version = "0.5.1"
@ -71,6 +120,64 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "4.5.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f6b81fb3c84f5563d509c59b5a48d935f689e993afa90fe39047f05adef9142"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca6706fd5224857d9ac5eb9355f6683563cc0541c7cd9d014043b57cbec78ac"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bac35c6dafb060fd4d275d9a4ffae97917c13a6327903a8be2153cd964f7085"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70"
[[package]]
name = "colorchoice"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422"
[[package]]
name = "console"
version = "0.15.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb"
dependencies = [
"encode_unicode",
"lazy_static",
"libc",
"windows-sys",
]
[[package]]
name = "crc"
version = "2.1.0"
@ -146,6 +253,18 @@ version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
[[package]]
name = "encode_unicode"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]]
name = "equivalent"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "errno"
version = "0.3.9"
@ -162,6 +281,12 @@ version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a"
[[package]]
name = "fixedbitset"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80"
[[package]]
name = "flacenc"
version = "0.4.0"
@ -216,6 +341,12 @@ dependencies = [
"byteorder",
]
[[package]]
name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]]
name = "heapless"
version = "0.8.0"
@ -227,6 +358,40 @@ dependencies = [
"stable_deref_trait",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "indexmap"
version = "2.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "insta"
version = "1.39.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "810ae6042d48e2c9e9215043563a58a80b877bc863228a74cf10c49d4620a6f5"
dependencies = [
"console",
"lazy_static",
"linked-hash-map",
"similar",
]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800"
[[package]]
name = "lazy_static"
version = "1.5.0"
@ -245,6 +410,12 @@ version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058"
[[package]]
name = "linked-hash-map"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
[[package]]
name = "linux-raw-sys"
version = "0.4.14"
@ -290,6 +461,7 @@ dependencies = [
"num-rational",
"num-traits",
"rayon",
"serde",
"simba",
"typenum",
]
@ -386,16 +558,30 @@ checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
name = "pdxdrive"
version = "0.1.0"
dependencies = [
"clap",
"flacenc",
"insta",
"nalgebra",
"petgraph",
"proptest",
"rayon",
"serde",
"thiserror",
"tracing",
"tracing-subscriber",
"yz-string-utils",
]
[[package]]
name = "petgraph"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db"
dependencies = [
"fixedbitset",
"indexmap",
]
[[package]]
name = "pin-project-lite"
version = "0.2.14"
@ -611,6 +797,12 @@ dependencies = [
"wide",
]
[[package]]
name = "similar"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1de1d4f81173b03af4c0cbed3c898f6bff5b870e4a7f5d6f4057d62a7a4b686e"
[[package]]
name = "smallvec"
version = "1.13.2"
@ -623,6 +815,12 @@ version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
version = "2.0.72"
@ -751,6 +949,12 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "valuable"
version = "0.1.0"

View file

@ -6,15 +6,24 @@ license = "EUPL-1.2"
[dependencies]
flacenc = "0.4"
petgraph = "0.6.5"
rayon = "1.10"
serde = "1.0"
thiserror = "1.0"
tracing = "0.1"
tracing-subscriber = "0.3"
yz-string-utils = "0.4.0"
yz-string-utils = "0.4"
[dependencies.clap]
version = "4.5.9"
features = ["derive"]
[dependencies.nalgebra]
version = "0.33"
features = ["rayon"]
features = ["rayon", "serde"]
[dev-dependencies]
proptest = "1.5.0"
proptest = "1.5"
[dev-dependencies.insta]
version = "1.39"

106
src/expr/linkplan.rs Normal file
View file

@ -0,0 +1,106 @@
//! query planning for link derivative computation.
//use super::Link;
use std::collections::BTreeMap;
/*
/// a node to be computed for a link
#[derive(Debug)]
struct PreHasseNode {
is_target: bool,
children: BTreeMap<u16, PreHasseNode>,
}
*/
/* fullfillment of links is sort-of complicated, because the tree of possiblities is really large
* start by splitting links according to their roots (the variable of which derivative of such is taken)
* split the derivation-vectors into levels (sum of their components)
*/
fn sum(drvs: &[u16]) -> usize {
drvs.iter().copied().map(usize::from).sum::<usize>()
}
fn parents(drvs: &[u16]) -> impl Iterator<Item = Box<[u16]>> + '_ {
drvs.iter()
.enumerate()
.filter_map(|(n, i)| i.checked_sub(1).map(|i2| (n, i2)))
.map(|(n, j)| {
let mut i2: Box<[u16]> = drvs.to_owned().into_boxed_slice();
i2[n] = j;
i2
})
}
/// calculate votes for each possible parent
fn calculate_votes(mut que: Vec<Box<[u16]>>) -> Vec<BTreeMap<Box<[u16]>, u16>> {
assert!(que.len() <= usize::from(u16::MAX));
// split que into levels
let mut levels = core::iter::repeat_with(BTreeMap::new)
.take(que.iter().map(|i| sum(&i[..])).max().map(|i| i.checked_add(1).unwrap()).unwrap_or(0))
.collect::<Vec<BTreeMap<Box<[u16]>, u16>>>();
for i in core::mem::replace(&mut que, Vec::new()) {
let mut l = &mut levels[sum(&i[..])];
l.insert(i, 1);
}
// process levels (start from largest)
for (lid, i) in levels.iter_mut().enumerate().rev() {
// insert queued items into this level
for j in core::mem::replace(&mut que, Vec::new()) {
assert_eq!(lid, sum(&j[..]));
let mut v = i.entry(j).or_default();
*v = v.checked_add(1).unwrap();
}
// handle all items in this level
for (j, v) in i.iter_mut() {
que.extend(parents(&j[..]));
}
}
levels
}
/// fill up the list of links such that for each derivative, at least one parent derivative exists,
/// and compute the parent
///
/// maximize reuse of the same derivatives
/*
pub fn fullfill_links(que: Vec<Link>) -> HashMap<u16, Vec<(Box<[u16]>, u16)>> {
let mut g = HashMap::<Vec<_>>::new();
// sort the links by root variable
for (vid, drv) in que {
g.entry(vid).or_default().push(drv);
}
for (vid, que) in que {
let votes = calculate_votes(que.clone());
for i in que {
for j in parents(i) {
}
}
}
}
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_calculate_votes() {
insta::assert_debug_snapshot!(calculate_votes(vec![
vec![0, 1, 2, 0].into_boxed_slice(),
vec![0, 1, 1, 1].into_boxed_slice(),
]));
}
}

View file

@ -5,6 +5,8 @@
*/
//! tools for handling expressions
mod linkplan;
use std::collections::BTreeMap;
use yz_string_utils::StrLexerBase;
@ -243,7 +245,7 @@ fn parse_expr<L: Clone + ParseLink>(slb: &mut StrLexerBase<'_>) -> Result<Expr<L
}
}
fn parse_expr_final<L: Clone + ParseLink>(slb: &mut StrLexerBase) -> Result<Expr<L>, ParseError> {
pub fn parse_expr_final<L: Clone + ParseLink>(slb: &mut StrLexerBase) -> Result<Expr<L>, ParseError> {
let x = parse_expr(slb)?;
if !slb.inp.is_empty() {
return Err(ParseError::InvalidTrail);

View file

@ -0,0 +1,74 @@
---
source: src/expr/linkplan.rs
expression: "calculate_votes(vec![vec![0, 1, 2, 0].into_boxed_slice(),\n vec![0, 1, 1, 1].into_boxed_slice(),])"
---
[
{
[
0,
0,
0,
0,
]: 3,
},
{
[
0,
0,
0,
1,
]: 2,
[
0,
0,
1,
0,
]: 3,
[
0,
1,
0,
0,
]: 2,
},
{
[
0,
0,
1,
1,
]: 1,
[
0,
0,
2,
0,
]: 1,
[
0,
1,
0,
1,
]: 1,
[
0,
1,
1,
0,
]: 2,
},
{
[
0,
1,
1,
1,
]: 1,
[
0,
1,
2,
0,
]: 1,
},
]

View file

@ -7,6 +7,89 @@
pub mod expr;
pub mod mesh;
fn main() {
println!("Hello, world!");
use std::path::PathBuf;
#[derive(clap::Parser)]
struct Cli {
/// The system to render
#[arg(short, long)]
system: PathBuf,
/// The shape of the grid (in amounts of points)
#[arg(long)]
shape: Vec<u16>,
/// The space-step size to use
#[arg(long)]
grid_step: f64,
/// The amount of iterations to render
#[arg(long)]
iters: usize,
/// The time-step size to use
#[arg(short, long)]
time_step: f64,
/// The initialization function
#[arg(short, long)]
init: String,
}
fn main() {
let cli = <Cli as clap::Parser>::parse();
// parse system
let system = {
let system = std::fs::read(&cli.system).expect("unable to read system to render");
let system = std::str::from_utf8(&*system).expect("unable to interpret system description as UTF-8");
expr::parse_system(system).expect("unable to parse system")
};
// parse init function
let init = expr::parse_expr_final::<expr::Link>(&mut yz_string_utils::StrLexerBase { inp: &cli.init, offset: 0 }).expect("unable to parse init function");
// construct shape
let shape = mesh::grid::Shape(cli.shape);
let shape_amount = match shape.amount() {
None => panic!("shape is too large"),
Some(0) => panic!("shape is empty"),
Some(1) => panic!("shape is too small"),
Some(x) => x,
};
// check monitor consistency with shape
for (n, (i, _)) in system.mons[..].iter().enumerate() {
if i.len() != shape.0.len() {
panic!("monitor {} has incompatible dimension {} vs {}", n, i.len(), shape.0.len());
}
}
// extract used links
let links = system.extract_links();
// check link consistency (should only refer to variables which exist)
{
let max_link = u16::try_from(system.vs.len()).expect("too many equations to track");
for i in &links {
if i.0 >= max_link {
panic!("invalid link {:?} found", i);
}
}
}
// initialize grid
let mut grid = nalgebra::DMatrix::<f64>::zeros(shape_amount, system.vs.len());
let mut grid_stage = nalgebra::DMatrix::<f64>::zeros(shape_amount, system.vs.len());
// data =
let mut grid_next = grid.clone();
// main loop
for i in 0..cli.iters {
if i % 10 == 0 {
println!("iteration {}.", i);
}
}
}

View file

@ -41,14 +41,21 @@ impl Shape {
.try_fold(1, |acc: usize, &i| acc.checked_mul(usize::from(i).checked_add(1)?))
}
pub fn decode_pos(&self, x: usize) -> Vec<u16> {
pub fn decode_pos_with_ret(&self, x: usize, out: &mut [u16]) {
self.0.iter()
.map(|&i| usize::from(i) + 1)
.fold((x, Vec::with_capacity(self.0.len())), |(x, mut ret): (usize, Vec<u16>), i| {
.zip(out.iter_mut())
.fold(x, |x: usize, (i, o)| {
let (j, r) = (x % i, x / i);
ret.push(j as u16);
(r, ret)
}).1
*o = j as u16;
r
});
}
pub fn decode_pos(&self, x: usize) -> Vec<u16> {
let mut ret = vec![0; self.0.len()];
self.decode_pos_with_ret(x, &mut ret);
ret
}
pub fn encode_pos(&self, x: &[u16]) -> Option<usize> {

View file

@ -7,6 +7,7 @@
pub mod grid;
/*
#[derive(Clone)]
pub enum Mesh {
Grid {
@ -14,3 +15,4 @@ pub enum Mesh {
data: nalgebra::DMatrix<f64>,
}
}
*/