Commit a87de536 authored by Benoit GAUZERE's avatar Benoit GAUZERE

Implementation of attributes genericity for costfunctions.py. Change edge from...

Implementation of attributes genericity for costfunctions.py. Change edge from a 3-tuple to a 2-tuple.
parent e5af6123
......@@ -11,8 +11,14 @@ class ConstantCostFunction:
self.cei_ = self.ced_ = cei
def cns(self, node_u, node_v, g1, g2):
""" return substitution edit operation cost between node_u of G1 and node_v of G2"""
return (g1.node[node_u]['label'] != g2.node[node_v]['label'])*self.cns_
""" return substitution edit operation cost between
node_u of G1 and node_v of G2"""
have_same_label = True
for label in g1.nodes[node_u].keys(): # should have same labels
have_same_label &= (g1.node[node_u][label] !=
g2.node[node_v][label])
return have_same_label*self.cns_
def cnd(self, u, G1):
return self.cnd_
......@@ -22,9 +28,14 @@ class ConstantCostFunction:
def ces(self, e1, e2, G1, G2):
"""
tester avec des attributs autres que symboliques en testant
l'operateur __eq__"""
return (e1[2]['label'] != e2[2]['label'])*self.ces_
An edge is a 2-tuple : [ firstnode, secondnode]
"""
have_same_label = True
for label in G1[e1[0]][e1[1]].keys(): # should have same labels
have_same_label &= (G1[e1[0]][e1[1]][label] !=
G2[e2[0]][e2[1]][label])
return have_same_label * self.ces_
def ced(self, e1, G1):
return self.ced_
......@@ -43,7 +54,7 @@ class RiesenCostFunction():
""" u et v sont des id de noeuds """
n = len(G1[u])
m = len(G2[v])
sub_C = np.ones([n+m, n+m]) * 1000 #np.inf
sub_C = np.ones([n+m, n+m]) * 1000 # np.inf
sub_C[n:, m:] = 0
i = 0
l_nbr_u = G1[u]
......@@ -59,12 +70,12 @@ class RiesenCostFunction():
i = 0
for nbr_u in l_nbr_u:
sub_C[i, m+i] = self.cf_.ced([u, nbr_u, G1[u][nbr_u]], G1)
sub_C[i, m+i] = self.cf_.ced([u, nbr_u], G1)
i += 1
j = 0
for nbr_v in l_nbr_v:
sub_C[n+j, j] = self.cf_.cei([v, nbr_v, G2[v][nbr_v]], G2)
sub_C[n+j, j] = self.cf_.cei([v, nbr_v], G2)
j += 1
row_ind, col_ind = self.lsap_solver_(sub_C)
cost = np.sum(sub_C[row_ind, col_ind])
......@@ -73,15 +84,14 @@ class RiesenCostFunction():
def cnd(self, u, G1):
cost = 0
for nbr in G1[u]:
cost += self.cf_.ced([u,nbr,G1[u][nbr]],G1)
return self.cf_.cnd(u,G1) + cost
cost += self.cf_.ced([u, nbr], G1)
return self.cf_.cnd(u, G1) + cost
def cni(self, v, G2):
cost = 0
for nbr in G2[v]:
cost += self.cf_.cei([v,nbr,G2[v][nbr]], G2)
cost += self.cf_.cei([v, nbr], G2)
return self.cf_.cni(v, G2) + cost
......@@ -102,9 +112,9 @@ class NeighboorhoodCostFunction():
l_nbr_v = G2[v]
for nbr_u in l_nbr_u:
j = 0
e1 = [u, nbr_u, G1[u][nbr_u]]
e1 = [u, nbr_u]
for nbr_v in G2[v]:
e2 = [v, nbr_v, G2[v][nbr_v]]
e2 = [v, nbr_v]
sub_C[i, j] = self.cf_.ces(e1, e2, G1, G2)
sub_C[i, j] += self.cf_.cns(nbr_u, nbr_v, G1, G2)
j += 1
......@@ -112,13 +122,13 @@ class NeighboorhoodCostFunction():
i = 0
for nbr_u in l_nbr_u:
sub_C[i, m+i] = self.cf_.ced([u, nbr_u, G1[u][nbr_u]], G1)
sub_C[i, m+i] = self.cf_.ced([u, nbr_u], G1)
sub_C[i, m+i] += self.cf_.cnd(nbr_u, G1)
i += 1
j = 0
for nbr_v in l_nbr_v:
sub_C[n+j, j] = self.cf_.cei([v, nbr_v, G2[v][nbr_v]], G2)
sub_C[n+j, j] = self.cf_.cei([v, nbr_v], G2)
sub_C[n+j, j] += self.cf_.cni(nbr_v, G2)
j += 1
......@@ -129,11 +139,11 @@ class NeighboorhoodCostFunction():
def cnd(self, u, G1):
cost = 0
for nbr in G1[u]:
cost += self.cf_.ced([u, nbr, G1[u][nbr]], G1)
cost += self.cf_.ced([u, nbr], G1)
return self.cf_.cnd(u, G1) + cost
def cni(self, v, G2):
cost = 0
for nbr in G2[v]:
cost += self.cf_.cei([v, nbr, G2[v][nbr]], G2)
cost += self.cf_.cei([v, nbr, G2], G2)
return self.cf_.cni(v, G2) + cost
import unittest
class TestGEDMethods(unittest.TestCase):
def test_ConstantCostFunction(self):
from pygraph.ged.costfunctions import ConstantCostFunction
import networkx as nx
cns = 1
cni = 2
ces = 3
cei = 4
cf = ConstantCostFunction(cns=cns, cni=cni, ces=ces, cei=cei)
g1 = nx.Graph()
g1.add_nodes_from([(1, {'label': 1}),
(2, {'label': 1}),
(3, {'label': 2})])
g1.add_edges_from([(1,2,{'label': '1', 'label2': 2}),
(2,3,{'label': '3', 'label2': 1})])
g2 = nx.Graph()
g2.add_nodes_from([(1, {'label': 1}),
(2, {'label': 2}),
(3, {'label': 2})])
g2.add_edges_from([(1,2,{'label': '3', 'label2': 1}),
(1,3,{'label': '1', 'label2': 2})])
self.assertEqual(cf.cns(1, 1, g1, g2), 0)
self.assertEqual(cf.cns(1, 2, g1, g2), cns)
self.assertEqual(cf.cni(4, g1), cni)
self.assertEqual(cf.cni(4, g1), cf.cnd(3, g1))
self.assertEqual(cf.ces([1, 2], [1, 3], g1, g2), 0)
self.assertEqual(cf.ces([1, 2], [1, 3], g1, g2),
cf.ces([1, 3], [1, 2], g2, g1))
self.assertEqual(cf.ces([1, 2], [1, 2], g1, g2), ces)
self.assertEqual(cf.cei([1, 3], g1), cei)
self.assertEqual(cf.cei([2, 3], g1), cf.ced([1, 3], g1))
def test_GED(self):
from pygraph.utils.graphfiles import loadGXL
from pygraph.ged.GED import ged
g = loadGXL('./test.gxl')
self.assertEqual(ged(g,g)[0],0)
class TestGraphFilesMethods(unittest.TestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment