mirror of
https://github.com/RAIRLab/Spectra.git
synced 2025-10-26 22:51:19 +00:00
Speeding up things
This commit is contained in:
parent
b2da460d5c
commit
bfaaf6d882
5 changed files with 319 additions and 105 deletions
|
|
@ -5,6 +5,7 @@ import com.naveensundarg.shadow.prover.utils.CollectionUtils;
|
|||
import com.naveensundarg.shadow.prover.utils.Pair;
|
||||
import com.naveensundarg.shadow.prover.utils.Sets;
|
||||
|
||||
import javax.swing.text.html.Option;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
|
@ -15,22 +16,40 @@ public class DepthFirstPlanner implements Planner {
|
|||
|
||||
|
||||
private static final int MAX_DEPTH = 4;
|
||||
private static final boolean EXHAUSTIVE_TILL_MAX_DEPTH = true;
|
||||
|
||||
|
||||
@Override
|
||||
public Optional<Set<Plan>> plan(Set<Formula> background, Set<Action> actions, State start, State goal) {
|
||||
|
||||
|
||||
return planInternal(Sets.newSet(), 0, background, actions, start, goal);
|
||||
if (!EXHAUSTIVE_TILL_MAX_DEPTH) {
|
||||
|
||||
return planInternal(Sets.newSet(), 0, MAX_DEPTH, background, actions, start, goal);
|
||||
|
||||
} else {
|
||||
|
||||
for (int i = 1; i <= MAX_DEPTH; i++) {
|
||||
|
||||
Optional<Set<Plan>> plans = planInternal(Sets.newSet(), 0, i, background, actions, start, goal);
|
||||
|
||||
if (plans.isPresent()) {
|
||||
return plans;
|
||||
}
|
||||
|
||||
}
|
||||
//
|
||||
return Optional.empty();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private Optional<Set<Plan>> planInternal(Set<Pair<State, Action>> history, int currentDepth, int maxDepth, Set<Formula> background, Set<Action> actions, State start, State goal) {
|
||||
|
||||
private Optional<Set<Plan>> planInternal(Set<Pair<State, Action>> history, int currentDepth, Set<Formula> background, Set<Action> actions, State start, State goal) {
|
||||
|
||||
if(currentDepth>=MAX_DEPTH){
|
||||
if (currentDepth >= maxDepth) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
|
|
@ -52,8 +71,7 @@ public class DepthFirstPlanner implements Planner {
|
|||
for (Pair<State, Action> stateActionPair : nextStateActionPairs.get()) {
|
||||
|
||||
|
||||
|
||||
Optional<Set<Plan>> planOpt = planInternal(history, currentDepth+1, background, actions, stateActionPair.first(), goal);
|
||||
Optional<Set<Plan>> planOpt = planInternal(history, currentDepth + 1, maxDepth, background, actions, stateActionPair.first(), goal);
|
||||
|
||||
if (planOpt.isPresent()) {
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,11 @@ package edu.rpi.rair.utils;
|
|||
|
||||
import com.diogonunes.jcdp.color.ColoredPrinter;
|
||||
import com.diogonunes.jcdp.color.api.Ansi;
|
||||
import com.naveensundarg.shadow.prover.Sandbox;
|
||||
import com.naveensundarg.shadow.prover.core.Problem;
|
||||
import com.naveensundarg.shadow.prover.core.Prover;
|
||||
import com.naveensundarg.shadow.prover.core.SnarkWrapper;
|
||||
import com.naveensundarg.shadow.prover.utils.ProblemReader;
|
||||
import com.naveensundarg.shadow.prover.utils.Reader;
|
||||
import edu.rpi.rair.Goal;
|
||||
import edu.rpi.rair.GoalTracker;
|
||||
|
|
@ -19,37 +24,124 @@ public class RunDemo {
|
|||
|
||||
static ColoredPrinter cp = new ColoredPrinter.Builder(1, false).build();
|
||||
|
||||
|
||||
static {
|
||||
|
||||
Prover prover = new SnarkWrapper();
|
||||
try {
|
||||
List<Problem> problems = ProblemReader.readFrom(Sandbox.class.getResourceAsStream("firstorder-completness-tests.clj"));
|
||||
|
||||
problems.forEach(problem -> {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
prover.prove(problem.getAssumptions(), problem.getGoal());
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
planningProblemWarmUp();
|
||||
System.out.println("WARM UP DONE");
|
||||
} catch (Reader.ParsingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Reader.ParsingException {
|
||||
|
||||
|
||||
List<GoalTrackingProblem> goalTrackingProblemList = (GoalTrackingProblem.readFromFile(Planner.class.getResourceAsStream("goal_management_2.clj")));
|
||||
|
||||
System.out.println();
|
||||
|
||||
List<GoalTrackingProblem> goalTrackingProblemList = (GoalTrackingProblem.readFromFile(Planner.class.getResourceAsStream("goal_management_3.clj")));
|
||||
|
||||
|
||||
GoalTrackingProblem goalTrackingProblem = goalTrackingProblemList.get(0);
|
||||
GoalTrackingProblem goalTrackingProblem = goalTrackingProblemList.get(0);
|
||||
|
||||
GoalTracker goalTracker = new GoalTracker(goalTrackingProblem.getPlanningProblem().getBackground(),
|
||||
goalTrackingProblem.getPlanningProblem().getStart(),
|
||||
goalTrackingProblem.getPlanningProblem().getActions());
|
||||
GoalTracker goalTracker = new GoalTracker(goalTrackingProblem.getPlanningProblem().getBackground(),
|
||||
goalTrackingProblem.getPlanningProblem().getStart(),
|
||||
goalTrackingProblem.getPlanningProblem().getActions());
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
long start = System.currentTimeMillis();
|
||||
|
||||
Goal g1 = goalTrackingProblem.getGoalNamed("G1");
|
||||
Goal g2 = goalTrackingProblem.getGoalNamed("G2");
|
||||
Goal g1 = goalTrackingProblem.getGoalNamed("G1");
|
||||
Goal g2 = goalTrackingProblem.getGoalNamed("G2");
|
||||
Goal g3 = goalTrackingProblem.getGoalNamed("G3");
|
||||
Goal g4 = goalTrackingProblem.getGoalNamed("G4");
|
||||
|
||||
tryAndAddGoal(g1, goalTracker);
|
||||
tryAndAddGoal(g2, goalTracker);
|
||||
|
||||
long end = System.currentTimeMillis();
|
||||
tryAndAddGoal(g1, goalTracker);
|
||||
|
||||
cp.println("--------------------------");
|
||||
cp.setForegroundColor(Ansi.FColor.CYAN);
|
||||
tryAndAddGoal(g2, goalTracker);
|
||||
|
||||
cp.print("Time Taken:");
|
||||
cp.clear();
|
||||
cp.print(" ");
|
||||
cp.setAttribute(Ansi.Attribute.BOLD);
|
||||
cp.print((end-start)/1000 + "s");
|
||||
tryAndAddGoal(g3, goalTracker);
|
||||
|
||||
tryAndAddGoal(g4, goalTracker);
|
||||
|
||||
|
||||
long end = System.currentTimeMillis();
|
||||
|
||||
cp.println("--------------------------");
|
||||
cp.setForegroundColor(Ansi.FColor.CYAN);
|
||||
|
||||
cp.print("Time Taken:");
|
||||
cp.clear();
|
||||
cp.print(" ");
|
||||
cp.setAttribute(Ansi.Attribute.BOLD);
|
||||
cp.print((end - start) / 1000 + "s");
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static void planningProblemWarmUp() throws Reader.ParsingException {
|
||||
|
||||
|
||||
for (int i = 0; i < 5; i++) {
|
||||
|
||||
System.out.println();
|
||||
|
||||
List<GoalTrackingProblem> goalTrackingProblemList = (GoalTrackingProblem.readFromFile(Planner.class.getResourceAsStream("goal_management_1.clj")));
|
||||
|
||||
|
||||
GoalTrackingProblem goalTrackingProblem = goalTrackingProblemList.get(0);
|
||||
|
||||
GoalTracker goalTracker = new GoalTracker(goalTrackingProblem.getPlanningProblem().getBackground(),
|
||||
goalTrackingProblem.getPlanningProblem().getStart(),
|
||||
goalTrackingProblem.getPlanningProblem().getActions());
|
||||
|
||||
long start = System.currentTimeMillis();
|
||||
|
||||
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");
|
||||
|
||||
|
||||
goalTracker.adoptGoal(g1);
|
||||
|
||||
System.out.print(".");
|
||||
|
||||
goalTracker.adoptGoal(g2);
|
||||
|
||||
|
||||
System.out.print(".");
|
||||
|
||||
goalTracker.adoptGoal(g3);
|
||||
|
||||
System.out.print(".");
|
||||
|
||||
goalTracker.adoptGoal(g4);
|
||||
|
||||
System.out.print(".");
|
||||
|
||||
goalTracker.adoptGoal(g5);
|
||||
|
||||
System.out.print(".");
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -57,20 +149,20 @@ public class RunDemo {
|
|||
static void tryAndAddGoal(Goal g, GoalTracker goalTracker) {
|
||||
|
||||
System.out.println("========================");
|
||||
printInfo("Trying to Add Goal:", g.getName());
|
||||
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());
|
||||
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() );
|
||||
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());
|
||||
printDebug1("Current Goals: ", goalTracker.getCurrentGoals().stream().map(Goal::getName).collect(Collectors.toSet()).toString());
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -103,8 +195,6 @@ public class RunDemo {
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static void printDebug1(String header, String message) {
|
||||
|
||||
cp.setForegroundColor(Ansi.FColor.BLACK);
|
||||
|
|
@ -118,7 +208,7 @@ public class RunDemo {
|
|||
cp.clear();
|
||||
}
|
||||
|
||||
static void printDebug2(String header, String message) {
|
||||
static void printDebug2(String header, String message) {
|
||||
|
||||
cp.setForegroundColor(Ansi.FColor.BLACK);
|
||||
cp.setBackgroundColor(Ansi.BColor.MAGENTA); //setting format
|
||||
|
|
@ -130,6 +220,7 @@ public class RunDemo {
|
|||
cp.println("");
|
||||
cp.clear();
|
||||
}
|
||||
|
||||
static void printFailure(String message) {
|
||||
|
||||
cp.setForegroundColor(Ansi.FColor.WHITE);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue