Skip to content

Commit 24df4ab

Browse files
committed
feat: ids algorithm
1 parent c2a2681 commit 24df4ab

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

src/main_scene/main_scene.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "../search/bfs/bfs.hpp"
44
#include "../search/ucs/ucs.hpp"
55
#include "../search/astar/astar.hpp"
6+
#include "../search/ids/ids.hpp"
67

78
MainScene::MainScene() {}
89
MainScene::~MainScene() {}
@@ -49,6 +50,7 @@ void MainScene::_notification(int p_what) {
4950
algo_button->add_item("Greedy Best First Search");
5051
algo_button->add_item("Uniform Cost Search");
5152
algo_button->add_item("A* Search");
53+
algo_button->add_item("Iterative Deepening Search");
5254
algo_button->select(0);
5355
}
5456

@@ -139,7 +141,7 @@ void MainScene::_on_solve_button_pressed() {
139141
is_searching = false;
140142
break;
141143
}
142-
case ASTAR:
144+
case ASTAR: {
143145
solution = astar::search_astar(board, pieces);
144146
is_searching = true;
145147

@@ -150,6 +152,19 @@ void MainScene::_on_solve_button_pressed() {
150152
}
151153
is_searching = false;
152154
break;
155+
}
156+
case IDS: {
157+
solution = ids::search_ids(board, pieces);
158+
is_searching = true;
159+
160+
if (is_solved = solution.is_solved) {
161+
UtilityFunctions::print("IDS found a solution!");
162+
} else {
163+
UtilityFunctions::print("IDS could not find a solution.");
164+
}
165+
is_searching = false;
166+
break;
167+
}
153168
default:
154169
UtilityFunctions::printerr("Unknown algorithm selected");
155170
break;
@@ -330,6 +345,10 @@ void MainScene::_on_algo_button_selected(int index) {
330345
UtilityFunctions::print("A* selected");
331346
algo_type = ASTAR;
332347
break;
348+
case IDS:
349+
UtilityFunctions::print("IDS selected");
350+
algo_type = IDS;
351+
break;
333352
default:
334353
UtilityFunctions::printerr("Unknown algorithm selected");
335354
break;

src/search/ids/ids.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "ids.hpp"
2+
3+
// helper
4+
SearchNode dls(SearchNode current_node, const Board& initial_board, int limit, int& nodes_total, set<string>& visited) {
5+
nodes_total++;
6+
7+
if (Utils::is_exit(initial_board, current_node.pieces)) {
8+
return current_node;
9+
}
10+
11+
// jika batas kedalaman saat ini (current_node.val) telah mencapai limit DLS, berhenti
12+
if (current_node.val >= limit) {
13+
return SearchNode({}, initial_board, {}, -1);
14+
}
15+
16+
vector<SearchNode> next = Utils::generate_next(current_node, [](const Board&, const vector<Piece>&, const SearchNode& parent_node) {
17+
return parent_node.val + 1;
18+
});
19+
20+
for (const auto& successor_node : next) {
21+
string next_state = Utils::state_to_string(successor_node.pieces);
22+
23+
if (visited.find(next_state) == visited.end()) {
24+
visited.insert(next_state);
25+
26+
SearchNode result_node = dls(successor_node, initial_board, limit, nodes_total, visited);
27+
28+
visited.erase(next_state); // backtrack
29+
30+
if (result_node.val != -1) {
31+
return result_node;
32+
}
33+
}
34+
}
35+
return SearchNode({}, initial_board, {}, -1); // tidak ada solusi ditemukan
36+
}
37+
38+
Solution ids::search_ids(const Board& initial_board, const vector<Piece>& initial_pieces) {
39+
auto time_start = chrono::high_resolution_clock::now();
40+
Solution result;
41+
result.node = 0;
42+
43+
const int MAX_DEPTH_LIMIT = 100;
44+
45+
for (int depth_limit = 0; depth_limit <= MAX_DEPTH_LIMIT; ++depth_limit) {
46+
SearchNode initial_node(initial_pieces, initial_board, {}, 0);
47+
set<string> visited_dls;
48+
49+
visited_dls.insert(Utils::state_to_string(initial_node.pieces));
50+
51+
SearchNode found = dls(initial_node, initial_board, depth_limit, result.node, visited_dls);
52+
53+
if (found.val != -1) {
54+
result.is_solved = true;
55+
result.moves = found.path;
56+
auto time_end = chrono::high_resolution_clock::now();
57+
result.duration = chrono::duration<double, milli>(time_end - time_start);
58+
59+
return result;
60+
}
61+
}
62+
auto time_end = chrono::high_resolution_clock::now();
63+
result.duration = chrono::duration<double, milli>(time_end - time_start);
64+
result.is_solved = false;
65+
66+
return result;
67+
}

src/search/ids/ids.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef IDS_HPP
2+
#define IDS_HPP
3+
4+
#include "../../main_scene/main_scene.hpp"
5+
#include "../../utils/utils.hpp"
6+
7+
class ids {
8+
public:
9+
static Solution search_ids(const Board& initial_board, const std::vector<Piece>& initial_pieces);
10+
};
11+
12+
#endif

src/utils/utils.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ enum AlgoType {
1818
BFS,
1919
UCS,
2020
ASTAR,
21+
IDS,
2122
};
2223

2324
struct Coordinates {

0 commit comments

Comments
 (0)