forked from 1chipML/1chipML
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonte_carlo.h
75 lines (63 loc) · 1.73 KB
/
monte_carlo.h
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
#ifndef ONECHIPML_MONTE_CARLO_RL
#define ONECHIPML_MONTE_CARLO_RL
#include <stdbool.h>
#include <stdint.h>
#ifndef mc_real
#define mc_real float
#endif
#ifndef UCB_MAX
#define UCB_MAX 1000
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
int8_t* values;
uint8_t nPlayers;
} Board;
typedef struct {
uint8_t xPos;
uint8_t yPos;
int8_t player;
} Action;
typedef void (*playActionType)(Board*, Action*);
typedef bool (*isValidActionType)(Board*, Action*, int);
typedef int (*getScoreType)(Board*, int);
typedef void (*getPossibleActionsType)(Board*, Action*);
typedef int (*getNumPossibleActionsType)(Board*);
typedef void (*removeActionType)(int, Action*, int);
typedef int (*getBoardSizeType)(void);
typedef bool (*isDoneType)(Board*);
typedef struct {
// Methods specific to the game
isValidActionType isValidAction;
playActionType playAction;
getScoreType getScore;
getPossibleActionsType getPossibleActions;
getNumPossibleActionsType getNumPossibleActions;
removeActionType removeAction;
getBoardSizeType getBoardSize;
isDoneType isDone;
int8_t lossValue;
int8_t drawValue;
} Game;
typedef struct Node {
uint16_t nVisits;
uint16_t score;
struct Node* parent;
struct Node* children;
Action action;
uint8_t nChildren;
} Node;
mc_real calcUCB(Node* node);
Node* findMaxUCB(Node* children, unsigned nChildren);
Node* selectChildren(Node* node, Board* board, Game* game);
void expandLeaf(Node* node, Game game, Board* board);
int mcEpisode(Node* node, int player, Game* game, Board* board);
void backpropagate(Node* node, int score);
Action mcGame(Board board, int player, Game game, int minSim, int maxSim,
mc_real goalValue);
#ifdef __cplusplus
}
#endif
#endif // ONECHIPML_MONTE_CARLO_RL