/** * Creates a new game according to the settings the user made. * * @param factory Player factory to use to create the players. * @return New game. */ QSharedPointer< ::Game::Game> NewGame::createGame(::Game::Players::Factory& factory) const { QSharedPointer< ::GameLogic::FourInALine::Game> fourInALine; QSharedPointer< ::Game::Game> game; auto firstPlayer = this->createFirstPlayer(factory); auto secondPlayer = this->createSecondPlayer(factory); auto boardConfigurationWidget = this->gameSetupWidget->getBoardConfigurationWidget(); auto timeLimitConfigurationWidget = this->gameSetupWidget->getTimeLimitConfigurationWidget(); auto gameConfigurationWidget = this->gameSetupWidget->getGameConfigurationWidget(); unsigned int nRows = boardConfigurationWidget->getNumberOfRows(); unsigned int nColumns = boardConfigurationWidget->getNumberOfColumns(); unsigned int firstMove = gameConfigurationWidget->getFirstMove(); fourInALine.reset(new ::GameLogic::FourInALine::Game(nRows, nColumns, firstMove)); if (timeLimitConfigurationWidget->hasTimeLimit()) { fourInALine->setTimeLimit(timeLimitConfigurationWidget->getTimeLimit()); fourInALine->setTimeoutAction(timeLimitConfigurationWidget->getTimeoutAction()); } game = QSharedPointer< ::Game::Game>(new ::Game::Game(fourInALine, firstPlayer, secondPlayer)); game->setAllowHint(gameConfigurationWidget->isAllowHintEnabled() && gameConfigurationWidget->getAllowHint()); game->setAllowUndo(gameConfigurationWidget->isAllowUndoEnabled() && gameConfigurationWidget->getAllowUndo()); game->setSaveHighscore(gameConfigurationWidget->isSaveHighscoreEnabled() && gameConfigurationWidget->getSaveHighscore()); return game; }
void TabuSearch::run(string movetype, int maxiter) { initializeTabuLists(); initializeLeafsAndNeighborhood(); LSMove* bestMove = NULL; int nic = 1; int init_min = 0; int cardinality = (*tree).edges.size(); if (((*graph).vertices.size() - cardinality) < cardinality) { init_min = (*graph).vertices.size() - cardinality; } else { init_min = cardinality; } int init_length; if (init_min < ((int)(((double)(*graph).vertices.size()) / 5.0))) { init_length = init_min; } else { init_length = (int)(((double)(*graph).vertices.size()) / 5.0); } in_length = init_length; out_length = init_length; int max_length = (int)(((double)(*graph).vertices.size()) / 3.0); int increment = ((int)((max_length - in_length) / 4.0)) + 1; int max_unimpr_iters = increment; if (max_unimpr_iters < 100) { max_unimpr_iters = 200; } int iter = 1; while (iter <= maxiter) { if ((nic % max_unimpr_iters) == 0) { if (in_length + increment > max_length) { in_length = init_length; out_length = init_length; cutTabuLists(); } else { in_length = in_length + increment; out_length = out_length + increment; } } if (bestMove != NULL) { delete(bestMove); } if (movetype == "first_improvement") { bestMove = getFirstMove(tree->weight(),currentSol->weight()); } else { bestMove = getBestMove(tree->weight(),currentSol->weight()); } if (bestMove != NULL) { currentSol->addVertex((bestMove->in)->lVertex); currentSol->addEdge((bestMove->in)->lEdge); currentSol->remove((bestMove->out)->lEdge); currentSol->remove((bestMove->out)->lVertex); adaptLeafs(bestMove); adaptNeighborhood(bestMove); adaptTabuLists((bestMove->in)->getEdge(),(bestMove->out)->getEdge()); delete(bestMove->in); delete(bestMove->out); currentSol->setWeight(currentSol->weight() + (bestMove->weight_diff)); } if (currentSol->weight() < tree->weight()) { tree->copy(currentSol); nic = 1; in_length = init_length; out_length = init_length; cutTabuLists(); } else { nic = nic + 1; } iter = iter + 1; } if (bestMove != NULL) { delete(bestMove); } for (list<Leaf*>::iterator aL = leafs.begin(); aL != leafs.end(); aL++) { delete(*aL); } leafs.clear(); for (list<Leaf*>::iterator aL = neighborhood.begin(); aL != neighborhood.end(); aL++) { delete(*aL); } neighborhood.clear(); in_list_map.clear(); out_list_map.clear(); in_list.clear(); out_list.clear(); tree = NULL; }