burlap.mdp.core.oo.state.generic.GenericOOState Java Examples

The following examples show how to use burlap.mdp.core.oo.state.generic.GenericOOState. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GridGame.java    From burlap with Apache License 2.0 6 votes vote down vote up
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;
		
	}
 
Example #2
Source File: MinecraftModel.java    From burlapcraft with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected void simMove(GenericOOState gs){

		//get agent and current position
		BCAgent agent = (BCAgent)gs.touch(CLASS_AGENT);


		//get objects and their positions
		List<ObjectInstance> blocks = gs.objectsOfClass(HelperNameSpace.CLASS_BLOCK);
		List<HelperPos> coords = new ArrayList<HelperPos>();
		for (ObjectInstance block : blocks) {
			int blockX = ((BCBlock)block).x;
			int blockY = ((BCBlock)block).y;
			int blockZ = ((BCBlock)block).z;
			coords.add(new HelperPos(blockX, blockY, blockZ));
		}

		//get resulting position
		int [][][] map = ((BCMap)gs.object(CLASS_MAP)).map;
		HelperPos newPos = this.moveResult(agent.x, agent.y, agent.z, agent.rdir, coords, map);

		//set the new position
		agent.x = newPos.x;
		agent.y = newPos.y;
		agent.z = newPos.z;

	}
 
Example #3
Source File: MinecraftModel.java    From burlapcraft with GNU Lesser General Public License v3.0 6 votes vote down vote up
protected void simDestroy(GenericOOState gs){

		//get agent and current position
		BCAgent agent = (BCAgent)gs.object(CLASS_AGENT);
		BCInventory inv = (BCInventory)gs.object(CLASS_INVENTORY);
		if(agent.selected < 0 || agent.selected > 8 || agent.vdir != 1){
			return;
		}

		int itemId = inv.inv[agent.selected].type;
		if(itemId != 278){
			return;
		}

		List<ObjectInstance> oblocks = gs.objectsOfClass(CLASS_BLOCK);

		List<BCBlock> blocks = new ArrayList<BCBlock>(oblocks.size());
		for(ObjectInstance ob : oblocks){
			blocks.add((BCBlock)ob);
		}

		this.destroyResult(agent.x, agent.y, agent.z, agent.rdir, blocks, gs);


	}
 
Example #4
Source File: MinecraftActionType.java    From burlapcraft with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public List<Action> allApplicableActions(State s) {
	BCAgent a = (BCAgent)((GenericOOState)s).object(CLASS_AGENT);

	List<ObjectInstance> blocks = ((OOState)s).objectsOfClass(HelperNameSpace.CLASS_BLOCK);
	for (ObjectInstance block : blocks) {
		if (HelperActions.blockIsOneOf(Block.getBlockById(((BCBlock)block).type), HelperActions.dangerBlocks)) {
			int dangerX = ((BCBlock)block).x;
			int dangerY = ((BCBlock)block).y;
			int dangerZ = ((BCBlock)block).z;
			if ((a.x == dangerX) && (a.y - 1 == dangerY) && (a.z == dangerZ) || (a.x == dangerX) && (a.y == dangerY) && (a.z == dangerZ)) {
				return new ArrayList<Action>();
			}
		}
	}

	//otherwise we pass check
	return Arrays.<Action>asList(new SimpleAction(typeName));
}
 
Example #5
Source File: ExampleOOGridWorld.java    From burlap_examples with MIT License 5 votes vote down vote up
public State sample(State s, Action a) {

			s = s.copy();
			GenericOOState gs = (GenericOOState)s;
			ExGridAgent agent = (ExGridAgent)gs.touch(CLASS_AGENT);
			int curX = agent.x;
			int curY = agent.y;

			int adir = actionDir(a);

			//sample direction with random roll
			double r = Math.random();
			double sumProb = 0.;
			int dir = 0;
			for(int i = 0; i < 4; i++){
				sumProb += this.transitionProbs[adir][i];
				if(r < sumProb){
					dir = i;
					break; //found direction
				}
			}

			//get resulting position
			int [] newPos = this.moveResult(curX, curY, dir);

			//set the new position
			agent.x = newPos[0];
			agent.y = newPos[1];

			//return the state we just modified
			return gs;
		}
 
Example #6
Source File: MinecraftModel.java    From burlapcraft with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public State sample(State s, Action a) {

	GenericOOState gs = (GenericOOState)s.copy();

	String aname = a.actionName();
	if(aname.equals(HelperNameSpace.ACTION_MOVE)){
		simMove(gs);
	}
	else if(aname.equals(HelperNameSpace.ACTION_ROTATE_LEFT)){
		simRotate(gs, HelperNameSpace.RotDirection.size - 1);
	}
	else if(aname.equals(HelperNameSpace.ACTION_ROTATE_RIGHT)){
		simRotate(gs, 1);
	}
	else if(aname.equals(HelperNameSpace.ACTION_AHEAD)){
		simPitch(gs, 0);
	}
	else if(aname.equals(HelperNameSpace.ACTION_DOWN_ONE)){
		simPitch(gs, HelperNameSpace.VertDirection.size - 1);
	}
	else if(aname.equals(HelperNameSpace.ACTION_PLACE_BLOCK)){
		simPlace(gs);
	}
	else if(aname.equals(HelperNameSpace.ACTION_DEST_BLOCK)){
		simDestroy(gs);
	}
	else if(aname.equals(HelperNameSpace.ACTION_CHANGE_ITEM)){
		simChangeItem(gs);
	}
	else{
		throw new RuntimeException("MinecraftModel is not defined for action " + aname);
	}

	return gs;
}
 
