コード例 #1
0
ファイル: board.cpp プロジェクト: elektronisk/zen
void Board::setupBoard(QSize dim) {
	
	if (!dim.isValid() || dim.isNull()) {
		dim = QSize(2,2);
	}
	boardSize = dim;

	int totaltiles = boardSize.width()*boardSize.height();
	tileArray = new Tile*[totaltiles]; //Array of pointers to tiles
	
	tileLayout = new QGridLayout(this);
	tileLayout->setSpacing(0);
	setLayout(tileLayout);

	Tile *tempTile;
	for (int x = 0; x < boardSize.width(); x++) {
		for (int y = 0; y < boardSize.height(); y++) {
			tempTile= new Tile(0, this); // Allocate tiles to the pointers
			tileArray[y*boardSize.height() + x] = tempTile;
			
			connect(tempTile, SIGNAL(rotated()), this, SLOT(checkSolved()));
			tileLayout->addWidget(tempTile, y, x);
		}
	}
	
	// go through the shared edges and randomize their status.
	srand(time(NULL));
	for (int x = 0; x < (boardSize.width() - 1); x++) {
		for (int y = 0; (y < boardSize.height() - 1); y++) {
			int u = rand() % 2;
			tileArray[y*boardSize.height() + x]->setEdge(EDGE_DOWN, u);
			tileArray[(y+1)*boardSize.height()+x]->setEdge(EDGE_UP, u);
			u = rand() % 2;
			tileArray[y*boardSize.height() + x]->setEdge(EDGE_RIGHT, u);
			tileArray[y*boardSize.height() + x +1]->setEdge(EDGE_LEFT,u);
		}
	}
	
	// go through the tiles and randomize their rotation
	
	for (int x = 0; x < (boardSize.width()); x++) {
		for (int y = 0; (y < boardSize.height()); y++) {
			tempTile = tileArray[y*boardSize.height() + x];
			int r = rand() % 4;
			tempTile->rotaten(r);
			if (tempTile->getEdgecount() == 0) {
				tempTile->setEnabled(false); // disable tiles with no edges
			}
		}
	}
	checkSolved();
}
コード例 #2
0
ファイル: LRTDPSolver.cpp プロジェクト: sandysa/mdp-lib
void LRTDPSolver::trial(mlcore::State* s)
{
    mlcore::State* tmp = s;
    std::list<mlcore::State*> visited;
    while (!tmp->checkBits(mdplib::SOLVED)) {
        if (problem_->goal(tmp))
            break;

        visited.push_front(tmp);

        bellmanUpdate(problem_, tmp);

        if (tmp->deadEnd())
            break;

        tmp = randomSuccessor(problem_, tmp, tmp->bestAction());
    }

    while (!visited.empty()) {
        tmp = visited.front();
        visited.pop_front();
        if (!checkSolved(tmp))
            break;
    }
}
コード例 #3
0
ファイル: LRTDPSolver.cpp プロジェクト: luisenp/mdp-lib
void LRTDPSolver::trial(mlcore::State* s) {
    mlcore::State* tmp = s;
    std::list<mlcore::State*> visited;
    double accumulated_cost = 0.0;
    while (!tmp->checkBits(mdplib::SOLVED)) {
        if (problem_->goal(tmp) || accumulated_cost > mdplib::dead_end_cost)
            break;

        visited.push_front(tmp);

        bellmanUpdate(problem_, tmp);

        if (tmp->deadEnd())
            break;

        accumulated_cost += problem_->cost(tmp, tmp->bestAction());
        tmp = randomSuccessor(problem_, tmp, tmp->bestAction());
    }

    if (dont_label_)
        return;

    while (!visited.empty()) {
        tmp = visited.front();
        visited.pop_front();
        bool solved = checkSolved(tmp);
        if (!solved) break;
    }
}
コード例 #4
0
ファイル: SSiPPSolver.cpp プロジェクト: luisenp/mdp-lib
Action* SSiPPSolver::solveLabeled(State* s0)
{
    beginTime_ = std::chrono::high_resolution_clock::now();
    while (!s0->checkBits(mdplib::SOLVED_SSiPP)) {
        State* currentState = s0;
        list<State*> visited;
        while (!currentState->checkBits(mdplib::SOLVED_SSiPP)) {
            visited.push_front(currentState);
            if (problem_->goal(currentState))
                break;
            // Constructing short-sighted SSP
            StateSet reachableStates, tipStates;
            if (useTrajProbabilities_) {
                getReachableStatesTrajectoryProbs(
                    problem_, currentState, reachableStates, tipStates, rho_);
            } else {
                reachableStates.insert(currentState);
                getReachableStates(problem_, reachableStates, tipStates, t_);
            }
            WrapperProblem wrapper(problem_);
            wrapper.overrideStates(&reachableStates);
            wrapper.overrideGoals(&tipStates);

            // Solving the short-sighted SSP
            optimalSolver(&wrapper, currentState);
            if (currentState->deadEnd())
                break;
            // Simulate best action
            currentState = randomSuccessor(problem_,
                                           currentState,
                                           greedyAction(problem_,
                                                        currentState));

            wrapper.cleanup();
            // Return if it ran out of time
            if (ranOutOfTime()) {
                return greedyAction(problem_, s0);
            }
        }
        while (!visited.empty()) {
            currentState = visited.front();
            visited.pop_front();
            if (!checkSolved(currentState))
                break;
        }
    }
    return greedyAction(problem_, s0);
}