Skip to content

Commit

Permalink
feat(grpc, solver): select the depth of each template individually fr…
Browse files Browse the repository at this point in the history
…om the warping plan
  • Loading branch information
Shi-Raida committed Feb 20, 2025
1 parent 8517ea5 commit 0fc8e97
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
5 changes: 4 additions & 1 deletion planning/grpc/server/src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use anyhow::{bail, ensure, Context, Error};
use aries::model::extensions::SavedAssignment;
use aries_grpc_server::chronicles::problem_to_chronicles;
use aries_grpc_server::serialize::{engine, serialize_plan};
use aries_grpc_server::warm_up::depth_from_option_plan;
use aries_plan_validator::validate_upf;
use aries_planners::solver;
use aries_planners::solver::{Metric, SolverResult, Strat};
use aries_planning::chronicles::analysis::hierarchy::hierarchical_is_non_recursive;
use aries_planning::chronicles::FiniteProblem;
use aries_planning::chronicles::{ChronicleTemplate, FiniteProblem};
use async_trait::async_trait;
use clap::{Args, Parser, Subcommand};
use env_param::EnvParam;
Expand Down Expand Up @@ -188,6 +189,7 @@ fn solve_blocking(
} else {
conf.min_depth
};
let depth_map = |ch: &ChronicleTemplate| depth_from_option_plan(conf.warm_up_plan.clone(), ch);

// callback that will be invoked each time an intermediate solution is found
let on_new_solution = |pb: &FiniteProblem, ass: Arc<SavedAssignment>| {
Expand All @@ -202,6 +204,7 @@ fn solve_blocking(
base_problem,
min_depth,
max_depth,
depth_map,
&conf.strategies,
metric,
htn_mode,
Expand Down
1 change: 1 addition & 0 deletions planning/grpc/server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
// license that can be found in the LICENSE file.
pub mod chronicles;
pub mod serialize;
pub mod warm_up;
14 changes: 14 additions & 0 deletions planning/grpc/server/src/warm_up.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use aries_planning::chronicles::ChronicleTemplate;

pub fn depth_from_option_plan(plan: Option<String>, ch: &ChronicleTemplate) -> u32 {
plan.map_or(0, |p| depth_from_plan(p, ch))
}

pub fn depth_from_plan(plan: String, ch: &ChronicleTemplate) -> u32 {
let ch_name = format!("{:?}", ch.label);

plan.split("\n")
.map(|line| line.trim())
.filter(|line| line.starts_with(ch_name.as_str()))
.count() as u32
}
8 changes: 7 additions & 1 deletion planning/planners/src/bin/lcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use aries::utils::input::Input;
use aries_planners::solver::{format_plan, solve, SolverResult};
use aries_planners::solver::{Metric, Strat};
use aries_planning::chronicles::analysis::hierarchy::hierarchical_is_non_recursive;
use aries_planning::chronicles::FiniteProblem;
use aries_planning::chronicles::{ChronicleTemplate, FiniteProblem};
use aries_planning::parsing::pddl::{find_domain_of, parse_pddl_domain, parse_pddl_problem, PddlFeature};
use aries_planning::parsing::pddl_to_chronicles;
use std::fs::File;
Expand Down Expand Up @@ -119,6 +119,11 @@ fn main() -> Result<()> {
0
};

// The depth map is a function that returns the depth of a given template.
// It is used when wramming up the planner from a known solution.
// The only implementation of this warm up is using the gRPC server.
let depth_map = |_: &ChronicleTemplate| 0;

// prints a plan to a standard output and to the provided file, if any
let print_plan = move |finite_problem: &FiniteProblem, assignment: &Domains, output_file: Option<&PathBuf>| {
if let Ok(plan_out) = format_plan(finite_problem, assignment, htn_mode) {
Expand All @@ -139,6 +144,7 @@ fn main() -> Result<()> {
spec,
min_depth,
max_depth,
depth_map,
&opt.strategies,
opt.optimize,
htn_mode,
Expand Down
9 changes: 5 additions & 4 deletions planning/planners/src/solver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ impl FromStr for Metric {

/// Search for plan based on the `base_problem`.
///
/// The solver will look for plan by generating subproblem of increasing `depth`
/// (for `depth` in `{min_depth, max_depth]`) where `depth` defines the number of allowed actions
/// in the subproblem.
/// The solver will look for plan by generating subproblem of increasing `depth`.
/// For a given chronicle template, its associated `depth` (the number of allowed instances)
/// is given by the `depth_map(template) + d` function, with `d` in `{min_depth, max_depth]`.
///
/// The `depth` parameter is increased until a plan is found or foes over `max_depth`.
///
Expand All @@ -80,6 +80,7 @@ pub fn solve(
mut base_problem: Problem,
min_depth: u32,
max_depth: u32,
depth_map: impl Fn(&ChronicleTemplate) -> u32,
strategies: &[Strat],
metric: Option<Metric>,
htn_mode: bool,
Expand Down Expand Up @@ -119,7 +120,7 @@ pub fn solve(
if htn_mode {
populate_with_task_network(&mut pb, &base_problem, depth)?;
} else {
populate_with_template_instances(&mut pb, &base_problem, |_| Some(depth))?;
populate_with_template_instances(&mut pb, &base_problem, |ch| Some(depth_map(ch) + depth))?;
}
let pb = Arc::new(pb);

Expand Down
2 changes: 1 addition & 1 deletion planning/unified/plugin/test/test_warm_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ def test_oneshot_with_warm_up_returns_same_plan(self, problem: Problem, plan: Pl
with OneshotPlanner(name="aries", params={"warm_up_plan": plan}) as planner:
planner.skip_checks = True
result = planner.solve(problem)
pytest.fail("Not implemented")
assert str(result.plan) == str(plan)

0 comments on commit 0fc8e97

Please sign in to comment.