Example #7
Source File: MinecraftModel.java    From burlapcraft with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void simRotate(GenericOOState gs, int direction){
	//get agent and current position
	BCAgent agent = (BCAgent)gs.touch(CLASS_AGENT);

	int rotDir = (direction + agent.rdir) % 4;

	//set the new rotation direction
	agent.rdir = rotDir;
}
 
Example #8
Source File: GridGame.java    From burlap with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
	
}
 
Example #9
Source File: GridGame.java    From burlap with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #10
Source File: MinecraftModel.java    From burlapcraft with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected void simChangeItem(GenericOOState gs){

		//get agent
		BCAgent agent = (BCAgent)gs.touch(CLASS_AGENT);
		int curItem = agent.selected;
		int nextItem = curItem >= 0 && curItem < 9 ? (curItem+1) % 9 : 0;
		agent.selected = nextItem;

	}
 
Example #11
Source File: MinecraftModel.java    From burlapcraft with GNU Lesser General Public License v3.0 5 votes vote down vote up
protected State destroyResult(int curX, int curY, int curZ, int rotDir, List<BCBlock> blocks, GenericOOState s) {
	//System.out.println("Destroying at: " + curX + "," + curY + "," + curZ);
	// go through blocks and see if any of them can be mined
	for (BCBlock block : blocks) {

		if (((block.x == curX) && (block.z == curZ + 1) && (block.y == curY) && (rotDir == 0))
				|| ((block.x == curX) && (block.z == curZ - 1) && (block.y == curY) && (rotDir == 2))
				|| ((block.x == curX + 1) && (block.z == curZ) && (block.y == curY) && (rotDir == 3))
				|| ((block.x == curX - 1) && (block.z == curZ) && (block.y == curY) && (rotDir == 1))) {
			// get id of the block
			int blockID = block.type;

			boolean present = false;

			BCInventory inv = (BCInventory)s.touch(CLASS_INVENTORY);
			int invInd = inv.indexOfType(blockID);
			if(invInd == -1){
				invInd = inv.firstFree();
			}
			if(invInd == -1){
				return s; //no free slots!
			}
			BCInventory.BCIStack [] stack = inv.touch();
			stack[invInd].type = blockID;
			stack[invInd].inc();


			s.removeObject(block.name());
			break;
		}
	}

	return s;

}
 
Example #12
Source File: GridGame.java    From burlap with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #13
Source File: GridGame.java    From burlap with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #14
Source File: GridGame.java    From burlap with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #15
Source File: ExampleOOGridWorld.java    From burlap_examples with MIT License 4 votes vote down vote up
public static void main(String [] args){

		ExampleOOGridWorld gen = new ExampleOOGridWorld();
		OOSADomain domain = gen.generateDomain();
		State initialState = new GenericOOState(new ExGridAgent(0, 0), new EXGridLocation(10, 10, "loc0"));
		SimulatedEnvironment env = new SimulatedEnvironment(domain, initialState);

		Visualizer v = gen.getVisualizer();
		VisualExplorer exp = new VisualExplorer(domain, env, v);

		exp.addKeyAction("w", ACTION_NORTH, "");
		exp.addKeyAction("s", ACTION_SOUTH, "");
		exp.addKeyAction("d", ACTION_EAST, "");
		exp.addKeyAction("a", ACTION_WEST, "");

		exp.initGUI();


	}
 
Example #16
Source File: CommandCheckMap.java    From burlapcraft with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void processCommand(ICommandSender sender, String[] args) {
	State s = MinecraftStateGeneratorHelper.getCurrentState(BurlapCraft.currentDungeon);
	System.out.println(((GenericOOState)s).object(CLASS_MAP).toString());
}
 
Example #17
Source File: MinecraftModel.java    From burlapcraft with GNU Lesser General Public License v3.0 4 votes vote down vote up
protected void simPlace(GenericOOState gs){

		BCAgent agent = (BCAgent)gs.object(CLASS_AGENT);

		if(agent.vdir != 1 || agent.selected < 0 || agent.selected > 8){
			return;
		}

		BCInventory inv = (BCInventory)gs.object(CLASS_INVENTORY);
		if(inv.inv[agent.selected].type == -1 || !HelperActions.blockIsOneOf(Block.getBlockById(inv.inv[agent.selected].type), mineableBlocks)){
			return;
		}

		List<ObjectInstance> oblocks = gs.objectsOfClass(CLASS_BLOCK);

		List<BCBlock> blocks = new ArrayList<BCBlock>(oblocks.size());
		for(ObjectInstance ob : oblocks){
			blocks.add((BCBlock)ob);
		}

		int [][][] map = ((BCMap)gs.object(CLASS_MAP)).map;

		placeResult(agent.x, agent.y, agent.z, agent.rdir, blocks, agent.selected, map, gs);

	}
 
