File size: 1,814 Bytes
00e6582
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <time.h>

// Activation Functions
#define RELU(x) (1.0 / (1.0 + exp(-(x))))
#define SIGMOID(x) (1.0 / (1.0 + exp(-(x))))

// Configuration structure to easily alter layer sizes
typedef struct {
    int input_size;
    int hidden_size;
    int output_size;
} NetworkConfig;

// The Recurrent Neural Network Structure
typedef struct {
    NetworkConfig config;

    // Weights
    double **W_ih; // Input to Hidden weights
    double **W_hh; // Hidden to Hidden (Recurrent) weights
    double **W_ho; // Hidden to Output weights

    // Biases
    double *b_h;   // Hidden layer biases
    double *b_o;   // Output layer biases

    // States (Persist across steps for recurrence)
    double *h_state;
    double *output;

    double fitness;
} RNN;

// Helper to allocate a 2D weight matrix
double** allocate_matrix(int rows, int cols);

// Helper to free a 2D weight matrix
void free_matrix(double **matrix, int rows);

// Random double between -min and max
double rand_double(double min, double max);

// Initialize network with random weights
RNN* create_network(NetworkConfig config);

void free_network(RNN *net);

// Reset recurrent state between evaluation trials
void reset_rnn_state(RNN *net);

// Forward pass execution
void forward_pass(RNN *net, double *input);

// Neuroevolution: Gaussian Mutation
void mutate(RNN *net, double mutation_rate, double mutation_strength);

// Neuroevolution: Uniform Crossover
void crossover(RNN *parentA, RNN *parentB, RNN *child);

// Clone helper to preserve elites
void copy_network(RNN *dest, RNN *src);

// Comparison function for sorting population by fitness (descending)
int compare_rnn(const void *a, const void *b);

// --- MAIN RUNTIME & EXAMPLE USAGE ---
int CreateNEV();