Java Code Examples for burlap.mdp.core.state.State#get()

The following examples show how to use burlap.mdp.core.state.State#get() . 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 boolean flatStatesEqual(State s1, State s2){

		if(s1 == s2){
			return true;
		}

		List<Object> keys1 = s1.variableKeys();
		List<Object> keys2 = s2.variableKeys();

		if(keys1.size() != keys2.size()){
			return false;
		}

		for(Object key : keys1){
			Object v1 = s1.get(key);
			Object v2 = s2.get(key);
			if(!this.valuesEqual(key, v1, v2)){
				return false;
			}
		}
		return true;

	}
 
Example 2
Source File: IISimpleHashableState.java    From burlap with Apache License 2.0 6 votes vote down vote up
protected boolean flatStatesEqual(State s1, State s2){

		if(s1 == s2){
			return true;
		}

		List<Object> keys1 = s1.variableKeys();
		List<Object> keys2 = s2.variableKeys();

		if(keys1.size() != keys2.size()){
			return false;
		}

		for(Object key : keys1){
			Object v1 = s1.get(key);
			Object v2 = s2.get(key);
			if(!this.valuesEqual(key, v1, v2)){
				return false;
			}
		}
		return true;

	}
 
Example 3
Source File: ExampleGridWorld.java    From burlap_examples with MIT License 5 votes vote down vote up
@Override
public void paint(Graphics2D g2, State s,
						float cWidth, float cHeight) {

	//agent will be filled in gray
	g2.setColor(Color.GRAY);

	//set up floats for the width and height of our domain
	float fWidth = ExampleGridWorld.this.map.length;
	float fHeight = ExampleGridWorld.this.map[0].length;

	//determine the width of a single cell on our canvas
	//such that the whole map can be painted
	float width = cWidth / fWidth;
	float height = cHeight / fHeight;

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

	//left coordinate of cell on our canvas
	float rx = ax*width;

	//top coordinate of cell on our canvas
	//coordinate system adjustment because the java canvas
	//origin is in the top left instead of the bottom right
	float ry = cHeight - height - ay*height;

	//paint the rectangle
	g2.fill(new Ellipse2D.Float(rx, ry, width, height));


}
 
Example 4
Source File: ExampleGridWorld.java    From burlap_examples with MIT License 5 votes vote down vote up
@Override
public double reward(State s, Action a, State sprime) {

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

	//are they at goal location?
	if(ax == this.goalX && ay == this.goalY){
		return 100.;
	}

	return -1;
}
 
Example 5
Source File: ExampleGridWorld.java    From burlap_examples with MIT License 5 votes vote down vote up
@Override
public boolean isTerminal(State s) {

	//get location of agent in next state
	int ax = (Integer)s.get(VAR_X);
	int ay = (Integer)s.get(VAR_Y);

	//are they at goal location?
	if(ax == this.goalX && ay == this.goalY){
		return true;
	}

	return false;
}
 
Example 6
Source File: IDSimpleHashableState.java    From burlap with Apache License 2.0 5 votes vote down vote up
protected int computeFlatHashCode(State s){

		HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(17, 31);

		List<Object> keys = s.variableKeys();
		for(Object key : keys){
			Object value = s.get(key);
			this.appendHashCodeForValue(hashCodeBuilder, key, value);
		}

		return hashCodeBuilder.toHashCode();
	}
 
Example 7
Source File: IISimpleHashableState.java    From burlap with Apache License 2.0 5 votes vote down vote up
protected int computeFlatHashCode(State s){

		HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(17, 31);

		List<Object> keys = s.variableKeys();
		for(Object key : keys){
			Object value = s.get(key);
			this.appendHashCodeForValue(hashCodeBuilder, key, value);
		}

		return hashCodeBuilder.toHashCode();
	}
 
Example 8
Source File: MountainCar.java    From burlap with Apache License 2.0 5 votes vote down vote up
/**
 * Changes the agents position in the provided state using car engine acceleration in the specified direction.
 * dir=+1 indicates forward acceleration; -1 backwards acceleration; 0 no acceleration (coast).
 * @param s the state in which the agents position should be modified
 * @param dir the direction of acceleration
 * @return the modified state s
 */
