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 {
private final State goalState;
private final int priority;
private final double priority;
private final String name;
private static final AtomicInteger nameCounter;
static {
nameCounter = new AtomicInteger(0);
}
private Goal(State goalState, int priority) {
private Goal(State goalState, double priority) {
this.goalState = goalState;
this.priority = priority;
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.priority = priority;
this.name = name;
}
public static Goal makeGoal(State goalState, int priority){
public static Goal makeGoal(State goalState, double 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);
@ -43,7 +43,7 @@ public class Goal {
return goalState;
}
public int getPriority() {
public double getPriority() {
return priority;
}
@ -67,14 +67,19 @@ public class Goal {
Goal goal = (Goal) o;
if (priority != goal.priority) return false;
return goalState.equals(goal.goalState);
if (Double.compare(goal.priority, priority) != 0) return false;
if (goalState != null ? !goalState.equals(goal.goalState) : goal.goalState != null) return false;
return name != null ? name.equals(goal.name) : goal.name == null;
}
@Override
public int hashCode() {
int result = goalState.hashCode();
result = 31 * result + priority;
int result;
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;
}
}

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()) {
return false;
return Optional.empty();
} else if (possiblePlans.get().isEmpty()) {
@ -50,14 +50,16 @@ public class GoalTracker {
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.
*/
currentGoals.add(goal);
return true;
return noConflictPlan;
} else {
@ -70,32 +72,34 @@ public class GoalTracker {
*/
boolean feasiblePlanExists = false;
int bestPriorityGap = 0;
double bestPriorityGap = 0;
Set<Goal> bestRemovalCandidates = null;
Plan feasiblePlan = null;
for (Plan plan : plans) {
Set<Goal> conflictingGoals = plan.getConflictingGoals(currentGoals);
int conflictSum = conflictingGoals.stream().mapToInt(Goal::getPriority).sum();
int gap = goal.getPriority() - conflictSum;
double conflictSum = conflictingGoals.stream().mapToDouble(Goal::getPriority).sum();
double gap = goal.getPriority() - conflictSum;
if(gap > 0 && gap > bestPriorityGap ){
feasiblePlanExists = true;
bestPriorityGap = gap;
feasiblePlan = plan;
bestRemovalCandidates= conflictingGoals;
}
}
if(feasiblePlanExists){
if(feasiblePlan!=null){
currentGoals.removeAll(bestRemovalCandidates);
currentGoals.add(goal);
return true;
return Optional.of(feasiblePlan);
}
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();
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));
goals.add(Goal.makeGoal(State.initializeWith(stateFormulae), priority, name));

View file

@ -3,17 +3,24 @@ package edu.rpi.rair.utils;
import com.diogonunes.jcdp.color.ColoredPrinter;
import com.diogonunes.jcdp.color.api.Ansi;
import com.naveensundarg.shadow.prover.utils.Reader;
import edu.rpi.rair.Goal;
import edu.rpi.rair.GoalTracker;
import edu.rpi.rair.Plan;
import edu.rpi.rair.Planner;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* Created by naveensundarg on 1/15/17.
*/
public class RunDemo {
public static void main(String[] args) throws Reader.ParsingException {
static ColoredPrinter cp = new ColoredPrinter.Builder(1, false).build();
public static void main(String[] args) throws Reader.ParsingException {
List<GoalTrackingProblem> goalTrackingProblemList = (GoalTrackingProblem.readFromFile(Planner.class.getResourceAsStream("goal_tracking_tests.clj")));
@ -25,31 +32,104 @@ public class RunDemo {
goalTrackingProblem.getPlanningProblem().getActions());
ColoredPrinter cp = new ColoredPrinter.Builder(1, false).build();
cp.setForegroundColor(Ansi.FColor.WHITE);
cp.setBackgroundColor(Ansi.BColor.BLUE); //setting format
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"));
Goal g1 = goalTrackingProblem.getGoalNamed("G1");
Goal g2 = goalTrackingProblem.getGoalNamed("G2");
Goal g3 = goalTrackingProblem.getGoalNamed("G3");
Goal g4 = goalTrackingProblem.getGoalNamed("G4");
Goal g5 = goalTrackingProblem.getGoalNamed("G5");
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();
}
}