void apply(sol::Solution const & solution) { int numIter = 0; int lastBestIter = -1; sol::Solution bestSolution(solution); sol::Solution currentSolution(_localSearch->apply(solution)); if (isBetter(currentSolution, bestSolution)) { lastBestIter = 0; bestSolution = currentSolution; } do { currentSolution = _perturbation->apply(currentSolution); currentSolution = _localSearch->apply(currentSolution); _pool->addSolution(currentSolution); if (isBetter(currentSolution, bestSolution)) { lastBestIter = numIter; bestSolution = currentSolution; } numIter++; boost::this_thread::interruption_point(); } while ((numIter - lastBestIter) <= _maxNumNonImprovIter); }
TreeOfNodes& GpModel::worstTournament( int k ) { if (k <= 0) { k = getTournamentSelection(); } TreeOfNodes** tournament = new TreeOfNodes*[k]; for (int i = 0; i < k; ++i) { tournament[i] = &getTree(_rnd_tournament.nextInt(0, getPopulationSize())); } TreeOfNodes* result = NULL; double fitness = isLowerFitnessBetter() ? -1.0 / 0.0 : 1.0 / 0.0; for (int i = 0; i < k; ++i) { if (!isBetter(tournament[i]->getFitness(), fitness)) { fitness = tournament[i]->getFitness(); result = tournament[i]; } } for (int i = 0; i < k; ++i) { tournament[i] = NULL; } delete [] tournament; return *result; }
// returns true if the node was actually expanded (sibling evaluated) // false otherwise (if there are no siblings, or if the node is a PV or subTreeRoot) bool expandNode(Node **fullCurrentFrontier, float *currentNodeVals, int i, float curBest, Node *subTreeRoot, bool *ignored) { Node *thisNode = fullCurrentFrontier[i]; bool isMaxLevel = thisNode->isMaxNode; // no need to explore any siblings if the current node itself is subtree root! if (thisNode == subTreeRoot) return false; Node *parentNode = thisNode->parent; assert(parentNode->nodeType == CUT_NODE); Node *currentParent = parentNode; while (currentParent->nChildsExplored == currentParent->nChildren) { // break out early if we reached the subTreeRoot! if (currentParent == subTreeRoot) { //if (isBetter(currentParent, currentNodeVals[i])) { currentParent->best = thisNode; currentParent->nodeVal = currentNodeVals[i]; thisNode->nodeType = PV_NODE; } return false; } // already explored the last child, need to explore more nodes of GrandParent of Parent node? // parent of parent should be an ALL node, with everything explored already ? currentParent->nodeVal = currentNodeVals[i]; thisNode = currentParent; currentParent = currentParent->parent; if (currentParent->nodeType == ALL_NODE) { //if (isBetter(currentParent, currentNodeVals[i])) // when moving up, only cross an ALL node only when // i) either it's the last child // ii) there is no 'open' child towards the right of current node // - open CHILD are the nodes that have possibility of being better than the current node bool isBest = true; // bool onRight = false; // TODO: Check only nodes towards the right. No need to check all nodes of parent // ANKAN - BUG! TODO!!! just checking nodes towards right doesn't work. Figure out why? for (int n = 0; n < currentParent->nChildren; n++) { if ((¤tParent->children[n]) != thisNode) { //if (!onRight) // continue; for (int k = 0; k < currentParent->children[n].numChildrenAtFrontier; k++) { int frontierOffset = currentParent->children[n].frontierOffset + k; if ((!ignored[frontierOffset]) && isBetter(currentParent->isMaxNode, currentNodeVals[frontierOffset], currentNodeVals[i])) { isBest = false; } else { ignored[frontierOffset] = true; } } if (!isBest) { break; } } //else //{ // onRight = true; //} } if (isBest) { currentParent->best = thisNode; currentParent->nodeVal = currentNodeVals[i]; } else { // wait for other better nodes to be sorted, out // don't move up return false; } } if (currentParent->nodeType == PV_NODE) { // Ankan TODO: might need to propogate up only if it's actually better currentParent->best = thisNode; thisNode->nodeType = PV_NODE; // currentParent->nodeVal = currentNodeVals[i]; // need to propogate the value all the way to root! while (currentParent) { currentParent->nodeVal = currentNodeVals[i]; // stop when the chain of PV nodes breaks! if (currentParent->nodeType != PV_NODE) break; currentParent = currentParent->parent; } return false; } } assert(currentParent->nodeType != PV_NODE); { // here we have a parent node with unexplored children, explore one of them //assert(currentParent->nodeType == CUT_NODE); Node *sibling = &(currentParent->children[currentParent->nChildsExplored]); sibling->nodeType = ALL_NODE; currentParent->nChildsExplored++; float siblingVal = exploreSubTree(sibling, curBest); if (currentParent->isMaxNode && siblingVal > currentNodeVals[i]) { currentNodeVals[i] = siblingVal; currentParent->best = sibling; fullCurrentFrontier[i] = sibling; } if ((!currentParent->isMaxNode) && siblingVal < currentNodeVals[i]) { currentNodeVals[i] = siblingVal; currentParent->best = sibling; fullCurrentFrontier[i] = sibling; } } currentParent->nodeVal = currentNodeVals[i]; // propogate the value all the way up if the parent's value was coming from this node Node *cur = currentParent; while (true) { Node *par = cur->parent; if (par->best == cur) { par->nodeVal = cur->nodeVal; cur = par; } else { break; } } return true; }