-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
stage_rca.cpp
210 lines (171 loc) · 6.75 KB
/
stage_rca.cpp
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
Copyright (C) 2009 - 2016 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.
*/
/**
* Candidate actions evaluator
* @file
*/
#include "stage_rca.hpp"
#include "ai/manager.hpp"
#include "ai/composite/ai.hpp"
#include "ai/composite/engine.hpp"
#include "ai/composite/property_handler.hpp"
#include "ai/composite/rca.hpp"
#include "ai/gamestate_observer.hpp"
#include "log.hpp"
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
namespace ai {
namespace testing_ai_default {
static lg::log_domain log_ai_testing_rca_default("ai/stage/rca");
#define DBG_AI_TESTING_RCA_DEFAULT LOG_STREAM(debug, log_ai_testing_rca_default)
#define LOG_AI_TESTING_RCA_DEFAULT LOG_STREAM(info, log_ai_testing_rca_default)
#define ERR_AI_TESTING_RCA_DEFAULT LOG_STREAM(err, log_ai_testing_rca_default)
candidate_action_evaluation_loop::candidate_action_evaluation_loop( ai_context &context, const config &cfg)
: stage(context,cfg)
, candidate_actions_()
, cfg_(cfg)
{
}
void candidate_action_evaluation_loop::on_create()
{
//init the candidate actions
BOOST_FOREACH(const config &cfg_element, cfg_.child_range("candidate_action")){
engine::parse_candidate_action_from_config(*this,cfg_element,back_inserter(candidate_actions_));
}
boost::function2<void, std::vector<candidate_action_ptr>&, const config&> factory_candidate_actions =
boost::bind(&testing_ai_default::candidate_action_evaluation_loop::create_candidate_action,*this,_1,_2);
register_vector_property(property_handlers(),"candidate_action",candidate_actions_, factory_candidate_actions);
}
void candidate_action_evaluation_loop::create_candidate_action(std::vector<candidate_action_ptr> &candidate_actions, const config &cfg)
{
engine::parse_candidate_action_from_config(*this,cfg,std::back_inserter(candidate_actions));
}
config candidate_action_evaluation_loop::to_config() const
{
config cfg = stage::to_config();
BOOST_FOREACH(candidate_action_ptr ca, candidate_actions_){
cfg.add_child("candidate_action",ca->to_config());
}
return cfg;
}
class desc_sorter_of_candidate_actions {
public:
bool operator()(const candidate_action_ptr &a, const candidate_action_ptr &b)
{
return a->get_max_score() > b->get_max_score();
}
};
bool candidate_action_evaluation_loop::do_play_stage()
{
LOG_AI_TESTING_RCA_DEFAULT << "Starting candidate action evaluation loop for side "<< get_side() << std::endl;
BOOST_FOREACH(candidate_action_ptr ca, candidate_actions_){
ca->enable();
}
//sort candidate actions by max_score DESC
std::sort(candidate_actions_.begin(),candidate_actions_.end(),desc_sorter_of_candidate_actions());
bool executed = false;
bool gamestate_changed = false;
do {
executed = false;
double best_score = candidate_action::BAD_SCORE;
candidate_action_ptr best_ptr;
//Evaluation
BOOST_FOREACH(candidate_action_ptr ca_ptr, candidate_actions_){
if (!ca_ptr->is_enabled()){
DBG_AI_TESTING_RCA_DEFAULT << "Skipping disabled candidate action: "<< *ca_ptr << std::endl;
continue;
}
if (ca_ptr->get_max_score()<=best_score) {
DBG_AI_TESTING_RCA_DEFAULT << "Ending candidate action evaluation loop because current score "<<best_score<<" is greater than the upper bound of score for remaining candidate actions "<< ca_ptr->get_max_score()<< std::endl;
break;
}
DBG_AI_TESTING_RCA_DEFAULT << "Evaluating candidate action: "<< *ca_ptr << std::endl;
double score = ca_ptr->evaluate();
DBG_AI_TESTING_RCA_DEFAULT << "Evaluated candidate action to score "<< score << " : " << *ca_ptr << std::endl;
if (score>best_score) {
best_score = score;
best_ptr = ca_ptr;
}
}
//Execution
if (best_score>candidate_action::BAD_SCORE) {
DBG_AI_TESTING_RCA_DEFAULT << "Executing best candidate action: "<< *best_ptr << std::endl;
gamestate_observer gs_o;
best_ptr->execute();
executed = true;
if (!gs_o.is_gamestate_changed()) {
//this means that this CA has lied to us in evaluate()
//we punish it by disabling it
DBG_AI_TESTING_RCA_DEFAULT << "Disabling candidate action because it failed to change the game state: "<< *best_ptr << std::endl;
best_ptr->disable();
//since we don't re-enable at this play_stage, if we disable this CA, other may get the chance to go.
} else {
gamestate_changed = true;
}
} else {
LOG_AI_TESTING_RCA_DEFAULT << "Ending candidate action evaluation loop due to best score "<< best_score<<"<="<< candidate_action::BAD_SCORE<<std::endl;
}
} while (executed);
LOG_AI_TESTING_RCA_DEFAULT << "Ended candidate action evaluation loop for side "<< get_side() << std::endl;
remove_completed_cas();
return gamestate_changed;
}
void candidate_action_evaluation_loop::remove_completed_cas()
{
std::vector<size_t> tbr; // indexes of elements to be removed
for (size_t i = 0; i != candidate_actions_.size(); ++i)
{
if (candidate_actions_[i]->to_be_removed())
{
tbr.push_back(i); // so we fill the array with the indexes
}
}
for (size_t i = 0; i != tbr.size(); ++i)
{
// we should go downwards, so that index shifts don't affect us
size_t index = tbr.size() - i - 1; // downcounting for is not possible using unsigned counters, so we hack around
std::string path = "stage[" + this->get_id() + "].candidate_action[" + candidate_actions_[tbr[index]]->get_name() + "]";
config cfg = config();
cfg["path"] = path;
cfg["action"] = "delete";
ai::manager::modify_active_ai_for_side(this->get_side(), cfg); // we remove the CA
}
// @note: this code might be more convenient, but is obviously faulty and incomplete, because of iterator invalidation rules
// If you see a way to complete it, please contact me(Nephro).
// for (std::vector<candidate_action_ptr>::iterator it = candidate_actions_.begin(); it != candidate_actions_.end(); )
// {
// if ((*it)->to_be_removed())
// {
// // code to remove a CA
// std::string path = "stage[" + this->get_id() + "].candidate_action[" + (*it)->get_name() + "]";
//
// config cfg = config();
// cfg["path"] = path;
// cfg["action"] = "delete";
//
// ai::manager::modify_active_ai_for_side(this->get_side(), cfg);
// }
// else
// {
// ++it; // @note: should I modify this to a while loop?
// }
// }
}
rca_context& candidate_action_evaluation_loop::get_rca_context()
{
return *this;
}
candidate_action_evaluation_loop::~candidate_action_evaluation_loop()
{
}
} // end of namespace testing_ai_default
} // end of namespace ai