burlap.mdp.core.oo.state.ObjectInstance Java Examples

The following examples show how to use burlap.mdp.core.oo.state.ObjectInstance. 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: LLState.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public MutableOOState addObject(ObjectInstance o) {

	if(o instanceof LLAgent){
		agent = (LLAgent)o;
	}
	else if(o instanceof LLBlock.LLPad){
		pad = (LLBlock.LLPad)o;
	}
	else if(o instanceof LLBlock){
		touchObstacles().add((LLBlock.LLObstacle)o);
	}
	else{
		throw new UnknownClassException(o.className());
	}

	return this;
}
 
Example #2
Source File: BlockDudeState.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public List<ObjectInstance> objectsOfClass(String oclass) {
	if(oclass.equals(CLASS_AGENT)){
		return Arrays.<ObjectInstance>asList(agent);
	}
	else if(oclass.equals(CLASS_MAP)){
		return Arrays.<ObjectInstance>asList(map);
	}
	else if(oclass.equals(CLASS_EXIT)){
		return Arrays.<ObjectInstance>asList(exit);
	}
	else if(oclass.equals(CLASS_BLOCK)){
		return new ArrayList<ObjectInstance>(blocks);
	}
	throw new RuntimeException("No object class " + oclass);
}
 
Example #3
Source File: GridWorldDomain.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isTrue(OOState st, String... params) {
	
	ObjectInstance agent = st.object(params[0]);
	ObjectInstance location = st.object(params[1]);
	
	int ax = (Integer)agent.get("x");
	int ay = (Integer)agent.get("y");
	
	int lx = (Integer)location.get("x");
	int ly = (Integer)location.get("y");
	
	if(ax == lx && ay == ly){
		return true;
	}
	
	return false;
}
 
Example #4
Source File: IISimpleHashableState.java    From burlap with Apache License 2.0 6 votes vote down vote up
protected int computeOOHashCode(OOState s){

		int [] hashCodes = new int[s.numObjects()];
		List<ObjectInstance> objects = s.objects();
		for(int i = 0; i < hashCodes.length; i++){
			ObjectInstance o = objects.get(i);
			int oHash = this.computeFlatHashCode(o);
			int classNameHash = o.className().hashCode();
			int totalHash = oHash + 31*classNameHash;
			hashCodes[i] = totalHash;
		}

		//sort for invariance to order
		Arrays.sort(hashCodes);
		HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(17, 31);
		hashCodeBuilder.append(hashCodes);
		return hashCodeBuilder.toHashCode();

	}
 
Example #5
Source File: IDSimpleHashableState.java    From burlap with Apache License 2.0 6 votes vote down vote up
protected boolean ooStatesEqual(OOState s1, OOState s2){
	if (s1 == s2) {
		return true;
	}
	if(s1.numObjects() != s2.numObjects()){
		return false;
	}

	List<ObjectInstance> theseObjects = s1.objects();
	for(ObjectInstance ob : theseObjects){
		ObjectInstance oByName = s2.object(ob.name());
		if(oByName == null){
			return false;
		}
		if(!flatStatesEqual(ob, oByName)){
			return false;
		}
	}

	return true;
}
 
Example #6
Source File: IDSimpleHashableState.java    From burlap with Apache License 2.0 6 votes vote down vote up
protected int computeOOHashCode(OOState s){

		int [] hashCodes = new int[s.numObjects()];
		List<ObjectInstance> objects = s.objects();
		for(int i = 0; i < hashCodes.length; i++){
			ObjectInstance o = objects.get(i);
			int oHash = this.computeFlatHashCode(o);
			int classNameHash = o.className().hashCode();
			int nameHash = o.name().hashCode();
			int totalHash = oHash + 31*classNameHash + 31*31*nameHash;
			hashCodes[i] = totalHash;
		}

		//sort for invariance to order
		Arrays.sort(hashCodes);
		HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(17, 31);
		hashCodeBuilder.append(hashCodes);
		return hashCodeBuilder.toHashCode();

	}
 
