def test_medium(self): """> Cycle-free check in medium network.""" # Folder must be root to load in make_net properly if os.getcwd().split('\\')[-1] == 'tests': os.chdir('..') # Create the connections (only keys matter!) connections = get_medium_net() # Test self.assertFalse(creates_cycle(connections=connections, test=(2, 1))) self.assertFalse(creates_cycle(connections=connections, test=(-2, 1))) self.assertFalse(creates_cycle(connections=connections, test=(-1, 2)))
def test_simple(self): """> Cycle check in simple network.""" # Folder must be root to load in make_net properly if os.getcwd().split('\\')[-1] == 'tests': os.chdir('..') # Create the connections (only keys matter!) connections = get_simple_net() # Test self.assertTrue(creates_cycle(connections=connections, test=(1, 1))) self.assertTrue(creates_cycle(connections=connections, test=(1, -1))) self.assertTrue(creates_cycle(connections=connections, test=(0, 1))) self.assertTrue(creates_cycle(connections=connections, test=(0, -1)))
def mutate_add_connection(self, config: GenomeConfig): """ Attempt to add a new connection. A connection starts in the input_node and ends in the output_node. The restrictions laid on the mutation are: - An output of the network may never be an input_node (sending-end) - An input of the network may never be an output_node (receiving-end) """ # List all the keys that are possible output nodes (i.e. all output and hidden nodes) possible_outputs = list(iterkeys(self.nodes)) out_node = choice(possible_outputs) # List all the keys that are possible input-nodes (i.e. all input and hidden nodes) possible_inputs = [ i for i in possible_outputs + config.keys_input if i not in config.keys_output ] in_node = choice(possible_inputs) # Check if the new connection would create a cycle, discard if so key = (in_node, out_node) recurrent = (config.rnn_prob_gru + config.rnn_prob_lstm + config.rnn_prob_simple_rnn) > 0 if (not recurrent) and creates_cycle( list(iterkeys(self.connections.items())), key): return # Don't duplicate connections if key in self.connections: self.enable_connection(config=config, key=key) return # Create the new connection self.create_connection(config, in_node, out_node)
def test_medium(self): """> Cycle check in medium network.""" # Folder must be root to load in make_net properly if os.getcwd().split('\\')[-1] == 'tests': os.chdir('..') # Create the connections (only keys matter!) connections = get_medium_net() # Test self.assertTrue(creates_cycle(connections=connections, test=(0, -1))) self.assertTrue(creates_cycle(connections=connections, test=(0, -2))) self.assertTrue(creates_cycle(connections=connections, test=(0, 1))) self.assertTrue(creates_cycle(connections=connections, test=(0, 2))) self.assertTrue(creates_cycle(connections=connections, test=(1, -1))) self.assertTrue(creates_cycle(connections=connections, test=(1, 1))) self.assertTrue(creates_cycle(connections=connections, test=(2, 2))) # Extend network connections.update({(1, 2): 1}) self.assertTrue(creates_cycle(connections=connections, test=(2, -1))) self.assertTrue(creates_cycle(connections=connections, test=(2, 1))) self.assertFalse(creates_cycle(connections=connections, test=(1, -2))) # Allowed!
def mutate(self, config: GenomeConfig): """Mutates this genome.""" if random() < config.node_add_prob: self.mutate_add_node(config) if random() < config.node_disable_prob: self.mutate_disable_node(config) if random() < config.conn_add_prob: self.mutate_add_connection(config) if random() < config.conn_disable_prob: self.mutate_disable_connection() # Mutate connection genes for cid, cg in self.connections.items(): mut_enabled = cg.mutate(config) if mut_enabled is not None: if mut_enabled and not creates_cycle( list(iterkeys(self.connections.items())), cid): self.enable_connection(config=config, key=cid) else: self.disable_connection(key=cid) # Mutate node genes (bias etc.) for ng in self.nodes.values(): ng.mutate(config)
def test_complex(self): """> Cycle check in complex network.""" # Folder must be root to load in make_net properly if os.getcwd().split('\\')[-1] == 'tests': os.chdir('..') # Create the connections (only keys matter!) connections = get_complex_net() # Test self.assertTrue(creates_cycle(connections=connections, test=(0, -1))) self.assertTrue(creates_cycle(connections=connections, test=(0, -2))) self.assertTrue(creates_cycle(connections=connections, test=(0, 1))) self.assertTrue(creates_cycle(connections=connections, test=(0, 2))) self.assertTrue(creates_cycle(connections=connections, test=(0, 3))) self.assertTrue(creates_cycle(connections=connections, test=(0, 4))) self.assertTrue(creates_cycle(connections=connections, test=(3, 3))) self.assertTrue(creates_cycle(connections=connections, test=(3, 2))) self.assertTrue(creates_cycle(connections=connections, test=(3, 1))) self.assertTrue(creates_cycle(connections=connections, test=(3, -1))) self.assertTrue(creates_cycle(connections=connections, test=(3, -2))) self.assertTrue(creates_cycle(connections=connections, test=(3, 4))) self.assertTrue(creates_cycle(connections=connections, test=(-1, -1))) self.assertFalse(creates_cycle(connections=connections, test=(1, 4))) # Allowed!