Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
adversarial_artificial_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 ArtificialGameState
51{
52 size_t depth = 0;
53 size_t code = 1;
54 int player = 1;
55};
56
57struct ArtificialGameMove
58{
59 size_t next_code = 1;
60 char label = 'A';
61};
62
63class ArtificialGameDomain
64{
65public:
66 using State = ArtificialGameState;
67 using Move = ArtificialGameMove;
68 using Score = int;
69
70 static bool is_terminal(const State &state)
71 {
72 return state.depth == 2;
73 }
74
75 static Score evaluate(const State &state)
76 {
77 return root_score(state.code)*state.player;
78 }
79
80 static void apply(State &state, const Move &move)
81 {
82 state.code = move.next_code;
83 state.player = -state.player;
84 ++state.depth;
85 }
86
87 static void undo(State &state, const Move&)
88 {
89 --state.depth;
90 state.player = -state.player;
91 state.code /= 2;
92 }
93
94 template <typename Visitor>
95 static bool for_each_successor(const State &state, Visitor visit)
96 {
97 if (state.depth >= 2)
98 return true;
99
100 switch (state.code)
101 {
102 case 1:
103 if (not visit(Move{2, 'A'}))
104 return false;
105 return visit(Move{3, 'B'});
106
107 case 2:
108 if (not visit(Move{4, 'a'}))
109 return false;
110 return visit(Move{5, 'b'});
111
112 case 3:
113 if (not visit(Move{6, 'a'}))
114 return false;
115 return visit(Move{7, 'b'});
116
117 default:
118 return true;
119 }
120 }
121
122private:
123 [[nodiscard]] static Score root_score(const size_t code) noexcept
124 {
125 switch (code)
126 {
127 case 2: return 6;
128 case 3: return 1;
129 case 4: return 3;
130 case 5: return 5;
131 case 6: return 2;
132 case 7: return 4;
133 default: return 0;
134 }
135 }
136};
137
138std::string signature(const SearchPath<ArtificialGameMove> &path)
139{
140 std::string out;
141 for (const auto &move : path)
142 out.push_back(move.label);
143 return out;
144}
145
146template <typename Result>
147void print_summary(const char *title, const Result &result)
148{
149 std::cout << title << '\n';
150 std::cout << " value: " << result.value << '\n';
151 std::cout << " pv: " << signature(result.principal_variation) << '\n';
152 std::cout << " visited: " << result.stats.visited_states << '\n';
153 std::cout << " cutoffs: " << result.stats.alpha_beta_cutoffs << '\n';
154}
155
156} // end namespace
157
158int main()
159{
160 const ArtificialGameState root;
161
162 auto negamax = negamax_search(ArtificialGameDomain{}, root);
163 auto alpha_beta = alpha_beta_search(ArtificialGameDomain{}, root);
164
165 print_summary("Negamax", negamax);
166 print_summary("Alpha-Beta", alpha_beta);
167
168 return 0;
169}
Umbrella header for the implicit state-space search framework.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4060
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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.
auto alpha_beta_search(Domain domain, typename Domain::State initial_state, ExplorationPolicy policy=Alpha_Beta< Domain >::default_policy(), SearchLimits limits={})
Convenience wrapper for one-shot Alpha-Beta search.
Definition Alpha_Beta.H:797
auto negamax_search(Domain domain, typename Domain::State initial_state, ExplorationPolicy policy=Negamax< Domain >::default_policy(), SearchLimits limits={})
Convenience wrapper for one-shot Negamax search.
Definition Negamax.H:1233