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 
19 #ifndef UTILS_H_DEFINED
20 #define UTILS_H_DEFINED
21 
22 #include <atomic>
23 #include <limits>
24 #include <string>
25 #include <fstream>
26 
27 #include "ThreadPool.h"
28 #include <vector>
29 #include <ale_interface.hpp>
30 
37 using namespace std;
38 
40 extern ALEInterface ALE;
50 namespace Utils {
51  template<class T>
59  void atomic_add(std::atomic<T> &f, T d) {
60  T old = f.load();
61  while (!f.compare_exchange_weak(old, old + d));
62  }
63 
64  template<typename T>
65 
74  T rotl(const T x, const int k) {
75  return (x << k) | (x >> (std::numeric_limits<T>::digits - k));
76  }
77 
85  inline bool is7bit(int c) {
86  return c >= 0 && c <= 127;
87  }
88 
97  size_t ceilMultiple(size_t a, size_t b);
98 
106  inline int secam_to_range_index(int i);
107 
116  void secam210x160_to_42x32(const std::vector<unsigned char>& screen,
117  std::vector<unsigned char>& screen42x32);
118 
125  void init_ALE();
126 
134  vector<vector<string>> read_Config();
135 }
136 
137 #endif
138 
bool is7bit(int c)
Check if a value is written on 7 bits.
Definition: Utils.hpp:85
Sur-implementation of threading.
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:31
ALEInterface ALE
Definition: Utils.cpp:108
int secam_to_range_index(int i)
Transform a secam color value to a power of two.
Definition: Utils.cpp:39
vector< vector< string > > read_Config()
Store values extracted from the config file to a vector.
Definition: Utils.cpp:83
An implementation of Thread pool.
Definition: ThreadPool.h:57
Utils::ThreadPool thread_pool
Definition: Utils.cpp:29
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:62
void atomic_add(std::atomic< T > &f, T d)
Perform an atomic addition.
Definition: Utils.hpp:59
T rotl(const T x, const int k)
Rotate bytes to the left.
Definition: Utils.hpp:74
A set of useful method and class.
Definition: Random.cpp:33
void init_ALE()
Initialize the emulator from the config file.
Definition: Utils.cpp:110