Skip to content

Commit 001de68

Browse files
committed
Switch to differential implementation
commit 45d4c06ef9efc4a6e0dd5e80d7c41c93b6b26b1e Merge: d86f2420 347b33a Author: Florian Fontan <[email protected]> Date: Sat Jun 20 23:36:01 2020 +0200 Merge branch 'dev' into differential commit d86f24202e4f09f6e387a70d66b2634e1643138f Author: Florian Fontan <[email protected]> Date: Thu May 14 21:04:14 2020 +0200 Implement differential branching scheme
1 parent 347b33a commit 001de68

11 files changed

+660
-772
lines changed

packingsolver/algorithms/a_star.hpp

+12-13
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ void AStar<Solution, BranchingScheme>::run()
5151

5252
// Initialize queue
5353
auto comp = branching_scheme_.compare(guide_id_);
54-
std::multiset<Node, decltype(comp)> q(comp);
55-
q.insert(Node(branching_scheme_));
54+
std::multiset<std::shared_ptr<const Node>, decltype(comp)> q(comp);
55+
q.insert(branching_scheme_.root());
5656

5757
while (!q.empty()) {
5858
node_number_++;
@@ -65,38 +65,37 @@ void AStar<Solution, BranchingScheme>::run()
6565
}
6666

6767
// Get node from the queue
68-
Node node_cur(*q.begin());
68+
auto node_cur = *q.begin();
6969
q.erase(q.begin());
70-
LOG_FOLD(info_, "node_cur" << std::endl << node_cur);
70+
LOG_FOLD(info_, "node_cur" << std::endl << *node_cur);
7171

7272
// Bound
73-
if (node_cur.bound(sol_best_)) {
73+
if (node_cur->bound(sol_best_)) {
7474
LOG(info_, " bound ×" << std::endl);
7575
continue;
7676
}
7777

78-
for (const Insertion& insertion: node_cur.children(info_)) {
78+
for (const Insertion& insertion: branching_scheme_.children(node_cur, info_)) {
7979
LOG(info_, insertion << std::endl);
80-
Node node_tmp(node_cur);
81-
node_tmp.apply_insertion(insertion, info_);
80+
auto child = branching_scheme_.child(node_cur, insertion);
8281
//LOG_FOLD(info_, "node_tmp" << std::endl << node_tmp);
8382

8483
// Bound
85-
if (node_tmp.bound(sol_best_)) {
84+
if (child->bound(sol_best_)) {
8685
LOG(info_, " bound ×" << std::endl);
8786
continue;
8887
}
8988

9089
// Update best solution
91-
if (sol_best_ < node_tmp) {
90+
if (sol_best_ < *child) {
9291
std::stringstream ss;
9392
ss << "A* (thread " << thread_id_ << ")";
94-
sol_best_.update(node_tmp.convert(sol_best_), ss, info_);
93+
sol_best_.update(child->convert(sol_best_), ss, info_);
9594
}
9695

9796
// Add child to the queue
98-
if (!node_tmp.full())
99-
q.insert(node_tmp);
97+
if (!child->full())
98+
q.insert(child);
10099
}
101100

102101
LOG_FOLD_END(info_, "");

packingsolver/algorithms/depth_first_search.hpp

+16-17
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,20 @@ class DepthFirstSearch
3333
GuideId guide_id_ = 0;
3434
Info info_ = Info();
3535

36-
void rec(const typename BranchingScheme::Node& node_cur);
36+
void rec(const std::shared_ptr<const typename BranchingScheme::Node>& node_cur);
3737

3838
};
3939

4040
/************************** Template implementation ***************************/
4141

4242
template <typename Solution, typename BranchingScheme>
43-
void DepthFirstSearch<Solution, BranchingScheme>::rec(const typename BranchingScheme::Node& node_cur)
43+
void DepthFirstSearch<Solution, BranchingScheme>::rec(const std::shared_ptr<const typename BranchingScheme::Node>& node_cur)
4444
{
4545
typedef typename BranchingScheme::Node Node;
4646
typedef typename BranchingScheme::Insertion Insertion;
4747

4848
LOG_FOLD_START(info_, "rec" << std::endl);
49-
LOG_FOLD(info_, "node_cur" << std::endl << node_cur);
49+
LOG_FOLD(info_, "node_cur" << std::endl << *node_cur);
5050

5151
// Check time
5252
if (!info_.check_time()) {
@@ -55,40 +55,39 @@ void DepthFirstSearch<Solution, BranchingScheme>::rec(const typename BranchingSc
5555
}
5656

5757
// Bound
58-
if (node_cur.bound(sol_best_)) {
58+
if (node_cur->bound(sol_best_)) {
5959
LOG(info_, " bound ×" << std::endl);
6060
return;
6161
}
6262

63-
std::vector<Node> children;
64-
for (const Insertion& insertion: node_cur.children(info_)) {
63+
std::vector<std::shared_ptr<const Node>> children;
64+
for (const Insertion& insertion: branching_scheme_.children(node_cur, info_)) {
6565
LOG(info_, insertion << std::endl);
66-
Node node_tmp(node_cur);
67-
node_tmp.apply_insertion(insertion, info_);
68-
LOG_FOLD(info_, "node_tmp" << std::endl << node_tmp);
66+
auto child = branching_scheme_.child(node_cur, insertion);
67+
LOG_FOLD(info_, "child" << std::endl << *child);
6968

7069
// Bound
71-
if (node_tmp.bound(sol_best_)) {
70+
if (child->bound(sol_best_)) {
7271
LOG(info_, " bound ×" << std::endl);
7372
continue;
7473
}
7574

7675
// Update best solution
77-
if (sol_best_ < node_tmp) {
76+
if (sol_best_ < *child) {
7877
std::stringstream ss;
7978
ss << "A* (thread " << thread_id_ << ")";
80-
sol_best_.update(node_tmp.convert(sol_best_), ss, info_);
79+
sol_best_.update(child->convert(sol_best_), ss, info_);
8180
}
8281

8382
// Add child to the queue
84-
if (!node_tmp.full())
85-
children.push_back(node_tmp);
83+
if (!child->full())
84+
children.push_back(child);
8685
}
8786

8887
auto comp = branching_scheme_.compare(guide_id_);
8988
sort(children.begin(), children.end(), comp);
9089

91-
for (const Node& child: children)
90+
for (const auto& child: children)
9291
rec(child);
9392

9493
LOG_FOLD_END(info_, "");
@@ -99,8 +98,8 @@ void DepthFirstSearch<Solution, BranchingScheme>::run()
9998
{
10099
typedef typename BranchingScheme::Node Node;
101100

102-
Node node(branching_scheme_);
103-
rec(node);
101+
std::shared_ptr<const Node> root = branching_scheme_.root();
102+
rec(root);
104103
}
105104

106105
}

0 commit comments

Comments
 (0)