Example #7
Source File: GridWorldDomain.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isTrue(OOState st, String... params) {
	
	ObjectInstance agent = st.object(params[0]);
	
	int ax = (Integer)agent.get("x");
	int ay = (Integer)agent.get("y");
	
	int cx = ax + xdelta;
	int cy = ay + ydelta;
	
	if(cx < 0 || cx >= GridWorldDomain.this.width || cy < 0 || cy >= GridWorldDomain.this.height || GridWorldDomain.this.map[cx][cy] == 1 || 
			(xdelta > 0 && (GridWorldDomain.this.map[ax][ay] == 3 || GridWorldDomain.this.map[ax][ay] == 4)) || (xdelta < 0 && (GridWorldDomain.this.map[cx][cy] == 3 || GridWorldDomain.this.map[cx][cy] == 4)) ||
			(ydelta > 0 && (GridWorldDomain.this.map[ax][ay] == 2 || GridWorldDomain.this.map[ax][ay] == 4)) || (ydelta < 0 && (GridWorldDomain.this.map[cx][cy] == 2 || GridWorldDomain.this.map[cx][cy] == 4)) ){
		return true;
	}
	
	return false;
}
 
Example #8
Source File: GridWorldVisualizer.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public void paintObject(Graphics2D g2, OOState s, ObjectInstance ob, float cWidth, float cHeight) {
	
	
	//set the color of the object
	g2.setColor(this.col);
	
	float domainXScale = this.dwidth;
	float domainYScale = this.dheight;
	
	//determine then normalized width
	float width = (1.0f / domainXScale) * cWidth;
	float height = (1.0f / domainYScale) * cHeight;

	float rx = (Integer)ob.get(VAR_X)*width;
	float ry = cHeight - height - (Integer)ob.get(VAR_Y)*height;
	
	if(this.shape == 0){
		g2.fill(new Rectangle2D.Float(rx, ry, width, height));
	}
	else{
		g2.fill(new Ellipse2D.Float(rx, ry, width, height));
	}
	
}
 
Example #9
Source File: GridGameStandardMechanics.java    From burlap with Apache License 2.0 6 votes vote down vote up
/**
 * Return true if the agent is able to move in the desired location; false if the agent moves into a solid wall
 * or if the agent randomly fails to move through a semi-wall that is in the way.
 * @param p0 the initial location of the agent
 * @param delta the desired change in direction of the agent
 * @param walls the list of walls in the world
 * @param vertical whether the list of provided walls are vertical or horizontal walls
 * @return true if the agent is able to move in the desired location; false otherwise
 */
protected boolean sampleWallCollision(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 true;
			}
			else if(wt == 1){ //stochastic wall
				double roll = rand.nextDouble();
				if(roll > pMoveThroughSWall){
					return true;
				}
			}
		}
	}
	
	return false;
}
 
Example #10
Source File: GridGameStandardMechanics.java    From burlap with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
	
}
 
Example #11
Source File: BlockDudeState.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public ObjectInstance object(String oname) {
	if(oname.equals(CLASS_AGENT)){
		return agent;
	}
	else if(oname.equals(CLASS_MAP)){
		return map;
	}
	else if(oname.equals(exit.name())){
		return exit;
	}
	int ind = this.blockForName(oname);
	if (ind != -1){
		return this.blocks.get(ind);
	}


	return null;
}
 
Example #12
Source File: GridGameStandardMechanics.java    From burlap with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the agent objects between these two states are equal
 * @param s1 the first state
 * @param s2 the second state
 * @return true if the agent objects between these two states are equal
 */
protected boolean agentsEqual(OOState s1, OOState s2){
	
	List<ObjectInstance> agents1 = s1.objectsOfClass(GridGame.CLASS_AGENT);
	for(ObjectInstance a1 : agents1){
		ObjectInstance a2 = s2.object(a1.name());
		
		int x1 = (Integer)a1.get(GridGame.VAR_X);
		int x2 = (Integer)a2.get(GridGame.VAR_X);
		
		int y1 = (Integer)a1.get(GridGame.VAR_Y);
		int y2 = (Integer)a2.get(GridGame.VAR_Y);
		
		if(x1 != x2 || y1 != y2){
			return false;
		}
		
	}
	
	return true;
}
 
