void gen(int kk, mat &a, vec &b) { const int m = kk+2; b.assign(m, 1<<kk); a.assign(m,vec(m,0)); a[0][0] = a[0][1] = 1; for (int i = 1; i < m; i++) { int kkk = kk-i+1; for (int j = 0; j <= kkk; j++) a[i][j+1] = c[kkk][j]; } }
void assign(indtype Nitem, indtype cap, indtype minCost_1, indtype *w, valtype *v) { this->Nitem = Nitem; cap = std::accumulate(w, w + Nitem, 0) - cap; this->cap = cap; INT nrow = cap + 1 - minCost_1, ncol = Nitem + 1; content.assign(nrow * ncol + ncol, -std::numeric_limits<valtype>::max()); dp01kp<valtype, indtype, false>::assign(minCost_1, w, (valtype**)(&content[0]), v, 0); valtype *val = &content[ncol]; for(INT i = 0; i < ncol; ++i) { dp01kp<valtype, indtype, false>::value[i] = val + nrow * i; } }
valtype gapBabDp(vec<signed char> ¤tSolution, vec<signed char> &Bcontainer, indtype Nagent, indtype Ntask, WV<valtype, indtype> **info, indtype *residualBudget, int maxCore, std::time_t timer, double tlimit, int &Nnode, int &Nkp) { vec<signed char*> Bv(Ntask); for(indtype j = 0; j < Ntask; ++j) { INT tmp = j * (INT(Nagent) + 1); Bv[j] = &Bcontainer[0] + tmp; Bv[j][Nagent] = 0; } signed char **B = &Bv[0]; vec<indtype> overloadedAgent(Nagent); vec<vec<indtype> > overloadedAgentTask(Nagent, vec<indtype>(Ntask)); vec<vec<indtype> > overloadedAgentWeight(Nagent, vec<indtype>(Ntask)); vec<vec<valtype> > overloadedAgentPenalty(Nagent, vec<valtype>(Ntask)); // will be used as values in knapsacking vec<vec<indtype> > nextAgent(Nagent, vec<indtype>(Ntask)); vec<vec<indtype> > reassign(Nagent, vec<indtype>(Ntask)); vec<vec<indtype> > stay(Nagent, vec<indtype>(Ntask)); vec<indtype> budgetExceedance(Nagent); vec<stackEle<valtype, indtype> > T(INT(Nagent) * Ntask); T.resize(0); currentSolution.resize(INT(Nagent) * Ntask); valtype currentSolutionRevenue = -std::numeric_limits<valtype>::max(); // Auxiliary containers. maxCore = std::min<int> (maxCore, Nagent); vec<indtype> agentCosts(Nagent); // bool postKnapsack = false; while(true) { std::time_t now; std::time(&now); if(std::difftime(now, timer) > tlimit) break; while(true) // Repeat backtracking until knapsacking becomes necessary. { valtype revenueUB = 0; bool needNoBacktrack = findOverloadedAgentsPenaltyWeightNextAgent( revenueUB, overloadedAgent, info, B, Nagent, Ntask, residualBudget, &budgetExceedance[0], &agentCosts[0], overloadedAgentTask, overloadedAgentWeight, // of size Nagent overloadedAgentPenalty, // will be used as values in knapsacking nextAgent, T); // Rcout << "needNoBacktrack = " << needNoBacktrack << "\n"; if(needNoBacktrack and revenueUB > currentSolutionRevenue) { if(overloadedAgent.size() > 0) break; currentSolution.assign(Bcontainer.begin(), Bcontainer.end()); currentSolutionRevenue = revenueUB; } /* Rcout << "revenueUB = " << revenueUB << "\n"; Rcout << "before backtrack, T.size() = " << T.size() << "\n"; for(indtype i = 1, iend = T.size(); i < iend; ++i) { Rcout << T[i].agent << ", " << T[i].task << ", " << int(B[T[i].task][T[i].agent]) << ", "; } Rcout << "\n"; for(indtype i = 0; i < Nagent; ++i) { for(indtype j = 0; j < Ntask; ++j) { if(B[j][i] >= 0) Rcout << " " << int(B[j][i]) << ", "; else Rcout << int(B[j][i]) << ", "; } Rcout << "\n"; } */ bool bt = backtrack(T, B, Nagent, info, residualBudget); /* Rcout << "after backtrack, T.size() = " << T.size() << "\n"; for(indtype i = 1, iend = T.size(); i < iend; ++i) { Rcout << T[i].agent << ", " << T[i].task << ", " << int(B[T[i].task][T[i].agent]) << ", "; } Rcout << "\n"; for(indtype i = 0; i < Nagent; ++i) { for(indtype j = 0; j < Ntask; ++j) { if(B[j][i] >= 0) Rcout << " " << int(B[j][i]) << ", "; else Rcout << int(B[j][i]) << ", "; } Rcout << "\n"; } */ if(!bt) return currentSolutionRevenue; } /* { Rcout << "After initialization, B = \n"; for(indtype i = 0; i < Nagent; ++i) { for(indtype j = 0; j < Ntask; ++j) { if(B[j][i] >= 0) Rcout << " " << int(B[j][i]) << ", "; else Rcout << int(B[j][i]) << ", "; } Rcout << "\n"; } Rcout << "Overloaded agent = \n"; for(indtype i = 0, iend = overloadedAgent.size(); i < iend; ++i) { Rcout << overloadedAgent[i] << ", "; } Rcout << "\n"; Rcout << "Budget exceedance = \n"; for(indtype i = 0, iend = overloadedAgent.size(); i < iend; ++i) { Rcout << budgetExceedance[overloadedAgent[i]] << ", "; } Rcout << "\n"; Rcout << "overloadedAgentTask = \n"; for(indtype i = 0, iend = overloadedAgent.size(); i < iend; ++i) { indtype a = overloadedAgent[i]; for(indtype j = 0, jend = overloadedAgentTask[a].size(); j < jend; ++j) { Rcout << overloadedAgentTask[a][j] << ", "; } Rcout << "\n"; } Rcout << "overloadedAgentWeight = \n"; for(indtype i = 0, iend = overloadedAgent.size(); i < iend; ++i) { indtype a = overloadedAgent[i]; for(indtype j = 0, jend = overloadedAgentWeight[a].size(); j < jend; ++j) { Rcout << overloadedAgentWeight[a][j] << ", "; } Rcout << "\n"; } Rcout << "next agent = \n"; for(indtype i = 0, iend = overloadedAgent.size(); i < iend; ++i) { indtype a = overloadedAgent[i]; for(indtype j = 0, jend = nextAgent[a].size(); j < jend; ++j) { Rcout << nextAgent[a][j] << ", "; } Rcout << "\n"; } Rcout << "overloadedAgentPenalty = \n"; for(indtype i = 0, iend = overloadedAgent.size(); i < iend; ++i) { indtype a = overloadedAgent[i]; for(indtype j = 0, jend = overloadedAgentPenalty[a].size(); j < jend; ++j) { Rcout << overloadedAgentPenalty[a][j] << ", "; } Rcout << "\n"; } } */ valtype totalPenalty = 0; ++Nnode; Nkp += overloadedAgent.size(); specialBiKpDPpara<valtype, indtype> ( totalPenalty, overloadedAgent, overloadedAgentWeight, overloadedAgentPenalty, stay, reassign, budgetExceedance, std::min<int> (maxCore, overloadedAgent.size())); // Set variables pointed by reassigned to -1. updateBafterKnapsacking<indtype> (overloadedAgent, B, nextAgent, overloadedAgentTask, reassign); /* { Rcout << "After KP, reassign = \n"; for(indtype i = 0, iend = overloadedAgent.size(); i < iend; ++i) { for(indtype j = 0, jend = reassign[i].size(); j < jend; ++j) { Rcout << reassign[i][j] << ", "; } Rcout << "\n"; } Rcout << "After KP, stay = \n"; for(indtype i = 0, iend = overloadedAgent.size(); i < iend; ++i) { for(indtype j = 0, jend = stay[i].size(); j < jend; ++j) { Rcout << stay[i][j] << ", "; } Rcout << "\n"; } Rcout << "After KP, B = \n"; for(indtype i = 0; i < Nagent; ++i) { for(indtype j = 0; j < Ntask; ++j) { if(B[j][i] >= 0) Rcout << " " << int(B[j][i]) << ", "; else Rcout << int(B[j][i]) << ", "; } Rcout << "\n"; } } */ valtype totalReve = 0; bool thereis = thereIsOverlodedAgent( info, B, Nagent, Ntask, residualBudget, &agentCosts[0], totalReve); // Rcout << "Knapsack solution, totalReve = " << totalReve << "\n"; // if(thereis) Rcout << "There is overloaded agent\n"; // else Rcout << "There is no overloaded agent\n"; if(!thereis) { if(totalReve > currentSolutionRevenue) { currentSolutionRevenue = totalReve; currentSolution.assign(Bcontainer.begin(), Bcontainer.end()); } bool bt = backtrack(T, B, Nagent, info, residualBudget); if(!bt) break; } else { pushAllBranchingVariableIntoStack<valtype, indtype, greedyBranch> ( T, B, Nagent, overloadedAgent, stay, overloadedAgentTask, overloadedAgentWeight, overloadedAgentPenalty, residualBudget, info); } // Rcout << "After KP locking, B = \n"; // for(indtype i = 0; i < Nagent; ++i) // { // for(indtype j = 0; j < Ntask; ++j) // { // if(B[j][i] >= 0) Rcout << " " << int(B[j][i]) << ", "; // else Rcout << int(B[j][i]) << ", "; // } // Rcout << "\n"; // } } return currentSolutionRevenue; }