Skip to content

Commit

Permalink
add env_param ARIES_PRINT_PLANNER_OUTPUT
Browse files Browse the repository at this point in the history
  • Loading branch information
Yirmandias committed Sep 1, 2023
1 parent 1451740 commit 152074a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
56 changes: 41 additions & 15 deletions planning/planners/src/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ static PRINT_INITIAL_PROPAGATION: EnvParam<bool> = EnvParam::new("ARIES_PRINT_IN
/// If set to true, will print the raw model (before preprocessing)
static PRINT_RAW_MODEL: EnvParam<bool> = EnvParam::new("ARIES_PRINT_RAW_MODEL", "false");

static PRINT_PLANNER_OUTPUT: EnvParam<bool> = EnvParam::new("ARIES_PRINT_PLANNER_OUTPUT", "true");

/// If set to true, will print the preprocessed model
static PRINT_MODEL: EnvParam<bool> = EnvParam::new("ARIES_PRINT_MODEL", "false");

Expand Down Expand Up @@ -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);
}
Expand All @@ -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 {
Expand All @@ -116,7 +124,9 @@ pub fn solve(
let on_new_sol = on_new_sol.clone();
move |ass: Arc<SavedAssignment>| 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,
Expand All @@ -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 {
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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), []);
Expand Down Expand Up @@ -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
}
10 changes: 7 additions & 3 deletions planning/planning/src/chronicles/preprocessing/statics.rs
Original file line number Diff line number Diff line change
@@ -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.
///
Expand Down Expand Up @@ -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<DiscreteValue> = Table::new(sf_name, sf.tpe.clone());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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");
}
}
}

Expand Down Expand Up @@ -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");
}
}
}
3 changes: 3 additions & 0 deletions planning/planning/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod chronicles;
pub mod classical;
pub mod parsing;
use env_param::EnvParam;

static PRINT_PLANNER_OUTPUT: EnvParam<bool> = EnvParam::new("ARIES_PRINT_PLANNER_OUTPUT", "true");

0 comments on commit 152074a

Please sign in to comment.