From cde99a0d0a6853e246ba375729d923bf8b2ae8bf Mon Sep 17 00:00:00 2001 From: Joop Schilder Date: Sat, 14 Nov 2020 11:16:17 +0100 Subject: [PATCH] Add code --- random_boolean_network.php | 93 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 random_boolean_network.php diff --git a/random_boolean_network.php b/random_boolean_network.php new file mode 100644 index 0000000..7416b44 --- /dev/null +++ b/random_boolean_network.php @@ -0,0 +1,93 @@ + %d\n", str_pad(decbin($i), NUM_CONNECTIONS, '0', STR_PAD_LEFT), $truth_table[$i]); + } +} + +function print_connections(array $connections): void +{ + print("Connections:\n"); + for ($i = 0; $i < NUM_NODES; $i++) { + printf("[%3d] => [%s]\n", $i, implode(', ', $connections[$i])); + } +} + +function format_nodes(array $nodes): string +{ + return implode(' ', array_map(fn($i): string => $i == 0 ? CHAR_ON_ZERO : CHAR_ON_ONE, $nodes)); +} + +function propagate(array $nodes, array &$connections, array &$truth_table): array +{ + $next_nodes = []; + for ($i = 0; $i < NUM_NODES; $i++) { + $tt_index = 0; + for ($j = 0; $j < NUM_CONNECTIONS; $j++) { + $tt_index += $nodes[$connections[$i][$j]] << (NUM_CONNECTIONS - 1 - $j); + } + $next_nodes[$i] = $truth_table[$tt_index]; + } + + return $next_nodes; +} + +print_truth_table($truth_table); +print("\n"); + +print_connections($connections); +print("\n"); + +print_generation(0, $nodes); +for ($i = 0; $i < NUM_GENERATIONS; $i++) { + $nodes = propagate($nodes, $connections, $truth_table); + print_generation($i, $nodes); +}