Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Задача 3. Регулярные запросы для всех пар вершин #3

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
169 changes: 169 additions & 0 deletions project/adjacency_matrix_fa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
import numpy as np
from pyformlang.finite_automaton import NondeterministicFiniteAutomaton, State, Symbol
from typing import Any, Iterable
from scipy import sparse
from project.vector_utils import create_bool_vector


class AdjacencyMatrixFA:
states_number: int
boolean_decomposition_matrices: dict[Any, sparse.csr_matrix]
start_states_ind: set[int]
final_states_ind: set[int]
state_to_ind: dict[State, int]
ind_to_state: dict[int, State]

def __init__(self, fa: NondeterministicFiniteAutomaton = None):
self.boolean_decomposition_matrices = {}
self.start_states_ind = set()
self.final_states_ind = set()
self.state_to_ind = {}
self.ind_to_state = {}

if fa is None:
self.states_number = 0
return

self.states_number = len(fa.states)

for ind, state in enumerate(fa.states, 0):
self.state_to_ind[state] = ind
self.ind_to_state[ind] = state

for start_state in fa.start_states:
self.start_states_ind.add(self.state_to_ind[start_state])

for final_state in fa.final_states:
self.final_states_ind.add(self.state_to_ind[final_state])

graph = fa.to_networkx()
edges = graph.edges(data="label")

nodes_connectivity = {}
labels = set()

for edge in edges:
u = edge[0]
v = edge[1]
label = edge[2]

if label is not None:
nodes_connectivity.setdefault(label, []).append((u, v))
labels.add(label)

for label in labels:
data = np.ones(len(nodes_connectivity[label]), dtype=bool)
rows = list(
map(lambda conn: self.state_to_ind[conn[0]], nodes_connectivity[label])
)
columns = list(
map(lambda conn: self.state_to_ind[conn[1]], nodes_connectivity[label])
)

decomposed_matrix = sparse.csr_matrix(
(data, (rows, columns)),
shape=(self.states_number, self.states_number),
)

self.boolean_decomposition_matrices[label] = decomposed_matrix

def accepts(self, word: Iterable[Symbol]) -> bool:
states_ind_vector = create_bool_vector(
self.states_number, self.start_states_ind
)

for symbol in word:
states_ind_vector = (
states_ind_vector @ self.boolean_decomposition_matrices[symbol]
)

final_states_ind_vector = create_bool_vector(
self.states_number, self.final_states_ind
)

return np.any(states_ind_vector & final_states_ind_vector)

def is_empty(self) -> bool:
states_ind_vector = create_bool_vector(
self.states_number, self.start_states_ind
)

transitive_closure_matrix = self.get_transitive_closure_matrix()

states_ind_vector = states_ind_vector @ transitive_closure_matrix

final_states_ind_vector = create_bool_vector(
self.states_number, self.final_states_ind
)

return not np.any(states_ind_vector & final_states_ind_vector)

def get_transitive_closure_matrix(self):
transitive_closure_matrix = sparse.csr_matrix(
(
np.ones(self.states_number, dtype=bool),
(range(self.states_number), range(self.states_number)),
),
shape=(self.states_number, self.states_number),
)

for matrix in self.boolean_decomposition_matrices.values():
transitive_closure_matrix = matrix + transitive_closure_matrix

transitive_closure_matrix = transitive_closure_matrix ** (
self.states_number - 1
)

return transitive_closure_matrix.tocsr()


def intersect_automata(
automaton1: AdjacencyMatrixFA, automaton2: AdjacencyMatrixFA
) -> AdjacencyMatrixFA:
n = automaton1.states_number
m = automaton2.states_number
kron_matrix_size = n * m

kron_boolean_decomposition_matrices = {}

kron_labels = (
automaton1.boolean_decomposition_matrices.keys()
& automaton2.boolean_decomposition_matrices.keys()
)

