fixed some bugs, added dockerfile for running on windows. output files must be extracted manually.

This commit is contained in:
Tom Zuidberg
2026-06-22 17:27:42 +02:00
parent b96e5f7e38
commit 8e0d2842af
4 changed files with 119 additions and 78 deletions

View File

@@ -171,6 +171,7 @@ class BooleanNetwork:
if type(state) is str:
for i in range(self.size):
self.nodes[i] = bool(int(state[i]))
self.time_step = 0
return self
if isinstance(state, (list, tuple)):
@@ -178,19 +179,23 @@ class BooleanNetwork:
f"SetState error: given state list contains elements of type different from bool. All elements must be bools. got {state}"
)
self.nodes = list(state).copy()
self.time_step = 0
return self
raise Exception(
"SetState error: end of function reached. given state is not of type string nor list[bool]."
)
def Update(self, n: int = 1, /, verbose=False) -> None:
def Update(
self, n: int = 1, /, verbose: bool = False, writeToFile: bool = False
) -> None:
assert type(n) is int and n >= 0, (
f"Update error: amount of updates must be an integer and positive. got {n=}"
)
assert self.__has_update_functions, "Update error: no update functions defined"
assert self.__has_update_scheme, "Update error: no update scheme defined"
assert type(verbose) is bool, "Update error: verbose must be a bool"
assert type(writeToFile) is bool, "Update error: writeToFile must be a bool"
selected_update: Callable
@@ -208,11 +213,29 @@ class BooleanNetwork:
case _:
raise Exception("Update error: update scheme selection went wrong")
for _ in range(n):
selected_update()
self.time_step += 1
if verbose:
print(self)
match (verbose, writeToFile):
case (False, False):
for _ in range(n):
selected_update()
self.time_step += 1
case (False, True):
with open("output.txt", "w") as f:
for _ in range(n):
selected_update()
self.time_step += 1
f.writelines([self.state, "\n"])
case (True, False):
for _ in range(n):
selected_update()
self.time_step += 1
print(self)
case (True, True):
with open("output.txt", "w") as f:
for _ in range(n):
selected_update()
self.time_step += 1
f.writelines([self.state, "\n"])
print(self)
def __str__(self) -> str:
return f"{self.time_step:>5} | {''.join(str(int(node)) for node in self.nodes)}"
@@ -222,13 +245,18 @@ class BooleanNetwork:
return "".join(str(int(node)) for node in self.nodes)
def GetStableProbabilityDistribution(self) -> np.ndarray:
matrix: np.ndarray = self.__get_transition_matrix()
eigenvalues, eigenvectors = scipy.linalg.eig(matrix.T)
stationary_vector = np.real(eigenvectors[:, np.isclose(eigenvalues, 1)])
stationary_distribution = stationary_vector / np.sum(stationary_vector)
return stationary_distribution.flatten()
matrix: np.ndarray = self.GetTransitionMatrix()
def __get_transition_matrix(self) -> np.ndarray:
eigenvalues, eigenvectors = scipy.linalg.eig(matrix.T)
idx = np.argmin(np.abs(eigenvalues - 1.0))
pi = eigenvectors[:, idx].real
pi = pi / pi.sum()
return pi
def GetTransitionMatrix(self) -> np.ndarray:
dimension = 2**self.size
matrix: np.ndarray = np.zeros((dimension, dimension))