LSMove* TabuSearch::getBestMove(double bestValue, double currentValue) { LSMove* bm = NULL; double weight_diff = 0.0; bool started = false; Leaf* inl = NULL; Leaf* outl = NULL; for (list<Leaf*>::iterator anIn = neighborhood.begin(); anIn != neighborhood.end(); anIn++) { for (list<Leaf*>::iterator anOut = leafs.begin(); anOut != leafs.end(); anOut++) { if (((*anIn)->lEdge)->otherVertex((*anIn)->lVertex) != (*anOut)->lVertex) { bool istabu = isTabu((*anIn)->lEdge,(*anOut)->lEdge); double help = ((*anIn)->lEdge)->weight() - ((*anOut)->lEdge)->weight(); if ((istabu && ((currentValue + help) < bestValue)) || (!istabu)) { if (started == false) { started = true; inl = *anIn; outl = *anOut; weight_diff = help; } else { if (help < weight_diff) { inl = *anIn; outl = *anOut; weight_diff = help; } } } } } } if ((inl != NULL) && (outl != NULL)) { bm = new LSMove(inl,outl); } return bm; }
void Between2DeleteAndInsert(double T, int i, int j, bool pass){ int tiafter = 0; int tjafter = 0; int dtiafter = 0; int dtjafter = 0; double ticost = 0; double tjcost = 0; double bcost = INF; bool ok = false; int pi,pj; if(!isTabu(i,path[j].route) && !isTabu(j,path[i].route) && (routeDemand[path[i].route] + d[j] - d[i] <= Q) && (routeDemand[path[j].route] + d[i] - d[j] <= Q)){ double tcost; tcost = cost; pi = path[i].route; pj = path[j].route; CDelete(i,dtiafter); //delete i and in dtiafter save a place after which was i CDelete(j,dtjafter); tcost -= cost; //saved cost after deleting if(CTryInsert(i, pj, tiafter, ticost) && CTryInsert(j, pi, tjafter, tjcost)){ bcost = - tcost + ticost + tjcost; ok = true; } CInsert(i, pi, dtiafter); CInsert(j, pj, dtjafter); } else return; if(!ok) return; double score = -bcost; if(!(score - EPS > 0 || (T && Feasable(score, T)) || pass)) return; int null; CDelete(i,null); CDelete(j,null); CInsert(i, pj, tiafter); CInsert(j, pi, tjafter); USED[4]++; //cost -= score; }
/* Finds best solution candidate from neighbourhood. */ SolutionCandidate getBestSolution(std::vector<SolutionCandidate>& neighbourhood, std::vector<TabuItem>& tabuList) { SolutionCandidate bestCandidate = neighbourhood[0]; float lowestCost = getRoutesLength(bestCandidate.solution); int bestCandidateIndex = 0; for (int i=1; i<neighbourhood.size(); i++) { if (getRoutesLength(neighbourhood[i].solution) < lowestCost) { if (!isTabu(neighbourhood[i], tabuList)) { bestCandidateIndex = i; bestCandidate = neighbourhood[i]; lowestCost = getRoutesLength(bestCandidate.solution); } } } take(neighbourhood,bestCandidateIndex); return bestCandidate; }