working version of the muri demo

This commit is contained in:
Naveen Sundar Govindarajulu 2017-01-15 22:13:40 -05:00
parent f3ddd342c6
commit c9201bc0b2
11 changed files with 290 additions and 155 deletions

View file

@ -8,31 +8,31 @@ import java.util.concurrent.atomic.AtomicInteger;
public class Goal { public class Goal {
private final State goalState; private final State goalState;
private final int priority; private final double priority;
private final String name; private final String name;
private static final AtomicInteger nameCounter; private static final AtomicInteger nameCounter;
static { static {
nameCounter = new AtomicInteger(0); nameCounter = new AtomicInteger(0);
} }
private Goal(State goalState, int priority) { private Goal(State goalState, double priority) {
this.goalState = goalState; this.goalState = goalState;
this.priority = priority; this.priority = priority;
this.name = "G" + nameCounter.incrementAndGet(); this.name = "G" + nameCounter.incrementAndGet();
} }
private Goal(State goalState, int priority, String name) { private Goal(State goalState, double priority, String name) {
this.goalState = goalState; this.goalState = goalState;
this.priority = priority; this.priority = priority;
this.name = name; this.name = name;
} }
public static Goal makeGoal(State goalState, int priority){ public static Goal makeGoal(State goalState, double priority){
return new Goal(goalState, priority); return new Goal(goalState, priority);
} }
public static Goal makeGoal(State goalState, int priority, String name){ public static Goal makeGoal(State goalState, double priority, String name){
return new Goal(goalState, priority, name); return new Goal(goalState, priority, name);
@ -43,7 +43,7 @@ public class Goal {
return goalState; return goalState;
} }
public int getPriority() { public double getPriority() {
return priority; return priority;
} }
@ -67,14 +67,19 @@ public class Goal {
Goal goal = (Goal) o; Goal goal = (Goal) o;
if (priority != goal.priority) return false; if (Double.compare(goal.priority, priority) != 0) return false;
return goalState.equals(goal.goalState); if (goalState != null ? !goalState.equals(goal.goalState) : goal.goalState != null) return false;
return name != null ? name.equals(goal.name) : goal.name == null;
} }
@Override @Override
public int hashCode() { public int hashCode() {
int result = goalState.hashCode(); int result;
result = 31 * result + priority; long temp;
result = goalState != null ? goalState.hashCode() : 0;
temp = Double.doubleToLongBits(priority);
result = 31 * result + (int) (temp ^ (temp >>> 32));
result = 31 * result + (name != null ? name.hashCode() : 0);
return result; return result;
} }
} }

View file

