/** * Returns a new {@link Location2} object that is the subtraction of a provided object from this object (this - o). This objects values * are not affected by this operation. * @param o the other object whose values should be subtract. * @return a new {@link Location2} object that is the subtraction of a provided object from this object (this - o). */ public Location2 subtract(Location2 o){ return new Location2(x-o.x, y-o.y); }
List<PropositionalFunction> generatePFs(){ return Arrays.asList(new AgentInUGoal(PF_IN_U_GOAL), new AgentInPGoal(PF_IN_P_GOAL)); }
/** * Returns the initial state for a classic coordination game, where the agent's personal goals are on opposite sides. * @return the coordination game initial state */ public static State getCorrdinationGameInitialState(){ GenericOOState s = new GenericOOState( new GGAgent(0, 0, 0, "agent0"), new GGAgent(2, 0, 1, "agent1"), new GGGoal(0, 2, 2, "g0"), new GGGoal(2, 2, 1, "g1") ); setBoundaryWalls(s, 3, 3); return s; }
GridGame gg = new GridGame(); OOSGDomain d = gg.generateDomain(); State s = GridGame.getTurkeyInitialState(); Visualizer v = GGVisualizer.getVisualizer(9, 9); SGVisualExplorer exp = new SGVisualExplorer(d, v, s);
public static State getTurkeyInitialState(){ GenericOOState s = new GenericOOState( new GGAgent(0, 0, 0, "agent0"), new GGAgent(2, 0, 1, "agent1"), new GGGoal(0, 3, 1, "g0"), new GGGoal(1, 2, 0, "g1"), new GGGoal(2, 3, 2, "g2"), new GGWall.GGHorizontalWall(0, 0, 1, 1, "w0"), new GGWall.GGHorizontalWall(2, 2, 1, 1, "w1") ); setBoundaryWalls(s, 3, 4); return s; }
Location2 p1 = p0.add(delta); if(p1.equals(anl)){ reset = true; break; reset = this.sampleWallCollision(p0, delta, s.objectsOfClass(GridGame.CLASS_DIM_V_WALL), true); reset = this.sampleWallCollision(p0, delta, s.objectsOfClass(GridGame.CLASS_DIM_H_WALL), false); p1 = p1.subtract(delta);
Location2 a2op = originalPositions.get(j); Location2 a2dp = resolvedPositions.get(j); if(a1op.equals(a2dp) && a1dp.equals(a2op)){ resolvedPositions.set(i, new Location2(a1op)); resolvedPositions.set(j, new Location2(a2op)); newNoopPositions.add(a1op); newNoopPositions.add(a2op); return this.backupNoOps(originalPositions, resolvedPositions, newNoopPositions);
/** * This method will recursively generate all possible joint location outcomes for a list of possible outcomes for each agent * @param locOutcomes the list of possible location outcomes for each agent * @param i the index from which to generate possible outcomes in a depth-first manner * @param locArray an array holding the current assignments for a given depth * @param stackedProb the probability of this location set occurring * @param discovered the discovered combinations thus far */ protected void generateAllLocationSetsHelper(List<List<Location2Prob>> locOutcomes, int i, Location2 [] locArray, double stackedProb, List<LocationSetProb> discovered){ if(i == locOutcomes.size()){ //bundle them up List <Location2> locs = new ArrayList<GridGameStandardMechanics.Location2>(locArray.length); Collections.addAll(locs, locArray); LocationSetProb lsp = new LocationSetProb(locs, stackedProb); discovered.add(lsp); return ; } //otherwise we're in recursive step List<Location2Prob> individualOutcomes = locOutcomes.get(i); for(Location2Prob lp : individualOutcomes){ locArray[i] = lp.l; this.generateAllLocationSetsHelper(locOutcomes, i+1, locArray, stackedProb*lp.p, discovered); } }
v.addStatePainter(ooStatePainter); ooStatePainter.addObjectClassPainter(GridGame.CLASS_GOAL, new CellPainter(maxX, maxY, goalColors, 0)); ooStatePainter.addObjectClassPainter(GridGame.CLASS_AGENT, new CellPainter(maxX, maxY, agentColors, 1)); ooStatePainter.addObjectClassPainter(GridGame.CLASS_DIM_V_WALL, new WallPainter(maxX, maxY, true)); ooStatePainter.addObjectClassPainter(GridGame.CLASS_DIM_H_WALL, new WallPainter(maxX, maxY, false));
@Override public OOSGDomain generateDomain() { OOSGDomain domain = new OOSGDomain(); domain.addStateClass(CLASS_AGENT, GGAgent.class) .addStateClass(CLASS_GOAL, GGGoal.class) .addStateClass(CLASS_DIM_H_WALL, GGWall.GGHorizontalWall.class) .addStateClass(CLASS_DIM_V_WALL, GGWall.GGVerticalWall.class); domain.addActionType(new UniversalActionType(ACTION_NORTH)) .addActionType(new UniversalActionType(ACTION_SOUTH)) .addActionType(new UniversalActionType(ACTION_EAST)) .addActionType(new UniversalActionType(ACTION_WEST)) .addActionType(new UniversalActionType(ACTION_NOOP)); OODomain.Helper.addPfsToDomain(domain, this.generatePFs()); domain.setJointActionModel(new GridGameStandardMechanics(domain, this.semiWallProb)); return domain; }
/** * Takes a list of possible location outcomes for each agent and generates all joint location outcomes * @param locOutcomes a list of possible location outcomes * @return all joint location outcomes */ protected List<LocationSetProb> getAllLocationSets(List<List<Location2Prob>> locOutcomes){ List <LocationSetProb> sets = new ArrayList<GridGameStandardMechanics.LocationSetProb>(); Location2 [] locArray = new Location2[locOutcomes.size()]; generateAllLocationSetsHelper(locOutcomes, 0, locArray, 1., sets); return sets; }
protected void generateAllPossibleCollisionWinnerAssignments(List <List<Integer>> collisionLists, int i, int [] assignment, List <List<Integer>> discovered){ if(i == collisionLists.size()){ //package it up List <Integer> assignmentList = new ArrayList<Integer>(assignment.length); for(int a : assignment){ assignmentList.add(a); } discovered.add(assignmentList); return ; } //otherwise we're in recursive step List<Integer> possibleWinners = collisionLists.get(i); for(int w : possibleWinners){ assignment[i] = w; this.generateAllPossibleCollisionWinnerAssignments(collisionLists, i+1, assignment, discovered); } }
/** * Indicates whether there are any wall collisions. * @param p0 the original location * @param delta the desired change in direction * @param walls possible wall objects * @param vertical true if the wall objects are vertical; false if they are horizontal * @return 0 if there is no collision with a wall, 1 if there is a collision with a solid wall, 2 if there is a potential collision with a semi-wall */ protected int wallCollision(Location2 p0, Location2 delta, List <ObjectInstance> walls, boolean vertical){ for(int i = 0; i < walls.size(); i++){ ObjectInstance w = walls.get(i); if(this.crossesWall(p0, delta, w, vertical)){ int wt = (Integer)w.get(GridGame.VAR_WT); if(wt == 0){ //solid wall return 1; } else if(wt == 1){ //stochastic wall return 2; } } } return 0; }
/** * Returns the initial state for a simple game in which both players can win without interfering with one another. * @return the simple game initial state */ public static State getSimpleGameInitialState(){ GenericOOState s = new GenericOOState( new GGAgent(0, 0, 0, "agent0"), new GGAgent(2, 0, 1, "agent1"), new GGGoal(0, 2, 1, "g0"), new GGGoal(2, 2, 2, "g1") ); setBoundaryWalls(s, 3, 3); return s; }
/** * Returns a new {@link Location2} object that is the sum of this object and the provided object. This objects values * are not affected by this operation. * @param o the other object whose values should be added. * @return a new {@link Location2} object that is the sum of this object and the provided object. */ public Location2 add(Location2 o){ return new Location2(x+o.x, y+o.y); }
/** * Returns the initial state for Friend Foe game. * @return the initial state for Friend Foe */ public static State getFriendFoeInitialState(){ GenericOOState s = new GenericOOState( new GGAgent(3, 0 ,0, "agent0"), new GGAgent(6, 0, 1, "agent1"), new GGGoal(0, 0, 1, "g0"), new GGGoal(4, 0, 0, "g1") ); setBoundaryWalls(s, 8, 1); return s; }
/** * Returns the attempted change in position by the agent for the given action. For instance, if the action is north, * it would result in an attempted position change of (0, +1). * @param actionName the action taken. * @return the attempted change in position for the given action. */ protected Location2 attemptedDelta(String actionName){ if(actionName.equals(GridGame.ACTION_NORTH)){ return new Location2(0, 1); } else if(actionName.equals(GridGame.ACTION_SOUTH)){ return new Location2(0, -1); } else if(actionName.equals(GridGame.ACTION_EAST)){ return new Location2(1, 0); } else if(actionName.equals(GridGame.ACTION_WEST)){ return new Location2(-1, 0); } else if(actionName.equals(GridGame.ACTION_NOOP)){ return new Location2(0, 0); } throw new RuntimeException("Error: Unknown action named '" + actionName + "' that GridGameStandardMechanics cannot handle"); }
/** * Returns the initial state for the Incredible game (a game in which player 0 can give an incredible threat). * @return the initial state for the Incredible game. */ public static State getIncredibleInitialState(){ GenericOOState s = new GenericOOState( new GGAgent(2, 0, 0, "agent0"), new GGAgent(3, 0, 1, "agent1"), new GGGoal(0, 0, 1, "g0"), new GGGoal(1, 0, 2, "g1") ); setBoundaryWalls(s, 4, 1); return s; }
/** * Returns the x-y position of an agent stored in a Location2 object. * @param s the state in which the agent exists * @param agentName the name of the agent. * @return a {@link GridGameStandardMechanics.Location2} object containing the agents position in the world. */ protected Location2 getLocation(OOState s, String agentName){ ObjectInstance a = s.object(agentName); Location2 loc = new Location2((Integer)a.get(GridGame.VAR_X), (Integer)a.get(GridGame.VAR_Y)); return loc; }
/** * Returns the initial state for a classic prisoner's dilemma formulated in a Grid Game. * @return the grid game prisoner's dilemma initial state */ public static State getPrisonersDilemmaInitialState(){ GenericOOState s = new GenericOOState( new GGAgent(3, 0, 0, "agent0"), new GGAgent(5, 0, 1, "agent1"), new GGGoal(0, 0, 1, "g0"), new GGGoal(4, 0, 0, "g1"), new GGGoal(8, 0, 2, "g2") ); setBoundaryWalls(s, 9, 1); return s; }