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

The following examples show how to use burlap.mdp.core.oo.state.OOState. 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: 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 #2
Source File: PropositionalFunction.java    From burlap with Apache License 2.0 6 votes vote down vote up
/**
 * Returns all possible groundings of this {@link PropositionalFunction} for the given {@link State}
 * @param s the {@link State} in which all groundings will be returned
 * @return a {@link List} of all possible groundings of this {@link PropositionalFunction} for the given {@link State}
 */
public List<GroundedProp> allGroundings(OOState s){
	
	List <GroundedProp> res = new ArrayList<GroundedProp>();
	
	if(this.getParameterClasses().length == 0){
		res.add(new GroundedProp(this, new String[]{}));
		return res; //no parameters so just the single gp without params
	}

	if(!(s instanceof OOState)){
		throw new RuntimeException("Cannot generate all GroundedProp objects for state " + s.getClass().getName() + " because it does not implement OOState");
	}

	List <List <String>> bindings = OOStateUtilities.getPossibleBindingsGivenParamOrderGroups((OOState)s, this.getParameterClasses(), this.getParameterOrderGroups());
	
	for(List <String> params : bindings){
		String [] aprams = params.toArray(new String[params.size()]);
		GroundedProp gp = new GroundedProp(this, aprams);
		res.add(gp);
	}
	
	return res;

}
 
Example #3
Source File: FrostbiteDomain.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isTrue(OOState st, String... params) {

	FrostbiteAgent agent = (FrostbiteAgent)st.object(params[0]);
	int ah = agent.height;

	if (ah != 0)
		return false;

	// Agent is on a platform
	if (getLandedPlatformSpeed((FrostbiteState)st) != 0)
		return false;

	int ay = agent.y + agentSize / 2;
	return ay >= gameIceHeight;
}
 
Example #4
Source File: VisualExplorer.java    From burlap with Apache License 2.0 6 votes vote down vote up
/**
 * Updates the propositional function evaluation text display for the given state.
 * @param s the input state on which propositional functions are to be evaluated.
 */
protected void updatePropTextArea(State s){

	if(!(domain instanceof OODomain) || !(s instanceof OOState)){
		return ;
	}

    StringBuilder buf = new StringBuilder();
	
	List <PropositionalFunction> props = ((OODomain)domain).propFunctions();
	for(PropositionalFunction pf : props){
		List<GroundedProp> gps = pf.allGroundings((OOState)s);
		for(GroundedProp gp : gps){
			if(gp.isTrue((OOState)s)){
				buf.append(gp.toString()).append("\n");
			}
		}
	}
	propViewer.setText(buf.toString());
	
	
}
 
Example #5
Source File: ObjectParameterizedActionType.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public List<Action> allApplicableActions(State s) {

	List <Action> res = new ArrayList<Action>();


	if(!(s instanceof OOState)){
		throw new RuntimeException("Cannot get object-parameterized grounded actions in state, because " + s.getClass().getName() + " does not implement OOState");
	}

	//otherwise need to do parameter binding
	List <List <String>> bindings = OOStateUtilities.getPossibleBindingsGivenParamOrderGroups((OOState)s, this.getParameterClasses(), this.getParameterOrderGroups());

	for(List <String> params : bindings){
		String [] aprams = params.toArray(new String[params.size()]);
		ObjectParameterizedAction ga = this.generateAction(aprams);
		if(this.applicableInState(s, ga)) {
			res.add(ga);
		}
	}

	return res;

}
 
Example #6
Source File: VisualWorldObserver.java    From burlap with Apache License 2.0 6 votes vote down vote up
private void updatePropTextArea(State s){

		if(!(domain instanceof OODomain) || !(s instanceof OOState)){
			return ;
		}

	    StringBuilder buf = new StringBuilder();
		
		List <PropositionalFunction> props = ((OODomain)domain).propFunctions();
		for(PropositionalFunction pf : props){
			//List<GroundedProp> gps = s.getAllGroundedPropsFor(pf);
			List<GroundedProp> gps = pf.allGroundings((OOState)s);
			for(GroundedProp gp : gps){
				if(gp.isTrue((OOState)s)){
					buf.append(gp.toString()).append("\n");
				}
			}
		}
		propViewer.setText(buf.toString());
		
		
	}
 
Example #7
Source File: SGVisualExplorer.java    From burlap with Apache License 2.0 6 votes vote down vote up
protected void updatePropTextArea(State s){

		if(!(domain instanceof OODomain) || !(s instanceof OOState)){
			return;
		}

	    StringBuilder buf = new StringBuilder();
		
		List <PropositionalFunction> props = ((OODomain)domain).propFunctions();
		for(PropositionalFunction pf : props){
			List<GroundedProp> gps = pf.allGroundings((OOState)s);
			for(GroundedProp gp : gps){
				if(gp.isTrue((OOState)s)){
					buf.append(gp.toString()).append("\n");
				}
			}
		}
		

		propViewer.setText(buf.toString());
		
		
	}
 
