-
Notifications
You must be signed in to change notification settings - Fork 2
/
back_end_CS.py
189 lines (167 loc) · 4.51 KB
/
back_end_CS.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#Case Studies
from class_game import *
import ast
from visual_automata.fa.dfa import VisualDFA
from visual_automata.fa.nfa import VisualNFA
import numpy as np
### Initialization function
def Init(Mat_transition,Istate,Fstate):
#State init
state_i=[]
N=len(Mat_transition)
M=len(Mat_transition[0])
for nn in range(N):
state_i.append("q"+str(nn))
state_i=set(state_i)
#Name_Transitions
list_trans_i=list(dict.fromkeys(Mat_transition.flatten()))
trans_i=[]
for ti in list_trans_i:
if ti:
trans_i.append(str(ti))
trans_i=set(trans_i)
Istate="q"+str(Istate-1)
FS=[]
for kk in range(len(Fstate)):
FS.append("q"+str(Fstate[kk]-1))
if len(FS)==1:
Fstate={FS}
else:
Fstate=set(FS)
dict_trans={}
for id_state in range(N):
dict_state={}
for id_transition in range(M):
value=Mat_transition[id_state][id_transition]
if value:
out="q"+str(id_transition)
dict_state[str(value)]={out}
dict_trans["q"+str(id_state)]=dict_state
#return state_i,trans_i,dict_trans,Istate,Fstate
nfa=VisualNFA(
states=state_i,
input_symbols=trans_i,
transitions=dict_trans,
initial_state=Istate,
final_states=Fstate)
table=nfa.table
graph=nfa.show_diagram()
return graph,table,nfa
#### Dining Cryptographe
def dining_crypt(ab="Head",ac="Tail",bc="Head",who_paid=[0,0,0]):
dining=game(3,["A","B","C"])
ab=ab
ac=ac
bc=bc
list_value=[ac,ab,bc]
#mat=[[0,ab,ac],[ab,0,bc],[ac,bc,0]]
mat=[[0,ab,ac],[0,0,0],[0,bc,0]]
dining.make_transition(mat)
for id in range(len(list_value)):
if list_value[id]=="Head":
list_value[id]=1
else:
list_value[id]=0
list_XOR=[]
for kk in range(3):
if not(who_paid[kk]):
list_XOR.append(list_value[kk]^list_value[(kk+1)%3])
else:
list_XOR.append(not(list_value[kk]^list_value[(kk+1)%3]))
list_XOR_label=[]
for XOR in list_XOR:
if XOR:
list_XOR_label.append("True")
else:
list_XOR_label.append("False")
rez=list_XOR[0]^list_XOR[1]^list_XOR[2]
f=dining.display_diagram(list_XOR_label)
if rez==0:
if sum(who_paid)==0:
f.attr(label='Result:NSA paid. Good Prediction',frontcolor='red')
else:
f.attr(label='Result:NSA paid. Bad Prediction')
else:
if sum(who_paid)==0:
f.attr(label='Result: Someone paid. Bad Prediction')
else:
f.attr(label='Result: Someone paid. Good Prediction')
return f
### ICGS train
def iCGS_train(ActT1="r",ActT2="r",ActC="6",boolean_Red=True,cs1='a',cs2='a',cs3='o',cs4='o',cs5='e',cs6='e'):
#icgs: M=< Ag={t1,t2,c}, AP={p,b,d}, S={sI,s1,s2,s3,s4,s5,s6,s7?}, so=SI, Actt1={r,l,i}, Act2={r,l,s,i},Actc={1,2,3,4,5,6,a,e,o,i}> #We swich Agent and State
test=game(load_file=True,AW=True)
if ActC=='6' and ActT1=='r' and ActT2=='r':
if cs6=='e':
SP=3
elif cs6=='o':
SP=7
else:
SP=6
elif ActC=='2' and ActT1=='l' and ActT2=='l':
if cs2=='o':
SP=7
else:
SP=2
elif ActC=='3' and ActT1=='r' and ActT2=='l':
if cs3=='e':
SP=2
elif cs3=='o':
SP=7
else:
SP=3
elif ActC=='4' and ActT1=='r' and ActT2=='s':
if cs4=='a':
SP=0
elif cs4=='o':
SP=7
else:
SP=4
elif ActC=='5' and ActT1=='l' and ActT2=='r':
if cs5=='a':
SP=0
elif cs5=='o':
SP=7
else:
SP=5
elif ActC=='1' and ActT1=='l' and ActT2=='s':
if cs1=='a':
SP=0
elif cs1=='o':
SP=7
else:
SP=1
else:
SP=0
if boolean_Red:
graph=test.display_diagram(Special_node=SP)
else:
graph=test.display_diagram()
return graph
### Example Graph
def example_strategy(h1,h2):
game_strategy=game(load_file=True,path1='example_strategy.txt',AW=True)
if h1[0]=='A' and h2[0]!='0':
if h2[1]=='D':
if (h1[2]=='A' and h2[2]=='D') or (h1[2]=='B' and h2[2]=='C'):
SP=3
else:
SP=2
elif h2[1]=='C':
SP=3
else:
SP=1
elif h1[0]=='B' and h2[0]!='0':
if (h1[2]=='A' and h2[2]=='D') or (h1[2]=='B' and h2[2]=='C'):
SP=3
else:
SP=2
else:
SP=0
graph=game_strategy.display_diagram(Special_node=SP)
return graph
def display_graph_MS(Mat_transition,List_name_agent,List_name_stat):
test=game(len(List_name_stat),List_name_stat,AW=True)
test.make_transition(Mat_transition)
test=test.display_diagram()
return test