12 #include <torchcraft/client.h> 13 #include <type_traits> 16 #include <autogradpp/autograd.h> 18 #include "buildtype.h" 21 #include "gamemechanics.h" 23 #include "unitsinfo.h" 29 static constexpr
NoValue noValue{};
30 template <
typename A,
typename B>
57 typename InvalidScore =
NoValue,
58 typename BestPossibleScore =
NoValue>
63 InvalidScore&& invalidScore = InvalidScore(),
64 BestPossibleScore&& bestPossibleScore = BestPossibleScore()) {
70 auto bestScore = score(*i);
74 for (; i != end; ++i) {
87 for (; i != end; ++i) {
109 typename InvalidScore =
NoValue,
110 typename BestPossibleScore =
NoValue>
114 InvalidScore&& invalidScore = InvalidScore(),
115 BestPossibleScore&& bestPossibleScore = BestPossibleScore()) {
119 std::forward<Score>(score),
120 std::forward<InvalidScore>(invalidScore),
121 std::forward<BestPossibleScore>(bestPossibleScore));
132 typename InvalidScore =
NoValue,
133 typename BestPossibleScore =
NoValue>
137 InvalidScore&& invalidScore = InvalidScore(),
138 BestPossibleScore&& bestPossibleScore = BestPossibleScore()) {
142 std::forward<Score>(score),
143 std::forward<InvalidScore>(invalidScore),
144 std::forward<BestPossibleScore>(bestPossibleScore));
145 if (i == range.end()) {
146 return typename std::remove_reference<decltype(*i)>::type{};
157 typename InvalidScore =
NoValue,
158 typename BestPossibleScore =
NoValue>
162 InvalidScore&& invalidScore = InvalidScore(),
163 BestPossibleScore&& bestPossibleScore = BestPossibleScore()) {
167 std::forward<Score>(score),
168 std::forward<InvalidScore>(invalidScore),
169 std::forward<BestPossibleScore>(bestPossibleScore));
170 if (i == range.end()) {
171 return (decltype(&*i))
nullptr;
177 return (buildType ? buildType->
name :
"null");
180 template <
typename Units>
183 if (units.size() != 0) {
184 for (
Unit const* unit : units) {
189 VLOG(2) <<
"Center of no units is (0, 0)";
195 template <
typename InputIterator>
200 VLOG(2) <<
"Center of no units is (0, 0)";
203 for (; start != end; start++, size++) {
211 return distance(unit->
x, unit->
y, x, y) <= radius;
214 template <
typename Units>
215 inline std::vector<Unit*>
222 template <
typename It>
225 float mind = FLT_MAX;
226 while (first != last) {
227 float d = float(x - first->x) * (x - first->x) +
228 float(y - first->y) * (y - first->y);
238 template <
typename Units>
241 std::unordered_set<Unit*> nearby;
242 for (
auto unit : units) {
249 nearby.insert(enemy);
257 inline std::tuple<int, int, float>
argmax(torch::Tensor
const& pos,
int scale) {
258 if (!pos.defined() || pos.dim() != 2) {
259 throw std::runtime_error(
"Two-dimensional tensor expected");
262 auto acc =
const_cast<torch::Tensor&
>(pos).accessor<float, 2>();
266 for (
int y = 0; y < acc.size(0); y++) {
267 for (
int x = 0; x < acc.size(1); x++) {
277 return std::make_tuple(xmax * scale, ymax * scale, max);
280 template <
typename T>
283 const std::vector<T>& add) {
284 for (
size_t i = 0; i < in.size(); ++i) {
289 template <
typename T>
292 const std::vector<T>& mul1,
293 const std::vector<T>& mul2) {
294 for (
size_t i = 0; i < in.size(); ++i) {
295 in[i] += mul1[i] * mul2[i];
298 template <
typename T>
301 const std::vector<T>& mul1,
303 for (
size_t i = 0; i < in.size(); ++i) {
304 in[i] += mul1[i] * mul2;
308 template <
typename T>
310 for (
size_t i = 0; i < in.size(); ++i) {
315 template <
typename T>
323 template <
typename T>
324 inline size_t argmax(
const std::vector<T>& v) {
325 return std::distance(v.begin(), std::max_element(v.begin(), v.end()));
328 template <
typename TCollection,
typename TKey>
329 bool contains(TCollection& collection, TKey& key) {
330 return collection.find(key) != collection.end();
335 template <
typename K,
typename V,
typename U>
337 dest.insert(src.begin(), src.end());
339 template <
typename K,
typename V,
typename U>
341 dest.insert(src.begin(), src.end());
343 template <
typename T,
typename U>
345 dest.insert(dest.end(), src.begin(), src.end());
353 template <
typename T,
typename... Args>
354 inline typename std::remove_reference<T>::type
cmerge(T&& c1, Args&&... cs) {
355 typename std::remove_reference<T>::type m;
Game state.
Definition: state.h:42
void inplace_flat_vector_div(std::vector< T > &in, T div)
Definition: algorithms.h:309
std::unordered_set< Unit * > findNearbyEnemyUnits(State *state, Units &&units)
Definition: algorithms.h:239
std::remove_reference< T >::type cmerge(T &&c1, Args &&...cs)
Merges two or more STL containers.
Definition: algorithms.h:354
bool isWithinRadius(Unit *unit, int32_t x, int32_t y, float radius)
Definition: algorithms.h:210
std::tuple< int, int, float > argmax(torch::Tensor const &pos, int scale)
Definition: algorithms.h:257
It getClosest(int x, int y, It first, It last)
Definition: algorithms.h:223
void cmergeInto(std::map< K, V > &dest, U &&src)
Definition: algorithms.h:336
Represents and holds information about buildable types (units, upgrades, techs).
Definition: buildtype.h:36
constexpr bool isEqualButNotNoValue(A &&a, B &&b)
Definition: algorithms.h:31
T l2_norm_vector(const std::vector< T > &v)
Definition: algorithms.h:316
UnitsInfo & unitsInfo()
Definition: state.h:116
bool contains(TCollection &collection, TKey &key)
Definition: algorithms.h:329
Definition: algorithms.h:28
std::string name
Definition: buildtype.h:44
auto filterUnits(Units &&units, UnaryPredicate pred)
Definition: filter.h:15
float constexpr kfLowest
Definition: basetypes.h:30
std::vector< Unit * > filterUnitsByDistance(Units &&units, int32_t x, int32_t y, float radius)
Definition: algorithms.h:216
auto getBestScore(Iterator begin, Iterator end, Score &&score, InvalidScore &&invalidScore=InvalidScore(), BestPossibleScore &&bestPossibleScore=BestPossibleScore())
This function iterates from begin to end, passing each value to the provided score function...
Definition: algorithms.h:59
Represents a unit in the game.
Definition: unitsinfo.h:35
float distance(int x1, int y1, int x2, int y2)
Walktile distance.
Definition: gamemechanics.h:49
void inplace_flat_vector_addcmul(std::vector< T > &in, const std::vector< T > &mul1, const std::vector< T > &mul2)
Definition: algorithms.h:290
std::string buildTypeString(BuildType const *buildType)
Definition: algorithms.h:176
const Units & enemyUnits()
All enemy units that are not dead (includes gone units).
Definition: unitsinfo.h:350
auto getBestScoreCopy(Range &&range, Score &&score, InvalidScore &&invalidScore=InvalidScore(), BestPossibleScore &&bestPossibleScore=BestPossibleScore())
This function is the same as getBestScore, but it returns a copy of the value retrieved by dereferenc...
Definition: algorithms.h:134
auto getBestScorePointer(Range &&range, Score &&score, InvalidScore &&invalidScore=InvalidScore(), BestPossibleScore &&bestPossibleScore=BestPossibleScore())
This function is the same as getBestScore, but it returns a pointer to the value of the dereferenced ...
Definition: algorithms.h:159
Main namespace for bot-related code.
Definition: areainfo.cpp:17
Position centerOfUnits(Units &&units)
Definition: algorithms.h:181
int x
Definition: unitsinfo.h:37
void inplace_flat_vector_add(std::vector< T > &in, const std::vector< T > &add)
Definition: algorithms.h:281
int y
Definition: unitsinfo.h:38
Vec2T< int > Position
Definition: basetypes.h:178