-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBranchAndBound.cpp
More file actions
63 lines (54 loc) · 2.26 KB
/
BranchAndBound.cpp
File metadata and controls
63 lines (54 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "BranchAndBound.hpp"
#include <unordered_map>
struct BBCompare {
bool operator()(const std::shared_ptr<SearchNode>& a,
const std::shared_ptr<SearchNode>& b) {
return a->cost > b->cost; // Минимальная стоимость сначала
}
};
std::vector<Situation> branchAndBoundSearch(const Situation& start) {
std::priority_queue<std::shared_ptr<SearchNode>,
std::vector<std::shared_ptr<SearchNode>>,
BBCompare> pq;
std::unordered_map<Situation, int, Situation::Hash> best_costs;
std::vector<Situation> path;
static int global_best = INT_MAX;
auto startNode = std::make_shared<SearchNode>(start, nullptr, 0);
pq.push(startNode);
best_costs[start] = 0;
while (!pq.empty()) {
auto current = pq.top();
pq.pop();
// Отсечение ветвей, если текущий путь хуже известного
if (current->cost > global_best) continue;
if (current->state.isGoal()) {
if (current->cost < global_best) {
global_best = current->cost;
// Сохраняем новый лучший путь
path.clear();
auto node = current;
while (node) {
path.push_back(node->state);
node = node->parent;
}
std::reverse(path.begin(), path.end());
}
continue;
}
auto nextStates = current->state.generateNextStates();
for (const auto& nextState : nextStates) {
// Расчет новой стоимости
int moveCost = current->state.getMoveCost(nextState);
int newCost = current->cost + moveCost;
// Проверяем есть ли уже лучшее решение для этого состояния
auto it = best_costs.find(nextState);
if (it == best_costs.end() || newCost < it->second) {
best_costs[nextState] = newCost;
auto newNode = std::make_shared<SearchNode>(
nextState, current, newCost);
pq.push(newNode);
}
}
}
return path;
}