From 152074a97a3b65410b75e49781a8f88875d056b3 Mon Sep 17 00:00:00 2001 From: Yirmandias Date: Fri, 1 Sep 2023 15:09:17 +0200 Subject: [PATCH] add env_param ARIES_PRINT_PLANNER_OUTPUT --- planning/planners/src/solver.rs | 56 ++++++++++++++----- .../src/chronicles/preprocessing/statics.rs | 10 +++- .../preprocessing/unused_effects.rs | 9 ++- planning/planning/src/lib.rs | 3 + 4 files changed, 58 insertions(+), 20 deletions(-) diff --git a/planning/planners/src/solver.rs b/planning/planners/src/solver.rs index 7236f52b..af42d52b 100644 --- a/planning/planners/src/solver.rs +++ b/planning/planners/src/solver.rs @@ -28,6 +28,8 @@ static PRINT_INITIAL_PROPAGATION: EnvParam = EnvParam::new("ARIES_PRINT_IN /// If set to true, will print the raw model (before preprocessing) static PRINT_RAW_MODEL: EnvParam = EnvParam::new("ARIES_PRINT_RAW_MODEL", "false"); +static PRINT_PLANNER_OUTPUT: EnvParam = EnvParam::new("ARIES_PRINT_PLANNER_OUTPUT", "true"); + /// If set to true, will print the preprocessed model static PRINT_MODEL: EnvParam = EnvParam::new("ARIES_PRINT_MODEL", "false"); @@ -81,9 +83,13 @@ pub fn solve( if PRINT_RAW_MODEL.get() { Printer::print_problem(&base_problem); } - println!("===== Preprocessing ======"); + if PRINT_PLANNER_OUTPUT.get() { + println!("===== Preprocessing ======"); + } aries_planning::chronicles::preprocessing::preprocess(&mut base_problem); - println!("=========================="); + if PRINT_PLANNER_OUTPUT.get() { + println!("=========================="); + } if PRINT_MODEL.get() { Printer::print_problem(&base_problem); } @@ -103,7 +109,9 @@ pub fn solve( } else { depth.to_string() }; - println!("{depth_string} Solving with depth {depth_string}"); + if PRINT_PLANNER_OUTPUT.get() { + println!("{depth_string} Solving with depth {depth_string}"); + } if htn_mode { populate_with_task_network(&mut pb, &base_problem, depth)?; } else { @@ -116,7 +124,9 @@ pub fn solve( let on_new_sol = on_new_sol.clone(); move |ass: Arc| on_new_sol(&pb, ass) }; - println!(" [{:.3}s] Populated", start.elapsed().as_secs_f32()); + if PRINT_PLANNER_OUTPUT.get() { + println!(" [{:.3}s] Populated", start.elapsed().as_secs_f32()); + } let result = solve_finite_problem( pb.clone(), strategies, @@ -126,7 +136,9 @@ pub fn solve( deadline, best_cost - 1, ); - println!(" [{:.3}s] Solved", start.elapsed().as_secs_f32()); + if PRINT_PLANNER_OUTPUT.get() { + println!(" [{:.3}s] Solved", start.elapsed().as_secs_f32()); + } let result = result.map(|assignment| (pb, assignment)); match result { @@ -150,18 +162,25 @@ pub fn solve( /// Returns true if the propagation succeeded. fn propagate_and_print(pb: &FiniteProblem) -> bool { let Ok(EncodedProblem { model, .. }) = encode(pb, None) else { - println!("==> Invalid model"); - return false + if PRINT_PLANNER_OUTPUT.get() { + println!("==> Invalid model"); + } + return false; }; let mut solver = init_solver(model); - - println!("\n======== AFTER INITIAL PROPAGATION ======\n"); + if PRINT_PLANNER_OUTPUT.get() { + println!("\n======== AFTER INITIAL PROPAGATION ======\n"); + } if solver.propagate().is_ok() { - let str = format_partial_plan(pb, &solver.model).unwrap(); - println!("{str}"); + if PRINT_PLANNER_OUTPUT.get() { + let str = format_partial_plan(pb, &solver.model).unwrap(); + println!("{str}"); + } true } else { - println!("==> Propagation failed."); + if PRINT_PLANNER_OUTPUT.get() { + println!("==> Propagation failed."); + } false } } @@ -296,8 +315,13 @@ fn solve_finite_problem( if PRINT_INITIAL_PROPAGATION.get() { propagate_and_print(&pb); } - let Ok( EncodedProblem { mut model, objective: metric, encoding }) = encode(&pb, metric) else { - return SolverResult::Unsat + let Ok(EncodedProblem { + mut model, + objective: metric, + encoding, + }) = encode(&pb, metric) + else { + return SolverResult::Unsat; }; if let Some(metric) = metric { model.enforce(metric.le_lit(cost_upper_bound), []); @@ -330,7 +354,9 @@ fn solve_finite_problem( }); if let SolverResult::Sol(_) = result { - solver.print_stats() + if PRINT_PLANNER_OUTPUT.get() { + solver.print_stats() + } } result } diff --git a/planning/planning/src/chronicles/preprocessing/statics.rs b/planning/planning/src/chronicles/preprocessing/statics.rs index a16a153f..710c7146 100644 --- a/planning/planning/src/chronicles/preprocessing/statics.rs +++ b/planning/planning/src/chronicles/preprocessing/statics.rs @@ -1,10 +1,10 @@ use crate::chronicles::*; use crate::chronicles::constraints::{Constraint, ConstraintType}; +use crate::PRINT_PLANNER_OUTPUT; use aries::model::extensions::{AssignmentExt, Shaped}; use aries::model::lang::{IAtom, SAtom}; use std::convert::TryFrom; - /// Detects state functions that are static (all of its state variable will take a single value over the entire planning window) /// and replaces the corresponding conditions and effects as table constraints. /// @@ -95,11 +95,15 @@ pub fn statics_as_tables(pb: &mut Problem) { // === at this point, we know that the state function is static, we can replace all conditions/effects by a single constraint === if first { - println!("Transforming static state functions as table constraints:"); + if PRINT_PLANNER_OUTPUT.get() { + println!("Transforming static state functions as table constraints:"); + } first = false; } let sf_name = pb.context.model.get_symbol(sf.sym).to_string(); - println!(" - {sf_name}"); + if PRINT_PLANNER_OUTPUT.get() { + println!(" - {sf_name}"); + } // table that will collect all possible tuples for the state variable let mut table: Table = Table::new(sf_name, sf.tpe.clone()); diff --git a/planning/planning/src/chronicles/preprocessing/unused_effects.rs b/planning/planning/src/chronicles/preprocessing/unused_effects.rs index ac9d3f1f..0cda1f3d 100644 --- a/planning/planning/src/chronicles/preprocessing/unused_effects.rs +++ b/planning/planning/src/chronicles/preprocessing/unused_effects.rs @@ -1,5 +1,6 @@ use crate::chronicles::constraints::Constraint; use crate::chronicles::{Condition, Effect, Problem, VarLabel}; +use crate::PRINT_PLANNER_OUTPUT; use aries::model::lang::FAtom; use aries::model::Model; use std::cmp::Ordering; @@ -57,7 +58,9 @@ pub fn remove_unusable_effects(pb: &mut Problem) { } if num_removed > 0 { - println!("Removed {num_removed} unusable effects"); + if PRINT_PLANNER_OUTPUT.get() { + println!("Removed {num_removed} unusable effects"); + } } } @@ -124,6 +127,8 @@ pub fn merge_unusable_effects(pb: &mut Problem) { } if num_removed > 0 { - println!("Merged {num_removed} unusable effects"); + if PRINT_PLANNER_OUTPUT.get() { + println!("Merged {num_removed} unusable effects"); + } } } diff --git a/planning/planning/src/lib.rs b/planning/planning/src/lib.rs index 19b2248b..1d3ed661 100644 --- a/planning/planning/src/lib.rs +++ b/planning/planning/src/lib.rs @@ -1,3 +1,6 @@ pub mod chronicles; pub mod classical; pub mod parsing; +use env_param::EnvParam; + +static PRINT_PLANNER_OUTPUT: EnvParam = EnvParam::new("ARIES_PRINT_PLANNER_OUTPUT", "true");