Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
state_search_framework_example.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
40# include <iostream>
41# include <string>
42
43# include <State_Search.H>
44
45using namespace Aleph;
46
47namespace
48{
49
50struct ArtificialTreeState
51{
52 size_t depth = 0;
53 size_t code = 1;
54};
55
56struct ArtificialDecisionTreeDomain
57{
58 struct Move
59 {
60 char label = 'L';
61 };
62
63 using State = ArtificialTreeState;
64
65 size_t leaf_depth = 2;
66
67 bool is_goal(const State &state) const
68 {
69 return state.depth == leaf_depth and (state.code == 5 or state.code == 6);
70 }
71
72 bool is_terminal(const State &state) const
73 {
74 return state.depth == leaf_depth;
75 }
76
77 void apply(State &state, const Move &move) const
78 {
79 state.code = state.code*2 + (move.label == 'R' ? 1u : 0u);
80 ++state.depth;
81 }
82
83 void undo(State &state, const Move &move) const
84 {
85 --state.depth;
86 state.code = (state.code - (move.label == 'R' ? 1u : 0u))/2;
87 }
88
89 template <typename Visitor>
90 bool for_each_successor(const State &state, Visitor visit) const
91 {
92 if (state.depth >= leaf_depth)
93 return true;
94
95 if (not visit(Move{'L'}))
96 return false;
97
98 return visit(Move{'R'});
99 }
100};
101
103{
104 std::string signature;
105 for (const auto &move : path)
106 signature.push_back(move.label);
107 return signature;
108}
109
110} // end namespace
111
112int main()
113{
114 ArtificialDecisionTreeDomain domain;
115 ExplorationPolicy policy;
116 policy.stop_at_first_solution = false;
117
119 Engine engine(domain, policy);
121
122 auto result = engine.search(ArtificialTreeState{}, collector);
123
124 std::cout << "Visited states: " << result.stats.visited_states << '\n';
125 std::cout << "Expanded states: " << result.stats.expanded_states << '\n';
126 std::cout << "Solutions found: " << result.stats.solutions_found << '\n';
127
128 for (auto it = collector.solutions().get_it(); it.has_curr(); it.next_ne())
129 std::cout << "Solution path: " << path_signature(it.get_curr().path) << '\n';
130
131 return 0;
132}
Umbrella header for the implicit state-space search framework.
Recursive depth-first backtracking over an implicit state space.
Collector that stores accepted solutions in an Aleph list.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
Exploration controls shared across engines.
bool stop_at_first_solution
Stop when the first goal is found.
static mt19937 engine