虽然xor问题还是跑不出来,但至少已经确定不是distance的错了
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from .genome import create_initialize_function, expand, expand_single
|
||||
from .genome import create_initialize_function, expand, expand_single, analysis
|
||||
from .distance import distance
|
||||
from .mutate import create_mutate_function
|
||||
from .forward import create_forward_function
|
||||
|
||||
@@ -5,6 +5,9 @@ from numpy.typing import NDArray
|
||||
|
||||
from algorithms.neat.genome.utils import flatten_connections, set_operation_analysis
|
||||
|
||||
EMPTY_NODE = np.full((1, 5), np.nan)
|
||||
EMPTY_CON = np.full((1, 4), np.nan)
|
||||
|
||||
|
||||
def distance(nodes1: NDArray, connections1: NDArray, nodes2: NDArray, connections2: NDArray) -> NDArray:
|
||||
"""
|
||||
@@ -13,15 +16,70 @@ def distance(nodes1: NDArray, connections1: NDArray, nodes2: NDArray, connection
|
||||
connections are a 3-d array with shape (2, N, N), axis 0 means [weights, enable]
|
||||
"""
|
||||
|
||||
node_distance = gene_distance(nodes1, nodes2, 'node')
|
||||
nd = node_distance(nodes1, nodes2) # node distance
|
||||
|
||||
# refactor connections
|
||||
keys1, keys2 = nodes1[:, 0], nodes2[:, 0]
|
||||
cons1 = flatten_connections(keys1, connections1)
|
||||
cons2 = flatten_connections(keys2, connections2)
|
||||
cd = connection_distance(cons1, cons2) # connection distance
|
||||
return nd + cd
|
||||
|
||||
connection_distance = gene_distance(cons1, cons2, 'connection')
|
||||
return node_distance + connection_distance
|
||||
|
||||
def node_distance(nodes1, nodes2, disjoint_coe=1., compatibility_coe=0.5):
|
||||
node_cnt1 = np.sum(~np.isnan(nodes1[:, 0]))
|
||||
node_cnt2 = np.sum(~np.isnan(nodes2[:, 0]))
|
||||
max_cnt = np.maximum(node_cnt1, node_cnt2)
|
||||
|
||||
nodes = np.concatenate((nodes1, nodes2), axis=0)
|
||||
keys = nodes[:, 0]
|
||||
sorted_indices = np.argsort(keys, axis=0)
|
||||
nodes = nodes[sorted_indices]
|
||||
nodes = np.concatenate([nodes, EMPTY_NODE], axis=0) # add a nan row to the end
|
||||
fr, sr = nodes[:-1], nodes[1:] # first row, second row
|
||||
nan_mask = np.isnan(nodes[:, 0])
|
||||
|
||||
intersect_mask = (fr[:, 0] == sr[:, 0]) & ~nan_mask[:-1]
|
||||
|
||||
non_homologous_cnt = node_cnt1 + node_cnt2 - 2 * np.sum(intersect_mask)
|
||||
nd = batch_homologous_node_distance(fr, sr)
|
||||
nd = np.where(np.isnan(nd), 0, nd)
|
||||
homologous_distance = np.sum(nd * intersect_mask)
|
||||
|
||||
val = non_homologous_cnt * disjoint_coe + homologous_distance * compatibility_coe
|
||||
|
||||
if max_cnt == 0: # consider the case that both genome has no gene
|
||||
return 0
|
||||
else:
|
||||
return val / max_cnt
|
||||
|
||||
|
||||
def connection_distance(cons1, cons2, disjoint_coe=1., compatibility_coe=0.5):
|
||||
con_cnt1 = np.sum(~np.isnan(cons1[:, 2])) # weight is not nan, means the connection exists
|
||||
con_cnt2 = np.sum(~np.isnan(cons2[:, 2]))
|
||||
max_cnt = np.maximum(con_cnt1, con_cnt2)
|
||||
|
||||
cons = np.concatenate((cons1, cons2), axis=0)
|
||||
keys = cons[:, :2]
|
||||
sorted_indices = np.lexsort(keys.T[::-1])
|
||||
cons = cons[sorted_indices]
|
||||
cons = np.concatenate([cons, EMPTY_CON], axis=0) # add a nan row to the end
|
||||
fr, sr = cons[:-1], cons[1:] # first row, second row
|
||||
|
||||
# both genome has such connection
|
||||
intersect_mask = np.all(fr[:, :2] == sr[:, :2], axis=1) & ~np.isnan(fr[:, 2]) & ~np.isnan(sr[:, 2])
|
||||
|
||||
non_homologous_cnt = con_cnt1 + con_cnt2 - 2 * np.sum(intersect_mask)
|
||||
cd = batch_homologous_connection_distance(fr, sr)
|
||||
cd = np.where(np.isnan(cd), 0, cd)
|
||||
homologous_distance = np.sum(cd * intersect_mask)
|
||||
|
||||
val = non_homologous_cnt * disjoint_coe + homologous_distance * compatibility_coe
|
||||
|
||||
if max_cnt == 0: # consider the case that both genome has no gene
|
||||
return 0
|
||||
else:
|
||||
return val / max_cnt
|
||||
|
||||
|
||||
def gene_distance(ar1, ar2, gene_type, compatibility_coe=0.5, disjoint_coe=1.):
|
||||
|
||||
@@ -319,6 +319,10 @@ def o2m_distance(r_nodes, r_connections, pop_nodes, pop_connections):
|
||||
distances = []
|
||||
for nodes, connections in zip(pop_nodes, pop_connections):
|
||||
d = distance(r_nodes, r_connections, nodes, connections)
|
||||
if d < 0:
|
||||
d = distance(r_nodes, r_connections, nodes, connections)
|
||||
print(d)
|
||||
assert False
|
||||
distances.append(d)
|
||||
distances = np.stack(distances, axis=0)
|
||||
return distances
|
||||
|
||||
Reference in New Issue
Block a user