-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleakage_simple.py
1159 lines (844 loc) · 29.4 KB
/
leakage_simple.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding: utf-8
# In[1]:
import sys
import getopt
import os
#from sets import Set
import json
import pprint
import itertools
from graphviz import Digraph
from math import log
import src.searchtree_v2 as st
import src.utils as utils
import csv
from pathlib import Path
# In[2]:
#agent is the agent we are trying to compromise. We are the adversary. All other agents are the adversary.
agent=0
domain="uav-factored"#"logistics-small-factored"#"uav-factored"#"logistics-small-factored"#"uav-factored"#"logistics-small-factored"#"logistics-small-factored" #
problem="example"#"test"#"example-big"#"example-big" #"test"#"test"#"probLOGISTICS-4-0"
st.configuration["privateActions"]=False
st.configuration["nTo1Mapping"]=1
st.configuration["useStateIDs"]=False
st.configuration["SecureMAFS"]=False
st.configuration["projectedHeuristic"]=False
st.configuration["debug"]=False
st.configuration["console"]=True
if st.configuration["console"]:
#read options
params = ["domain=","problem=","agent=","n-to-1-mapping=","secure-mafs","debug","proj"]
#print(str(sys.argv))
try:
opts, args = getopt.getopt(sys.argv[1:],'',params)
#print("opts:"+str(opts))
#print("args:"+str(args))
except getopt.GetoptError:
print ('bad leakage.py params: ' + str(params))
sys.exit(2)
for opt, arg in opts:
print("opt="+str(opt)+",arg="+str(arg))
if opt == "--domain":
domain = arg
elif opt == "--problem":
problem = arg
elif opt == "--agent":
agent = int(arg)
elif opt == "--n-to-1-mapping":
st.configuration["nTo1Mapping"] = int(arg)
elif opt == "--secure-mafs":
st.configuration["SecureMAFS"]=True
elif opt == "--debug":
st.configuration["debug"]=True
elif opt == "--proj":
st.configuration["projectedHeuristic"]=True
st.configuration["useStateIDs"] = False
if st.configuration["nTo1Mapping"] >= 1000:
st.configuration["useStateIDs"] = True
root="traces/"+domain+"/"+problem+"/"+str(st.configuration["nTo1Mapping"])
if st.configuration["projectedHeuristic"]:
root=root+"-proj"
agentFile=root+"/agent"+str(agent)+".json"
#adversaryFile=root+"/agent"+str(adversary)+".json"
if st.configuration["SecureMAFS"]:
outputFile=root+"-SecureMAFS"
print(root)
outputFile=root+"/agent"+str(agent)
outputCSVFile = "./results_simple.csv"
# In[3]:
#load data
varMap = {}
stateMap = {}
#states = {}
opMap = {}
operators = set()
advers = set()
states = []
plan = []
for fileName in os.listdir(root):
agentID = -1
if fileName.find("agent")!= -1 and fileName.find(".json")!= -1:
#print("next: "+fileName[fileName.find("agent")+5:fileName.find(".json")])
agentID=int(fileName[fileName.find("agent")+5:fileName.find(".json")])
#print(agentID)
if agentID != -1 and agentID != agent:
print("processing " + fileName)
advers.add(agentID)
f = open(root+"/"+fileName)
data = json.load(f)
f.close()
#load variables
for v in data["variables"]:
#print(v)
var = st.Variable(v)
varMap[var.hash] = var
if st.configuration["debug"]:
print("variables:")
pprint.pprint(varMap)
#load states
order = 0
secureMAFSStates = set()
for s in data["states"]:
state = st.State(s,varMap,order)
#ignore states not sent by Secure-MAFS
#TODO: verify correctness
if st.configuration["SecureMAFS"] and state.senderID == agent:
if state.getSecureMAFSSignature() in secureMAFSStates:
continue
else:
secureMAFSStates.add(state.getSecureMAFSSignature())
if not state.hash in stateMap:
stateMap[state.hash] = state
else:
print("WARNING: " + str(state) + " already in stateMap: " + str(stateMap[state.hash]))
#states[agentID].append(state)
order += 1
states.append(state)
#states = [s for s in stateMap.values()]
#load operators (and convert to label non-preserving projection)
allOps = filter(lambda x: x["ownerID"]==agent,data["operators"])
for op in allOps:
#print(op)
operator = st.Operator(op)
if operator.hash in opMap:
opMap[operator.hash].process(operator)
else:
opMap[operator.hash] = operator
operators = operators | set(opMap.values())
plan = data["plan"]
received = list(filter(lambda x: x.isReceivedFromAgent(agent) and x.iparentID != -1, states))
sent = list(filter(lambda x: x.isSent() or x.isInit(), states))
print("done!")
print("variables:" + str(len(varMap)))
print("operators:" + str(len(operators)))
print("states:" + str(len(states)))
print("received:" + str(len(received)))
print("sent:" + str(len(sent)))
if st.configuration["debug"]:
print("varMap:")
pprint.pprint(varMap)
if len(states) < 25:
print("stateMap:")
pprint.pprint(stateMap)
print("states:")
pprint.pprint(states)
if len(received) < 25:
print("received:")
pprint.pprint(received)
if len(sent) < 25:
print("sent:")
pprint.pprint(sent)
print("opMap:")
pprint.pprint(opMap)
print("operators:")
pprint.pprint(operators)
print("plan:")
pprint.pprint(plan)
# In[4]:
#load agent data (instead of estimates)
f = open(agentFile)
agentData = json.load(f)
f.close()
agentPrivateVariables = 0
agentDomainSize = 0
for var in agentData["variables"]:
if var["isPrivate"]:
agentPrivateVariables += 1
agentDomainSize = max(agentDomainSize,len(var["vals"]))
for op in agentData["operators"]:
if op["isPrivate"]:
st.configuration["privateActions"]=True
break
print("agent private variables: " + str(agentPrivateVariables))
print("agent domain size: " + str(agentDomainSize))
print("private actions: " + str(st.configuration["privateActions"]))
# In[5]:
#find actions responsible for the i-parent transitions
#compute maximum g received from adversary
#only states with secondMaxG can be considered
maxg = 0
for state in received:
iparentHash = state.getIParentHash()
if iparentHash in stateMap:
iparent = stateMap[iparentHash]
for op in operators:
state.isIParent(op,iparent,agent)
else:
print("WARNING!: i-parent state " + iparentHash + " not found!")
print(state)
if st.configuration["debug"]:
print(iparentHash+" -> " + state.hash + ": " + str(state.getIParentTransition()))
if state.cost>=maxg:
maxg = state.cost
secmaxg = 0
for state in received:
if state.cost>=secmaxg and state.cost<maxg:
secmaxg=state.cost
print("maxg="+str(maxg))
print("second maxg="+str(secmaxg))
st.configuration["maxG"]=secmaxg
# In[6]:
#pebd states
def findPEBDStates(s1,states):
pebd = set()
for s2 in states:
#publicly equivalent?
if s1.publicValues == s2.publicValues:
#distinct?
if s1.isDistinct(s2,agent):
pebd.add(s2.hash)
return pebd
def isPEBD(s1,s2):
#publicly equivalent?
if s1.publicValues == s2.publicValues:
#distinct?
if s1.isDistinct(s2,agent):
return True
return False
def isPEBDHash(s1h,s2h):
if s1h in stateMap and s2h in stateMap:
s1 = stateMap[s1h]
s2 = stateMap[s2h]
#publicly equivalent?
if s1.publicValues == s2.publicValues:
#distinct?
if s1.isDistinct(s2,agent):
return True
return False
# In[7]:
if st.configuration["debug"] and len(states) < 25:
print("publicly equivalent but distinct states:")
for s in received:
pebd = findPEBDStates(s,received)
print(str(s.hash)+": "+str(pebd))
# In[8]:
#show search tree in graphviz
if st.configuration["debug"] and len(states) < 50:
from graphviz import Digraph
def getNodeID(state):
return str(state.agentID)+str(state.stateID)
dot = Digraph(comment=root,engine="dot")
with dot.subgraph(name='agents') as dotA:
dotA.node("agent" + str(agent),"agent" + str(agent))
with dot.subgraph(name='states') as dotS:
#dotS.attr(rankdir='LR')
#dotS.attr(rank='same')
dotS.attr(ordering='out')
x = 10
y = 10
for state in states:
label = state.printStateDotLabel()
position = str(x)+","+str(y)+"!"
#add state, special case for initial state
id = getNodeID(state)
if state.isInit():
dotS.node(id, label, shape='invhouse',pos=position)
elif state.isGoal():
dotS.node(id, label, shape='house', style='bold',pos=position)
else:
if state.isReceivedFromAgent(agent):
dotS.node(id, label, shape='box',color='red',pos=position)
else:
dotS.node(id, label, shape='box',pos=position)
y += 1
x = 8+(x+1)%4
prev = -1
done = set()
for state in states:
#add received edge if not initial state
if state.isReceived() or state.isGoal():
if state.senderID==agent:
dotS.edge("agent"+str(state.senderID), getNodeID(state),color='red',constraint='false')
if state.senderID==-1:
#dotS.edge(str(state["stateID"]),"agent"+str(adversary))
dotS.edge(str(state.agentID)+str(state.parentID),getNodeID(state),style='bold',color='grey',constraint='false')
#add i-parent edge if received from the agent
if state.isReceivedFromAgent(agent):
iparent = stateMap[state.getIParentHash()]
label="\n".join(str(op) for op in state.getIParentTransition())
dotS.edge(getNodeID(iparent), getNodeID(state),style='dashed',label=label,constraint='false')
#invisible link from previous state
if prev != -1 and prev.agentID == state.agentID:
dotS.edge(getNodeID(prev), getNodeID(state),style='invis')
prev=state
#publicly equivalent but distinct edges
if state.isReceivedFromAgent(agent):
pebd = findPEBDStates(state,received)
for ps in pebd:
fromS = getNodeID(state)
toS = ps.replace(":","")
code = str({fromS,toS})
if not code in done:
dotS.edge(fromS, toS,color='orange',style='dashed',dir='none',constraint='false')
done.add(code)
#print(done)
#print variables
for var in varMap:
print(str(var)+" (private=" + str(varMap[var].isPrivate) + "):")
for val in varMap[var].vals:
print( " " + val + " = "+ varMap[var].vals[val])
#print(dot.source)
print(secureMAFSStates)
dot.render(outputFile)
#dot
# In[9]:
#debug
if st.configuration["debug"]:
for op in operators:
print(op.representative + ":"+str(op.applicableInIParent))
# In[10]:
#find all direct child states of init to determine init applicability
initStates = set()
for adv in advers:
for s in list(filter(lambda x: x.agentID == adv, states)):
if s.senderID != -1:
break
initStates.add(s.hash)
print("initial states:")
print(initStates)
# In[11]:
#debug print
if st.configuration["debug"]:
#pprint.pprint(transitions)
# pprint.pprint(pe)
for op in operators:
print(str(op)+": "+ str(op.transitions))
# In[12]:
#determine RHS based on combinations of properties
#check cache:
cacheCSVFile = "ts_cache.csv"
outCSV = Path(cacheCSVFile)
exists = outCSV.is_file()
found = False
if exists:
with open("ts_cache.csv") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['agentDomainSize']==str(agentDomainSize) and row['agentPrivateVariables']==str(agentPrivateVariables):
print("Reading from cache file!")
print(row)
found = True
rhs_t = float(row['ts'])
rhs_ia = float(row['ia'])
rhs_nia = float(row['nia'])
rhs_pi = float(row['pi'])
rhs_pd = float(row['pd'])
rhs_pn = float(row['pn'])
rhs_ia_pi = float(row['ia_pi'])
rhs_ia_pd = float(row['ia_pd'])
rhs_ia_pn = float(row['ia_pn'])
rhs_nia_pi = float(row['nia_pi'])
rhs_nia_pd = float(row['nia_pd'])
rhs_nia_pn = float(row['nia_pn'])
rhs_pi_pn = float(row['pi_pn'])
rhs_pd_pn = float(row['pd_pn'])
rhs_ia_pi_pn = float(row['ia_pi_pn'])
rhs_ia_pd_pn = float(row['ia_pd_pn'])
rhs_nia_pi_pn = float(row['nia_pi_pn'])
rhs_nia_pd_pn = float(row['nia_pd_pn'])
if not found:
print("Extrapolate:")
print("ts")
ts = (2**(agentDomainSize**2) - 1)**agentPrivateVariables
print(ts)
rhs_t = log(ts,2)
rhs_ia = rhs_t * 0.9623365224
rhs_nia = rhs_t * 0.7608990865
rhs_pi = rhs_t * 0.9079967542
rhs_pd = rhs_t * 1
rhs_pn = rhs_t * 0.9079967542
rhs_ia_pi = rhs_t * 0.9079967542
rhs_ia_pd = rhs_t * 0.9623365224
rhs_ia_pn = rhs_t * 0.8873803107
rhs_nia_pi = rhs_t * 0.7416588191
rhs_nia_pd = rhs_t * 0.7608990865
rhs_nia_pn = rhs_t * 0.6776340665
rhs_pi_pn = rhs_t * 0.8590151103
rhs_pd_pn = rhs_t * 0.9079967542
rhs_ia_pi_pn = rhs_t * 0.8590151103
rhs_ia_pd_pn = rhs_t * 0.8873803107
rhs_nia_pi_pn = rhs_t * 0.607375089
rhs_nia_pd_pn = rhs_t * 0.6776340665
# In[13]:
#initialize LP
LP = st.LP({})
#basic operators
for op in operators:
LP.addDisjunctiveConstraint([op.hash],rhs_t)
#only for statistics and correctness test, actions for which we know the property holds
ia = set()
nia = set()
pd = set()
pi = set()
pn = set()
# In[14]:
#load and process plan
print(plan)
apr_ia = "none"
opNames = [op.opNames for op in operators]
if st.configuration["debug"]:
print(opNames)
for opName in plan:
print(opName)
for op in operators:
if opName in op.opNames:
apr_ia = op
print("apr_ia = " + opName)
break
if apr_ia != "none":
break
if st.configuration["debug"]:
print( str(apr_ia) + " is init-applicable because it is the first action of agent "+str(agent)+" in the plan")
if apr_ia != "none":
ia.add(apr_ia.hash) #also learned from the plan in the post
LP.addDisjunctiveConstraint([apr_ia.hash],rhs_ia)
print ("apriori ia: " + str(apr_ia))
print(ia)
# In[15]:
#init applicable operators
print(initStates)
print(ia)
for s in received:
# print(str(s)+" - " + s.getIParentHash())
if s.getIParentHash() in initStates:
if st.configuration["debug"]:
print( "one of " + str(s.getIParentTransition()) + " is init-applicable because " + s.getIParentHash() + " is an initial state")
if len(s.getIParentTransition()) > 0:
LP.addDisjunctiveConstraint(s.getIParentTransition(),rhs_ia)
for opHash in s.getIParentTransition():
ia.add(opHash)
#init-unapplicable operators
for op in operators:
if not op.hash in ia:
nia.add(op.hash)
LP.addDisjunctiveConstraint(s.getIParentTransition(),rhs_nia)
print(ia)
# In[16]:
#privately independent operators
for op in operators:
# print(op)
for pair in itertools.combinations(op.transitions,2):
s1 = pair[0]
s2 = pair[1]
s1ip = stateMap[s1].getIParentHash()
s2ip = stateMap[s2].getIParentHash()
if stateMap[s1].isInit() and stateMap[s2].isInit():
continue
if isPEBDHash(s1ip,s2ip):
ops = stateMap[s1].getIParentTransition() & stateMap[s2].getIParentTransition()
if st.configuration["debug"]:
print( "one of " + str(ops) + " is privately-independent because " + str(s1ip) + " is PEBD to " + str(s2ip))
ops_ia = ops & ia
ops_nia = ops & nia
ops_unknown = ops - ops_ia - ops_nia
LP.addDisjunctiveConstraint3(ops_ia,rhs_ia_pi,ops_nia,rhs_nia_pi,ops_unknown,rhs_pi)
if len(ops) == 1:
pi.update(ops)
# In[17]:
# #privately dependent operators
# #if there are publicly equivalent (not necessarily distinct) states s1,s2 s.t. op is applied in one and not in the other, op is pd in at least one variable
# #TODO: this does not capture the situation where we know that one of the pebd iparents is the real iparent and the other is not and thus the op is pd
# #TODO: test!
# for op in operators:
# # print("check " + op.hash)
# for s1 in op.transitions:
# s1ip = stateMap[s1].getIParentHash()
# for s2ip in pebd[s1ip]:
# for s2 in stateMap[s2ip].successors:
# if not op.hash in stateMap[s2].getIParentTransition():
# ops = stateMap[s1].getIParentTransition() - stateMap[s2].getIParentTransition()
# if st.configuration["debug"]:
# print( "some of " + str(ops) + " is privately-dependent because it is applicable in " + str(s1ip) + " but not in " + str(s2ip) + " which are PEBD")
# ops_ia = ops & ia
# ops_nia = ops & nia
# ops_unknown = ops - ops_ia - ops_nia
# LP.addDisjunctiveConstraint3(ops_ia,rhs_ia_pd,ops_nia,rhs_nia_pd,ops_unknown,rhs_pd)
# if len(ops) == 1:
# pd.update(ops)
# In[18]:
#privately nondeterministic operators
for op in operators:
# print(op)
for pair in itertools.combinations(op.transitions,2):
s1 = pair[0]
s2 = pair[1]
if isPEBDHash(s1,s2):
s1ip = stateMap[s1].getIParentHash()
s2ip = stateMap[s2].getIParentHash()
if s1ip != s2ip:
continue
ops = stateMap[s1].getIParentTransition() & stateMap[s2].getIParentTransition()
if st.configuration["debug"]:
print( "one of " + str(ops) + " is privately-nondeterministic because " + str(s1) + " is PEBD to " + str(s2))
ops_ia = ops & ia
ops_nia = ops & nia
ops_unknown = ops - ops_ia - ops_nia
LP.addDisjunctiveConstraint3(ops_ia,rhs_ia_pn,ops_nia,rhs_nia_pn,ops_unknown,rhs_pn)
if len(ops) == 1:
pn.update(ops)
# In[19]:
#combinations
ops_pi_pn = pi & pn
LP.addDisjunctiveConstraint(ops_pi_pn,rhs_pi_pn)
ops_pd_pn = pd & pn
LP.addDisjunctiveConstraint(ops_pd_pn,rhs_pd_pn)
ops_ia_pi_pn = ia & pi & pn
LP.addDisjunctiveConstraint(ops_ia_pi_pn,rhs_ia_pi_pn)
ops_ia_pd_pn = ia & pd & pn
LP.addDisjunctiveConstraint(ops_ia_pd_pn,rhs_ia_pd_pn)
ops_nia_pi_pn = nia & pi & pn
LP.addDisjunctiveConstraint(ops_nia_pi_pn,rhs_nia_pi_pn)
ops_nia_pd_pn = nia & pd & pn
LP.addDisjunctiveConstraint(ops_nia_pd_pn,rhs_nia_pd_pn)
# In[20]:
#solve the LP
import pulp
model = pulp.LpProblem("Leakage", pulp.LpMaximize)
M = 1000
for var in LP.varMap:
LP.addLPVar(var,pulp.LpVariable(str(var), lowBound=0, cat='Continuous'))
model += (
pulp.lpSum([LP.lpVarMap[v] for v in LP.lpVarMap])
)
countC = 0
for dc in LP.constraints:
if len(dc.constraints) > 1:
binCs = []
countB = 0
for c in dc.constraints:
binC = pulp.LpVariable("c"+str(countC)+"b"+str(countB), cat='Binary')
binCs.append(binC)
countB += 1
model += (
pulp.lpSum([coef * LP.lpVarMap[var] for coef in c.coefs for var in c.vars]) <= c.RS + M - M * binC
)
model += (
pulp.lpSum(binCs) == 1
)
elif len(dc.constraints) == 1:
model += (
pulp.lpSum([coef * LP.lpVarMap[var] for coef in dc.constraints[0].coefs for var in dc.constraints[0].vars]) <= dc.constraints[0].RS
)
countC += 1
if st.configuration["debug"]:
print(LP.varMap)
print(model)
model.solve()
pulp.LpStatus[model.status]
if st.configuration["debug"]:
for variable in model.variables():
print(str(variable.name) + " = " + str(variable.varValue))
print ("objective value: " + str(pulp.value(model.objective)))
post = pulp.value(model.objective)
# In[21]:
#compute the leakage
print("apriori:")
apr = 0
for op in operators:
if op == apr_ia:
print(str(op) + " = " + str(rhs_ia))
apr += rhs_ia
else:
print(str(op) + " = " + str(rhs_t))
apr += rhs_t
leakage = apr - post
print(str(apr)+" - "+str(post)+" = " + str(leakage))
# In[22]:
#determine the ground truth leakage
#let's do this more efficiently! We can just evaluate the true model - no formula and logic inference
# gt_formula = True
gt_ia = set()
gt_pi = set()
gt_pd = set()
gt_pn = set()
if apr_ia != "none":
gt_ia.add(apr_ia.hash) #the op applicable acoording to the plan
def getOpPubHash(op,pubVars):
pubPre = {}
pubEff = {}
for v in pubVars:
if v in op.pre:
pubPre[v] = op.pre[v]
if v in op.eff:
pubEff[v] = op.eff[v]
return str(pubPre)+"->"+str(pubEff)
#determine private vars
privateVars = set()
allPrivVarVals = {}
publicVars = set()
for varData in agentData["variables"]:
if varData["isPrivate"]:
privateVars.add(varData["varID"])
allPrivVarVals[str(varData["varID"])] = set()
for val in varData["vals"]:
allPrivVarVals[str(varData["varID"])].add(str(varData["varID"])+":"+str(val))
else:
publicVars.add(varData["varID"])
#map ops to their private effects
gtOpMap = {}
gtOps = set()
for opData in agentData["operators"]:
if opData["ownerID"] != agent or opData["isPrivate"]:
continue
#print(str(opData))
op = st.Operator(opData)
#print(str(op))
gtOps.add(getOpPubHash(op,publicVars))
#init applicable?
for stateData in agentData["states"]:
if stateData["context"] != "init" and stateData["senderID"]==-1:
break
applicable = True
for var in op.pre:
if stateData["values"][var] != op.pre[var]:
applicable = False
break
if applicable:
gt_ia.add(getOpPubHash(op,publicVars))
# gt_formula = gt_formula & opInitApplicableInState(getOpPubHash(op,publicVars))
break
#prepare for pi,pd,pn detection
opph = getOpPubHash(op,publicVars)
if not opph in gtOpMap:
gtOpMap[opph] = {}
gtOpMap[opph]["allPrivateEffSets"] = set()
gtOpMap[opph]["allPrivatePre"] = set()
gtOpMap[opph]["maxPrivatePreVars"] = 0
allPrivateEff = set()
for v in op.eff:
if v in privateVars:
allPrivateEff.add(str(v)+":"+str(op.eff[v]))
gtOpMap[opph]["allPrivateEffSets"].add(str(allPrivateEff)) #for pn #WARNING: relies on the uniqueness of string representation of sets!
privVars = 0
for v in op.pre:
if v in privateVars:
gtOpMap[opph]["allPrivatePre"].add(str(v)+":"+str(op.pre[v])) #for pd
privVars += 1 # for pi
gtOpMap[opph]["maxPrivatePreVars"] = max (gtOpMap[opph]["maxPrivatePreVars"],privVars)
if st.configuration["debug"]:
print("allPrivVarVals:")
# pprint.pprint(allPrivVarVals)
for opph in gtOpMap:
if st.configuration["debug"]:
print(str(opph)+":"+str(gtOpMap[opph]))
#privately nondeterministic?
if len(gtOpMap[opph]["allPrivateEffSets"]) > 1: #TODO: what if the private effects are the same? should be unique!
gt_pn.add(opph)
# gt_formula = gt_formula & opPrivatelyNondeterministic(opph,1)
#the most benevolent definition of private independency:
#privately independent?
if len(gtOpMap[opph]["allPrivatePre"]) > 1:#and gtOpMap[opph]["maxPrivatePreVars"] < len(allPrivVarVals.keys()):
#applicable in more than one private var-val
gt_pi.add(opph)
# gt_formula = gt_formula & opPrivatelyIndependent(opph,1)
for var in allPrivVarVals:
#privately dependent?
# print(str(opph) + " pd for "+str(var)+"? " + str(allPrivVarVals[var] & gtOpMap[opph]["allPrivatePre"]))
#if there is a private variable with at least one defined value in pre (the intersection is not empty)
#but not with all values defined
if allPrivVarVals[var] & gtOpMap[opph]["allPrivatePre"] and allPrivVarVals[var] - gtOpMap[opph]["allPrivatePre"]:
# print("yes")
gt_pd.add(opph)
# gt_formula = gt_formula & opPrivatelyDependent(opph,1)
# print(gt_formula)
# In[23]:
#compute ground truth leakage
#compute leakage
gt_nia = gtOps - gt_ia
gt_ia_pi = gt_ia & gt_pi
gt_ia_pd = gt_ia & gt_pd
gt_ia_pn = gt_ia & gt_pn
gt_nia_pi = gt_nia & gt_pi
gt_nia_pd = gt_nia & gt_pd
gt_nia_pn = gt_nia & gt_pn
gt_pi_pn = gt_pi & gt_pn
gt_pd_pn = gt_pd & gt_pn
gt_ia_pi_pn = gt_ia & gt_pi & gt_pn
gt_ia_pd_pn = gt_ia & gt_pd & gt_pn
gt_nia_pi_pn = gt_nia & gt_pi & gt_pn
gt_nia_pd_pn = gt_nia & gt_pd & gt_pn
print(gt_ia)
print(gt_nia)
gt_post = 0
print("gtOps:"+str(gtOps))
for op in gtOps:
t = rhs_t
if op in gt_ia:
t = min(t,rhs_ia)
if op in gt_nia:
t = min(t,rhs_nia)
if op in gt_pi:
t = min(t,rhs_pi)
if op in gt_pd:
t = min(t,rhs_pd)
if op in gt_pn:
t = min(t,rhs_pn)
if op in gt_ia_pi:
t = min(t,rhs_ia_pi)
if op in gt_ia_pd:
t = min(t,rhs_ia_pd)
if op in gt_ia_pn:
t = min(t,rhs_ia_pn)
if op in gt_pi_pn:
t = min(t,rhs_pi_pn)
if op in gt_pd_pn:
t = min(t,rhs_pd_pn)
if op in gt_nia_pi:
t = min(t,rhs_nia_pi)
if op in gt_nia_pd:
t = min(t,rhs_nia_pd)
if op in gt_nia_pn:
t = min(t,rhs_nia_pn)
if op in gt_ia_pi_pn:
t = min(t,rhs_ia_pi_pn)
if op in gt_ia_pd_pn:
t = min(t,rhs_ia_pd_pn)