ActionConditions Planner::findActions(Actions actions, Conditions preConditions){ ActionConditions result; for(auto& a: actions){ Conditions postA = a->getPreConditions(); if(satisfies(postA, preConditions)){ Conditions yetToSatisfy; std::set_difference(preConditions.begin(),preConditions.end(), postA.begin(), postA.end(), std::inserter(yetToSatisfy, yetToSatisfy.begin()) ); Conditions combined; Conditions preA = a->getPostConditions(); std::set_union(preA.begin(), preA.end(), yetToSatisfy.begin(), yetToSatisfy.end(), std::inserter(combined, combined.begin())); bool c = false; for(auto& m : mutexes) if(satisfies(combined, m)){ c=true;break; } if(!c) result.insert({combined,a}); } } return result; }
void PrintConditions(int source, int dest, const Conditions & conditions) { printf("------------------------------约束条件如下:\n"); printf("起点: 「%d」, 终点: 「%d」\n", source, dest); printf("必须经过的点:"); bool firstBlood = false; for(Conditions::const_iterator iter = conditions.begin(); iter != conditions.end(); ++iter) { if(firstBlood) printf("|"); printf("%d", *iter); firstBlood = true; } printf("\n"); }
void SK66( int node, int source, int dest, int iterCount, const Graph & graph, const EdgeInfoDict & edgeInfoDict, const Conditions & conditions, SK66_D_dict & ddict, SK66_F_dict & fdict, ShortestPathDict & pathDict) { // 当迭代次数为0时, 直接计算node->dest单源最短路径,存入结果字典里 if(iterCount == 0) { std::pair<int, int> pathToBeSolve(node, dest); if(!pathDict.count(pathToBeSolve)) Dijkstra(graph, edgeInfoDict, node, pathDict); if(!pathDict.count(pathToBeSolve)) { std::pair<std::pair<int, int>, int> key; key.first = pathToBeSolve; key.second = 0; Path path; path.first = 0xffffff; // 六个f, 足够表示无穷大了, 防止在后续加法中溢出 fdict[key] = path; } else { std::pair<std::pair<int, int>, int> key; key.first = pathToBeSolve; key.second = 0; ShortestPathDict::const_iterator PPath = pathDict.find(pathToBeSolve); Path path = PPath->second; fdict[key] = path; } } else { // 当迭代次数大于0的时候 std::pair<std::pair<int, int>, int> key; key.first = std::pair<int, int>(node, dest); key.second = iterCount; Path minCostPath; minCostPath.first = 0x7fffffff; for(Conditions::const_iterator iter = conditions.begin(); iter != conditions.end(); ++iter) { if(*iter == node) continue; // 计算D(v_i, v_l) std::pair<int, int> leftHalfPathToBeSolve(node, *iter); if(!pathDict.count(leftHalfPathToBeSolve)) { Dijkstra(graph, edgeInfoDict, node, pathDict); } if(!pathDict.count(leftHalfPathToBeSolve)) { Path leftHalfPath; leftHalfPath.first = 0xffffff; ddict[leftHalfPathToBeSolve] = leftHalfPath; } else { ShortestPathDict::const_iterator PPath = pathDict.find(leftHalfPathToBeSolve); Path leftHalfPath = PPath->second; ddict[leftHalfPathToBeSolve] = leftHalfPath; } // 计算F(v_l, t) // 筛选出最小值 std::pair<std::pair<int, int>, int> rightHalfPathToBeSolve; rightHalfPathToBeSolve.first.first = *iter; rightHalfPathToBeSolve.first.second = dest; rightHalfPathToBeSolve.second = iterCount-1; if(!fdict.count(rightHalfPathToBeSolve)) { SK66(*iter, source, dest, iterCount-1, graph, edgeInfoDict, conditions, ddict, fdict, pathDict); } if(ddict[leftHalfPathToBeSolve].first + fdict[rightHalfPathToBeSolve].first < minCostPath.first) { minCostPath.first = ddict[leftHalfPathToBeSolve].first + fdict[rightHalfPathToBeSolve].first; minCostPath.second.first.clear(); minCostPath.second.second.clear(); minCostPath.second.first.insert(minCostPath.second.first.end(), ddict[leftHalfPathToBeSolve].second.first.begin(), ddict[leftHalfPathToBeSolve].second.first.end()); minCostPath.second.first.insert(minCostPath.second.first.end(), fdict[rightHalfPathToBeSolve].second.first.begin(), fdict[rightHalfPathToBeSolve].second.first.end()); minCostPath.second.second.insert( minCostPath.second.second.end(), ddict[leftHalfPathToBeSolve].second.second.begin(), ddict[leftHalfPathToBeSolve].second.second.end()); minCostPath.second.second.insert( minCostPath.second.second.end(), fdict[rightHalfPathToBeSolve].second.second.begin(), fdict[rightHalfPathToBeSolve].second.second.end()); } } fdict[key] = minCostPath; } }