Example #8
Source File: EpisodeSequenceVisualizer.java    From burlap with Apache License 2.0 6 votes vote down vote up
protected void updatePropTextArea(State s){

		if(!(domain instanceof OODomain) || !(s instanceof OOState)){
			return ;
		}

	    StringBuilder buf = new StringBuilder();
		
		List <PropositionalFunction> props = ((OODomain)domain).propFunctions();
		for(PropositionalFunction pf : props){
			//List<GroundedProp> gps = s.getAllGroundedPropsFor(pf);
			List<GroundedProp> gps = pf.allGroundings((OOState)s);
			for(GroundedProp gp : gps){
				if(gp.isTrue((OOState)s)){
					buf.append(gp.toString()).append("\n");
				}
			}
		}
		
		propViewer.setText(buf.toString());
		
		
		
	}
 
Example #9
Source File: PFFeatures.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public double[] features(State s) {
	
	List<Double> featureValueList = new LinkedList<Double>();
	for(PropositionalFunction pf : this.pfsToUse){
		//List<GroundedProp> gps = s.getAllGroundedPropsFor(pf);
		List<GroundedProp> gps = pf.allGroundings((OOState)s);
		for(GroundedProp gp : gps){
			if(gp.isTrue((OOState)s)){
				featureValueList.add(1.);
			}
			else{
				featureValueList.add(0.);
			}
		}
	}
	
	double [] fv = new double[featureValueList.size()];
	int i = 0;
	for(double f : featureValueList){
		fv[i] = f;
		i++;
	}
	
	return fv;
}
 
Example #10
Source File: GameSequenceVisualizer.java    From burlap with Apache License 2.0 6 votes vote down vote up
private void updatePropTextArea(State s){

		if(!(domain instanceof OODomain) || !(s instanceof OOState)){
			return ;
		}

	    StringBuilder buf = new StringBuilder();
		
		List <PropositionalFunction> props = ((OODomain)domain).propFunctions();
		for(PropositionalFunction pf : props){
			List<GroundedProp> gps = pf.allGroundings((OOState)s);
			for(GroundedProp gp : gps){
				if(gp.isTrue((OOState)s)){
					buf.append(gp.toString()).append("\n");
				}
			}
		}
		
		propViewer.setText(buf.toString());
		
		
		
	}
 
Example #11
Source File: CommandReachable.java    From burlapcraft with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void processCommand(ICommandSender p_71515_1_, String[] p_71515_2_) {

	MinecraftDomainGenerator mdg = new MinecraftDomainGenerator();
	SADomain domain = mdg.generateDomain();

	State in = MinecraftStateGeneratorHelper.getCurrentState(BurlapCraft.currentDungeon);
	List<State> reachable = StateReachability.getReachableStates(in, domain, new SimpleHashableStateFactory());
	for(State s : reachable){
		OOState os = (OOState)s;
		BCAgent a = (BCAgent)os.object(CLASS_AGENT);
		System.out.println(a.x + ", " + a.y + ", " + a.z + ", " + a.rdir + ", "+ a.vdir + ", " + a.selected);
	}
	System.out.println(reachable.size());

}
 
Example #12
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 #13
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 #14
Source File: LunarLanderDomain.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isTrue(OOState st, String... params) {

	LLAgent agent = (LLAgent)st.object(params[0]);
	LLBlock.LLPad pad = (LLBlock.LLPad)st.object(params[1]);
	
	
	double l = pad.left;
	double r = pad.right;
	double b = pad.bottom;
	double t = pad.top;
	
	double x = agent.x;
	double y = agent.y;
	
	//on pad means landed on surface, so y should be equal to top
	if(x >= l && x < r && y >= b && y <= t){
		return true;
	}
	

	return false;
}
 
Example #15
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 #16
Source File: GoldBlockTF.java    From burlapcraft with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean isTerminal(State s) {
	OOState os = (OOState)s;

	BCAgent a = (BCAgent)os.object(CLASS_AGENT);

	HelperGeometry.Pose agentPose = HelperGeometry.Pose.fromXyz(a.x, a.y, a.z);

	HelperGeometry.Pose goalPose = getGoalPose(s);

	//are they at goal location or dead
	double distance = goalPose.distance(agentPose);
	//System.out.println("Distance: " + distance + " goal at: " + goalPose);

	if (distance < 0.5) {
		return true;
	} else {
		return false;
	}
}
 
Example #17
Source File: MinecraftStateGeneratorHelper.java    From burlapcraft with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void validate(State s) {

		BCAgent a = (BCAgent)((OOState)s).object(HelperNameSpace.CLASS_AGENT);


		if (a.x < 0) {
			throw new IllegalStateException("Invalid agent x: " + a.x);
		}
		if (a.y < 0) {
			throw new IllegalStateException("Invalid agent y: " + a.y);
		}
		if (a.z < 0) {
			throw new IllegalStateException("Invalid agent z: " + a.z);
		}

	}
 
