All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.java.burlap.domain.singleagent.blocksworld.BlocksWorldState Maven / Gradle / Ivy

Go to download

The Brown-UMBC Reinforcement Learning and Planning (BURLAP) Java code library is for the use and development of single or multi-agent planning and learning algorithms and domains to accompany them. The library uses a highly flexible state/observation representation where you define states with your own Java classes, enabling support for domains that discrete, continuous, relational, or anything else. Planning and learning algorithms range from classic forward search planning to value-function-based stochastic planning and learning algorithms.

The newest version!
package burlap.domain.singleagent.blocksworld;

import burlap.mdp.core.oo.state.MutableOOState;
import burlap.mdp.core.oo.state.OOStateUtilities;
import burlap.mdp.core.oo.state.OOVariableKey;
import burlap.mdp.core.oo.state.ObjectInstance;
import burlap.mdp.core.state.MutableState;
import burlap.mdp.core.state.State;
import burlap.mdp.core.state.StateUtilities;
import burlap.mdp.core.state.annotations.ShallowCopyState;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static burlap.domain.singleagent.blocksworld.BlocksWorld.*;

/**
 * @author James MacGlashan.
 */
@ShallowCopyState
public class BlocksWorldState implements MutableOOState{

	protected Map blocks = new HashMap();

	public BlocksWorldState() {

	}

	public BlocksWorldState(Map blocks) {
		this.blocks = blocks;
	}


	@Override
	public MutableOOState addObject(ObjectInstance o) {
		if(!(o instanceof BlocksWorldBlock)){
			throw new RuntimeException("Can only add BlocksWorldBlock ObjectInstances");
		}
		this.blocks.put(o.name(), (BlocksWorldBlock)o);
		return this;
	}

	@Override
	public MutableOOState removeObject(String oname) {
		this.blocks.remove(oname);
		return this;
	}

	@Override
	public MutableOOState renameObject(String objectName, String newName) {
		BlocksWorldBlock ob = this.blocks.get(objectName);
		if(ob != null){
			this.blocks.remove(objectName);
			this.blocks.put(newName, (BlocksWorldBlock)ob.copyWithName(newName));
		}
		return this;
	}

	@Override
	public int numObjects() {
		return this.blocks.size();
	}

	@Override
	public ObjectInstance object(String oname) {
		return this.blocks.get(oname);
	}

	@Override
	public List objects() {
		return new ArrayList(this.blocks.values());
	}

	@Override
	public List objectsOfClass(String oclass) {
		if(!oclass.equals(CLASS_BLOCK)){
			throw new RuntimeException("Unsupported object class " + oclass);
		}
		return new ArrayList(this.blocks.values());
	}

	@Override
	public MutableState set(Object variableKey, Object value) {
		OOVariableKey key = OOStateUtilities.generateKey(variableKey);
		BlocksWorldBlock ob = this.blocks.get(key.obName);
		if(ob != null){
			ob = ob.copy();
			if(key.obVarKey.equals(VAR_ON)){
				ob.on = (String)value;
			}
			else if(key.obVarKey.equals(VAR_CLEAR)){
				ob.clear = StateUtilities.stringOrBoolean(value);
			}
			else if(key.obVarKey.equals(VAR_COLOR)){
				ob.color = (Color)value;
			}
			this.blocks.put(ob.name(), ob);
		}
		return this;
	}

	@Override
	public List variableKeys() {
		return OOStateUtilities.flatStateKeys(this);
	}

	@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);
	}

	@Override
	public State copy() {
		return new BlocksWorldState(new HashMap(blocks));
	}

	@Override
	public String toString() {
		return OOStateUtilities.ooStateToString(this);
	}
}