@ -32,7 +32,7 @@ public class GoalTracker {
} }
public boolean adoptGoal(Goal goal) { public Optional<Plan> adoptGoal(Goal goal) {
@ -40,7 +40,7 @@ public class GoalTracker {
if (!possiblePlans.isPresent()) { if (!possiblePlans.isPresent()) {
return false; return Optional.empty();
} else if (possiblePlans.get().isEmpty()) { } else if (possiblePlans.get().isEmpty()) {
@ -50,14 +50,16 @@ public class GoalTracker {
Set<Plan> plans = possiblePlans.get(); Set<Plan> plans = possiblePlans.get();
Optional<Plan> noConflictPlan = plans.stream().filter(plan -> plan.noConflicts(currentGoals)).findAny();
if (plans.stream().anyMatch(plan -> plan.noConflicts(currentGoals))) {
if (noConflictPlan.isPresent()) {
/* /*
* If there is any plan without any goal conflicts, then adopt the goal. * If there is any plan without any goal conflicts, then adopt the goal.
*/ */
currentGoals.add(goal); currentGoals.add(goal);
return true; return noConflictPlan;
} else { } else {
@ -70,32 +72,34 @@ public class GoalTracker {
*/ */
boolean feasiblePlanExists = false; boolean feasiblePlanExists = false;
int bestPriorityGap = 0; double bestPriorityGap = 0;
Set<Goal> bestRemovalCandidates = null; Set<Goal> bestRemovalCandidates = null;
Plan feasiblePlan = null;
for (Plan plan : plans) { for (Plan plan : plans) {
Set<Goal> conflictingGoals = plan.getConflictingGoals(currentGoals); Set<Goal> conflictingGoals = plan.getConflictingGoals(currentGoals);
int conflictSum = conflictingGoals.stream().mapToInt(Goal::getPriority).sum(); double conflictSum = conflictingGoals.stream().mapToDouble(Goal::getPriority).sum();
int gap = goal.getPriority() - conflictSum; double gap = goal.getPriority() - conflictSum;
if(gap > 0 && gap > bestPriorityGap ){ if(gap > 0 && gap > bestPriorityGap ){
feasiblePlanExists = true; feasiblePlanExists = true;
bestPriorityGap = gap; bestPriorityGap = gap;
feasiblePlan = plan;
bestRemovalCandidates= conflictingGoals; bestRemovalCandidates= conflictingGoals;
} }
} }
if(feasiblePlanExists){ if(feasiblePlan!=null){
currentGoals.removeAll(bestRemovalCandidates); currentGoals.removeAll(bestRemovalCandidates);
currentGoals.add(goal); currentGoals.add(goal);
return true; return Optional.of(feasiblePlan);
} }
else { else {
return false; return Optional.empty();
} }
@ -107,5 +111,7 @@ public class GoalTracker {
} }
public Set<Goal> getCurrentGoals() {
return currentGoals;
}
} }

View file

@ -66,7 +66,7 @@ public class GoalTrackingProblem {
String name = entry.getKey().toString(); String name = entry.getKey().toString();
Map<?, ?> goalSpec = (Map<?,?>)entry.getValue(); Map<?, ?> goalSpec = (Map<?,?>)entry.getValue();
int priority = Math.toIntExact((Long) goalSpec.get(PRIORITY)); double priority = ((Double) goalSpec.get(PRIORITY));
Set<Formula> stateFormulae = PlanningProblem.readFrom((List<?>) goalSpec.get(STATE)); Set<Formula> stateFormulae = PlanningProblem.readFrom((List<?>) goalSpec.get(STATE));
goals.add(Goal.makeGoal(State.initializeWith(stateFormulae), priority, name)); goals.add(Goal.makeGoal(State.initializeWith(stateFormulae), priority, name));

View file

@ -3,18 +3,25 @@ package edu.rpi.rair.utils;
import com.diogonunes.jcdp.color.ColoredPrinter; import com.diogonunes.jcdp.color.ColoredPrinter;
import com.diogonunes.jcdp.color.api.Ansi; import com.diogonunes.jcdp.color.api.Ansi;
import com.naveensundarg.shadow.prover.utils.Reader; import com.naveensundarg.shadow.prover.utils.Reader;
import edu.rpi.rair.Goal;
import edu.rpi.rair.GoalTracker; import edu.rpi.rair.GoalTracker;
import edu.rpi.rair.Plan;
import edu.rpi.rair.Planner; import edu.rpi.rair.Planner;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/** /**
* Created by naveensundarg on 1/15/17. * Created by naveensundarg on 1/15/17.
*/ */
public class RunDemo { public class RunDemo {
static ColoredPrinter cp = new ColoredPrinter.Builder(1, false).build();
public static void main(String[] args) throws Reader.ParsingException { public static void main(String[] args) throws Reader.ParsingException {
List<GoalTrackingProblem> goalTrackingProblemList = (GoalTrackingProblem.readFromFile(Planner.class.getResourceAsStream("goal_tracking_tests.clj"))); List<GoalTrackingProblem> goalTrackingProblemList = (GoalTrackingProblem.readFromFile(Planner.class.getResourceAsStream("goal_tracking_tests.clj")));
@ -25,31 +32,104 @@ public class RunDemo {
goalTrackingProblem.getPlanningProblem().getActions()); goalTrackingProblem.getPlanningProblem().getActions());
ColoredPrinter cp = new ColoredPrinter.Builder(1, false).build(); Goal g1 = goalTrackingProblem.getGoalNamed("G1");
Goal g2 = goalTrackingProblem.getGoalNamed("G2");
Goal g3 = goalTrackingProblem.getGoalNamed("G3");
cp.setForegroundColor(Ansi.FColor.WHITE); Goal g4 = goalTrackingProblem.getGoalNamed("G4");
cp.setBackgroundColor(Ansi.BColor.BLUE); //setting format Goal g5 = goalTrackingProblem.getGoalNamed("G5");
cp.println("Adding goal G1");
cp.clear();
boolean res1 = goalTracker.adoptGoal(goalTrackingProblem.getGoalNamed("G1"));
cp.setForegroundColor(Ansi.FColor.WHITE);
cp.setBackgroundColor(Ansi.BColor.BLUE); //setting format
cp.println("Adding goal G2");
cp.clear();
goalTracker.adoptGoal(goalTrackingProblem.getGoalNamed("G2"));
cp.setForegroundColor(Ansi.FColor.WHITE);
cp.setBackgroundColor(Ansi.BColor.BLUE); //setting format
cp.println("Adding goal G3");
cp.clear();
goalTracker.adoptGoal(goalTrackingProblem.getGoalNamed("G3"));
tryAndAddGoal(g1, goalTracker);
tryAndAddGoal(g2, goalTracker);
tryAndAddGoal(g3, goalTracker);
tryAndAddGoal(g4, goalTracker);
tryAndAddGoal(g5, goalTracker);
} }
static void tryAndAddGoal(Goal g, GoalTracker goalTracker) {
System.out.println("========================");
printInfo("Trying to Add Goal:", g.getName());
Optional<Plan> possibleGoalPlan = goalTracker.adoptGoal(g);
if (possibleGoalPlan.isPresent()) {
printSuccess("Successfully added:" , g.getName());
printDebug1("Current Goals:" , goalTracker.getCurrentGoals().stream().map(Goal::getName).collect(Collectors.toSet()).toString());
Plan plan = possibleGoalPlan.get();
printDebug2("Plan:" , plan.getActions().isEmpty()? "No plan needed. Already satisfied." : plan.getActions().toString() );
} else {
printFailure("Could not add " + g.getName());
printDebug1("Current Goals: " , goalTracker.getCurrentGoals().stream().map(Goal::getName).collect(Collectors.toSet()).toString());
}
}
static void printInfo(String header, String message) {
cp.setForegroundColor(Ansi.FColor.WHITE);
cp.setBackgroundColor(Ansi.BColor.BLUE); //setting format
cp.print(header);
cp.clear();
cp.print(" ");
cp.setAttribute(Ansi.Attribute.BOLD);
cp.print(message);
cp.println("");
cp.clear();
}
static void printSuccess(String header, String message) {
cp.setForegroundColor(Ansi.FColor.BLACK);
cp.setBackgroundColor(Ansi.BColor.GREEN); //setting format
cp.print(header);
cp.clear();
cp.print(" ");
cp.setAttribute(Ansi.Attribute.BOLD);
cp.print(message);
cp.println("");
cp.clear();
}
static void printDebug1(String header, String message) {
cp.setForegroundColor(Ansi.FColor.BLACK);
cp.setBackgroundColor(Ansi.BColor.YELLOW); //setting format
cp.print(header);
cp.clear();
cp.print(" ");
cp.setAttribute(Ansi.Attribute.BOLD);
cp.print(message);
cp.println("");
cp.clear();
}
static void printDebug2(String header, String message) {
cp.setForegroundColor(Ansi.FColor.BLACK);
cp.setBackgroundColor(Ansi.BColor.MAGENTA); //setting format
cp.print(header);
cp.clear();
cp.print(" ");
cp.setAttribute(Ansi.Attribute.BOLD);
cp.print(message);
cp.println("");
cp.clear();
}
static void printFailure(String message) {
cp.setForegroundColor(Ansi.FColor.WHITE);
cp.setBackgroundColor(Ansi.BColor.RED); //setting format
cp.print(message);
cp.clear();
cp.println("");
cp.clear();
}
} }

View file

@ -127,7 +127,9 @@
(in prisoner room1) (in prisoner room1)
(open (door room2)) (open (door room2))
(not (open (door room1)))] (not (open (door room1)))]
:goal [(interrogates commander prisoner)] :goal [(interrogates commander prisoner)]
:actions :actions
[(define-action open-door [?room] [(define-action open-door [?room]
{:preconditions [(not (open (door ?room)))] {:preconditions [(not (open (door ?room)))]
@ -135,6 +137,7 @@
:deletions [(not (open (door ?room)))]}) :deletions [(not (open (door ?room)))]})
(define-action accompany [?person ?room1 ?room2] (define-action accompany [?person ?room1 ?room2]
{:preconditions [(not (= ?room1 ?room2)) {:preconditions [(not (= ?room1 ?room2))
(in ?person ?room1) (in ?person ?room1)
@ -144,17 +147,18 @@
:additions [(in ?person ?room2) :additions [(in ?person ?room2)
(in self ?room2)] (in self ?room2)]
:deletions [(in ?person ?room1) :deletions [(in ?person ?room1)
(in self ?room1)]}) (in self ?room1)]})
(define-action request-move [?person ?room2 ?room1] (define-action move [?person ?room2 ?room1]
{:preconditions [(not (= ?room1 ?room2)) {:preconditions [(not (= ?room1 ?room2))
(in ?person ?room2) (in ?person ?room2)
(not (= ?person prisoner))
(open (door ?room1)) (open (door ?room1))
(open (door ?room2))] (open (door ?room2))]
:additions [(in ?person ?room1)] :additions [(in ?person ?room1)]
:deletions [(in ?person ?room2)]}) :deletions [(in ?person ?room2)]})
(define-action get-interrogated [?room] (define-action get-interrogated [?room]
@ -162,23 +166,20 @@
(in prisoner ?room)] (in prisoner ?room)]
:additions [(interrogates commander prisoner)] :additions [(interrogates commander prisoner)]
:deletions []}) :deletions []})
] ]
:expected-plans ( :expected-plans ([(open-door room1)
(move commander room2 room1)
(get-interrogated room1)]
[(open-door room1) [(open-door room1)
(request-move commander room2 room1) (move prisoner room1 room2)
(get-interrogated room1) (get-interrogated room2)]
]
[(open-door room1) [(open-door room1)
(accompany prisoner room1 room2) (accompany prisoner room1 room2)
(get-interrogated room2) (get-interrogated room2)])}
]
)
}

