Skip to content

Commit

Permalink
Cleaned for release
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexorg committed Nov 15, 2023
1 parent 5a4ca3d commit 873e45e
Show file tree
Hide file tree
Showing 17 changed files with 1,096 additions and 500 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pddl_rs"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
description = "PDDL parser"
license = "MIT"
Expand All @@ -17,4 +17,3 @@ enumset = "1.0.12"
ariadne = "0.3.0"
nom = "7.1.3"
lazy_static = "1.4.0"
rand = "0.8.5"
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ use pddl_rs::{Sources, Objects, search::{a_star, AstarInternals}};
let domain_filename = PathBuf::from("sample_problems/simple_domain.pddl");
let problem_filename = PathBuf::from("sample_problems/simple_problem.pddl");
let sources = Sources::load(domain_filename, problem_filename);
let (domain, problem, c_problem) = sources.compile();
let (domain, problem) = sources.parse();
let c_problem = sources.compile(&domain, &problem);
println!("Compiled problem needs {} bits of memory and uses {} actions.", c_problem.memory_size, c_problem.actions.len());
let mut args = AstarInternals::new();
if let Some(solution) = a_star(&c_problem, &mut args) {
let mut args = AstarInternals::new(&c_problem.action_graph);
if let Some(solution) = a_star(&c_problem, Some(&domain), Some(&problem), &mut args) {
println!("Solution is {} actions long.", solution.len());
for action_id in &solution {
let action = c_problem.actions.get(*action_id as usize).unwrap();
println!("\t{}{:?}", domain.actions[action.domain_action_idx as usize].name(), action.args.iter().map(|(row, col)| problem.objects.get_object(*row,*col).item.1).collect::<Vec<&str>>());
println!("\t{}{:?}", domain.actions[action.domain_action_idx as usize].name(), action.args.iter().map(|(row, col)| problem.objects.get_object_name(*row,*col).1).collect::<Vec<&str>>());
}
}
```
Expand Down
50 changes: 29 additions & 21 deletions src/compiler.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
use std::{collections::HashMap, slice::Iter};
use std::{collections::HashMap, ops::Range, slice::Iter};

mod maps;
use maps::Maps;
use enumset::{EnumSet, EnumSetType};
mod passes;

// optimization mods
pub mod action_graph;
mod inertia;

pub use crate::parser::{ast::{*, name::Name}, *};
pub use crate::parser::{
ast::{name::Name, *},
*,
};
use crate::{Error, ErrorKind};

use self::{action_graph::ActionGraph, maps::{validate_problem, map_objects}, passes::Compiler};
use self::{
action_graph::ActionGraph,
maps::{map_objects, validate_problem},
passes::Compiler,
};

pub type PredicateUsize = u16;
pub type ASTActionUsize = u16;
Expand Down Expand Up @@ -69,7 +75,7 @@ pub trait Runnable {
fn run_mut(self, state: &mut [bool], functions: &mut [IntValue]);
fn state_miss_count(self, state: &[bool]) -> IntValue;
fn disasm(self) -> String;
fn decomp(self, memory_map:&Vec<AtomicFormula<Name>>) -> String;
fn decomp(self, memory_map: &Vec<AtomicFormula<Name>>) -> String;
}

impl Runnable for &[Instruction] {
Expand Down Expand Up @@ -211,15 +217,19 @@ impl Runnable for &[Instruction] {
result
}

fn decomp(self, memory_map:&Vec<AtomicFormula<Name>>) -> String {
fn decomp(self, memory_map: &Vec<AtomicFormula<Name>>) -> String {
let mut result = String::with_capacity(self.len() * 6);
for instruction in self {
if result.len() > 0 {
result.push_str(", ");
}
match instruction {
Instruction::ReadState(addr) => result.push_str(&format!("RS({})", memory_map[*addr as usize])),
Instruction::SetState(addr) => result.push_str(&format!("WS({})", memory_map[*addr as usize])),
Instruction::ReadState(addr) => {
result.push_str(&format!("RS({})", memory_map[*addr as usize]))
}
Instruction::SetState(addr) => {
result.push_str(&format!("WS({})", memory_map[*addr as usize]))
}
Instruction::ReadFunction(addr) => result.push_str(&format!("RF({})", *addr)),
Instruction::SetFunction(addr) => result.push_str(&format!("WF({})", *addr)),
Instruction::And(count) => result.push_str(&format!("AND_{}", *count)),
Expand All @@ -238,7 +248,7 @@ impl Runnable for &[Instruction] {
/// All instrutions use shared memory offsets
/// no larger than `self.memory_size`
#[derive(Debug, PartialEq)]
pub struct CompiledProblem {
pub struct CompiledProblem<'src> {
/// How many bits needed to fully represent this problem's state
/// (Actual memory used depends on type used to represent state.
/// A Vec<bool> will use `memory_size` bytes.)
Expand All @@ -248,12 +258,14 @@ pub struct CompiledProblem {
/// All compiled actions for this problem. Each domain action compiles into
/// multiple [`CompiledAction`]s due to object permutations for various predicates and types.
pub actions: Vec<CompiledAction>,
/// Mapping of domain action index to a range of compiled actions representing those actions for all used problem objects
pub domain_action_ranges: Vec<Range<CompiledActionUsize>>,
/// Executable bytecode to set initial conditions
pub init: Vec<Instruction>,
/// Executable bytecode to check if the goal is met
pub goal: Vec<Instruction>,
/// Priority list of compiled actions to try
pub action_graph: ActionGraph,
pub action_graph: ActionGraph<'src>,
}

/// Flatenned representation of Actions inside [`CompiledProblem`]s
Expand Down Expand Up @@ -303,14 +315,16 @@ impl Optimization {
pub fn compile_problem<'src, 'ast>(
domain: &'ast Domain<'src>,
problem: &'ast Problem<'src>,
) -> Result<CompiledProblem, Vec<Error>> {
) -> Result<CompiledProblem<'src>, Vec<Error>>
where
'ast: 'src,
{
validate_problem(domain, problem)?;
let mut maps = map_objects(domain, problem)?;
let maps = map_objects(domain, problem)?;
let mut compiler = Compiler::new(domain, problem, maps);
compiler.compile()
}


/// Given a list of types, use a type to object map and generate all possible
/// permutations of the objects fitting the list.
///
Expand All @@ -330,10 +344,7 @@ where
use ErrorKind::UndefinedType;

fn _has_collition<'parent, 'src>(
args: &[(
Name<'src>,
(PredicateUsize, PredicateUsize),
)],
args: &[(Name<'src>, (PredicateUsize, PredicateUsize))],
iterators: &[(&'src str, Iter<'parent, (PredicateUsize, PredicateUsize)>)],
) -> bool {
for i in 0..iterators.len() {
Expand Down Expand Up @@ -361,10 +372,7 @@ where
fn _args_iter<'parent, 'src>(
type_to_objects: &'parent HashMap<&'src str, Vec<(PredicateUsize, PredicateUsize)>>,
iterators: &mut [(&'src str, Iter<'parent, (PredicateUsize, PredicateUsize)>)],
args: &mut [(
Name<'src>,
(PredicateUsize, PredicateUsize),
)],
args: &mut [(Name<'src>, (PredicateUsize, PredicateUsize))],
pos: usize,
) -> bool {
if let Some(arg) = iterators[pos].1.next() {
Expand Down

0 comments on commit 873e45e

Please sign in to comment.