Example #13
Source File: OOStateGridder.java    From burlap with Apache License 2.0 6 votes vote down vote up
/**
 * Generates a set of states spaced along along. If there are objects in the input state for which a gridding
 * has not been specified, objects of those classes will be held constant in the returned states over the grid.
 * @param s the input state to grid
 * @return a set of states spaced along along
 */
public List<State> gridState(MutableOOState s){

	//generate specs for all object-wise keys
	FlatStateGridder flatGridder = new FlatStateGridder();
	for(Map.Entry<String, FlatStateGridder> classGird : this.classesToGrid.entrySet()){

		List<ObjectInstance> objects = s.objectsOfClass(classGird.getKey());
		for(ObjectInstance o : objects){
			for(Map.Entry<Object, VariableGridSpec> spec : classGird.getValue().specs()){
				OOVariableKey okey = new OOVariableKey(o.name(), spec.getKey());
				flatGridder.gridDimension(okey, spec.getValue());
			}
		}

	}

	//then grid
	return flatGridder.gridState(s);

}
 
Example #14
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 #15
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 #16
Source File: FrostbiteState.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public MutableOOState addObject(ObjectInstance o) {

	if(o instanceof FrostbiteAgent){
		agent = (FrostbiteAgent)o;
	}
	else if(o instanceof FrostbiteIgloo){
		igloo = (FrostbiteIgloo)o;
	}
	else if(o instanceof FrostbitePlatform){
		this.touchPlatforms().add((FrostbitePlatform)o);
	}
	else{
		throw new RuntimeException("Cannot add object of type " + o.getClass().getName());
	}

	return this;
}
 
Example #17
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 #18
Source File: GoldBlockTF.java    From burlapcraft with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Find the gold block and return its pose.
 * @param s the state
 * @return the pose of the agent being one unit above the gold block.
 */
public static HelperGeometry.Pose getGoalPose(State s) {

	OOState os = (OOState)s;

	List<ObjectInstance> blocks = os.objectsOfClass(HelperNameSpace.CLASS_BLOCK);
	for (ObjectInstance oblock : blocks) {
		BCBlock block = (BCBlock)oblock;
		if (block.type == 41) {
			int goalX = block.x;
			int goalY = block.y;
			int goalZ = block.z;

			return HelperGeometry.Pose.fromXyz(goalX, goalY + 1, goalZ);
		}
	}
	return null;
}
 
Example #19
Source File: FrostbiteState.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public ObjectInstance object(String oname) {
	if(oname.equals(CLASS_AGENT)){
		return agent;
	}
	else if(oname.equals(CLASS_IGLOO)){
		return igloo;
	}
	else{
		int ind = OOStateUtilities.objectIndexWithName(platforms, oname);
		if(ind != -1){
			return platforms.get(ind);
		}
	}
	throw new UnknownObjectException(oname);
}
 
Example #20
Source File: FrostbiteVisualizer.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public void paintObject(Graphics2D g2, OOState s, ObjectInstance ob,
						float cWidth, float cHeight) {



	int x = (Integer)ob.get(FrostbiteDomain.VAR_X);
	int y = (Integer)ob.get(FrostbiteDomain.VAR_Y);
	int size = (Integer)ob.get(FrostbiteDomain.VAR_SIZE);
	boolean activated = (Boolean)ob.get(FrostbiteDomain.VAR_ACTIVATED);
	if (activated)
		g2.setColor(activatedPlatformColor);
	else
		g2.setColor(Color.white);

	g2.fill(new Rectangle2D.Double(x, y, size, size));

	if (x + size > gameWidth)
		g2.fill(new Rectangle2D.Double(x - gameWidth, y, size, size));
	else if (x < 0)
		g2.fill(new Rectangle2D.Double(x + gameWidth, y, size, size));
}
 