Example #18
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 #19
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 #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: 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 #22
Source File: IISimpleHashableState.java    From burlap with Apache License 2.0 5 votes vote down vote up
protected boolean ooStatesEqual(OOState s1, OOState s2){
	if(s1 == s2){
		return true;
	}
	if(s1.numObjects() != s2.numObjects()){
		return false;
	}

	Set<String> matchedObjects = new HashSet<String>();
	for(Map.Entry<String, List<ObjectInstance>> e1 : OOStateUtilities.objectsByClass(s1).entrySet()){
		String oclass = e1.getKey();
		List<ObjectInstance> objects = e1.getValue();

		List<ObjectInstance> oobjects = s2.objectsOfClass(oclass);
		if(objects.size() != oobjects.size()){
			return false;
		}

		for(ObjectInstance o : objects){
			boolean foundMatch = false;
			for(ObjectInstance oo : oobjects){
				String ooname = oo.name();
				if(matchedObjects.contains(ooname)){
					continue;
				}
				if(flatStatesEqual(o, oo)){
					foundMatch = true;
					matchedObjects.add(ooname);
					break;
				}
			}
			if(!foundMatch){
				return false;
			}
		}

	}

	return true;
}
 
Example #23
Source File: LunarLanderRF.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public double reward(State s, Action a, State sprime) {
	if(onPad.someGroundingIsTrue((OOState)sprime)){
		return goalReward;
	}

	if(this.onGround.someGroundingIsTrue((OOState)sprime) || this.touchingPad.someGroundingIsTrue((OOState)sprime) || this.touchingSurface.someGroundingIsTrue((OOState)sprime)){
		return collisionReward;
	}

	return defaultReward;
}
 
Example #24
Source File: BlockDude.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isTrue(OOState st, String... params) {

	BlockDudeAgent a = (BlockDudeAgent)st.object(params[0]);
	BlockDudeCell e = (BlockDudeCell)st.object(params[1]);

	if(a.x == e.x && a.y == e.y){
		return true;
	}

	return false;
}
 
Example #25
Source File: LunarLanderDomain.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isTrue(OOState st, String... params) {

	LLAgent agent = (LLAgent)st.object(params[0]);
	double y = agent.y;
	
	if(y == ymin){
		return true;
	}
	
	return false;
}
 
Example #26
Source File: BlocksWorld.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isTrue(OOState st, String... params) {
	BlocksWorldState s = (BlocksWorldState)st;
	BlocksWorldBlock src = (BlocksWorldBlock)s.object(params[0]);
	BlocksWorldBlock target = (BlocksWorldBlock)s.object(params[1]);
	return src.on.equals(target.name());

}
 
Example #27
Source File: GGVisualizer.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) {
	
	int colInd = 0;
	if(ob.className().equals(GridGame.CLASS_AGENT)){
		colInd = (Integer)ob.get(GridGame.VAR_PN);
	}
	else if(ob.className().equals(GridGame.CLASS_GOAL)){
		colInd = (Integer)ob.get(GridGame.VAR_GT);
	}
	
	g2.setColor(this.cols.get(colInd));
	
	float domainXScale = maxX;
	float domainYScale = maxY;
	
	//determine then normalized width
	float width = (1.0f / domainXScale) * cWidth;
	float height = (1.0f / domainYScale) * cHeight;
	
	float rx = (Integer)ob.get(GridGame.VAR_X)*width;
	float ry = cHeight - height - (Integer)ob.get(GridGame.VAR_Y)*height;
	
	if(shape == 0){
		g2.fill(new Rectangle2D.Float(rx, ry, width, height));
	}
	else if(shape == 1){
		g2.fill(new Ellipse2D.Float(rx, ry, width, height));
	}
	
}
 
Example #28
Source File: GridGame.java    From burlap with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the personal goal rewards. If a single common personal goal reward was set then that is returned. If different personal goal rewards were defined for each
 * player number, then that is queried and returned instead.
 * @param s the state in which the agent player numbers are defined
 * @param agentName the agent name for which the person goal reward is to be returned
 * @return the personal goal reward for the specified agent.
 */
protected double getPersonalGoalReward(OOState s, String agentName){
	if(this.personalGoalRewards == null){
		return this.pGoalReward;
	}
	
	int pn = (Integer)s.object(agentName).get(GridGame.VAR_PN);
	return this.personalGoalRewards.get(pn);
	
}
 
Example #29
Source File: IRLExample.java    From burlap_examples with MIT License 5 votes vote down vote up
@Override
public double[] features(State s) {

	double [] fv = new double[this.numLocations];

	int aL = this.getActiveLocationVal((OOState)s);
	if(aL != -1){
		fv[aL] = 1.;
	}

	return fv;
}
 
Example #30
Source File: GridGame.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isTrue(OOState s, String... params) {
	
	ObjectInstance agent = s.object(params[0]);
	int ax = (Integer)agent.get(VAR_X);
	int ay = (Integer)agent.get(VAR_Y);
	int apn = (Integer)agent.get(VAR_PN);
	
	//find all universal goals
	List <ObjectInstance> goals = s.objectsOfClass(CLASS_GOAL);
	for(ObjectInstance goal : goals){
		
		int gt = (Integer)goal.get(VAR_GT);
		if(gt == apn+1){
		
			int gx = (Integer)goal.get(VAR_X);
			int gy = (Integer)goal.get(VAR_Y);
			if(gx == ax && gy == ay){
				return true;
			}
			
		}
		
		
	}
	
	return false;
}