Skip to content

Commit

Permalink
integration tests init + github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenGar committed Feb 21, 2024
1 parent ad1dc11 commit aac4954
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 11 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Build
run: cd lbf && cargo build --verbose
run: |
cd lbf
cargo build --verbose
- name: Tests
run: |
cd lbf
cargo test -- --no-capture
- name: Docs
run : |
cd lbf
cargo doc
2 changes: 1 addition & 1 deletion jagua-rs/src/geometry/geo_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub trait CollidesWith<T> {

/// Trait for types that can detect almost-collisions between itself and an object from type T.
/// Useful in situations where fp arithmetic precision could be problematic.
/// Leans towards false positives rather than false negatives.
/// Should be implemented to lean towards false positives rather than false negatives.
pub trait AlmostCollidesWith<T> {
fn almost_collides_with(&self, other: &T) -> bool;
}
Expand Down
6 changes: 4 additions & 2 deletions lbf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ fern = "0.6.2"
log = "0.4.20"
humantime = "2.1.0"
itertools = "0.12.0"
svg = "0.14.0"
svg = "0.15.1"
ordered-float = "4.2.0"
clap = { version = "4.4.18", features = ["derive"] }
mimalloc = "0.1.39"
tribool = "0.3.0"
almost = "0.2.0"
test-case = "3.3.1"

[dev-dependencies]
criterion = "0.5.1"
Expand All @@ -46,5 +47,6 @@ harness = false
[profile.release]
opt-level = 3

[profile.bench]
[profile.test]
debug-assertions = true
opt-level = 3
12 changes: 5 additions & 7 deletions lbf/src/lbf_optimizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ pub const STDDEV_ROT_END_FRAC: f64 = 0.5 * (PI / 180.0);
pub const ITEM_LIMIT: usize = usize::MAX;

pub struct LBFOptimizer {
instance: Instance,
problem: Problem,
config: Config,
pub instance: Instance,
pub problem: Problem,
pub config: Config,
/// SmallRng is a fast, non-cryptographic PRNG <https://rust-random.github.io/book/guide-rngs.html>
rng: SmallRng,
pub rng: SmallRng,
}

impl LBFOptimizer {
Expand Down Expand Up @@ -130,7 +130,7 @@ impl LBFOptimizer {
}
}

fn find_placement(problem: &Problem, item: &Item, config: &Config, rng: &mut impl Rng) -> Option<PlacingOption> {
pub fn find_placement(problem: &Problem, item: &Item, config: &Config, rng: &mut impl Rng) -> Option<PlacingOption> {
let layouts_to_sample =
(0..problem.layouts().len()).map(|i| (LayoutIndex::Existing(i)))
.chain((0..problem.empty_layouts().len())
Expand Down Expand Up @@ -235,5 +235,3 @@ pub fn sample_layout(problem: &Problem, layout_index: LayoutIndex, item: &Item,

best.map(|b| b.0)
}


74 changes: 74 additions & 0 deletions lbf/tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#[cfg(test)]
mod tests {
use std::path::Path;
use std::sync::Once;

use log::info;
use rand::{Rng, SeedableRng};
use rand::prelude::IteratorRandom;
use rand::prelude::SmallRng;
use test_case::test_case;

use jagua_rs::entities::problems::problem::LayoutIndex;
use jagua_rs::entities::problems::problem::ProblemGeneric;
use jagua_rs::io::parser::Parser;
use lbf::config::Config;
use lbf::io;
use lbf::lbf_optimizer::LBFOptimizer;

const N_ITEMS_TO_REMOVE: usize = 5;

static INIT_LOGGER : Once = Once::new();

#[test_case("../assets/swim.json"; "swim")]
#[test_case("../assets/shirts.json"; "shirts")]
#[test_case("../assets/trousers.json"; "trousers")]
#[test_case("../assets/mao.json"; "mao.json")]
#[test_case("../assets/Baldacci/Test1.json"; "Baldacci/Test1")]
#[test_case("../assets/Baldacci/Test2.json"; "Baldacci/Test2")]
#[test_case("../assets/Baldacci/Test3.json"; "Baldacci/Test3")]
#[test_case("../assets/Baldacci/Test4.json"; "Baldacci/Test4")]
#[test_case("../assets/Baldacci/Test5.json"; "Baldacci/Test5")]
#[test_case("../assets/Baldacci/Test6.json"; "Baldacci/Test6")]
fn test_instance(instance_path: &str) {
INIT_LOGGER.call_once(|| {
io::init_logger(Some(log::LevelFilter::Info));
});

let instance = Path::new(instance_path);

info!("Testing instance: {:?}", instance);
// parse the instance
let mut config = Config::default();
config.n_samples_per_item = 100;
let json_instance = io::read_json_instance(&instance);
let parser = Parser::new(config.poly_simpl_config, config.cde_config, true);
let instance = parser.parse(&json_instance);

let mut optimizer = LBFOptimizer::new(instance.clone(), config, SmallRng::seed_from_u64(0));

let mut rng = SmallRng::seed_from_u64(0);

// to a first optimization run
optimizer.solve();

{
// remove some items
let problem = &mut optimizer.problem;
for i in 0..N_ITEMS_TO_REMOVE {
//pick random existing layout
let layout_index = LayoutIndex::Existing(rng.gen_range(0..problem.layouts().len()));
let random_placed_item = match problem.get_layout(&layout_index).placed_items().iter().choose(&mut rng) {
Some(pi) => pi.uid.clone(),
None => break,
};
// remove the item
problem.remove_item(layout_index, &random_placed_item, false);
}
// flush changes
problem.flush_changes();
// reoptimize
optimizer.solve();
}
}
}

0 comments on commit aac4954

Please sign in to comment.