public State move(State s, int dir){

	double p0 = (Double)s.get(ATT_X);
	double v0 = (Double)s.get(ATT_V);

	double netAccel = (physParams.acceleration * dir) - (physParams.gravity * Math.cos(physParams.cosScale*p0));

	double v1 = v0 + physParams.timeDelta * netAccel;
	if(v1 < physParams.vmin){
		v1 = physParams.vmin;
	}
	else if(v1 > physParams.vmax){
		v1 = physParams.vmax;
	}

	double p1 = p0 + physParams.timeDelta*v1; //original mechanics in paper defined this way
	//double p1 = p0 + this.timeDelta*v0 + .5*netAccel*this.timeDelta*this.timeDelta; //more accurate estimate

	if(p1 < physParams.xmin){
		p1 = physParams.xmin;
		v1 = 0.;
	}
	else if(p1 > physParams.xmax){
		p1 = physParams.xmax;
		v1 = 0.;
	}


	((MutableState)s).set(ATT_X, p1);
	((MutableState)s).set(ATT_V, v1);

	return s;

}
 
Example 9
Source File: GraphDefinedDomain.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public State sample(State s, Action a) {

	s = s.copy();

	int aId = ((GraphActionType.GraphAction)a).aId;

	int n = (Integer)s.get(VAR);

	Map<Integer, Set<NodeTransitionProbability>> actionMap = transitionDynamics.get(n);
	Set<NodeTransitionProbability> transitions = actionMap.get(aId);

	double roll = rand.nextDouble();
	double sumP = 0.;
	int selection = 0;
	for(NodeTransitionProbability ntp : transitions){
		sumP += ntp.probability;
		if(roll < sumP){
			selection = ntp.transitionTo;
			break;
		}
	}

	((MutableState)s).set(VAR, selection);

	return s;
}
 
Example 10
Source File: GraphDefinedDomain.java    From burlap with Apache License 2.0 5 votes vote down vote up
protected boolean applicableInState(State s){
	int n = (Integer)s.get("node");

	Map<Integer, Set<NodeTransitionProbability>> actionMap = transitionDynamics.get(n);
	Set<NodeTransitionProbability> transitions = actionMap.get(aId);
	if(transitions == null){
		return false;
	}
	if(transitions.isEmpty()){
		return false;
	}

	return true;
}
 
Example 11
Source File: TigerObservations.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public State sample(State state, Action action){
	//override for faster sampling
	if(action.actionName().equals(TigerDomain.ACTION_LEFT) || action.actionName().equals(TigerDomain.ACTION_RIGHT)){
		return this.observationReset();
	}
	else if(action.actionName().equals(TigerDomain.ACTION_LISTEN)){
		String tigerVal = (String)state.get(TigerDomain.VAR_DOOR);
		double r = RandomFactory.getMapped(0).nextDouble();
		if(r < this.listenAccuracy){
			if(tigerVal.equals(TigerDomain.VAL_LEFT)){
				return this.observationLeft();
			}
			else{
				return this.observationRight();
			}
		}
		else{
			//then nosiy listen; reverse direction
			if(tigerVal.equals(TigerDomain.VAL_LEFT)){
				return this.observationRight();
			}
			else{
				return this.observationLeft();
			}
		}
	}
	else if(action.actionName().equals(TigerDomain.ACTION_DO_NOTHING)){
		return this.observationNothing();
	}

	throw new RuntimeException("Unknown action " + action.actionName() + "; cannot return observation sample.");
}
 
Example 12
Source File: MountainCarVisualizer.java    From burlap with Apache License 2.0 4 votes vote down vote up
@Override
public void paint(Graphics2D g2, State s, float cWidth, float cHeight) {
	double worldWidth = physParams.xmax - physParams.xmin;

	double renderAgentWidth = 0.04*cWidth;

	double ox = (Double)s.get(ATT_X);
	double oy = Math.sin(this.physParams.cosScale*ox);

	double nx = (ox - this.physParams.xmin) / worldWidth;
	double ny = (oy + 1) / 2;

	double sx = (nx * cWidth) - (renderAgentWidth / 2);
	double sy = cHeight - (ny * (cHeight-30)+15) - (renderAgentWidth / 2);


	g2.setColor(Color.red);

	g2.fill(new Rectangle2D.Double(sx, sy, renderAgentWidth, renderAgentWidth));
}
 
Example 13
Source File: GraphDefinedDomain.java    From burlap with Apache License 2.0 4 votes vote down vote up
@Override
public List<StateTransitionProb> stateTransitions(State s, Action a) {

	int aId = ((GraphActionType.GraphAction)a).aId;

	List <StateTransitionProb> result = new ArrayList<StateTransitionProb>();

	int n = (Integer)s.get(VAR);

	Map<Integer, Set<NodeTransitionProbability>> actionMap = transitionDynamics.get(n);
	Set<NodeTransitionProbability> transitions = actionMap.get(aId);

	for(NodeTransitionProbability ntp : transitions){

		State ns = s.copy();
		((MutableState)ns).set(VAR, ntp.transitionTo);

		StateTransitionProb tp = new StateTransitionProb(ns, ntp.probability);
		result.add(tp);

	}


	return result;

}
 
Example 14
Source File: TigerObservations.java    From burlap with Apache License 2.0 4 votes vote down vote up
@Override
public double probability(State observation, State state,
						  Action action) {


	String oVal = (String)observation.get(TigerDomain.VAR_HEAR);
	String tigerVal = (String)state.get(TigerDomain.VAR_DOOR);

	if(action.actionName().equals(TigerDomain.ACTION_LEFT) || action.actionName().equals(TigerDomain.ACTION_RIGHT)){
		if(oVal.equals(TigerDomain.DOOR_RESET)){
			return 1.;
		}
		return 0.;
	}

	if(action.actionName().equals(TigerDomain.ACTION_LISTEN)){
		if(tigerVal.equals(TigerDomain.VAL_LEFT)){
			if(oVal.equals(TigerDomain.HEAR_LEFT)){
				return this.listenAccuracy;
			}
			else if(oVal.equals(TigerDomain.HEAR_RIGHT)){
				return 1.-this.listenAccuracy;
			}
			else{
				return 0.;
			}
		}
		else{
			if(oVal.equals(TigerDomain.HEAR_LEFT)){
				return 1.-this.listenAccuracy;
			}
			else if(oVal.equals(TigerDomain.HEAR_RIGHT)){
				return this.listenAccuracy;
			}
			else{
				return 0.;
			}
		}
	}

	//otherwise we're in the noop
	if(action.actionName().equals(TigerDomain.ACTION_DO_NOTHING)){
		if(oVal.equals(TigerDomain.HEAR_NOTHING)){
			return 1.;
		}
		else{
			return 0.;
		}
	}

	throw new RuntimeException("Unknown action " + action.actionName() + "; cannot return observation probability.");
}
 
Example 15
Source File: MountainCar.java    From burlap with Apache License 2.0 3 votes vote down vote up
@Override
public boolean isTerminal(State s) {

	double x = (Double)s.get(ATT_X);

	return x >= threshold;

}
 
Example 16
Source File: StateValuePainter2D.java    From burlap with Apache License 2.0 2 votes vote down vote up
@Override
public void paintStateValue(Graphics2D g2, State s, double value, float cWidth, float cHeight) {

	Number x = (Number)s.get(xKey);
	Number y = (Number)s.get(yKey);

	float xval;
	float yval;
	float width;
	float height;
	

	width = cWidth / (float)(xRange.span() / xWidth);
	height = cHeight / (float)(yRange.span() / yWidth);

	float normX = (float)xRange.norm(x.doubleValue());
	xval = normX * cWidth;


	float normY = (float)yRange.norm(y.doubleValue());
	yval = cHeight - height - normY*cHeight;
	
	
	
	Color col = this.colorBlend.color(value);
	g2.setColor(col);
	
	g2.fill(new Rectangle2D.Float(xval, yval, width, height));
	
	if(this.renderValueString){
		
		g2.setColor(this.vsFontColor);
		g2.setFont(new Font("sansserif", Font.BOLD, this.vsFontSize));
		String fstring = String.format("%."+this.vsPrecision+"f", value);
		
		float sxval = xval + this.vsOffsetFromLeft*width;
		float syval = yval + this.vsOffsetFromTop*height;
		
		g2.drawString(fstring, sxval, syval);
		
	}
	

}