Commit d1e0ac0c authored by Arthur Bit-Monnot's avatar Arthur Bit-Monnot

[planning] Return a structured plan instead of its string representation.

parent e4169c17
......@@ -82,14 +82,14 @@ object Main extends App {
case None =>
}
def solveTask(problemFile: File, deadline: Deadline)(implicit cfg: Config): IO[Option[Any]] = {
def solveTask(problemFile: File, deadline: Deadline)(implicit cfg: Config): IO[Option[Plan]] = {
IO {
info(problemFile.getName)
solve(problemFile, deadline)
}
}
def solve(problemFile: File, deadline: Deadline)(implicit cfg: Config): Option[String] = {
def solve(problemFile: File, deadline: Deadline)(implicit cfg: Config): Option[Plan] = {
implicit val plannerConf: PlannerConfig =
PlannerConfig(minInstances = cfg.minInstances, maxInstances = cfg.maxInstances)
info("Parsing...")
......
......@@ -3,13 +3,11 @@ package dahu.planning.pddl.planner
import java.io.File
import dahu.planning.pddl.parser._
import dahu.planning.model.common.Predef
import dahu.planning.planner._
import dahu.utils.Vec
import scala.concurrent.duration._
import scala.concurrent.{Await, TimeoutException}
import scala.util.{Failure, Success, Try}
import scala.util.{Failure, Success}
case class Config(problemFile: File = null,
domainFile: Option[File] = None,
......@@ -69,13 +67,17 @@ object Main extends App {
implicit val plannerConfig: PlannerConfig =
PlannerConfig(config.minInstances, config.maxInstances)
implicit val predef: Predef = parser.predef
implicit val predef: PddlPredef = parser.predef
parser.parse(domain, problem) match {
case Success(model) =>
Planner.solveIncremental(model, config.maxInstances, Deadline.now + config.maxRuntime) match {
case Some(sol) =>
println(sol)
case Some(Plan(operators)) =>
println("\n== Solution ==")
operators
.map(PddlOperator(_))
.sortBy(_.start)
.foreach(println)
case None =>
println("\nFAIL")
}
......@@ -86,3 +88,21 @@ object Main extends App {
None
}
}
case class PddlOperator(name: String, args: Vec[String], start: Double, duration: Double) {
override def toString: String = s"$start: ($name ${args.mkString(", ")}) [$duration]"
}
object PddlOperator {
def toPddlTime(t: Int)(implicit predef: PddlPredef): Double = t.toDouble / predef.discretization.toDouble
def apply(gen: Operator[cats.Id])(implicit predef: PddlPredef): PddlOperator = gen match {
case Operator(name, args, start, end, true) =>
new PddlOperator(
name,
Vec(args.map(_.toString): _*),
toPddlTime(start),
toPddlTime(end - start))
}
}
......@@ -4,4 +4,8 @@ case class Operator[F[_]](name: F[String],
args: F[Seq[Literal]],
start: F[Int],
end: F[Int],
present: F[Boolean])
present: F[Boolean]) {
override def toString = s"[$start, $end] $name($args)"
}
case class Plan(operators: Seq[Operator[cats.Id]])
\ No newline at end of file
......@@ -25,12 +25,12 @@ object Planner {
def solveIncremental(model: core.CoreModel, maxSteps: Int, deadline: Deadline)(
implicit cfg: PlannerConfig,
predef: Predef): Option[String] = {
predef: Predef): Option[Plan] = {
val q = new java.util.concurrent.ConcurrentLinkedQueue[Integer]()
for(i <- cfg.minInstances to cfg.maxInstances)
q.add(i)
val task: IO[Option[String]] = IO {
val task: IO[Option[Plan]] = IO {
while(deadline.hasTimeLeft) {
val step: Integer = q.poll()
if(step == null)
......@@ -49,20 +49,11 @@ object Planner {
}
task.unsafeRunTimed(deadline.timeLeft).flatten
// Try {
// Await.result(future, (deadline - System.currentTimeMillis()).millis)
// } match {
// case Success(solution) =>
// solution
// case Failure(to: TimeoutException) => None
// case Failure(e) => throw e
// }
}
def solveIncrementalStep(model: core.CoreModel, step: Int, deadline: Deadline)(
implicit cfg: PlannerConfig,
predef: Predef): Option[String] = {
predef: Predef): Option[Plan] = {
if(deadline.isOverdue())
return None
......@@ -104,7 +95,7 @@ object Planner {
solution
}
def solve(chronicle: Chronicle, deadline: Deadline): Option[String] = {
def solve(chronicle: Chronicle, deadline: Deadline): Option[Plan] = {
if(deadline.isOverdue)
return None
val sat = chronicle.toSatProblem
......@@ -119,15 +110,12 @@ object Planner {
evaluatedSolution(ass) match {
case Res(operators) =>
Some(
operators
.asInstanceOf[Seq[Operator[cats.Id]]]
.filter(_.present)
.sortBy(_.start)
.map {
case Operator(name, args, s, e, _) => s"[$s, $e] $name(${args.mkString(",")})"
}
.mkString("\n")
)
Plan(
operators
.asInstanceOf[Seq[Operator[cats.Id]]]
.filter(_.present)
.sortBy(_.start)
))
case x => unexpected(x.toString)
}
case None => None
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment