void Solver::init(char* map) { solution = 0; this->noExpandedNodes = 0; this->gameMap = new Map(map); //cout << gameMap.width() << " " << gameMap.height() << endl; Coordinate normalizedStartPos = gameMap->calcNormalizedPosition(); State::initZobristHash(gameMap->width(), gameMap->height()); State initialState = State(normalizedStartPos, gameMap->getBoxes(), gameMap->getStart()); parentStates.insert(psMap(initialState.getHash(),parentState(0,stateMove(initialState.getMoveLoc(),initialState.getMoveType())))); queue = new BucketQueue(1); queue->push(intStatePair(heuristic(initialState, gameMap), initialState)); reverseQueue = new BucketQueue(1); vector<State> goalStates = gameMap->getAllEndStates(); for(size_t i=0; i<goalStates.size(); i++) { State goalState = goalStates[i]; reverseParentStates.insert(psMap(goalState.getHash(), parentState(0,stateMove(goalState.getMoveLoc(), goalState.getMoveType())))); reverseQueue->push(intStatePair(reverseHeuristic(goalState, gameMap), goalState)); possibleEndStates.insert(pair<U64,State>(goalState.getHash(), goalState)); } }
/*! Sets this history state's default state to be the given \a state. \a state must be a sibling of this history state. */ void QtHistoryState::setDefaultState(QtAbstractState *state) { Q_D(QtHistoryState); if (state && state->parentState() != parentState()) { qWarning("QtHistoryState::setDefaultState: state %p does not belong " "to this history state's group (%p)", state, parentState()); return; } d->defaultState = state; }
/*! Sets this history state's default state to be the given \a state. \a state must be a sibling of this history state. Note that this function does not set \a state as the initial state of its parent. */ void QHistoryState::setDefaultState(QAbstractState *state) { Q_D(QHistoryState); if (state && state->parentState() != parentState()) { qWarning("QHistoryState::setDefaultState: state %p does not belong " "to this history state's group (%p)", state, parentState()); return; } if (d->defaultState != state) { d->defaultState = state; emit defaultStateChanged(QHistoryState::QPrivateSignal()); } }
//----------------------------------------------------------------------------- void ExState::onExit( QEvent* e ) { // Print out the state we are exiting and it's parents #ifdef _EX_FSM_PRINT_INFO_ QString state = name(); ExState* parent = qobject_cast<ExState*>( parentState() ); while ( parent != 0 ) { state = parent->name() + "->" + state; parent = qobject_cast<ExState*>( parent->parentState() ); } LOG_D("ExState") << prefix() << " <<< Exiting state: '" << state << "'"; #endif QState::onExit(e); Q_FOREACH(ExEvent* const & savedEvent, static_cast<ExStatePrivate*>(m_pImpl)->m_savedEvents) { #ifdef _EX_FSM_PRINT_INFO_ LOG_D("ExState") << prefix() << " - " << name() << ": Post saved event: '" << savedEvent->name() << "'"; #endif static_cast<ExStateMachine*>(machine())->putEvent(savedEvent); } static_cast<ExStatePrivate*>(m_pImpl)->m_savedEvents.clear(); }
bool StateMachineDebugInterface::isDescendantOf(State ascendant, State state) const { if (state == rootState()) return false; State parent = parentState(state); if (parent == ascendant) return true; return isDescendantOf(ascendant, parent); }
void Solver::solve() { bool win = false; parentState finalMove; while (!win && this->queue->size() > 0) { toPushQueue.clear(); toPushParents.clear(); int chunksLeft = this->chunksize; while (chunksLeft-- > 0 && this->queue->size() > 0) { State state = (this->queue->pop()).second; vector<State> newStates = this->gameMap->getSuccessorStates(state); for (size_t j = 0; j < newStates.size(); j++) { State stateChild = newStates[j]; toPushParents.push_back(psMap(stateChild.getHash(), parentState(state.getHash(), stateMove(stateChild.getMoveLoc(), stateChild.getMoveType())))); int heur = this->heuristic(stateChild, this->gameMap); pair<int, State> pa(heur, stateChild); toPushQueue.push_back(pa); } } for (size_t i = 0; i < toPushParents.size(); i++) { // TODO trying a bloom filter here could be a good idea, if profiling indicates a hotspot if (this->parentStates.insert(toPushParents[i]).second) { this->queue->push(toPushQueue[i]); // TODO trying a bloom filter here could be a good idea, if profiling indicates a hotspot if(this->reverseParentStates.count(toPushParents[i].first)) { // TODO when is this used this->isDone = true; win = true; U64 moveId = toPushParents[i].first; while (true) { parentState reverseParent = this->reverseParentStates[moveId]; parentState parent = parentState(moveId, reverseParent.second); U64 nextMoveId = reverseParent.first; this->parentStates.insert(psMap(nextMoveId, parent)); moveId = nextMoveId; if(moveId == 0) { finalMove = parent; break; } } } } } // TODO convert to local variable??? toPushQueue.clear(); toPushParents.clear(); chunksLeft = this->reverseChunksize; while (chunksLeft-- > 0 && this->reverseQueue->size() > 0) { State state = (this->reverseQueue->pop()).second; vector<State> newStates = this->gameMap->getPredecessorStates(state); for (size_t j = 0; j < newStates.size(); j++) { State stateChild = newStates[j]; toPushParents.push_back(psMap(stateChild.getHash(), parentState(state.getHash(), stateMove(stateChild.getMoveLoc(), stateChild.getMoveType())))); int heur = this->heuristic(stateChild, this->gameMap); pair<int, State> pa(heur, stateChild); toPushQueue.push_back(pa); } } for (size_t i = 0; i < toPushParents.size(); i++) { // TODO trying a bloom filter here could be a good idea, if profiling indicates a hotspot // break; if (this->reverseParentStates.insert(toPushParents[i]).second) { this->reverseQueue->push(toPushQueue[i]); } } // TODO reverse insert and check (do not check against forward) } if (win) { string history = this->gameMap->backtrack(finalMove, &(this->parentStates)); this->solution = new char[history.size()+5]; strcpy(this->solution, history.c_str()); cerr << this->parentStates.size() << " + " << this->reverseParentStates.size() << " states visited" << endl; } }