Example #18
Source File: MinecraftStateGeneratorHelper.java    From burlapcraft with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static State getCurrentState(Dungeon d) {

		dungeonPose = d.getPose();
		length = d.getLength();
		width = d.getWidth();
		height = d.getHeight();

		GenericOOState s = new GenericOOState();

		for (int i = 0; i < height; i++) {
			for (int j = 0; j < length; j++) {
				for (int k = 0; k < width; k++) {
					Block block = HelperActions.getBlock(
							dungeonPose.getX() + j, dungeonPose.getY() + i,
							dungeonPose.getZ() + k);

					if (HelperActions.blockIsOneOf(block,
							HelperActions.mineableBlocks)
							|| HelperActions.blockIsOneOf(block,
									HelperActions.dangerBlocks)) {
						int blockID = HelperActions.getBlockId(
								dungeonPose.getX() + j, dungeonPose.getY() + i,
								dungeonPose.getZ() + k);
						int keyX = (int) dungeonPose.getX() + j;
						int keyY = (int) dungeonPose.getY() + i;
						int keyZ = (int) dungeonPose.getZ() + k;
						String blockName;
						blockName = "block";
						String key = keyX + "," + keyY + "," + keyZ;
						if (blockNameMap.containsKey(key)) {
							blockName = blockNameMap.get(key);
						}
						else {
							blockName += blockCount;
							blockNameMap.put(key, blockName);
							blockCount += 1;
						}

						BCBlock blockInstance = new BCBlock(j, i, k, blockID, blockName);
						s.addObject(blockInstance);
					}
				}
			}

		}

		HelperPos curPos = HelperActions.getPlayerPosition();
		int rotateDirection = HelperActions.getYawDirection();
		int rotateVertDirection = HelperActions.getPitchDirection();
		//int selectedItemID = HelperActions.getCurrentItemID();
		int selectedItemID = HelperActions.currentItemIndex();
		System.out.println("Player position: " + curPos);
		System.out.println("Dungeon: " + dungeonPose);

		BCAgent agent = new BCAgent(
				(int)(curPos.x - dungeonPose.getX()),
				(int)(curPos.y - dungeonPose.getY()),
				(int)(curPos.z - dungeonPose.getZ()),
				rotateDirection,
				rotateVertDirection,
				selectedItemID);


		BCInventory inv = new BCInventory();
		Minecraft mc = Minecraft.getMinecraft();
		for(int i = 0; i < 9; i++){
			ItemStack itemStack = mc.thePlayer.inventory.mainInventory[i];
			if(itemStack == null){
				inv.inv[i] = new BCInventory.BCIStack(-1, 0);
			}
			else {
				Item item = itemStack.getItem();
				inv.inv[i] = new BCInventory.BCIStack(Item.getIdFromItem(item), itemStack.stackSize);
			}
		}

		s.addObject(inv);


		s.addObject(agent);


		BCMap map = new BCMap(getMap(d));
		s.addObject(map);

		validate(s);
		return s;
	}
 
Example #19
Source File: MinecraftModel.java    From burlapcraft with GNU Lesser General Public License v3.0 3 votes vote down vote up
protected void simPitch(GenericOOState gs, int vertDirection){

		//get agent and current position
		BCAgent agent = (BCAgent)gs.touch(CLASS_AGENT);

		//set agent's vert dir
		agent.vdir = vertDirection;


	}
 
Example #20
Source File: GridGame.java    From burlap with Apache License 2.0 3 votes vote down vote up
/**

	
	
	/**
	 * Sets boundary walls of a domain. This method will add 4 solid walls (top left bottom right) to create
	 * a playing field in which the agents can interact.
	 * @param s the state in which the walls should be added
	 * @param w the width of the playing field
	 * @param h the height of the playing field
	 */
	public static void setBoundaryWalls(GenericOOState s, int w, int h){

		int numV = s.objectsOfClass(CLASS_DIM_V_WALL).size();
		int numH = s.objectsOfClass(CLASS_DIM_H_WALL).size();
		
		s.addObject(new GGWall.GGVerticalWall(0, h-1, 0, 0, "h"+numH))
				.addObject(new GGWall.GGVerticalWall(0, h-1, w, 0, "h"+(numH+1)))
				.addObject(new GGWall.GGHorizontalWall(0, w-1, 0, 0, "v"+numV))
				.addObject(new GGWall.GGHorizontalWall(0, w-1, h, 0, "v"+(numV+1)) );

		
		
	}