Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: nthykier/school-stuff
base: c71724ffa9
...
head fork: nthykier/school-stuff
compare: 0658e6ec2a
  • 3 commits
  • 11 files changed
  • 0 commit comments
  • 1 contributor
View
28 src/dk/dtu/mas/HappyGame.java
@@ -80,13 +80,40 @@ public void plannersSetup() {
//TODO add initial desire dividing between planners
}
+ public boolean checkIfAllActionsNoop(Action[] al) {
+ int counter=0;
+ for (int i=0; i<al.length; i++) {
+ if (Action.NO_OP.equals(al[i])) {
+ counter++;
+ }
+ }
+ if (counter == al.length) {
+ return true;
+ }
+ return false;
+ }
+
+ //debug
+ public void printPlannersReservations() {
+ /*System.err.println("_______________reservarions");
+ for (int z=0;z<planners.size();z++){
+ System.err.println("actor:"+planners.get(z).getActor().getObjectLetter()+" reserved: "+((HappyPlanner)planners.get(z)).getReservations().toString());
+ }*/
+ }
+
/**
* main loop called from tests (as opposed from server loop which is called from sokoban game and includes action resolving on the server side
*/
public void mainLoop() { //this loop is for the tests only, SokobanGame will have its own loop similar to this one but using server communication methods
Action[] acts = new Action[map.getActorCount()];
while(true) {
+ //printPlannersReservations();
+
getPlannersNextActions(acts);
+ if (checkIfAllActionsNoop(acts)) {
+ System.err.println("all noops, breaking");
+ break;
+ }
//System.err.println(formatActionsArrayToOutputString(acts));
try {
map.applyActions(acts);
@@ -357,6 +384,7 @@ private static final SokobanPlanner getPlannerForActor(List<? extends SokobanPla
}
public void mapFromServerSetup() {
+ this.map = new SimpleSokobanMap();
try {
this.map.parseMap(System.in);
} catch (IOException e) {
View
10 src/dk/dtu/mas/SokobanGame.java
@@ -9,6 +9,14 @@
public class SokobanGame {
+ public static void main(String[] args) throws IOException {
+ HappyGame g = new HappyGame();
+ g.serverLikeLoop();
+
+ }
+
+
+
/**
* The main game function setup: map planners agents etc loop: ask for an
* action from each planner (do in order) create action list from the
@@ -23,7 +31,7 @@
* @param args
* @throws IOException
*/
- public static void main(String[] args) throws IOException {
+ public void mainOLD(String[] args) throws IOException {
SimpleSokobanMap map = new SimpleSokobanMap();
map.parseMap(System.in);
View
2  src/dk/dtu/mas/SokobanMap.java
@@ -115,5 +115,7 @@
public MapClassification getMapClassification();
+ public List<SokobanActor> getAllActors();
+
}
View
11 src/dk/dtu/mas/map/SimpleSokobanMap.java
@@ -113,6 +113,17 @@ public int getActorCount() {
}
@Override
+ public List<SokobanActor> getAllActors() {
+ List<SokobanActor> asd = new ArrayList<SokobanActor>();
+ for (int i=0;i<actors.length;i++) {
+ if (actors[i] != null) {
+ asd.add(actors[i]);
+ }
+ }
+ return asd;
+ }
+
+ @Override
public SokobanActor getActor(int id) {
if (id < 0 || id >= actorCount) {
throw new IndexOutOfBoundsException(id + " - " + actorCount);
View
2  src/dk/dtu/mas/path/CostFunction.java
@@ -27,4 +27,6 @@
* @return The cost.
*/
public int price(MapNode p1, MapNode p2);
+
+ public CostFunction getCostFunction();
}
View
4 src/dk/dtu/mas/path/ManhattanCost.java
@@ -17,4 +17,8 @@ public int price(MapNode p1, MapNode p2) {
public int estimatedCost(MapNode p1, MapNode p2) {
return Math.abs(p2.x-p1.x)+Math.abs(p2.y-p1.y);
}
+
+ public CostFunction getCostFunction() {
+ return null;
+ }
}
View
33 src/dk/dtu/mas/path/ReservedNodesCostFunction.java
@@ -0,0 +1,33 @@
+package dk.dtu.mas.path;
+
+import dk.dtu.mas.MapNode;
+import dk.dtu.mas.SokobanActor;
+
+public class ReservedNodesCostFunction implements CostFunction {
+
+ private CostFunction cfin;
+ private SokobanActor actor;
+
+ public ReservedNodesCostFunction(SokobanActor a, CostFunction cc) {
+ this.cfin = cc;
+ this.actor = a;
+ }
+
+ @Override
+ public int price(MapNode p1, MapNode p2) {
+ if (p2.isReserved() && !actor.equals(p2.getReserver())) {
+ return IMPASSABLE;
+ }
+ return cfin.price(p1, p2);
+ }
+
+ @Override
+ public int estimatedCost(MapNode p1, MapNode p2) {
+ return cfin.estimatedCost(p1, p2);
+ }
+
+ @Override
+ public CostFunction getCostFunction() {
+ return cfin;
+ }
+}
View
151 src/dk/dtu/mas/planner/HappyPlanner.java
@@ -1,9 +1,23 @@
package dk.dtu.mas.planner;
-import static dk.dtu.mas.util.HighOrderUtils.*;
+import static dk.dtu.mas.util.HighOrderUtils.all;
+import static dk.dtu.mas.util.HighOrderUtils.any;
+import static dk.dtu.mas.util.HighOrderUtils.count;
+import static dk.dtu.mas.util.HighOrderUtils.filter;
+import static dk.dtu.mas.util.HighOrderUtils.filterCollection;
+import static dk.dtu.mas.util.HighOrderUtils.first;
import static dk.dtu.mas.util.PlannerUtils.getPositionListFromPlan;
import static dk.dtu.mas.util.PlannerUtils.isAdjPos;
-import static dk.dtu.mas.util.Predicates.*;
+import static dk.dtu.mas.util.Predicates.EMPTY_NODE;
+import static dk.dtu.mas.util.Predicates.NODE2OBJECT;
+import static dk.dtu.mas.util.Predicates.NON_EMPTY_NODE;
+import static dk.dtu.mas.util.Predicates.NOT_SOLVED_BOX;
+import static dk.dtu.mas.util.Predicates.SOLVED_BOX;
+import static dk.dtu.mas.util.Predicates.andPred;
+import static dk.dtu.mas.util.Predicates.except;
+import static dk.dtu.mas.util.Predicates.mapPred;
+import static dk.dtu.mas.util.Predicates.only;
+import static dk.dtu.mas.util.Predicates.orPred;
import java.util.ArrayList;
import java.util.Collection;
@@ -29,14 +43,15 @@
import dk.dtu.mas.SokobanObject;
import dk.dtu.mas.SokobanPlanner;
import dk.dtu.mas.map.Area;
-import dk.dtu.mas.map.Area.Type;
import dk.dtu.mas.map.MapClassification;
import dk.dtu.mas.map.MapNodeVisitor;
import dk.dtu.mas.map.PartialOrder;
import dk.dtu.mas.map.PartialOrderImpl;
+import dk.dtu.mas.map.Area.Type;
import dk.dtu.mas.path.CostFunction;
import dk.dtu.mas.path.ManhattanCost;
import dk.dtu.mas.path.PathfindingAlgorithm;
+import dk.dtu.mas.path.ReservedNodesCostFunction;
import dk.dtu.mas.planner.Desire.DesireType;
import dk.dtu.mas.planner.Intention.IntentionType;
import dk.dtu.mas.planner.Request.RequestState;
@@ -50,6 +65,7 @@
Stack<Plan> plans; //LIFO - next plan is plans.peek()
Set<MapNode> reservations;
+ public Set<MapNode> getReservations(){return reservations;}
Stack<Request> requests;
@@ -58,79 +74,47 @@
private PartialOrder<Position> partialOrder = new PartialOrderImpl<Position>();
private static final PathfindingAlgorithm algo = PathfindingAlgorithm.ASTAR;
- private final CostFunction cf_manhattan_ignore_this_actor_and_his_boxes = new ManhattanCost() {
+ private final CostFunction cf_manhattan_ignore_this_actor_and_his_boxes = new ReservedNodesCostFunction(actor,new ManhattanCost() {
@Override
public int price(MapNode s, MapNode dest) {
- if (dest.isReserved() && !actor.equals(dest.getReserver())) {
- return IMPASSABLE;
- }
SokobanObject obj = dest.getObject();
if (obj == null || obj.getObjectLetter() == actor.getObjectLetter() || (obj.isBox() && obj.getColor() == actor.getColor())) {
return 1;
}
return IMPASSABLE;
}
- };
- private final CostFunction cf_manhattan_ignore_this_actor = new ManhattanCost() {
+ });
+ private final CostFunction cf_manhattan_ignore_this_actor = new ReservedNodesCostFunction(actor,new ManhattanCost() {
@Override
public int price(MapNode s, MapNode dest) {
- if (dest.isReserved() && !actor.equals(dest.getReserver())) {
- return IMPASSABLE;
- }
SokobanObject obj = dest.getObject();
if (obj == null || obj.getObjectLetter() == actor.getObjectLetter()) {
return 1;
}
return IMPASSABLE;
}
- };
- private final CostFunction cf_manhattan_ignore_all_actors = new ManhattanCost() {
+ });
+ private final CostFunction cf_manhattan_ignore_all_actors = new ReservedNodesCostFunction(actor,new ManhattanCost() {
@Override
public int price(MapNode s, MapNode dest) {
- if (dest.isReserved() && !actor.equals(dest.getReserver())) {
- return IMPASSABLE;
- }
SokobanObject obj = dest.getObject();
if (obj == null || !obj.isBox()) {
return 1;
}
return IMPASSABLE;
}
- };
-
- private final CostFunction cf_manhattan_ignore_this_actor_and_his_boxes_IGNORE_RESERVATIONS = new ManhattanCost() {
+ });
+ private final CostFunction cf_manhattan_ignore_all_actors_and_their_boxes = new ReservedNodesCostFunction(actor,new ManhattanCost() {
@Override
public int price(MapNode s, MapNode dest) {
- if (dest.isReserved() && !actor.equals(dest.getReserver())) {
- return IMPASSABLE;
- }
SokobanObject obj = dest.getObject();
- if (obj == null || obj.getObjectLetter() == actor.getObjectLetter() || (obj.isBox() && obj.getColor() == actor.getColor())) {
+ if (obj == null) {
return 1;
}
return IMPASSABLE;
}
- };
- private final CostFunction cf_manhattan_ignore_this_actor_IGNORE_RESERVATIONS = new ManhattanCost() {
- @Override
- public int price(MapNode s, MapNode dest) {
- SokobanObject obj = dest.getObject();
- if (obj == null || obj.getObjectLetter() == actor.getObjectLetter()) {
- return 1;
- }
- return IMPASSABLE;
- }
- };
- private final CostFunction cf_manhattan_ignore_all_actors_IGNORE_RESERVATIONS = new ManhattanCost() {
- @Override
- public int price(MapNode s, MapNode dest) {
- SokobanObject obj = dest.getObject();
- if (obj == null || !obj.isBox()) {
- return 1;
- }
- return IMPASSABLE;
- }
- };
+ });
+
private static final CostFunction manhattan = CostFunction.MANHATTAN_COST;
@@ -438,8 +422,8 @@ public boolean plannerLoop() {
//no plan -> make plan
boolean worked=false;
int counter=0;
- int fix=0;//0 = normal, 1 = allowBlockingBoxFix, 2 = allowBlockingAgentFix
- int maxFix=2;
+ int fix=0;//0 = normal, 1 = allowBlockingBoxFix, 2 = allowBlockingAgentFix, 3 = blocking not-our boxes fix
+ int maxFix=3;
//clearPlans();
if (plans.isEmpty() && !(requests.isEmpty() || claims.isEmpty())) {
// Shouldn't happen, but if it does - just fix it
@@ -469,8 +453,10 @@ public boolean plannerLoop() {
pl = planForIntention(in,cf_manhattan_ignore_this_actor);
} else if (fix==1) {
pl = planForIntention(in,cf_manhattan_ignore_this_actor_and_his_boxes);
- } else {
+ } else if (fix==2) {
pl = planForIntention(in,cf_manhattan_ignore_all_actors);
+ } else {
+ pl = planForIntention(in,cf_manhattan_ignore_all_actors_and_their_boxes);
}
//Check if we can claim the goal, if not we drop the plan
if(in.getIntentionType() == IntentionType.SOLVEGOAL && !claim(in.getGoalPosition())) {
@@ -597,7 +583,8 @@ public void reevaluate() {
break;
}
if (counter>10) {
- throw new AssertionError("Actor["+actor.getObjectLetter()+"]"+actor.getColor()+": plannerLoop looped > 10 times");
+ break;
+ //throw new AssertionError("Actor["+actor.getObjectLetter()+"]"+actor.getColor()+": plannerLoop looped > 10 times");
}
}
}
@@ -680,6 +667,41 @@ public void invokeIntention(Intention givenIn) {
dropPlans();
}
+ public void makeRequestsForBlockingOthersBoxes(List<SokobanObject> blockingBoxesOther, List<MapNode> frep) {
+ List<SokobanActor> otherActors = map.getAllActors();
+ if (otherActors == null || otherActors.isEmpty()) {
+ throw new AssertionError("other actors not found");
+ }
+ for (int i=0;i<blockingBoxesOther.size();i++) {
+ SokobanObject bbox = blockingBoxesOther.get(i);
+ SokobanActor actee = null;
+ for (SokobanActor ar : otherActors) {
+ if (actor.equals(ar)) { //ignore myself
+ continue;
+ }
+ if (ar.getColor()!=bbox.getColor()) {
+ continue; //not this actor
+ }
+ actee = ar;
+ }
+ if (actee == null) {
+ throw new AssertionError("could not find an actor for a blocking box");
+ }
+
+ //ignored actors/boxes set
+ Set<SokobanObject> ign = new HashSet<SokobanObject>();
+ Set<MapNode> keepClear = new HashSet<MapNode>();
+ ign.add(actor);
+ ign.addAll(map.getBoxList(actor.getColor()));
+ Intention in = new IntentionFree(frep,actor,ign,keepClear);
+ Request rq = new Request(actor,actee,in);
+ rq.getIntention().setRequest(rq);
+ requests.push(rq);
+
+ }
+ }
+
+
public void makeRequestsForBlockingActors(List<SokobanActor> blockingActors, List<MapNode> frep) {
for (SokobanActor ar : blockingActors) {
if (ar.equals(actor)) { //ignore myself
@@ -694,14 +716,17 @@ public void makeRequestsForBlockingActors(List<SokobanActor> blockingActors, Lis
Request rq = new Request(actor,ar,in);
rq.getIntention().setRequest(rq);
requests.push(rq);
-
}
}
public List<Plan> planForBlockingBoxes(IntentionFree inten, final CostFunction cfunc, List<SokobanObject> blockingBoxes) {
Set<MapNode> keepFree = inten.getKeepfree();
+ if (keepFree.isEmpty()) {
+ keepFree = new HashSet<MapNode>(inten.getFreePositions());
+ }
List<MapNode> goalForBlocking = PlannerUtils.findEmptyPositions(map, blockingBoxes, keepFree);
+
if (goalForBlocking == null || goalForBlocking.isEmpty()) {
SokobanObject box = first(new Predicate<SokobanObject>() {
@Override
@@ -759,11 +784,11 @@ public Plan planForLeavingArea(IntentionFree inten, CostFunction cfunc, List<Map
Set<Position> frepSet = new HashSet<Position>(frep);
MapNode goalForActor = PlannerUtils.findEmptyPosition(map, actor, actor, frepSet);
if (cfunc == cf_manhattan_ignore_all_actors) {
- cfunc = cf_manhattan_ignore_all_actors_IGNORE_RESERVATIONS;
+ cfunc = cf_manhattan_ignore_all_actors.getCostFunction();
} else if (cfunc == cf_manhattan_ignore_this_actor) {
- cfunc = cf_manhattan_ignore_this_actor_IGNORE_RESERVATIONS;
+ cfunc = cf_manhattan_ignore_this_actor.getCostFunction();
} else if (cfunc == cf_manhattan_ignore_this_actor_and_his_boxes) {
- cfunc = cf_manhattan_ignore_this_actor_and_his_boxes_IGNORE_RESERVATIONS;
+ cfunc = cf_manhattan_ignore_this_actor_and_his_boxes.getCostFunction();
}
Plan pp2 = planMovement(actor.getPosition(), goalForActor, cfunc);
return pp2;
@@ -771,21 +796,31 @@ public Plan planForLeavingArea(IntentionFree inten, CostFunction cfunc, List<Map
public List<Plan> planForFreePos(IntentionFree inten, final CostFunction cfunc) {
List<MapNode> frep = inten.getFreePositions();
- List<SokobanObject> blockingBoxes = new ArrayList<SokobanObject>();
+ List<SokobanObject> blockingBoxesOur = new ArrayList<SokobanObject>();
+ List<SokobanObject> blockingBoxesOther = new ArrayList<SokobanObject>();
List<SokobanActor> blockingActors = new ArrayList<SokobanActor>();
//find blocking boxes and blocking actors
for (MapNode fr : filter(NON_EMPTY_NODE, frep)) {
SokobanObject obj = fr.getObject();
if (obj.isBox()) {
- blockingBoxes.add(obj);
+ if (obj.getColor() == actor.getColor()) {
+ blockingBoxesOur.add(obj);
+ } else {
+ blockingBoxesOther.add(obj);
+ }
} else if (!obj.isBox()) {
blockingActors.add((SokobanActor)obj);
}
}
+ //not my boxes are blocking
+ if (!blockingBoxesOther.isEmpty() && inten.getAuthor() == actor) {
+ makeRequestsForBlockingOthersBoxes(blockingBoxesOther, frep);
+ }
+
//only actors blocking
- if (blockingBoxes.isEmpty() && !blockingActors.isEmpty() && inten.getAuthor() == actor) { //this is for me only as opposed to (1asd)
+ if (blockingBoxesOur.isEmpty() && !blockingActors.isEmpty() && inten.getAuthor() == actor) { //this is for me only as opposed to (1asd)
makeRequestsForBlockingActors(blockingActors, frep);
List<Plan> lp = new ArrayList<Plan>();
lp.add(new SimplePlan());
@@ -794,8 +829,8 @@ public Plan planForLeavingArea(IntentionFree inten, CostFunction cfunc, List<Map
//blocking boxes
List<Plan> blockingBoxesPlans = new ArrayList<Plan>();
- if (!blockingBoxes.isEmpty()) {
- blockingBoxesPlans = planForBlockingBoxes(inten, cfunc, blockingBoxes);
+ if (!blockingBoxesOur.isEmpty()) {
+ blockingBoxesPlans = planForBlockingBoxes(inten, cfunc, blockingBoxesOur);
}
if (blockingBoxesPlans == null) {
blockingBoxesPlans = new ArrayList<Plan>();
View
2  test/dk/dtu/mas/planner/HappyGameGivenLevels/FOMAsimple3.java
@@ -23,7 +23,7 @@ public void setUp() throws Exception {
//create game for local tests
game = new HappyGame();
game.localSetup(map);
- //game.localGuiSetup(200);
+ game.localGuiSetup(200);
}
View
49 test/dk/dtu/mas/planner/HappyGame_test_FOMAsimplex.java
@@ -0,0 +1,49 @@
+package dk.dtu.mas.planner;
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import dk.dtu.mas.HappyGame;
+import dk.dtu.mas.map.SimpleSokobanMap;
+import dk.dtu.mas.map.util.TestUtil;
+
+
+public final class HappyGame_test_FOMAsimplex {
+
+ private SimpleSokobanMap map;
+ private HappyGame game;
+ private String[] mapstr = new String[] {
+ "blue:0,A",
+ "red:1,B",
+ "+++++++++++++",
+ "+0 b Aa",
+ "+++++ B 1++++",
+ "+++++++++++++",
+ };
+ @Before
+ public void setUp() throws Exception {
+ map = new SimpleSokobanMap();
+ map.parseMap(TestUtil.str2stream(mapstr));
+
+ //create game for local tests
+ game = new HappyGame();
+ game.localSetup(map);
+ game.localGuiSetup(600);
+
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ game = null;
+ map = null;
+ }
+
+ @Test
+ public void test() throws Exception {
+ game.mainLoop();
+ assertTrue("Can solve map", map.isSolved());
+ }
+}
View
2  test/dk/dtu/mas/planner/HappyGame_test_FOSAsimple13.java
@@ -35,7 +35,7 @@ public void setUp() throws Exception {
//create game for local tests
game = new HappyGame();
game.localSetup(map);
- game.localGuiSetup(100);
+ //game.localGuiSetup(100);
}

No commit comments for this range

Something went wrong with that request. Please try again.