1
0
Fork 0

Add CIM and ACIM classes

parallel_struct_est
philpMartin 4 years ago
parent 5051b484f9
commit 8a9a0d44ed
  1. 20
      main_package/classes/amalgamated_cims.py
  2. 9
      main_package/classes/conditional_intensity_matrix.py
  3. 68
      main_package/classes/rate_matrix.py

@ -0,0 +1,20 @@
import conditional_intensity_matrix as cim
import numpy as np
class AmalgamatedCims:
def __init__(self, states_number,list_of_keys, list_of_matrices_dims):
self.actual_cims = {}
self.init_cims_structure(list_of_keys, list_of_matrices_dims)
self.states_per_variable = states_number
def init_cims_structure(self, keys, dims):
for key, dim in (keys, dims):
self.actual_cims[key] = np.empty(dim, dtype=cim.ConditionalIntensityMatrix)
for block_matrix in self.actual_cims.values():
for matrix in block_matrix:
matrix = cim.ConditionalIntensityMatrix(self.states_per_variable)
def compute_matrix_indx(self, row, col):
return self.state_per_variable * row + col

@ -0,0 +1,9 @@
import numpy as np
class ConditionalIntensityMatrix:
def __init__(self, dimension):
self.state_residence_times = np.zeros(shape=(1, dimension))
self.state_transition_matrix = np.zeros(shape=(dimension, dimension))
self.cim = np.zeros(shape=(dimension, dimension))

@ -1,68 +0,0 @@
import numpy as np
import network_graph as dg
import sample_path as sp
import priority_queue as pq
class RateMatrix():
"""
Rappresenta la matrice Q di una generica CTMC costruita a partire dalle informazioni contenute nel grafo dinamico
"""
def __init__(self, graph, dim):
self.graph = graph
self.matrix = np.zeros(shape=(dim,dim))
self.pr_queue = pq.PriorityQueue()
def build_matrix(self):
root = self.graph.get_root_node()
root.color = dg.node.Color.GRAY
self.pr_queue.enqueue(root)
while not self.pr_queue.is_empty():
n = self.pr_queue.dequeue()
adjacency_list = self.graph.get_neighbours(n)
#print(adjacency_list)
time = self.graph.graph[n.state_id]["Time"]
sum_of_qs = 0.0
for nd, weight in adjacency_list.items():
sum_of_qs += self.calculate_off_diagonal_element_and_fill_matrix(n.node_id, nd.node_id, weight, time)
if self.graph.graph[nd.state_id]["Node"].color == dg.node.Color.WHITE:
self.graph.graph[nd.state_id]["Node"].color = dg.node.Color.GRAY
self.pr_queue.enqueue(nd)
n.color = dg.node.Color.BLACK
self.calculate_diagonal_element_and_fill_matrix(sum_of_qs, n.node_id)
def calculate_off_diagonal_element_and_fill_matrix(self, start_node, arrival_node, weight, time):
q = weight / time
self.matrix[start_node][arrival_node] = q
return q
def calculate_diagonal_element_and_fill_matrix(self, sum_of_qs, start_node):
self.matrix[start_node][start_node] = -sum_of_qs
# A Simple Test #
s1 = sp.SamplePath()
s1.build_trajectories()
print(s1.get_number_trajectories())
g1 = dg.DynamicGraph(s1)
g1.build_graph()
print(g1.graph)
#print(g1.states_number)
Q = RateMatrix(g1, g1.states_number)
#print(Q.matrix)
Q.build_matrix()
print(Q.matrix)
non_zero_values = 0
val = 0.0
for coeff in Q.matrix[0][1:]:
if(coeff != 0):
non_zero_values += 1
val += coeff
print(non_zero_values == len(Q.graph.graph["222"]["Arcs"].keys()))