for label in kron_labels:
kron_boolean_decomposition_matrices[label] = sparse.kron(
automaton1.boolean_decomposition_matrices[label],
automaton2.boolean_decomposition_matrices[label],
)

start_states_ind = set()
final_states_ind = set()

for start_state_ind1 in automaton1.start_states_ind:
for start_state_ind2 in automaton2.start_states_ind:
start_states_ind.add(start_state_ind1 * m + start_state_ind2)

for final_state_ind1 in automaton1.final_states_ind:
for final_state_ind2 in automaton2.final_states_ind:
final_states_ind.add(final_state_ind1 * m + final_state_ind2)

state_to_ind = {}
ind_to_state = {}

for i in range(0, kron_matrix_size):
state_to_ind[i] = i
ind_to_state[i] = i

adjacency_matrix_fa = AdjacencyMatrixFA()

adjacency_matrix_fa.states_number = kron_matrix_size
adjacency_matrix_fa.boolean_decomposition_matrices = (
kron_boolean_decomposition_matrices
)
adjacency_matrix_fa.start_states_ind = start_states_ind
adjacency_matrix_fa.final_states_ind = final_states_ind
adjacency_matrix_fa.state_to_ind = state_to_ind
adjacency_matrix_fa.ind_to_state = ind_to_state

return adjacency_matrix_fa
46 changes: 46 additions & 0 deletions project/rpq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import networkx as nx
from project.adjacency_matrix_fa import AdjacencyMatrixFA, intersect_automata
from project.graph_utils import graph_to_nfa
from project.regex_utils import regex_to_dfa


def tensor_based_rpq(
regex: str, graph: nx.MultiDiGraph, start_nodes: set[int], final_nodes: set[int]
) -> set[tuple[int, int]]:
regex_dfa = regex_to_dfa(regex)
regex_adjacency_matrix_fa = AdjacencyMatrixFA(regex_dfa)

graph_nfa = graph_to_nfa(graph, start_nodes, final_nodes)
graph_adjacency_matrix_fa = AdjacencyMatrixFA(graph_nfa)

intersect = intersect_automata(graph_adjacency_matrix_fa, regex_adjacency_matrix_fa)

transitive_closure_matrix = intersect.get_transitive_closure_matrix()

res = set()
for graph_start_state_ind in graph_adjacency_matrix_fa.start_states_ind:
for regex_start_state_ind in regex_adjacency_matrix_fa.start_states_ind:
for graph_final_state_ind in graph_adjacency_matrix_fa.final_states_ind:
for regex_final_state_ind in regex_adjacency_matrix_fa.final_states_ind:
intersect_start_state_ind = (
graph_start_state_ind * regex_adjacency_matrix_fa.states_number
+ regex_start_state_ind
)
intersect_final_state_ind = (
graph_final_state_ind * regex_adjacency_matrix_fa.states_number
+ regex_final_state_ind
)

if transitive_closure_matrix[
intersect_start_state_ind, intersect_final_state_ind
]:
res_start_state = graph_adjacency_matrix_fa.ind_to_state[
graph_start_state_ind
]
res_final_state = graph_adjacency_matrix_fa.ind_to_state[
graph_final_state_ind
]

res.add((res_start_state, res_final_state))

return res
10 changes: 10 additions & 0 deletions project/vector_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import numpy as np


def create_bool_vector(vector_size, true_indexes):
vector = np.zeros(vector_size, dtype=bool)

for true_ind in true_indexes:
vector[true_ind] = True

return vector
9 changes: 7 additions & 2 deletions tests/autotests/test_task03.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@

# Fix import statements in try block to run tests
try:
from project.task2 import regex_to_dfa
from project.task3 import intersect_automata, AdjacencyMatrixFA, tensor_based_rpq
from project.regex_utils import regex_to_dfa
from project.adjacency_matrix_fa import (
AdjacencyMatrixFA,
intersect_automata,
)
from project.rpq import tensor_based_rpq

except ImportError:
pytestmark = pytest.mark.skip("Task 3 is not ready to test!")

Expand Down