Skip to content

Commit aa5a44e

Browse files
committed
Added semigroup to machine transformation
1 parent b20c3e8 commit aa5a44e

File tree

3 files changed

+111
-7
lines changed

3 files changed

+111
-7
lines changed

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,33 @@ You can plot, layout and export the automata with the `--dot` flag.
3737
python3 main.py --aut examples/lecture_1.csv -N 5 --dot test.png
3838
```
3939

40+
## Running the program on a given semigroup
41+
Currently you can only specify an semigroup in code.
42+
43+
```python
44+
states = ["a", "b"]
45+
semigroup = pd.DataFrame(
46+
{"0":["0", "1"],
47+
"1": ["1", "0"]}
48+
)
49+
semigroup.set_index(["0", "1"])
50+
51+
def action(s: State, sge: SemigroupElement) -> Optional[State]:
52+
if str(sge) == "0":
53+
return s
54+
elif str(sge) == "1":
55+
if s == "a":
56+
return "b"
57+
if s == "b":
58+
return "a"
59+
else:
60+
print(f"SemigroupElement {sge} with State {s}")
61+
raise Exception("Operation not defined")
62+
63+
res = semigroup_to_machine((states, semigroup, action))
64+
plot(res, "test")
65+
```
66+
4067

4168
## Automate format
4269
To sepcify an automate via csv use the following format

src/automate.py

+52-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import pandas as pd
22
from graphviz import Digraph
33
from itertools import product
4-
from typing import Dict, Tuple, List
4+
from typing import Callable, Dict, Tuple, List
55
from tabulate import tabulate
66

77
State = str
88
Letter = str
9+
SemigroupElement = str
910
StateMachine = Dict[Tuple[State, Letter], State]
1011

1112
EqvTable = pd.DataFrame
1213
Semigroup = pd.DataFrame
14+
Action = Callable[[State, SemigroupElement], State]
15+
TransformationSemigroup = (List[State], Semigroup, Action)
1316

1417

1518
def get_states(transitions):
@@ -154,9 +157,51 @@ def format_semitable(s: Semigroup):
154157
s.columns = [f"[{col}]" for col in s.columns]
155158
return tabulate(s, headers="keys", tablefmt="grid")
156159

157-
#res2 = create_table(l1, ['a','b','c'], N = 5)
158-
#u, class_, _ = add_representatives(l1, res2)
159-
#
160-
##print(u)
161-
##
162-
#r = eqv_class_to_semigroup(l1,['a','b','c'], u)
160+
161+
162+
def compatability(states: List[State], sg: Semigroup, a: Action, filter_sames = True):
163+
pass
164+
165+
def faithfullness(states: List[State], sg: Semigroup, a: Action, filter_sames = True):
166+
# TODO Think about this some more
167+
action_results = []
168+
for s in states:
169+
for g1 in sg.index:
170+
for g2 in sg.index:
171+
if g1 != g2:
172+
#q*g1
173+
res1 = a(s, g1)
174+
#q*g2
175+
res2 = a(s, g2)
176+
action_results.append([s, g1, res1 == res2, s, g2])
177+
178+
action_results = pd.DataFrame(action_results)
179+
action_results.columns = ['q', 'g1', 'is_same', 'q', 'q2']
180+
return action_results[action_results['is_same'] == True]
181+
182+
183+
184+
185+
def execute_semigroup(states: List[State], sg: Semigroup, a: Action, filter_sames = True):
186+
# TODO Think about this some more
187+
action_results = []
188+
for s in states:
189+
for g1 in sg.index:
190+
res = a(s, g1)
191+
action_results.append([s, g1, res])
192+
193+
action_results = pd.DataFrame(action_results)
194+
action_results.columns = ['q', 'g1', 'action_result']
195+
return action_results
196+
197+
198+
199+
def semigroup_to_machine(tsg: TransformationSemigroup):
200+
action_table = execute_semigroup(tsg[0], tsg[1], tsg[2])
201+
transformations = {}
202+
203+
for _, q1, l, q2 in action_table.itertuples():
204+
transformations[(q1, str(l))] = q2
205+
206+
return transformations
207+

tests/test.py

+32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
from src.automate import *
3+
from typing import Optional
34

45
import unittest
56

@@ -62,3 +63,34 @@ def test_two_state_machine_representative(self):
6263
r = create_table(TestAutomatonToState.one_state_two_trans, ['a', "b"], N = 3)
6364
_, t, _ = add_representatives(TestAutomatonToState.one_state_two_trans, r)
6465
self.assertEqual(list(t["eqv_class"]), ["a", "a", "b", "b", "ba", "ba"])
66+
67+
68+
69+
class TestSemigroupToAutomaton(unittest.TestCase):
70+
def semigroup_to_machine(self):
71+
states = ["a", "b"]
72+
semigroup = pd.DataFrame(
73+
{"0":["0", "1"],
74+
"1": ["1", "0"]}
75+
)
76+
semigroup.set_index(["0", "1"])
77+
78+
def action(s: State, sge: SemigroupElement) -> Optional[State]:
79+
if str(sge) == "0":
80+
return s
81+
elif str(sge) == "1":
82+
if s == "a":
83+
return "b"
84+
if s == "b":
85+
return "a"
86+
else:
87+
print(f"SemigroupElement {sge} with State {s}")
88+
raise Exception("Operation not defined")
89+
90+
res = semigroup_to_machine((states, semigroup, action))
91+
print(res)
92+
plot(res, "test")
93+
94+
95+
96+

0 commit comments

Comments
 (0)