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

main.java.burlap.behavior.singleagent.auxiliary.gridset.FlatStateGridder 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.behavior.singleagent.auxiliary.gridset;

import burlap.mdp.core.state.MutableState;
import burlap.mdp.core.state.State;

import java.util.*;

/**
 * This class is used to generate a set of continuous states that are spaced over grid points. The grid dimensions are specified
 * using the {@link #gridDimension(Object, VariableGridSpec)} or {@link #gridDimension(Object, double, double, int)}
 * method. Gridding is performed by manipulating (and copying) an input {@link MutableState}. If the input state
 * contains state variables that do not have a grid spec, then those values will hold constant.
 * @author James MacGlashan.
 */
public class FlatStateGridder {

	protected Map gridSpecs = new HashMap();

	/**
	 * Specify a state variable as a dimension of the grid
	 * @param varKey the variable key
	 * @param lowerVal the lower value of grid
	 * @param upperVal the upper value of the grid
	 * @param numGridPoints the number of grid points that spans the lower to upper bound
	 * @return this object, so that the builder paradigm can be used for specifying multiple dimensions.
	 */
	public FlatStateGridder gridDimension(Object varKey, double lowerVal, double upperVal, int numGridPoints){
		gridSpecs.put(varKey, new VariableGridSpec(lowerVal, upperVal, numGridPoints));
		return this;
	}

	/**
	 * Specify a state variable as a dimension of a the grid
	 * @param varKey the variable key
	 * @param gridSpec the grid specification
	 * @return this object, so that the builder paradigm can be used for specifying multiple dimensions.
	 */
	public FlatStateGridder gridDimension(Object varKey, VariableGridSpec gridSpec){
		gridSpecs.put(varKey, gridSpec);
		return this;
	}

	/**
	 * Returns the grid spec defined for the variable key
	 * @param varKey the variable key
	 * @return the grid spec
	 */
	public VariableGridSpec gridSpec(Object varKey){
		return this.gridSpecs.get(varKey);
	}

	/**
	 * Returns the set of all grid specs defined. The key in each returned entry is the variable key.
	 * @return the set of all grid specs defined.
	 */
	public Set> specs(){
		return this.gridSpecs.entrySet();
	}


	/**
	 * Grids the input state. Any variables in the state for which grid specifications have not be specified will be held
	 * constant in the set of returned states.
	 * @param s the input state to grid.
	 * @return the list of states spaced on the grid
	 */
	public List gridState(MutableState s){
		s = (MutableState)s.copy();
		List gridded = new ArrayList();
		this.gridStateHelper(s, new ArrayList>(gridSpecs.entrySet()), 0, gridded);
		return gridded;
	}

	protected void gridStateHelper(MutableState s, List> gridDims, int index, List createdStates){
		if(index == gridDims.size()){
			createdStates.add(s.copy());
		}
		else{
			Object key = gridDims.get(index).getKey();
			VariableGridSpec spec = gridDims.get(index).getValue();
			double cellWidth = spec.cellWidth();
			for(int i = 0; i < spec.numGridPoints; i++){
				double value = i*cellWidth + spec.lowerVal;
				s.set(key, value);
				this.gridStateHelper(s, gridDims, index+1, createdStates);
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy