-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathANNA.h
94 lines (76 loc) · 2.95 KB
/
ANNA.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//#define NDEBUG true
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <stdbool.h>
#include <float.h>
#include <assert.h>
#include <string.h>
#define LEARNING_RATE 0.01
//#define
#define NUMBER_OF_CONNECTIONS (4 + 1)
#define NUMBER_OF_INPUTS 2//3
#define WEIGHT_LENGTH (NUMBER_OF_CONNECTIONS + NUMBER_OF_INPUTS)
#define COOLING_CONSTANT 1.0
#define NETWORK_NONLOCALITY 2
typedef struct Synapse {
// 0 are actual weights, 1 is gradients, 2 is gradient average, 3 is squared gradient avg
float offsetWeights[4][WEIGHT_LENGTH];
float periodWeights[4][WEIGHT_LENGTH];
float tightnessWeights[4][WEIGHT_LENGTH];
float amplitudeWeight[4];
float localLearningRate;
} Synapse;
typedef struct Neuron {
unsigned long lastUpdated;
unsigned int timesUsedSinceReinforcement;
unsigned long timesReinforced;
float cumulativeValue; // tracks frequency of use
int externalInputIndices[NUMBER_OF_INPUTS];
int connectionIndices[NUMBER_OF_CONNECTIONS];
Synapse synapses[NUMBER_OF_CONNECTIONS];
int actionCode;
float localLearningRate;
} Neuron;
typedef struct Reinforcement {
unsigned long iterationNumber;
float feedback;
} Reinforcement;
typedef struct Network {
int networkSize;
float neuronAccumulationRate;
Neuron* neurons;
Neuron* currentNeuron;
unsigned long currentIteration;
unsigned long mostRecentReinforcement;
int allocatedReinforcementHistoryLength;
int numberOfReinforcements;
Reinforcement* feedbackHistory;
} Network;
Network* constructNetwork(int numberOfNeurons, int maxReinforcementHistory, float neuronAccumulationRate);
int runUntilOutput(Network* network, float input[NUMBER_OF_INPUTS]);
void initialize();
int wrapIndex(int index, int size);
float cool(Neuron* neuron, unsigned long currentIteration);
void getInputs(Network* network, float *rawInputs, Neuron *neuron, float *combinedInputVector);
int stepNetwork(Network* network, float rawInputs[NUMBER_OF_INPUTS]);
void setAction(Network* network, int index, int actionCode);
void updateWeights(Neuron *neuron, float reinforcement);
void reinforce(Network* network, float feedback);
float getReinforcement(Network* network, unsigned int lastIterationNumber);
void deleteNetwork(Network* network);
int processNeuron(Neuron* neuron, float* inputs);
float max(float a, float b);
float min(float a, float b);
float clamp(float value, float min, float max);
void resetAccumulations(Network* network);
double trainToConvergence(Network* network, float errorCutoff, float learningRate);
int getInputLength();
void setInputs(Network* network, int neuronNumber, int* inputIndices);
void resetNetwork(Network* network);
int getIterationsSinceReinforcement(Network* network);
float getNeuronErrorRate(Network* network, int neuron);
float getNeuronAccumulation(Network* network, int neuron);
void applyADAM(float weights[4][WEIGHT_LENGTH], float normalizedReinforcement, unsigned long timesReinforced, float minClamp, float maxClamp);
#define lengthof(array) (sizeof(array) / sizeof(array[0]))