-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
rca.hpp
192 lines (133 loc) · 4.11 KB
/
rca.hpp
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
Copyright (C) 2009 - 2018 by Yurii Chernyi <terraninfo@terraninfo.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* candidate action framework
*/
#pragma once
#include "ai/composite/component.hpp"
#include "ai/composite/contexts.hpp"
//============================================================================
namespace ai {
class candidate_action : public virtual rca_context_proxy, public component {
public:
//this is a score guaranteed to be <=0, thus candidate action with this score will not be selected for execution
static const double BAD_SCORE;
//this is a score guaranteed to be very high, higher than any 'normal' candidate action score
static const double HIGH_SCORE;
candidate_action( rca_context &context, const config &cfg );
/**
* Destructor
*/
virtual ~candidate_action();
/**
* Evaluate the candidate action, resetting the internal state of the action
* @return the score
* @retval >0 if the action is good
* @retval <=0 if the action is not good
*/
virtual double evaluate() = 0;
/**
* Execute the candidate action
*/
virtual void execute() = 0;
/**
* Is this candidate action enabled ?
*/
bool is_enabled() const;
/**
* Enable the candidate action
*/
void enable();
/**
* Disable the candidate action
*/
void disable();
/**
* Get the usual score of the candidate action without re-evaluation
*/
double get_score() const;
/**
* Get the upper bound of the score of the candidate action without re-evaluation
*/
double get_max_score() const;
/**
* Get the name of the candidate action (useful for debug purposes)
*/
virtual std::string get_name() const
{ return name_; }
/**
* Get the type of the candidate action (useful for debug purposes)
*/
const std::string& get_type() const;
virtual std::string get_id() const
{ return id_; }
virtual std::string get_engine() const
{ return engine_; }
int get_recursion_count() const;
/**
* serialize
*/
virtual config to_config() const;
virtual void set_to_be_removed();
virtual bool to_be_removed();
private:
recursion_counter recursion_counter_;
bool enabled_;
std::string engine_;
double score_;
double max_score_;
std::string id_;
std::string name_;
std::string type_;
bool to_be_removed_;
};
typedef std::shared_ptr<candidate_action> candidate_action_ptr;
class candidate_action_factory;
class candidate_action_factory{
bool is_duplicate(const std::string &name);
public:
typedef std::shared_ptr< candidate_action_factory > factory_ptr;
typedef std::map<std::string, factory_ptr> factory_map;
typedef std::pair<const std::string, factory_ptr> factory_map_pair;
static factory_map& get_list() {
static factory_map *candidate_action_factories;
if (candidate_action_factories==nullptr) {
candidate_action_factories = new factory_map;
}
return *candidate_action_factories;
}
virtual candidate_action_ptr get_new_instance( rca_context &context, const config &cfg ) = 0;
candidate_action_factory( const std::string &name )
{
if (is_duplicate(name)) {
return;
}
factory_ptr ptr_to_this(this);
get_list().emplace(name,ptr_to_this);
}
virtual ~candidate_action_factory() {}
};
template<class CANDIDATE_ACTION>
class register_candidate_action_factory : public candidate_action_factory {
public:
register_candidate_action_factory( const std::string &name )
: candidate_action_factory( name )
{
}
virtual candidate_action_ptr get_new_instance( rca_context &ai, const config &cfg ){
return std::make_shared<CANDIDATE_ACTION>(ai, cfg);
}
};
//============================================================================
std::ostream &operator<<(std::ostream &s, const ai::candidate_action& ca);
} //end of namespace ai