From 9b187604fc3af94e0b7d13c5fdd2ec273f43116f Mon Sep 17 00:00:00 2001 From: AJGQ Date: Sun, 30 May 2021 14:57:40 +0100 Subject: [PATCH] add function to randomly tweak the weights --- genann.c | 9 +++++++++ genann.h | 3 +++ 2 files changed, 12 insertions(+) diff --git a/genann.c b/genann.c index b05fa4f..f707476 100644 --- a/genann.c +++ b/genann.c @@ -201,6 +201,15 @@ void genann_randomize(genann *ann) { } } +void genann_random_tweak(genann *ann, double max) { + int i; + for (i = 0; i < ann->total_weights; ++i) { + /* -max <= r <= max */ + double r = (GENANN_RANDOM() - 0.5) * 2 * max; + ann->weight[i] += r; + } +} + void genann_free(genann *ann) { /* The weight, output, and delta pointers go to the same buffer. */ diff --git a/genann.h b/genann.h index e4b7383..a9baf6d 100644 --- a/genann.h +++ b/genann.h @@ -79,6 +79,9 @@ genann *genann_read(FILE *in); /* Sets weights randomly. Called by init. */ void genann_randomize(genann *ann); +/* Modifies the weights randomly, with max as a threshold */ +void genann_random_tweak(genann *ann, double max); + /* Returns a new copy of ann. */ genann *genann_copy(genann const *ann);