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

@@ -2,8 +2,10 @@ from functools import partial
from types import FunctionType
from typing import Callable, Optional
import numpy as np
from simple_term_menu import TerminalMenu
from simulator import BooleanNetwork
from typing_extensions import Concatenate
def ShowMenu(options: list[str], title: str = "", highlight_entry: int = 0) -> int:
@@ -22,8 +24,9 @@ def ShowMenu(options: list[str], title: str = "", highlight_entry: int = 0) -> i
return selectedIndex # pyright: ignore[reportReturnType]
def Continue() -> None:
ShowMenu(options=[None, "Continue"])
def Continue(title="") -> None:
ShowMenu(title=title, options=[None, "Continue"], highlight_entry=1)
print()
# def MainMenu() -> None:
@@ -47,70 +50,56 @@ def MainMenu() -> None:
def SelectExampleMenu() -> None:
def bn4nodeSync() -> None:
bn = (
BooleanNetwork(4)
.UseSynchronousScheme()
.SetFunctions(
[
lambda x1, x2, x3, x4: not x2,
lambda x1, x2, x3, x4: x1,
lambda x1, x2, x3, x4: x1 ^ x4,
lambda x1, x2, x3, x4: x3,
]
)
)
functions = [
lambda x1, x2, x3, x4: not x2,
lambda x1, x2, x3, x4: x1,
lambda x1, x2, x3, x4: x1 ^ x4,
lambda x1, x2, x3, x4: x3,
]
bn = BooleanNetwork(4).UseSynchronousScheme().SetFunctions(functions)
global currentFunction
funcStrings = ["not x2", "x1", "x1 ^ x4", "x3"]
currentFunction = partial(BooleanNetworkMenu, bn, funcStrings)
currentFunction = partial(BooleanNetworkMenu, bn, functions, funcStrings)
def bn4nodeSeq() -> None:
functions = [
lambda x1, x2, x3, x4: not x2,
lambda x1, x2, x3, x4: x1,
lambda x1, x2, x3, x4: x1 ^ x4,
lambda x1, x2, x3, x4: x3,
]
bn = (
BooleanNetwork(4)
.UseSequentialScheme([1, 2, 3, 4])
.SetFunctions(
[
lambda x1, x2, x3, x4: not x2,
lambda x1, x2, x3, x4: x1,
lambda x1, x2, x3, x4: x1 ^ x4,
lambda x1, x2, x3, x4: x3,
]
)
.SetFunctions(functions)
)
global currentFunction
funcStrings = ["not x2", "x1", "x1 ^ x4", "x3"]
currentFunction = partial(BooleanNetworkMenu, bn, funcStrings)
currentFunction = partial(BooleanNetworkMenu, bn, functions, funcStrings)
def bn3nodeSync() -> None:
bn = (
BooleanNetwork(3)
.UseSynchronousScheme()
.SetFunctions(
[
lambda x1, x2, x3: not x3,
lambda x1, x2, x3: not x1,
lambda x1, x2, x3: not x2,
]
)
)
functions = [
lambda x1, x2, x3: not x3,
lambda x1, x2, x3: not x1,
lambda x1, x2, x3: not x2,
]
bn = BooleanNetwork(3).UseSynchronousScheme().SetFunctions(functions)
global currentFunction
funcStrings = ["not x3", "not x1", "not x2"]
currentFunction = partial(BooleanNetworkMenu, bn, funcStrings)
currentFunction = partial(BooleanNetworkMenu, bn, functions, funcStrings)
def bn3nodeSeq() -> None:
functions = [
lambda x1, x2, x3: not x3,
lambda x1, x2, x3: not x1,
lambda x1, x2, x3: not x2,
]
bn = (
BooleanNetwork(3)
.UseSequentialScheme([1, 2, 3])
.SetFunctions(
[
lambda x1, x2, x3: not x3,
lambda x1, x2, x3: not x1,
lambda x1, x2, x3: not x2,
]
)
BooleanNetwork(3).UseSequentialScheme([1, 2, 3]).SetFunctions(functions)
)
global currentFunction
funcStrings = ["not x3", "not x1", "not x2"]
currentFunction = partial(BooleanNetworkMenu, bn, funcStrings)
currentFunction = partial(BooleanNetworkMenu, bn, functions, funcStrings)
def ReturnHelper() -> None:
global currentFunction
@@ -148,22 +137,23 @@ def MainMenu() -> None:
def BooleanNetworkMenu(
bn: Optional[BooleanNetwork] = None, funcStrings: Optional[list[str]] = None
bn: Optional[BooleanNetwork] = None,
funcs: Optional[list[Callable[Concatenate[bool, ...], bool]]] = None,
funcStrings: Optional[list[str]] = None,
) -> None:
global currentFunction
boolNetwork: BooleanNetwork = BooleanNetwork(3)
verbose: bool = False
writeToFile: bool = False
done: bool = False
functions: list = [None for _ in range(3)]
functionStrings: list = [None for _ in range(3)]
functionsDirtyFlag = False
current_highlight = 0
if bn is not None and funcStrings is not None:
if bn is not None and funcStrings is not None and funcs is not None:
boolNetwork = bn
verbose = False
functions = [None for _ in range(3)]
functions = funcs
functionStrings = funcStrings
def Menu() -> None:
@@ -178,10 +168,11 @@ def BooleanNetworkMenu(
for i in range(1, boolNetwork.size + 1)
],
None,
f"Toggle verbose update: (current: {verbose})",
"Update once",
"Update once (hold ENTER for continuous updates)",
"Update multiple times",
"Get stationairy distribution",
f"Write to file (works only with multi-update) (current: {writeToFile})",
"Get Markov-Chain matrix (resets timeStep and state)",
"Get stationairy distribution (resets timeStep and state)",
None,
"Return to main menu",
]
@@ -192,14 +183,19 @@ def BooleanNetworkMenu(
SetUpdateSchemeHelper,
*[partial(SetFunctionHelper, i) for i in range(boolNetwork.size)],
None,
ToggleVerboseHelper,
UpdateHelper,
MultiUpdateHelper,
ToggleWriteToFileHelper,
GetMarkovChainHelper,
GetStableDistrHelper,
None,
ReturnHelper,
]
if boolNetwork.updateScheme in ("synchronous", "sequential", None):
options.pop(-3)
actions.pop(-3)
selectedIndex = ShowMenu(
options=options, title=title, highlight_entry=current_highlight
)
@@ -235,7 +231,6 @@ def BooleanNetworkMenu(
return
try:
boolNetwork.SetState(state)
return
except AssertionError as e:
print("Invalid input:", e)
@@ -310,7 +305,7 @@ def BooleanNetworkMenu(
print("Error while simulating once:", e)
def MultiUpdateHelper() -> None:
nonlocal boolNetwork, verbose, functions, functionsDirtyFlag
nonlocal boolNetwork, writeToFile, functions, functionsDirtyFlag
if functionsDirtyFlag:
try:
boolNetwork.SetFunctions(functions)
@@ -335,17 +330,23 @@ def BooleanNetworkMenu(
except ValueError:
print("Invalid input")
try:
boolNetwork.Update(n, verbose=verbose)
if verbose:
Continue()
print("\n")
boolNetwork.Update(
n, verbose=not writeToFile or n < 1000, writeToFile=writeToFile
)
Continue("Writing to file completed") if writeToFile else Continue()
except Exception as e:
print("Error while simulating:", e)
def GetMarkovChainHelper() -> None:
nonlocal boolNetwork
print(boolNetwork.GetTransitionMatrix())
Continue()
boolNetwork.SetState([False for _ in range(boolNetwork.size)])
def GetStableDistrHelper() -> None:
print(boolNetwork.GetStableProbabilityDistribution())
Continue()
print("\n")
boolNetwork.SetState([False for _ in range(boolNetwork.size)])
def SetFunctionHelper(index: int) -> None:
nonlocal boolNetwork, functions, functionStrings, functionsDirtyFlag
@@ -374,9 +375,9 @@ def BooleanNetworkMenu(
except Exception as e:
print("Error while parsing function:", e)
def ToggleVerboseHelper():
nonlocal verbose
verbose = not verbose
def ToggleWriteToFileHelper():
nonlocal writeToFile
writeToFile = not writeToFile
def ReturnHelper():
nonlocal done
@@ -391,7 +392,7 @@ def BooleanNetworkMenu(
def main() -> None:
global currentFunction
currentFunction = MainMenu
np.set_printoptions(precision=4, linewidth=300, sign=" ")
while True:
currentFunction()