IAtari
Genetic algorithm generating AI capable to play Atari2600 games.
Utils.hpp
Go to the documentation of this file.
1 /*
2  This file is part of Leela Zero.
3  Copyright (C) 2017 Gian-Carlo Pascutto
4 
5  Leela Zero is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Leela Zero is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Leela Zero. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
24 #ifndef UTILS_H_DEFINED
25 #define UTILS_H_DEFINED
26 
27 #include <atomic>
28 #include <limits>
29 #include <string>
30 #include <fstream>
31 
32 #include "ThreadPool.h"
33 #include <vector>
34 #include <ale_interface.hpp>
35 
36 using namespace std;
37 
39 extern ALEInterface ALE;
46 namespace Utils {
47  template<class T>
55  void atomic_add(std::atomic<T> &f, T d) {
56  T old = f.load();
57  while (!f.compare_exchange_weak(old, old + d));
58  }
59 
60  template<typename T>
61 
70  T rotl(const T x, const int k) {
71  return (x << k) | (x >> (std::numeric_limits<T>::digits - k));
72  }
73 
81  inline bool is7bit(int c) {
82  return c >= 0 && c <= 127;
83  }
84 
93  size_t ceilMultiple(size_t a, size_t b);
94 
102  inline int secam_to_range_index(int i);
103 
112  void secam210x160_to_42x32(const std::vector<unsigned char>& screen,
113  std::vector<unsigned char>& screen42x32);
114 
121  void init_ALE();
122 
129  vector<vector<string>> read_Config();
130 }
131 
132 #endif
133 
bool is7bit(int c)
Check if a value is written on 7 bits.
Definition: Utils.hpp:81
size_t ceilMultiple(size_t a, size_t b)
Calculate the closest value from a such that this new value is a multiple of b.
Definition: Utils.cpp:25
ALEInterface ALE
Definition: Utils.cpp:102
int secam_to_range_index(int i)
Transform a secam color value to a power of two.
Definition: Utils.cpp:33
vector< vector< string > > read_Config()
Store values extracted from the config file to a vector.
Definition: Utils.cpp:77
Definition: ThreadPool.h:39
Utils::ThreadPool thread_pool
Definition: Utils.cpp:23
void secam210x160_to_42x32(const std::vector< unsigned char > &screen, std::vector< unsigned char > &screen42x32)
Reduce the screen from 210x160 to 42x32.
Definition: Utils.cpp:56
void atomic_add(std::atomic< T > &f, T d)
Perform an atomic addition.
Definition: Utils.hpp:55
T rotl(const T x, const int k)
Rotate bytes to the left.
Definition: Utils.hpp:70
A set of useful method and class.
Definition: Random.cpp:27
void init_ALE()
Initialize the emulator from the config file.
Definition: Utils.cpp:104