View file

@ -1,62 +1,84 @@
{:definitions {:definitions
{:name "Moving Between Rooms" {:name "demo 1"
:background [(not (= room1 room2)) :background [
(forall [?x] (iff (Locked ?x) (not (Open ?x)))) (forall [?x ?room1 ?room2]
(forall [?x ?y ?z] (if (and (In ?x ?y) (not (= ?z ?y))) (if (not (= ?room1 ?room2))
(not (In ?x ?z))))] (if (in ?x ?room1) (not (in ?x ?room2))) ))
:start [(In self room1) (not (= room1 room2))
(In commander room2) (not (= prisoner commander))
(In prisoner room1) (not (= self prisoner))
(Open (door room2)) (not (= self commander))
(not (Open (door room1)))] (person prisoner)
(person commander)
]
:start [(in self room1)
(in commander room2)
(in prisoner room1)
(open (door room2))
(not (open (door room1)))]
:goal [] :goal []
:actions :actions
[(define-action open-door [?room] [(define-action open-door [?room]
{:preconditions [(not (Open (door ?room)))] {:preconditions [(not (open (door ?room)))]
:additions [(Open (door ?room))] :additions [(open (door ?room))]
:deletions [(not (Open (door ?room)))]}) :deletions [(not (open (door ?room)))]})
(define-action move-thing-from-to [?thing ?room1 ?room2]
(define-action accompany [?person ?room1 ?room2]
{:preconditions [(not (= ?room1 ?room2)) {:preconditions [(not (= ?room1 ?room2))
(In ?thing ?room1) (in ?person ?room1)
(Open (door ?room1)) (in self ?room1)
(Open (door ?room2))] (open (door ?room1))
(open (door ?room2))]
:additions [(In ?thing ?room2)] :additions [(in ?person ?room2)
:deletions [(In ?thing ?room1) (in self ?room2)]
(In self ?room1)]})
(define-action accompany-from-to [?thing ?room1 ?room2] :deletions [(in ?person ?room1)
(in self ?room1)]})
(define-action move [?person ?room2 ?room1]
{:preconditions [(not (= ?room1 ?room2)) {:preconditions [(not (= ?room1 ?room2))
(In self ?room1) (in ?person ?room2)
(In ?thing ?room1) (open (door ?room1))
(Open (door ?room1)) (open (door ?room2))]
(Open (door ?room2))]
:additions [(In ?thing ?room2) :additions [(in ?person ?room1)]
(In ?self ?room2)]
:deletions [(In ?thing ?room1)
(In self ?room1)]})
(define-action interrogate [?A ?B] :deletions [(in ?person ?room2)]})
{:preconditions [(In ?room ?A)
(In ?room ?B)]
:additions [(Interrogates ?A ?B)] (define-action get-interrogated [?room]
:deletions [(In ?thing ?room1) {:preconditions [(in commander ?room)
(In self ?room1)]})] (in prisoner ?room)]
:additions [(interrogates commander prisoner)]
:deletions []})
]
} }
:goals {G1 {:priority 1 :goals {G1 {:priority 1.0
:state [(not (Open (door room1)))]} :state [(not (open (door room1)))]}
G2 {:priority 1 G2 {:priority 1.0
:state [(In prisoner room1)]} :state [(in prisoner room1)]}
G3 {:priority 1 G3 {:priority 1.0
:state [(forall [?room] :state [(forall [?room]
(if (In prisoner ?room) (if (in prisoner ?room)
(In self ?room)))] (in self ?room)))]}
}
G4 {:priority 2.0
:state [(interrogates commander prisoner)]}
G5 {:priority 1.0
:state [(in prisoner room2)
(in self room2)]}
} }

View file

@ -35,7 +35,6 @@ public class GoalTrackerTest {
cp.println("Adding goal G1"); cp.println("Adding goal G1");
cp.clear(); cp.clear();
boolean res1 = goalTracker.adoptGoal(goalTrackingProblem.getGoalNamed("G1"));
cp.setForegroundColor(Ansi.FColor.WHITE); cp.setForegroundColor(Ansi.FColor.WHITE);
cp.setBackgroundColor(Ansi.BColor.BLUE); //setting format cp.setBackgroundColor(Ansi.BColor.BLUE); //setting format

View file

@ -1,62 +1,84 @@
{:definitions {:definitions
{:name "Moving Between Rooms" {:name "demo 1"
:background [(not (= room1 room2)) :background [
(forall [?x] (iff (Locked ?x) (not (Open ?x)))) (forall [?x ?room1 ?room2]
(forall [?x ?y ?z] (if (and (In ?x ?y) (not (= ?z ?y))) (if (not (= ?room1 ?room2))
(not (In ?x ?z))))] (if (in ?x ?room1) (not (in ?x ?room2))) ))
:start [(In self room1) (not (= room1 room2))
(In commander room2) (not (= prisoner commander))
(In prisoner room1) (not (= self prisoner))
(Open (door room2)) (not (= self commander))
(not (Open (door room1)))] (person prisoner)
(person commander)
]
:start [(in self room1)
(in commander room2)
(in prisoner room1)
(open (door room2))
(not (open (door room1)))]
:goal [] :goal []
:actions :actions
[(define-action open-door [?room] [(define-action open-door [?room]
{:preconditions [(not (Open (door ?room)))] {:preconditions [(not (open (door ?room)))]
:additions [(Open (door ?room))] :additions [(open (door ?room))]
:deletions [(not (Open (door ?room)))]}) :deletions [(not (open (door ?room)))]})
(define-action move-thing-from-to [?thing ?room1 ?room2]
(define-action accompany [?person ?room1 ?room2]
{:preconditions [(not (= ?room1 ?room2)) {:preconditions [(not (= ?room1 ?room2))
(In ?thing ?room1) (in ?person ?room1)
(Open (door ?room1)) (in self ?room1)
(Open (door ?room2))] (open (door ?room1))
(open (door ?room2))]
:additions [(In ?thing ?room2)] :additions [(in ?person ?room2)
:deletions [(In ?thing ?room1) (in self ?room2)]
(In self ?room1)]})
(define-action accompany-from-to [?thing ?room1 ?room2] :deletions [(in ?person ?room1)
(in self ?room1)]})
(define-action move [?person ?room2 ?room1]
{:preconditions [(not (= ?room1 ?room2)) {:preconditions [(not (= ?room1 ?room2))
(In self ?room1) (in ?person ?room2)
(In ?thing ?room1) (open (door ?room1))
(Open (door ?room1)) (open (door ?room2))]
(Open (door ?room2))]
:additions [(In ?thing ?room2) :additions [(in ?person ?room1)]
(In ?self ?room2)]
:deletions [(In ?thing ?room1)
(In self ?room1)]})
(define-action interrogate [?A ?B] :deletions [(in ?person ?room2)]})
{:preconditions [(In ?room ?A)
(In ?room ?B)]
:additions [(Interrogates ?A ?B)] (define-action get-interrogated [?room]
:deletions [(In ?thing ?room1) {:preconditions [(in commander ?room)
(In self ?room1)]})] (in prisoner ?room)]
:additions [(interrogates commander prisoner)]
:deletions []})
]
} }
:goals {G1 {:priority 1 :goals {G1 {:priority 1.0
:state [(not (Open (door room1)))]} :state [(not (open (door room1)))]}
G2 {:priority 1 G2 {:priority 1.0
:state [(In prisoner room1)]} :state [(in prisoner room1)]}
G3 {:priority 1 G3 {:priority 1.0
:state [(forall [?room] :state [(forall [?room]
(if (In prisoner ?room) (if (in prisoner ?room)
(In self ?room)))] (in self ?room)))]}
}
G4 {:priority 2.0
:state [(interrogates commander prisoner)]}
G5 {:priority 1.0
:state [(in prisoner room2)
(in self room2)]}
} }