Example #21
Source File: GridWorldState.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public List<ObjectInstance> objectsOfClass(String oclass) {
	if(oclass.equals("agent")){
		return Arrays.<ObjectInstance>asList(agent);
	}
	else if(oclass.equals("location")){
		return new ArrayList<ObjectInstance>(locations);
	}
	throw new RuntimeException("Unknown class type " + oclass);
}
 
Example #22
Source File: FrostbiteState.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public List<ObjectInstance> objectsOfClass(String oclass) {
	if(oclass.equals(CLASS_AGENT)){
		return Arrays.<ObjectInstance>asList(agent);
	}
	else if(oclass.equals(CLASS_IGLOO)){
		return Arrays.<ObjectInstance>asList(igloo);
	}
	else if(oclass.equals(CLASS_PLATFORM)){
		return new ArrayList<ObjectInstance>(platforms);
	}
	throw new UnknownClassException(oclass);

}
 
Example #23
Source File: GridWorldState.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public List<ObjectInstance> objects() {
	List<ObjectInstance> obs = new ArrayList<ObjectInstance>(1+locations.size());
	obs.add(agent);
	obs.addAll(locations);
	return obs;
}
 
Example #24
Source File: LLAgent.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectInstance copyWithName(String objectName) {
	if(!objectName.equals(CLASS_AGENT))
		throw new RuntimeException("Lunar lander agent number must be " + CLASS_AGENT);

	return copy();
}
 
Example #25
Source File: BlocksWorldState.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public List<ObjectInstance> objectsOfClass(String oclass) {
	if(!oclass.equals(CLASS_BLOCK)){
		throw new RuntimeException("Unsupported object class " + oclass);
	}
	return new ArrayList<ObjectInstance>(this.blocks.values());
}
 
Example #26
Source File: BlockDudeState.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public List<ObjectInstance> objects() {
	ArrayList<ObjectInstance> obs = new ArrayList<ObjectInstance>(this.blocks);
	obs.add(agent);
	obs.add(map);
	obs.add(exit);
	return obs;
}
 
Example #27
Source File: BlockDudeState.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public MutableOOState addObject(ObjectInstance o) {

	if(!(o instanceof BlockDudeCell) || !o.className().equals(CLASS_BLOCK)){
		throw new RuntimeException("Can only add block objects to state.");
	}
	//copy on write
	this.blocks = new ArrayList<BlockDudeCell>(blocks);
	blocks.add((BlockDudeCell) o);

	return this;
}
 
Example #28
Source File: BlockDudeVisualizer.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public void paintObject(Graphics2D g2, OOState s, ObjectInstance ob, float cWidth, float cHeight) {

	g2.setColor(Color.green);

	float domainXScale = (maxx) - minx;
	float domainYScale = (maxy) - miny;

	//determine then normalized width
	float width = (1.0f / domainXScale) * cWidth;
	float height = (1.0f / domainYScale) * cHeight;


	int [][] map = (int[][])ob.get(BlockDude.VAR_MAP);

	for(int x = 0; x < map.length; x++){
		for(int y = 0; y < map[0].length; y++){
			float rx = x * width;
			float ry = cHeight - height - y * height;

			if(map[x][y] == 1) {
				g2.fill(new Rectangle2D.Float(rx, ry, width, height));
			}
		}
	}

}
 
Example #29
Source File: BlocksWorldState.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(Object variableKey) {
	OOVariableKey key = OOStateUtilities.generateKey(variableKey);
	ObjectInstance ob = this.blocks.get(key.obName);
	if(ob == null){
		throw new RuntimeException("Unknown object " + ob.name());
	}
	return ob.get(key.obVarKey);
}
 
Example #30
Source File: ExampleOOGridWorld.java    From burlap_examples with MIT License 5 votes vote down vote up
@Override
public boolean isTrue(OOState s, String... params) {
	ObjectInstance agent = s.object(params[0]);
	ObjectInstance location = s.object(params[1]);

	int ax = (Integer)agent.get(VAR_X);
	int ay = (Integer)agent.get(VAR_Y);

	int lx = (Integer)location.get(VAR_X);
	int ly = (Integer)location.get(VAR_Y);

	return ax == lx && ay == ly;

}