Java Code Examples for burlap.statehashing.HashableStateFactory#hashState()

The following examples show how to use burlap.statehashing.HashableStateFactory#hashState() . 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: TestHashing.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleHashFactoryIdentifierIndependent() {
	SADomain domain = (SADomain)this.gridWorldTest.getDomain();
	State startState = this.gridWorldTest.generateState();
	HashableStateFactory factory = new SimpleHashableStateFactory();
	Set<HashableState> hashedStates = this.getReachableHashedStates(startState, domain, factory);
	assert(hashedStates.size() == 104);
	
	Set<HashableState> renamedStates = new HashSet<HashableState>();
	for (HashableState state : hashedStates) {
		State source = state.s();
		State renamed = this.renameObjects((GridWorldState)source.copy());
		HashableState renamedHashed = factory.hashState(renamed);
		renamedStates.add(renamedHashed);
	}
	hashedStates.addAll(renamedStates);
	assert(hashedStates.size() == 104);
}
 
Example 2
Source File: TestHashing.java    From burlap with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleHashFactoryIdentifierDependent() {
	SADomain domain = (SADomain)this.gridWorldTest.getDomain();
	State startState = this.gridWorldTest.generateState();
	HashableStateFactory factory = new SimpleHashableStateFactory(false);
	Set<HashableState> hashedStates = this.getReachableHashedStates(startState, domain, factory);
	assert(hashedStates.size() == 104);
	
	Set<HashableState> renamedStates = new HashSet<HashableState>();
	for (HashableState state : hashedStates) {
		State source = state.s();
		State renamed = this.renameObjects((GridWorldState)source.copy());
		HashableState renamedHashed = factory.hashState(renamed);
		renamedStates.add(renamedHashed);
	}
	hashedStates.addAll(renamedStates);
	assert(hashedStates.size() == 208);
}
 
Example 3
Source File: TestHashing.java    From burlap with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleHashFactoryLargeStateIdentifierDependent() {
	SADomain domain = (SADomain)this.gridWorldTest.getDomain();
	State startState = this.generateLargeGW(domain, 100);
	HashableStateFactory factory = new SimpleHashableStateFactory(false);
	Set<HashableState> hashedStates = this.getReachableHashedStates(startState, domain, factory);
	int size = hashedStates.size();
	Set<Integer> hashes = new HashSet<Integer>();
	for (HashableState hs : hashedStates) {
		hashes.add(hs.hashCode());
	}
	System.err.println("Hashed states: " + hashedStates.size() + ", hashes: " + hashes.size());
	if (hashedStates.size() != hashes.size()) {
		System.err.println("Hashed states: " + hashedStates.size() + ", hashes: " + hashes.size());
	}
	
	Set<HashableState> renamedStates = new HashSet<HashableState>();
	for (HashableState state : hashedStates) {
		State source = state.s();
		State renamed = this.renameObjects((GridWorldState)source.copy());
		HashableState renamedHashed = factory.hashState(renamed);
		renamedStates.add(renamedHashed);
	}
	hashedStates.addAll(renamedStates);
	assert(hashedStates.size() == size * 2);
	
}
 
Example 4
Source File: StateReachability.java    From burlap with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the set of {@link State} objects that are reachable from a source state.
 * @param from the source state
 * @param inDomain the domain of the state
 * @param usingHashFactory the state hashing factory to use for indexing states and testing equality.
 * @return the set of {@link State} objects that are reachable from a source state.
 */
public static Set <HashableState> getReachableHashedStates(State from, SADomain inDomain, HashableStateFactory usingHashFactory){

	if(!(inDomain.getModel() instanceof FullModel)){
		throw new RuntimeException( "State reachablity requires a domain with a FullModel, but one is not provided");
	}

	FullModel model = (FullModel)inDomain.getModel();

	Set<HashableState> hashedStates = new HashSet<HashableState>();
	HashableState shi = usingHashFactory.hashState(from);
	List <ActionType> actionTypes = inDomain.getActionTypes();
	int nGenerated = 0;
	
	LinkedList <HashableState> openList = new LinkedList<HashableState>();
	openList.offer(shi);
	hashedStates.add(shi);
	long firstTime = System.currentTimeMillis();
	long lastTime = firstTime;
	while(!openList.isEmpty()){
		HashableState sh = openList.poll();

		
		List<Action> gas = ActionUtils.allApplicableActionsForTypes(actionTypes, sh.s());
		for(Action ga : gas){
			List <TransitionProb> tps = model.transitions(sh.s(), ga);
			nGenerated += tps.size();
			for(TransitionProb tp : tps){
				HashableState nsh = usingHashFactory.hashState(tp.eo.op);
				
				if (hashedStates.add(nsh) && !tp.eo.terminated) {
					openList.offer(nsh);
				}
			}
		}
		
		long currentTime = System.currentTimeMillis();
		if (currentTime - 1000 >= lastTime) {
			DPrint.cl(debugID, "Num generated: " + (nGenerated) + " Unique: " + (hashedStates.size()) + 
					" time: " + ((double)currentTime - firstTime)/1000.0);				
			lastTime = currentTime;
		}
	}
	
	DPrint.cl(debugID, "Num generated: " + nGenerated + "; num unique: " + hashedStates.size());
	
	return hashedStates;
}
 
