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

The following examples show how to use burlap.mdp.core.state.State#copy() . 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: IPModel.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public State sample(State s, Action a) {

	s = s.copy();

	double baseForce = 0.;
	if(a.actionName().equals(CartPoleDomain.ACTION_LEFT)){
		baseForce = -physParams.actionForce;
	}
	else if(a.actionName().equals(CartPoleDomain.ACTION_RIGHT)){
		baseForce = physParams.actionForce;
	}


	double roll = RandomFactory.getMapped(0).nextDouble() * (2 * physParams.actionNoise) - physParams.actionNoise;
	double force = baseForce + roll;

	return updateState(s, force);
}
 
Example 2
Source File: BlockDudeModel.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public State sample(State s, Action a) {

	BlockDudeState bs = (BlockDudeState)s.copy();
	String aname = a.actionName();
	if(aname.equals(ACTION_WEST)){
		moveHorizontally(bs, -1);
	}
	else if(aname.equals(ACTION_EAST)){
		moveHorizontally(bs, 1);
	}
	else if(aname.equals(ACTION_UP)){
		moveUp(bs);
	}
	else if(aname.equals(ACTION_PICKUP)){
		putdownBlock(bs);
	}
	else if(aname.equals(ACTION_PUT_DOWN)){
		pickupBlock(bs);
	}
	else {
		throw new RuntimeException("Unknown action " + aname);
	}
	return bs;
}
 
Example 3
Source File: LunarLanderModel.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public State sample(State s, Action a) {

	LLState ls = (LLState)s.copy();

	double force  = 0.;
	if(a.actionName().equals(LunarLanderDomain.ACTION_TURN_LEFT)){
		incAngle(ls, -1);
	}
	else if(a.actionName().equals(LunarLanderDomain.ACTION_TURN_RIGHT)){
		incAngle(ls, 1);
	}
	else if(a instanceof LunarLanderDomain.ThrustType.ThrustAction){
		force = ((LunarLanderDomain.ThrustType.ThrustAction)a).thrust;
	}

	updateMotion(ls, force);

	return ls;
}
 
Example 4
Source File: GridWorldDomain.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Override
public State sample(State s, Action a) {

	s = s.copy();

	double [] directionProbs = transitionDynamics[actionInd(a.actionName())];
	double roll = rand.nextDouble();
	double curSum = 0.;
	int dir = 0;
	for(int i = 0; i < directionProbs.length; i++){
		curSum += directionProbs[i];
		if(roll < curSum){
			dir = i;
			break;
		}
	}

	int [] dcomps = movementDirectionFromIndex(dir);
	return move(s, dcomps[0], dcomps[1]);

}
 
Example 5
Source File: TestHashing.java    From burlap with Apache License 2.0 5 votes vote down vote up
public Set<HashableState> generateRandomStates(SADomain domain, State state, HashableStateFactory factory, int width, int numStates, boolean moveLocations) {
	Set<HashableState> hashedStates = new HashSet<HashableState>();
	Random random = new Random();
	int misses = 0;
	int prevSize = 0;
	while (hashedStates.size() < numStates) {
		if (hashedStates.size() == prevSize) {
			misses++;
		}
		if (misses > 100) {
			break;
		}
		prevSize = hashedStates.size();
		if (prevSize > 0 && prevSize % 10000 == 0) {
			System.out.println("\t" + prevSize);
		}
		GridWorldState copy = (GridWorldState)state.copy();
		copy.touchAgent().x = random.nextInt(width);
		copy.agent.y = random.nextInt(width);

		
		if (moveLocations) {
			List<GridLocation> locations = copy.deepTouchLocations();
			for(GridLocation loc : locations){
				loc.x = random.nextInt(width);
				loc.y = random.nextInt(width);
			}
		}
		hashedStates.add(factory.hashState(copy));
	}
	return hashedStates;
}
 
Example 6
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 7
Source File: TestHashing.java    From burlap with Apache License 2.0 5 votes vote down vote up
public Set<HashableState> generateStates(SADomain domain, State state, HashableStateFactory factory, int width) {
	Set<HashableState> hashedStates = new HashSet<HashableState>();
	for (int i = 0; i < width; ++i) {
		for (int j =0 ; j < width; ++j) {
			GridWorldState copy = (GridWorldState)state.copy();
			copy.touchAgent().x = i;
			copy.agent.y = j;
			hashedStates.add(factory.hashState(copy));
		}
	}
	return hashedStates;
}
 
Example 8
Source File: CPClassicModel.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();

	if(a.actionName().equals(CartPoleDomain.ACTION_RIGHT)){
		return moveClassicModel(s, 1);
	}
	else if(a.actionName().equals(CartPoleDomain.ACTION_LEFT)){
		return moveClassicModel(s, -1);
	}
	throw new RuntimeException("Unknown action " + a.actionName());

}
 
Example 9
Source File: CPCorrectModel.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();

	if(a.actionName().equals(CartPoleDomain.ACTION_RIGHT)){
		return moveCorrectModel(s, 1);
	}
	else if(a.actionName().equals(CartPoleDomain.ACTION_LEFT)){
		return moveCorrectModel(s, -1);
	}
	throw new RuntimeException("Unknown action " + a.actionName());

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

	FrostbiteState fs = (FrostbiteState)s.copy();

	String aname = a.actionName();
	if(aname.equals(ACTION_EAST)){
		move(fs, 1, 0);
	}
	else if(aname.equals(ACTION_WEST)){
		move(fs, -1, 0);
	}
	else if(aname.equals(ACTION_NORTH)){
		move(fs, 0, -1);
	}
	else if(aname.equals(ACTION_SOUTH)){
		move(fs, 0, 1);
	}
	else if(aname.equals(ACTION_IDLE)){
		move(fs, 0, 0);
	}
	else{
		throw new RuntimeException("Unknown action " + a.toString());
	}

	return fs;
}
 
Example 11
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 12
Source File: BWModel.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Override
public State sample(State s, Action a) {

	BlocksWorldState bs = (BlocksWorldState)s.copy();

	if(a.actionName().equals(ACTION_STACK)){
		return stack(bs, (ObjectParameterizedAction)a);
	}
	else if(a.actionName().equals(ACTION_UNSTACK)){
		return unstack(bs, (ObjectParameterizedAction)a);
	}

	throw new RuntimeException("Unknown action " + a.toString());
}
 
Example 13
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 14
Source File: ExampleGridWorld.java    From burlap_examples with MIT License 5 votes vote down vote up
@Override
public State sample(State s, Action a) {

	s = s.copy();
	EXGridState gs = (EXGridState)s;
	int curX = gs.x;
	int curY = gs.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
	gs.x = newPos[0];
	gs.y = newPos[1];

	//return the state we just modified
	return gs;
}
 
Example 15
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 16
Source File: MountainCar.java    From burlap with Apache License 2.0 4 votes vote down vote up
@Override
public State sample(State s, Action a) {
	s = s.copy();
	return move(s, dir(a.actionName()));
}
 
Example 17
Source File: StaticRepeatedGameModel.java    From burlap with Apache License 2.0 4 votes vote down vote up
@Override
public State sample(State s, JointAction ja) {
	return s.copy();
}
 
Example 18
Source File: IdentityStateMapping.java    From burlap with Apache License 2.0 4 votes vote down vote up
@Override
public State mapState(State s) {
	return s.copy();
}