-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenetic_learning_algorithm.h
More file actions
128 lines (108 loc) · 3.36 KB
/
Copy pathgenetic_learning_algorithm.h
File metadata and controls
128 lines (108 loc) · 3.36 KB
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
#ifndef GENETIC_ALGORITHM_H
#define GENETIC_ALGORITHM_H
#include <stdint.h>
#define SAMPLE_SIZE 100
#define MOVE_SIZE 10000
// random seed number.
extern uint32_t random_state;
/*
Player struct:
to simplify starting with int arr to focus on learning algorithm.
later will do bit sized movements (with uint32/16).
binary to start with - 1 means jump 0 means no-jump, frame-per-command.
*/
typedef struct player
{
int *moves;
int matches;
} Player;
/**
* @brief random number between 0 - max_num.
* @param max_num - upper limit to returned number.
* @return - random number between 0 - max_num.
*/
int randomize(unsigned int max_num);
/**
* @brief merges two arrays with 50% of getting from each parent.
* @param parent_a - int array of the first parent.
* @param parent_b - int array of the second parent.
* @return - integer array randomized from both parents.
*/
int *make_next_gen(int parent_a[], int parent_b[], int *child);
/**
* @brief allocates integer array of size size, user responsible to free.
* @param size - the size of the wanted array.
* @return - array of the wanted size, NULL if failed.
*/
int *dynamic_int_arr(int size);
/**
* @brief flips bits in int array depending on the mutation rate given (0 to 1 and 1 to 0).
* @param genetic_arr - pointer to array to be changed with mutations.
*/
void mutation(int *genetic_arr);
/**
* @brief compares source array to goal array.
* @param source_arr - array to compare to.
* @param goal_arr - array to be compared to.
* @return - number of identical values.
*/
int compare_arrays(const int *source_arr, const int *goal_arr);
/**
* @brief allocating memory for player struct
* @return - player struct pointer, NULL if failed.
*/
Player *create_player();
/**
* @brief prints move array of struct player.
* @param player - player struct to print moves from.
*/
void print_moves(Player *player);
/**
* @brief initializing the first generation randomly.
*/
Player **init_gen_zero();
/**
* @brief initiating random movements, 1 is jump 0 is no jump 50\50 chance at each.
* @param move_arr - array of moves.
*/
void init_random_moves(int *move_arr);
/**
* @brief prints the current player array.
* @param player_arrey - array to print.
*/
void print_player_array(Player **player_array);
/**
* @brief initiates the move match field of struct player.
* @param gen_zero - initial generation of players, array of structs.
* @param goal_array - the wanted configuration.
*/
void compare_moves_gen_zero(Player **gen_zero, int *goal_array);
/**
* @brief qsort compare function.
* @param a - first object to compare.
* @param b - second object to compare.
*/
int compare(const void *a, const void *b);
/**
* @brief Prints integer array.
* @param arr - array to print.
* @param size - size of the array.
*/
void print_array(int *arr, unsigned int size);
/**
* @brief replaces half of the prev generation to a new one (upper half).
* for a good complexity function assumes gen array is pre sorted by survivability.
* @param prev_gen - a pointer to an array of player pointers.
* @param goal - array of the wanted player moves.
*/
void fill_next_gen(Player **prev_gen, int *goal);
/**
* @brief freeing memory of generation pointer array.
*/
void free_gen(Player **gen);
/**
* @brief randomizing function not based on operating system based on bit shifts.
* @return random uint32_t.
*/
uint32_t xorshift32();
#endif