Example 5
Source File: StateReachability.java    From burlap with Apache License 2.0 4 votes vote down vote up
/**
 * Finds the set of states ({@link burlap.statehashing.HashableState}) that are reachable under a policy from a source state. Reachability under a source policy means
 * that the space of actions considered are those that have non-zero probability of being selected by the
 * policy and all possible outcomes of those states are considered.
 * @param domain the domain containing the model to use for evaluating reachable states
 * @param p the policy that must be followed
 * @param from the source {@link State} from which the policy would be initiated.
 * @param usingHashFactory the {@link burlap.statehashing.HashableStateFactory} used to hash states and test equality.
 * @return a {@link java.util.Set} of {@link burlap.statehashing.HashableState} objects that could be reached.
 */
public static Set<HashableState> getPolicyReachableHashedStates(SADomain domain, EnumerablePolicy p, State from, HashableStateFactory usingHashFactory){

	if(!(domain.getModel() instanceof FullModel)){
		throw new RuntimeException( "State reachablity requires a domain with a FullModel, but one is not provided");
	}

	FullModel model = (FullModel)domain.getModel();

	Set<HashableState> hashedStates = new HashSet<HashableState>();
	HashableState shi = usingHashFactory.hashState(from);
	int nGenerated = 0;

	LinkedList <HashableState> openList = new LinkedList<HashableState>();
	openList.offer(shi);
	hashedStates.add(shi);

	MyTimer timer = new MyTimer(true);
	while(!openList.isEmpty()){
		HashableState sh = openList.poll();


		List<ActionProb> policyActions = p.policyDistribution(sh.s());
		for(ActionProb ap : policyActions){
			if(ap.pSelection > 0){
				List <TransitionProb> tps = model.transitions(sh.s(), ap.ga);
				nGenerated += tps.size();
				for(TransitionProb tp : tps){
					HashableState nsh = usingHashFactory.hashState(tp.eo.op);

					if (hashedStates.add(nsh) && !tp.eo.terminated) {
						openList.offer(nsh);
					}
				}
			}
		}

		if(timer.peekAtTime() > 1){
			timer.stop();
			DPrint.cl(debugID, "Num generated: " + (nGenerated) + " Unique: " + (hashedStates.size()) +
					" time: " + timer.getTime());
			timer.start();
		}
	}

	timer.stop();

	DPrint.cl(debugID, "Num generated: " + nGenerated + "; num unique: " + hashedStates.size());

	return hashedStates;
}
 
Example 6
Source File: TestHashing.java    From burlap with Apache License 2.0 4 votes vote down vote up
public Set <HashableState> getReachableHashedStates(State from, SADomain inDomain, HashableStateFactory usingHashFactory){
	
	Set<HashableState> hashedStates = new HashSet<HashableState>();
	HashableState shi = usingHashFactory.hashState(from);
	List <ActionType> actionTypes = inDomain.getActionTypes();
	
	LinkedList <HashableState> openList = new LinkedList<HashableState>();
	openList.offer(shi);
	hashedStates.add(shi);
	while(!openList.isEmpty()){
		HashableState sh = openList.poll();

		List<Action> gas = ActionUtils.allApplicableActionsForTypes(actionTypes, sh.s());
		for(Action ga : gas){
			List <TransitionProb> tps = ((FullModel)inDomain.getModel()).transitions(sh.s(), ga);
			for(TransitionProb tp : tps){
				HashableState nsh = usingHashFactory.hashState(tp.eo.op);
				
				for (HashableState hashedState : hashedStates) {
					boolean sameObject = (hashedState == nsh);
					boolean valueEquals = (hashedState.equals(nsh));
					boolean hashEquals = (hashedState.hashCode() == nsh.hashCode());
					if (sameObject || valueEquals) {
						assert(hashEquals); // Same state, hashes need to be equal
					}
					if (!hashEquals) {
						assert(!sameObject && !valueEquals);
					}
				}
				
				if(!hashedStates.contains(nsh)){
					openList.offer(nsh);
					hashedStates.add(nsh);
				}
			}
			
		}
		
	}
	
	return hashedStates;
}