-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsimulation.py
3006 lines (2513 loc) · 151 KB
/
simulation.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
#
# NAT protocol simulation
#
#
import os
import sys
import fileinput
import re
import random
import math
from operator import itemgetter, attrgetter
import subprocess
from optparse import OptionParser
import copy
from scipy.stats import binom
from scipy.stats import nbinom
from scipy.stats import norm
from scipy.stats import poisson
from scipy.stats import chisquare
import numpy as np
import matplotlib.pyplot as plt
import time
import argparse
from dateutil import parser as dparser
import calendar
import bisect
import heapq
from heapq import heappush, heappop
import resource
import gc
# Multiple plots
from mpl_toolkits.axes_grid1 import host_subplot
import mpl_toolkits.axisartist as AA
import pylab as P
# MLE distribution fitting with RPy - python binding for R
from rpy2.robjects import r
from rpy2.robjects import IntVector
from rpy2.robjects.packages import importr
# Load the MASS library for distribution fitting
# See more at: http://thomas-cokelaer.info/blog/2011/08/fitting-distribution-by-combing-r-and-python/#sthash.TiVb9HpI.dpuf
MASS = importr('MASS')
def coe(x):
return 1.0 / (0.163321 * math.log(64.2568 * x))
def charproc(i, m):
return chr( int(ord('a') + ( (float(i)/m) * ((ord('z')-ord('a'))) )) )
# Fibonacchi list generator
def fibGenerator():
'''
Fibonacci sequence generator
'''
a, b = 0, 1
yield 0
while True:
a, b = b, a + b
yield a
def poissonProcGenerator(lmbd):
'''
Generates samples from Poisson NAT (inc +1) process generator
'''
x = np.random.poisson(lmbd)
yield x
while True:
x += 1 + np.random.poisson(lmbd)
yield x
def f7(seq):
'''
Removes duplicates from list while keeping the order
'''
seen = set()
seen_add = seen.add
return [ x for x in seq if x not in seen and not seen_add(x)]
def probRound(x):
'''
Probabilistic rounding of a number. If x=1.2 with 80% it will be rounded to 1, 20% to 2
'''
flr = math.floor(x)
cel = math.ceil(x)
if flr == cel: return x
if random.random() <= (x-flr):
return int(cel)
else:
return int(flr)
def hashcode(s):
'''
Simple hashcode implementation for strings and integers.
'''
h = 0
if isinstance(s, ( int, long ) ): return s
for c in s:
h = (31 * h + ord(c)) & 0xFFFFFFFF
return ((h + 0x80000000) & 0xFFFFFFFF) - 0x80000000
def getMem():
'''
Returns current memory consumption in MB (float)
'''
return (resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1000.0)
def graph(plt, x='Sample', y='Port number', loc=1):
if loc!=-1:
plt.legend(loc=loc)
plt.xlabel('Sample')
plt.ylabel('Port number') #,rotation='horizontal')
plt.grid(True)
plt.show()
plt.close()
class Strategy(object):
'''
Base abstract class for NAT strategy
'''
def init(self, params=None):
raise Exception("Not implemented yet...")
def reset(self, nats=[], sim=None, params=None):
self.init(None)
def next(self, party, step):
raise Exception("Not implemented yet...") # return (srcPort, dstPort)
def silent(self, time1, time2, lmbd):
'''
Tells to strategy how long did silent period take just before start and guessed lambda.
'''
pass
class Nat(object):
'''
Base abstract class for NAT allocation
'''
# timeout of a created connection in milliseconds
timeout = 3*60*1000
# port pool available for new allocations
pool = None
poolLen = 0
def init(self, params=None):
raise Exception("Not implemented yet...")
def reset(self):
raise Exception("Not implemented yet...")
def alloc(self, srcIP, srcPort, dstIP, dstPort, timeNow, timeAdd=None, refreshOnly=False):
raise Exception("Not implemented yet...")
def occupy(self, num, timeNow):
raise Exception("Not implemented yet...")
def freePorts(self):
raise Exception("Not implemented yet...")
def peekNext(self):
raise Exception("Not implemented yet...")
class Quartet: #(object):
'''
SrcIP, srcPort, DstIP, DstPort.
Was new-style class [http://docs.python.org/2/reference/datamodel.html#newstyle] - inheriting from object,
but the execution was slower since it is uses very often.
'''
srcIP=0
srcPort=0
dstIP=0
dstPort=0
def __init__(self, srcIP, srcPort, dstIP, dstPort):
self.srcIP = srcIP
self.srcPort = srcPort
self.dstIP = dstIP
self.dstPort = dstPort
def __cmp__(self, other):
if other!=None \
and self.srcIP == other.srcIP \
and self.srcPort == other.srcPort \
and self.dstIP == other.dstIP \
and self.dstPort == other.dstPort: return 0
else: return 1
def __eq__(self, other):
return self.__cmp__(other) == 0
def __ne__(self, other):
return self.__cmp__(other) != 0
def __str__(self):
return "%s:%05d --> %s:%05d" % (self.srcIP, self.srcPort, self.dstIP, self.dstPort)
def __hash__(self):
prime=31
result=1
result = prime * result + hashcode(self.srcIP)
result = prime * result + int(self.srcPort)
result = prime * result + hashcode(self.dstIP)
result = prime * result + int(self.dstPort)
return result
class SymmetricNat(Nat):
'''
Base class for symmetric NAT.
'''
# allocation table for NAT; key = quartet; value = external port
allocations = None
# port -> (quartet, expire time). Quartet may be null
allocatedPorts = None
# priority queue (heap), priority=expire time, tuple stored=(expire time, port, quartet)
expireHeap = None
# port is the key
# port -> quartet, expire
# quartet -> port
def init(self, params=None):
self.allocations = {}
self.pool = range(0, 65536)
self.poolLen = len(self.pool)
self.allocatedPorts = {}
self.expireHeap = []
def reset(self):
self.allocations = {}
self.allocatedPorts = {}
self.expireHeap = []
def nextPort(self):
'''
Uses port pool array and pointer to last allocated port to obtain next in the sequence.
In case of a random allocation, randomly generates index to a pool and returns a port on the index.
'''
raise Exception("Not implemented yet... This class is abstract, you have to override this method in a subclass")
def peekPort(self, prev=None):
raise Exception("Not implemented yet... This class is abstract, you have to override this method in a subclass")
def peekNext(self, timeNow):
return self.nextFreePort(timeNow, True)
def nextFreePort(self, timeNow, peek=False):
'''
Returns next free port in the sequence, takes existing associations into account and their expiration.
Can be used to determine internal state of NAT, call does not affect internal state - reentrant function call.
Runs in O(portNum) in worst case but in average it runs fast since to take it longer
there would have to be a long block of allocated non-expired connections - this is implemented
as it would be in routers with incremental/random allocation without more complex state structures.
'''
tries=0 # pool exhaustion check
port=-1
prev=None
while tries <= self.poolLen:
port,prev = self.peekPort(prev) # nextPort() # linear port allocation rule here
tries += 1 # check pool exhaustion
if port in self.allocatedPorts:
# next port is already allocated, what about timeout?
tup = self.allocatedPorts[port]
# check expiration first
if (tup[1] + self.timeout) < timeNow:
if (tup[0] != None):
del self.allocations[tup[0]] # expired -> delete from allocation table
del self.allocatedPorts[port] # delete from allocation set
break # slot is free now, can allocate
else: continue # slot is in use, continue with search
else: break # slot is free, assign
# check if pool is exhausted - all ports are allocated currently
if tries >= self.poolLen or port==-1:
print "Port pool exhausted"
raise Exception("Port pool exhausted")
# reflect to internal NAT state - move iterator
if peek==False:
for i in range(0,tries): self.nextPort()
# return resulting port, should not be -1
return port
def alloc(self, srcIP, srcPort, dstIP, dstPort, timeNow, timeAdd=None, refreshOnly=False):
'''
Basic allocation method for new connection
'''
q = Quartet(srcIP, srcPort, dstIP, dstPort)
if timeAdd == None: timeAdd = timeNow
# Check for existing allocation for a given quartet
if q in self.allocations:
port = self.allocations.get(q)
# check expiration time, if a record is too old, it has to be removed from allocation table.
tup = self.allocatedPorts.get(port)
if (tup[1] + self.timeout) < timeNow:
del self.allocatedPorts[port] # delete from allocation set
del self.allocations[q] # expired -> delete from allocation table
else:
self.allocatedPorts[port] = (q, timeAdd) # update last query access
return port # external port returned
# If here -> not in allocation list, create a new allocation
# New allocation is created only if desired
if refreshOnly: return -1
# Get next free port from port pool that is free for use
# time parameter passed will expire all existing connections
port=self.nextFreePort(timeNow)
# Create a new allocation
self.allocatedPorts[port] = (q, timeAdd)
self.allocations[q] = port
# Add to heap
heappush(self.expireHeap, (timeAdd, port, q))
# Timeout all entries - internal table cleaning with probability 1:100
if random.randint(0, 100) == 0:
self.cleanHeap(timeNow)
return port
def occupy(self, num, timeNow):
'''
Simulates another connections created randomly
'''
for i in range(0, num):
port = self.nextFreePort(timeNow)
self.allocatedPorts[port] = (None, timeNow)
heappush(self.expireHeap, (timeNow, port, None)) # Add to heap
return 1
def freePorts(self):
return (self.poolLen - len(self.allocatedPorts))
def cleanHeap(self, timeNow):
'''
Performs timeouting for all expired records - based on priority queue on access time
'''
while(len(self.expireHeap) > 0):
cur, port, q = self.expireHeap[0]
if (cur + self.timeout) >= timeNow: break # if minimal is not expired -> return
# Expired - does it exist in real allocation table?
if port in self.allocatedPorts \
and cur == self.allocatedPorts[port][1] \
and q == self.allocatedPorts[port][0]:
# Record exists and is expired, thus clean it
if (q != None):
del self.allocations[q] # expired -> delete from allocation table
del self.allocatedPorts[port] # delete from allocation set
# Remove element from queue
heappop(self.expireHeap)
#sys.stdout.write("X")
#sys.stdout.flush()
def trulyFreePorts(self, timeNow):
self.cleanHeap(timeNow)
return self.freePorts()
class SymmetricRandomNat(SymmetricNat):
'''
Symmetric NAT with random allocation function
'''
def nextPort(self):
'''
Randomly generates index to a pool and returns a port on the index.
'''
return self.pool[random.randint(0, self.poolLen-1)]
def peekPort(self, prev=None):
return (self.nextPort(), None)
class SymmetricIncrementalNat(SymmetricNat):
# index of last allocated port. Index to pool[]
lastPort = 0
def reset(self):
super(SymmetricIncrementalNat, self).reset()
self.lastPort = 0
def nextPort(self):
'''
Uses port pool array and pointer to last allocated port to obtain next in the sequence.
Modifies internal state. Acts like next() in iterators.
'''
self.lastPort = (self.lastPort + 1) % self.poolLen # linear port allocation rule here
return self.pool[self.lastPort] # just a shortcut
def peekPort(self, prev=None):
'''
Determines next free port without moving anything
'''
if prev==None: prev = self.lastPort
tmp = (prev + 1) % self.poolLen # linear port allocation rule here
return (self.pool[tmp], tmp) # just a shortcut
class TheirStragegy(Strategy):
'''
Strategy of changing source port - published by other team
'''
delta = [900,900]
startPos=[0,0]
def init(self, params=None):
pass
def silent(self, time1, time2, lmbd):
#self.delta = [max(100, int(time1*lmbd*3)), max(100, int(time2*lmbd*3))]
self.startPos = self.delta
return self.delta
def next(self, party, step):
if party==0: return (step,self.delta[0])
if party==1: return (step,self.delta[1])
class IJStragegy(Strategy):
startPos=[0,0]
b = []
def init(self, params=None):
pass
def reset(self, nats=[], sim=None, params=[]):
self.b = []
c = 0
for step in range(0, 1500):
self.b.append(c)
#c = NatSimulation.poisson(sim.lmbd, 2 * sim.portScanInterval * (step))
#c += int(2.0 * sim.lmbd * sim.portScanInterval)
c += 2
#c += random.randint(0, 2)
#print "step=",step, "smpl=", sim.lmbd * 2 * sim.portScanInterval * (step)
def silent(self, time1, time2, lmbd):
#self.startPos=[int(lmbd * time1), int(lmbd * time2)]
#self.startPos=[NatSimulation.poisson(lmbd, time1), NatSimulation.poisson(lmbd, time2)]
return self.startPos
def next(self, party, step):
if party==0: return (0, int(self.startPos[0]+self.b[step]))
if party==1: return (0, int(self.startPos[1]+self.b[step]))
class I2JStragegy(Strategy):
'''
Baby step, giant step strategy for low noise links. Works 100 % if lmbd*time is low.
'''
startPos=[0,0]
def init(self, params=None):
pass
def silent(self, time1, time2, lmbd):
# Sets different starting point for algorithm than zero. Takes silent period
# duration into account together with predicted workload to start algorithm
# on a right place. It there are too many errors in the prediction (new connections)
# small-step-big-step can have problems with cathing them up.
#
# Use expected value instead of a random sample as a starting point. E(X) = lmbd, X ~ Po(lmbd)
# should be the central.
#if lmbd >= 0.035:
c = 0
if lmbd >= 0.035: c=-10000.0*lmbd*lmbd + 950.0*lmbd - 21.0
if lmbd >= 0.05: c=2
self.startPos=[int(0 * lmbd * time1), int(0 * lmbd * time2)]
#self.startPos=[NatSimulation.poisson(lmbd, time1), NatSimulation.poisson(lmbd, time2)]
return self.startPos
def next(self, party, step):
# This random stepping used to work
#if party==0: return (0, int(self.startPos[0]+random.randint(1,3)*step )) #int(self.startPos[0]+step-150*(step/100)))
#if party==1: return (0, int(self.startPos[0]+random.randint(1,5)*step )) #int(self.startPos[1]+2*step-230*(step/100)))
if party==0: return (0, int(self.startPos[0]+step )) #int(self.startPos[0]+step-150*(step/100)))
if party==1: return (0, int(self.startPos[0]+2*step )) #int(self.startPos[1]+2*step-230*(step/100)))
class SimpleStrategy(Strategy):
'''
Taking E[X_i] as a strategic move in step i, X is a probabilistic distribution over state in step i.
If a process of new connections on NAT is Poisson process then the following formula holds (from simulation):
E[X] = 1 + lambda * time
'''
startPos=[0,0]
b = []
ln = 0
def init(self, params=None):
pass
def reset(self, nats=[], sim=None, params=[]):
self.b = []
for step in range(0, 1000):
x = step * (1 + sim.lmbd * sim.portScanInterval)
x = probRound(x) # probabilistic rounding
#x = round(x)
self.b.append(x)
self.ln = len(self.b)
def silent(self, time1, time2, lmbd):
self.startPos=[int(lmbd * time1), int(lmbd * time2)]
#self.startPos=[NatSimulation.poisson(lmbd, time1), NatSimulation.poisson(lmbd, time2)]
return self.startPos
def next(self, party, step):
if party==0: return (0, int(round(self.startPos[0]+self.b[min(step, self.ln-1)])))
if party==1: return (0, int(round(self.startPos[1]+self.b[min(step, self.ln-1)])))
class FiboStrategy(Strategy):
'''
Experimental Fibonacci strategy. Skip is variable, works well for some lambda & time.
'''
fibn = []
b = []
startPos=[0,0]
def init(self, params=None):
self.fibn = []
fib = fibGenerator()
for n in range(22):
self.fibn.append(next(fib))
for i in range(1, len(self.fibn)-1):
for j in range(0, self.fibn[i-1]):
#int(NatSimulation.poisson(0.1, 10 * (1+self.fibn[i+1] + j) ))
self.b.append(self.fibn[i+1] + j)
#sys.exit(1)
def silent(self, time1, time2, lmbd):
#self.startPos=[int(lmbd * time1), int(lmbd * time2)]
#self.startPos=[NatSimulation.poisson(lmbd, time1), NatSimulation.poisson(lmbd, time2)]
return self.startPos
def next(self, party, step):
if party==0: return (0, self.startPos[party] + self.b[step])
if party==1: return (0, self.startPos[party] + 2*self.b[step])
return
class BinomialStrategy(Strategy):
'''
Uses assumption that probability distribution on ports in given time can
be approximated by a binomial distribution.
This strategy turned out to not perform very well compared to SimpleStrategy
which takes expected value of those distributions, not their samples.
'''
startPos=[0,0]
nats = None
sim = None
lmbd = 0.1
dupl = False
coef = 1.4773
b = [[],[]]
def init(self, params=None):
self.gen()
def reset(self, nats=[], sim=None, params=[]):
self.sim = sim
if len(nats)==2: self.nats = nats
if self.sim!=None: self.lmbd = sim.lmbd
self.gen()
pass
def genPart(self, party):
# lambda on both sides
#lmbd = self.sim.lmbd if self.sim!=None else self.lmbd
# port scan interval from simulation
#t = self.sim.portScanInterval if self.sim != None else 10
#ns = [ 3.987181708, 7.974882353, 12.004749541, 15.800322829, 20.390325999, 24.367375517, 28.668607075, 32.949910378, 37.506133178, 41.742443414, 44.800671135, 48.348672329, 52.821276380, 57.469178456, 61.474596007, 65.717914907, 70.423188639, 74.391720812, 80.017952700, 84.947892878, 88.203774099, 91.590841110, 95.205556545, 99.328019471, 102.335360039, 106.544729418, 110.719261929, 114.837410091, 118.764939628, 123.672331073, 127.524354350, 131.119204224, 134.643451575, 139.673688887, 143.690764059, 145.763838876, 148.316423448, 152.739506297, 156.567037203, 159.356910963, 162.939505412, 166.931804752, 170.402291816, 173.796693505, 178.723474588, 182.610998631, 187.522111612, 191.497479355, 194.844053936, 198.151948314, 202.685181458, 206.063161886, 210.433559408, 214.854480521, 219.355133828, 224.807999456, 228.268685200, 230.684844072, 233.924019960, 236.544276658, 240.074916053, 243.360578932, 246.887385964, 251.681038671, 253.527177951, 257.873032343, 262.067410122, 266.011990338, 268.756623153, 272.397092680, 276.681009062, 279.824882078, 283.003213645, 288.024411096, 290.480014345, 295.706772374, 299.195893027, 303.246913292, 306.367333651, 309.352070189, 314.518994099, 318.785078211, 321.262194392, 323.623806732, 326.734251317, 330.667318523, 337.515610709, 340.768483839, 345.380901282, 348.867650735, 350.765196322, 356.134619000, 359.947363053, 363.589257206, 365.320871305, 369.100701085, 372.607317584, 377.613479891, 380.082908277, 382.375454754, 386.603164439, 389.512921070, 394.881574325, 399.226888711, 404.170855236, 410.035440173, 413.076100259, 417.063025857, 421.989219390, 425.093581289, 428.450424438, 429.687481362, 434.261591594, 439.003389572, 441.120935893, 444.311236119, 447.969036565, 452.291434031, 456.400836442, 459.702616071, 463.792495328, 468.037806798, 471.520976639, 475.481177998, 480.745843961, 484.088470817, 488.169975740, 492.774751900, 496.569341452, 500.919194602, 504.889967968, 507.996624646, 511.918461434, 515.349733107, 521.488208018, 526.830310060, 529.555800595, 532.264874680, 536.493128373, 541.813100192, 547.660295512, 549.901891913, 555.627221888, 560.343337996, 566.570148054, 569.379898112, 574.689291433, 578.308625540, 582.058224648, 586.744790724, 591.564407412, 597.052031487, 600.776404480, 606.743258412, 609.819878816, 613.076859428, 617.377557416, 621.211435938, 623.926934463, 627.234168357, 632.164093673, 635.355861142, 640.128661053, 642.947152483, 649.402630037, 653.769544480, 657.489285630, 661.039070242, 666.938391550, 671.077316685, 676.640805238, 679.972289053, 683.760785852, 686.977626228, 689.637979296, 695.459963956, 700.990250275, 705.268515110, 710.816285684, 715.004435241, 717.008173908, 720.386968273, 724.901978835, 729.085516011, 732.165207246, 736.705305934, 740.510331316, 744.748695762, 747.826582497, 752.286011505, 755.377885485, 758.701583710, 760.705686270, 766.434018948, 767.998782148, 774.689022529, 778.999985506, 779.984268269, 780.996216317, 785.979715413, 791.940414230, 798.814193636, 800.725141224, 803.882039485, 808.976581370, 815.169135991, 818.259605275, 824.419928104, 829.259454537, 833.278741593, 840.135888264, 840.947197091, 844.927117976, 847.354808921, 852.254740348, 857.004946736, 861.011230287, 862.568913099, 866.398110813, 873.162345540, 876.836924868, 881.349769800, 885.974309246, 889.434423720, 893.013902864, 896.482193941, 900.092924177, 907.287603875, 914.401075464, 918.964752765, 921.992288059, 924.462911936, 930.588538795, 936.753131141, 938.937118688, 942.743991988, 943.942932634, 947.521877123, 953.931862626, 958.506144790, 964.164109983, 968.172619755, 972.739310051, 969.687957009, 974.316839601, 981.630981790, 985.598045695, 987.978599859, 993.241516089, 997.986439204, 1001.602926576, 1004.661467642, 1007.138116728, 1012.587549700, 1017.789953593, 1020.840023374, 1021.417777849, 1025.788677217, 1032.246996521, 1036.416071668, 1042.214057526, 1046.283082647, 1047.275840730, 1047.891744091, 1052.871207799, 1055.990217835, 1060.226687972, 1065.594811557, 1069.016534049, 1071.673128674, 1076.237926463, 1083.809755129, 1088.836667647, 1092.208220240, 1096.867886099, 1103.935196998, 1107.280340541, 1112.396143698, 1116.370064162, 1118.297137197, 1121.973464143, 1128.287327942, 1130.747151240, 1136.618741938, 1144.547820432, 1148.841259345, 1154.337531906, 1159.340151020, 1164.816821020, 1165.216439278, 1165.284452559, 1165.686748613, 1170.884540216, 1177.142170631, 1180.903799413, 1184.235344969, 1184.860166119, 1191.044450373, 1194.084433291, 1196.881537232, 1195.914056537, 1200.110966589, 1203.308485429, 1205.946284108, 1206.598497010, 1212.395306209, 1215.254289782, 1221.395742807, 1221.237104748, 1226.081618790, 1228.887751617, 1234.131021954, 1237.793961841, 1243.667236861, 1249.017506729, 1254.583133460, 1258.245545676, 1262.114247057, 1264.287701850, 1267.365466426, 1272.061818488, 1274.039711924, 1278.519927790, 1283.327344241, 1285.275360216, 1289.609408516, 1295.899257916, 1299.599128997, 1301.204892811, 1304.742602615, 1306.774359234, 1310.714514029, 1318.252170756, 1322.337964687, 1327.543876239, 1327.519325650, 1332.798881586, 1338.116899680, 1340.952671571, 1345.277405692, 1343.899510103, 1345.863269413, 1351.003999473, 1353.660891814, 1356.848773376, 1364.242829740, 1368.824840165, 1371.484388061, 1376.088690319, 1384.915536581, 1390.997543695, 1398.706567504, 1402.724404630, 1406.187146439, 1407.400441539, 1413.956942841, 1420.849625958, 1424.694628591, 1433.316353584, 1438.343460229, 1441.707474431, 1449.321090142, 1455.638336681, 1462.528947559, 1466.608444008, 1473.751774700, 1477.434563749, 1484.231056018, 1491.854035951, 1493.945686633, 1497.268593763, 1500.302428141, 1504.365195263, 1509.666996729, 1512.260488173, 1513.992579970, 1516.071802341, 1521.944140491, 1525.046455807, 1525.830585809, 1530.299746194, 1534.762050094, 1538.149471093, 1543.311152208, 1545.380514799, 1547.795618469, 1553.225259433, 1561.454394625, 1565.578930528, 1569.062005615, 1576.902593236, 1583.988463446, 1591.084666558, 1596.889889913, 1597.533551527, 1603.452562517, 1609.226971576, 1611.136441800, 1615.886983558, 1621.327760221, 1627.072468071, 1628.059490052, 1626.708734052, 1627.704839171, 1633.999115232, 1638.825161981, 1639.943180826, 1640.033789009, 1646.282749474, 1649.738676313, 1654.785503253, 1662.049514770, 1669.012483753, 1672.481950560, 1673.224897077, 1679.364318498, 1682.122511150, 1686.668500294, 1687.739671886, 1694.166112799, 1699.872618097, 1704.124255847, 1708.339834680, 1715.045103706, 1721.397118555, 1720.331966462, 1728.215754240, 1733.518903630, 1736.388522089, 1738.547180122, 1743.915905607, 1747.761473356, 1752.003615680, 1761.271333825, 1764.347706908, 1770.742338071, 1776.424693178, 1779.360850727, 1781.870413333, 1783.914178271, 1785.382242934, 1787.680934108, 1796.408992818, 1799.647989713, 1802.803114162, 1806.643533326, 1811.031777933, 1815.036474075, 1818.522075238, 1821.251807467, 1825.068656848, 1825.852587642, 1828.071222851, 1832.288384943, 1832.902529968, 1836.313313839, 1840.743859719, 1845.663218081, 1847.572265501, 1853.550012212, 1857.060485999, 1863.080580915, 1867.427802885, 1870.532628109, 1870.812682051, 1876.154354219, 1885.630224855, 1894.770491928, 1897.876806025, 1895.599960058, 1903.649643908, 1906.888218476, 1914.045066165, 1917.365414130, 1923.078010870, 1928.358698010, 1933.080114530, 1939.239217626, 1939.447664990, 1941.027810362, 1947.740529041, 1949.062100880, 1952.089317479, 1955.964188906, 1959.481783555, 1961.426767226, 1972.951639066, 1975.719621248, 1979.649957171, 1981.112969712, 1985.773607890, 1987.869819312, 1990.090878744, 1992.925415276, 1994.395581343, 1999.159001443, 2002.313427992, 2009.387560894, 2008.906205490, 2010.079502289, 2013.537889948, 2015.803510765, 2022.107712706, 2025.668564400, 2028.013600609, 2030.400019087, 2034.125017024, 2037.914841863, 2040.087289370, 2046.347145211, 2047.166509203, 2044.152770095, 2047.416161357, 2048.831185222, 2050.251911423, 2051.308213167, 2053.492873574, 2059.859288690, 2064.816827107, 2068.777482531, 2072.687857826, 2078.531511015, 2081.071992723, 2086.487329926, 2095.590113487, 2101.827366876, 2106.916311920, 2108.902742879, 2115.586533194, 2119.763531865, 2126.739382363, 2132.634293551, 2134.728595546, 2135.757791843, 2135.205170386, 2140.740688643, 2144.904187504, 2149.289169240, 2154.981132955, 2158.332144605, 2162.582743985, 2164.728295215, 2171.183204312, 2175.535420657, 2177.777556064, 2186.020810514, 2190.130653380, 2191.566316217, 2195.405356885, 2199.471560911, 2203.679078260, 2211.296611841, 2210.324621173, 2214.010754995, 2217.849658576, 2222.861799538, 2225.194269966, 2227.699779622, 2226.829399267, 2229.305948284, 2233.183657438, 2235.118070866, 2239.915883846, 2244.589426059, 2248.731190498, 2248.111131981, 2250.970675725, 2250.965425845, 2254.199162245, 2261.967596447, 2270.201790924, 2275.872184044, 2283.620489723, 2284.632576315, 2284.931341173, 2288.642996264, 2294.056002582, 2295.718242317, 2296.463693357, 2299.422969461, 2307.257697207, 2318.640850251, 2322.302137052, 2329.317288373, 2331.961337918, 2342.004214611, 2341.151774037, 2347.106839795, 2352.034659404, 2353.588144588, 2361.261414423, 2362.862501767, 2363.495510838, 2364.082278583, 2369.663850245, 2372.211682910, 2376.793072822, 2379.238884583, 2381.505023056, 2381.441002925, 2384.313648383, 2390.914929305, 2395.606486302, 2401.114525779, 2402.901881953, 2403.103713236, 2408.455008767, 2411.377461928, 2413.628511119, 2418.296665526, 2425.952673113, 2431.243096956, 2435.632635457, 2438.041855949, 2447.124968479, 2452.417098006, 2452.858072293, 2455.183073338, 2458.768922967, 2467.769732700, 2471.599831436, 2472.601691363, 2473.754730081, 2480.834303144, 2482.697263692, 2489.124067224, 2497.904996379, 2501.221387960, 2511.274323127, 2514.764209151, 2515.126028493, 2516.373546356, 2517.273435414, 2526.490050838, 2530.761927144, 2526.758342071, 2530.518742087, 2532.902112946, 2531.762791883, 2533.596262996, 2543.199719932, 2548.642209552, 2551.304317615, 2556.097174560, 2560.200159960, 2566.026412566, 2568.152241733, 2571.475818701, 2576.017552445, 2578.835507769, 2586.565095600, 2590.043688818, 2597.569293860, 2609.651122880, 2613.749205658, 2621.078429121, 2628.974219460, 2628.144879855, 2629.999310759, 2634.691624964, 2637.334705152, 2637.523760809, 2643.012728903, 2644.333920851, 2642.461277951, 2642.054079194, 2651.068973765, 2653.949697461, 2653.114718776, 2660.566456104, 2666.271641970, 2667.849777504, 2671.128745446, 2676.279300996, 2684.565213203, 2685.714610698, 2688.797313763, 2695.559968618, 2700.207584877, 2704.483388987, 2712.670778048, 2717.540408541, 2718.061140119, 2731.978347238, 2732.376963406, 2738.321379230, 2743.192573914, 2745.506027908, 2747.197886655, 2747.346497579, 2747.628482200, 2750.368729245, 2755.258841110, 2759.897757527, 2771.645200141, 2776.897645662, 2779.513552777, 2779.194392205, 2786.106657558, 2792.825499330, 2793.449870589, 2797.055710286, 2803.007204853, 2805.991436784, 2810.493663560, 2815.804227185, 2816.406949795, 2822.494634479, 2828.263734219, 2832.669692429, 2835.897050669, 2838.687765748, 2844.843900333, 2848.653637035, 2851.085482892, 2856.442641827, 2860.013543098, 2862.683260519, 2861.884368541, 2868.710851778, 2872.940438395, 2876.081903430, 2879.242090635, 2886.265678744, 2894.639944000, 2893.183811225, 2899.570473905, 2902.030835201, 2905.303975512, 2901.615035013, 2907.843998028, 2908.269657441, 2909.329382566, 2907.777648552, 2907.675821388, 2912.776747328, 2915.201395797, 2916.568037701, 2915.688419191, 2919.501114442, 2923.654090807, 2924.207255345, 2930.004535038, 2930.774127834, 2937.729692102, 2937.585038099, 2943.431349222, 2950.329049085, 2956.567075377, 2960.719549676, 2961.450145931, 2965.838822016, 2969.787606656, 2975.706971272, 2984.324587963, 2984.878581820, 2986.624657065, 2986.052396871, 2989.342995568, 2993.143558608, 3002.844952689, 3008.701409014, 3015.308956188, 3024.451561359, 3031.294499567, 3040.044819679, 3046.666237126, 3049.151713820, 3054.973904788, 3059.442916827, 3061.528538763, 3066.252238975, 3075.556332356, 3083.860912636, 3080.454512456, 3082.026835988, 3082.596435173, 3086.187064499, 3089.453177040, 3090.999905219, 3099.497556438, 3103.463302765, 3108.359552263, 3114.558534944, 3114.371335118, 3119.709948881, 3120.942650983, 3120.370089822, 3120.924366152, 3125.096011055, 3131.594608371, 3132.726619698, 3139.318311322, 3142.688556367, 3140.482475731, 3139.871083374, 3143.860058704, 3154.904504813, 3156.749529520, 3162.445680101, 3164.501108465, 3169.283536609, 3169.607177562, 3167.635164597, 3173.610835566, 3182.864210501, 3191.608399217, 3195.588751731, 3200.369009039, 3204.738843440, 3207.161623400, 3206.917125285, 3208.983858547, 3209.776444243, 3214.108756480, 3215.089937747, 3220.139723016, 3220.807280069, 3223.070851217, 3221.217856140, 3227.229359527, 3234.690723751, 3239.663939025, 3244.442078259, 3247.698617798, 3258.137751646, 3261.105943785, 3265.521799809, 3269.609170356, 3271.758547698, 3273.723886291, 3279.709420239, 3288.573898095, 3295.557305037, 3305.182338555, 3313.945683737, 3315.655688251, 3322.141334676, 3331.528550306, 3331.689511454, 3333.126758868, 3334.242164985, 3333.977000812, 3340.236866083, 3343.633150475, 3351.038543876, 3355.127320971, 3358.960225522, 3368.303042344, 3370.210178763, 3376.307210316, 3375.551902732, 3377.664509480, 3378.622055836, 3378.604016918, 3379.842306194, 3382.497245807, 3385.550296532, 3392.429082833, 3400.354512672, 3401.107119887, 3407.749841791, 3406.101282580, 3409.851452532, 3413.581025195, 3415.645884303, 3425.675796276, 3425.764640027, 3434.052296868, 3440.218666380, 3444.447467244, 3453.198163533, 3459.525374911, 3463.958283818, 3467.106066215, 3472.693288424, 3474.850999676, 3476.870283347, 3480.795205750, 3485.358025711, 3489.544737629, 3488.132213834, 3490.268892140, 3498.003385389, 3497.454923356, 3500.409077562, 3501.518793783, 3504.152672545, 3506.515938406, 3508.986197706, 3512.089068955, 3512.682432796, 3518.459965871, 3517.640472888, 3520.286813436, 3524.029878091, 3525.878649137, 3529.835527872, 3534.603442124, 3541.357294967, 3547.694329050, 3556.901516417, 3559.825111279, 3562.525646498, 3561.559684561, 3565.382540337, 3574.231355234, 3578.693171847, 3578.885822164, 3586.959187842, 3586.703984108, 3590.313589959, 3602.893686104, 3608.774631436, 3613.019603012, 3612.367471455, 3619.011918712, 3622.072223732, 3630.316607386, 3640.015494119, 3643.142955859, 3651.882135463, 3657.272788029, 3663.211803146, 3660.276217405, 3658.389953098, 3664.276155198, 3667.144888979, 3665.849065329, 3663.655600317, 3675.241341749, 3678.780648482, 3686.207705190, 3683.509617589, 3689.760180024, 3699.064555725, 3710.346297884, 3711.875317126, 3711.430283391, 3715.074792102, 3714.680279138, 3721.668206155, 3731.217831714, 3730.202884632, 3730.766257206, 3738.634310698, 3746.057468402, 3746.599157976, 3749.088345083, 3753.109685716, 3765.945743252, 3768.219014225, 3773.633370821, 3777.721977464, 3775.149730131, 3780.885925747, 3785.722358563, 3787.945211854, 3797.425825146, 3803.985173683, 3802.467378805, 3808.081769493, 3819.681696514, 3819.818664549, 3831.538667876, 3843.641035642, 3852.584809905, 3856.877224787, 3865.698444371, 3870.988682832, 3877.214445418, 3885.303877466, 3888.577555701, 3896.221475795, 3902.892659535, 3899.755023377, 3905.643681188, 3910.331224371, 3911.614826620, 3914.289911725, 3931.661523140, 3935.758422771, 3944.049832136, 3949.630485409, 3953.231210186, 3954.868803238, 3961.826582196, 3966.047270390, 3974.114039192, 3971.566782255, 3980.896811046, 3984.180911913, 3993.672524079, 3997.787077876, 3998.844356704, 4004.556069670, 4006.080400388, 4006.25558456, 4008.25558456, 4010 ]
#ps = [ 0.500905187, 0.500446254, 0.499468979, 0.505837766, 0.490615010, 0.492149842, 0.487585601, 0.484717555, 0.479340270, 0.478510561, 0.490474795, 0.496497605, 0.492297835, 0.487506534, 0.488110568, 0.487002670, 0.483178917, 0.484389924, 0.475285842, 0.471077017, 0.476156496, 0.480362441, 0.483261709, 0.483203030, 0.488720614, 0.488382675, 0.487869943, 0.487959455, 0.488428657, 0.485288823, 0.486561177, 0.488425783, 0.490427119, 0.486970743, 0.487448170, 0.494246039, 0.499166568, 0.497827981, 0.498600481, 0.502388001, 0.503553756, 0.503597263, 0.505105883, 0.506742667, 0.503938278, 0.504149261, 0.501572850, 0.501538716, 0.503319439, 0.504951886, 0.503447757, 0.504889855, 0.503827433, 0.502784023, 0.501653178, 0.498467138, 0.499628759, 0.503017875, 0.504628811, 0.507439883, 0.508339655, 0.509602667, 0.510442441, 0.508662078, 0.512946190, 0.512027174, 0.511497404, 0.511429578, 0.513685573, 0.514161875, 0.513415794, 0.514807686, 0.516081772, 0.514067538, 0.516575298, 0.514257414, 0.514968967, 0.514643326, 0.515949263, 0.517427926, 0.515339306, 0.514714807, 0.517036872, 0.519342510, 0.520464565, 0.520293934, 0.515669778, 0.516634044, 0.515548484, 0.516156771, 0.519037242, 0.516855678, 0.516963365, 0.517256757, 0.520250046, 0.520331713, 0.520775065, 0.519111765, 0.521122091, 0.523203039, 0.522695670, 0.523909706, 0.521854686, 0.521227918, 0.519803438, 0.517226999, 0.518238649, 0.518043525, 0.516679076, 0.517606969, 0.518235920, 0.521398946, 0.520500096, 0.519450203, 0.521504153, 0.522206915, 0.522436108, 0.521886072, 0.521618238, 0.522214126, 0.522023539, 0.521542911, 0.521920789, 0.521819183, 0.520326495, 0.520789515, 0.520515010, 0.519726303, 0.519781586, 0.519294135, 0.519171338, 0.519961723, 0.519929676, 0.520332083, 0.518115263, 0.516680599, 0.517793969, 0.518894282, 0.518523696, 0.517070554, 0.515180674, 0.516723809, 0.514970449, 0.514209379, 0.512080280, 0.513055696, 0.511757578, 0.512058418, 0.512181063, 0.511495295, 0.510694011, 0.509330484, 0.509511022, 0.507818086, 0.508499658, 0.509032750, 0.508754159, 0.508807439, 0.509822517, 0.510294904, 0.509488918, 0.510075723, 0.509380410, 0.510290774, 0.508308382, 0.507988484, 0.508171627, 0.508465861, 0.506950873, 0.506778864, 0.505546809, 0.506007679, 0.506135782, 0.506642410, 0.507583124, 0.506222958, 0.505073786, 0.504858777, 0.503762234, 0.503606946, 0.505027715, 0.505409198, 0.505054491, 0.504884533, 0.505471165, 0.505047265, 0.505149198, 0.504986450, 0.505571491, 0.505241084, 0.505817032, 0.506214312, 0.507509549, 0.506331126, 0.507900024, 0.506092107, 0.505869586, 0.507798190, 0.509693634, 0.509022806, 0.507695772, 0.505826515, 0.507069129, 0.507564767, 0.506853238, 0.505459274, 0.505987094, 0.504660533, 0.504128832, 0.504112704, 0.502376587, 0.504261387, 0.504251303, 0.505147779, 0.504598191, 0.504110742, 0.504095864, 0.505527377, 0.505603134, 0.503959890, 0.504140493, 0.503842419, 0.503462906, 0.503755857, 0.503951393, 0.504245375, 0.504411031, 0.502616368, 0.500910391, 0.500582855, 0.501095081, 0.501923435, 0.500760304, 0.499615090, 0.500571541, 0.500651082, 0.502145398, 0.502371092, 0.501098264, 0.500787191, 0.499939787, 0.499932750, 0.499653910, 0.503285203, 0.502929006, 0.501234587, 0.501254241, 0.502069984, 0.501413394, 0.501024243, 0.501208000, 0.501660128, 0.502404180, 0.501655585, 0.501069399, 0.501524419, 0.503191751, 0.502980401, 0.501773511, 0.501698897, 0.500837228, 0.500777092, 0.502197396, 0.503802614, 0.503314552, 0.503716598, 0.503602679, 0.502938447, 0.503208120, 0.503823214, 0.503548320, 0.501884207, 0.501393475, 0.501685292, 0.501385634, 0.499984964, 0.500278908, 0.499767824, 0.499784093, 0.500704313, 0.500852309, 0.499822506, 0.500508004, 0.499688224, 0.497990726, 0.497856423, 0.497208991, 0.496764819, 0.496162821, 0.497701869, 0.499362365, 0.500927372, 0.500406641, 0.499438738, 0.499539251, 0.499837471, 0.501254255, 0.500323225, 0.500706301, 0.501192793, 0.503281149, 0.503176970, 0.503489012, 0.504055121, 0.505457782, 0.504680443, 0.505145964, 0.504257530, 0.505947615, 0.505567158, 0.506041662, 0.505518287, 0.505631971, 0.504856751, 0.504306622, 0.503667540, 0.503778934, 0.503805104, 0.504536744, 0.504894616, 0.504608652, 0.505390997, 0.505172728, 0.504829733, 0.505631883, 0.505499414, 0.504589841, 0.504713019, 0.505625827, 0.505797234, 0.506543915, 0.506538833, 0.505152971, 0.505098710, 0.504609461, 0.506131690, 0.505616121, 0.505097425, 0.505523882, 0.505379186, 0.507392402, 0.508151174, 0.507697091, 0.508144399, 0.508435290, 0.507140947, 0.506892905, 0.507368371, 0.507125743, 0.505347208, 0.504582775, 0.503237503, 0.503218307, 0.503395300, 0.504386939, 0.503448004, 0.502419951, 0.502459815, 0.500839189, 0.500479350, 0.500705041, 0.499439638, 0.498641855, 0.497668098, 0.497664392, 0.496597197, 0.496717633, 0.495798142, 0.494625065, 0.495275301, 0.495516705, 0.495863091, 0.495854067, 0.495445685, 0.495923292, 0.496677335, 0.497301248, 0.496709951, 0.497001253, 0.498055162, 0.497918138, 0.497764067, 0.497977352, 0.497619031, 0.498234184, 0.498742593, 0.498292051, 0.496951177, 0.496927229, 0.497086665, 0.495876792, 0.494917367, 0.493967428, 0.493425129, 0.494478504, 0.493893501, 0.493359740, 0.494018495, 0.493802109, 0.493386976, 0.492864095, 0.493799032, 0.495434114, 0.496347852, 0.495648249, 0.495392565, 0.496258535, 0.497445727, 0.496755737, 0.496921611, 0.496629320, 0.495671033, 0.494793064, 0.494976343, 0.495952099, 0.495339689, 0.495711813, 0.495553453, 0.496416488, 0.495723881, 0.495248403, 0.495190416, 0.495136847, 0.494371022, 0.493705021, 0.495175592, 0.494077431, 0.493739756, 0.494073641, 0.494607342, 0.494221423, 0.494283581, 0.494223295, 0.492756331, 0.493031729, 0.492383099, 0.491937544, 0.492242144, 0.492666579, 0.493222045, 0.493946886, 0.494433197, 0.493151617, 0.493372485, 0.493616742, 0.493673037, 0.493576430, 0.493590081, 0.493746880, 0.494105646, 0.494173190, 0.495057929, 0.495544697, 0.495482156, 0.496410467, 0.496584974, 0.496482982, 0.496241563, 0.496803734, 0.496287121, 0.496428742, 0.495895888, 0.495812582, 0.496056998, 0.497035972, 0.496696233, 0.495263381, 0.493923778, 0.494167481, 0.495806721, 0.494753960, 0.494968499, 0.494158898, 0.494346770, 0.493930457, 0.493612211, 0.493452492, 0.492918868, 0.493896905, 0.494524496, 0.493842062, 0.494537860, 0.494802667, 0.494838610, 0.494959743, 0.495488802, 0.493605307, 0.493924537, 0.493956720, 0.494604808, 0.494449718, 0.494935629, 0.495385015, 0.495692108, 0.496342957, 0.496157734, 0.496371041, 0.495628031, 0.496745740, 0.497456443, 0.497602158, 0.498033362, 0.497459554, 0.497563924, 0.497968554, 0.498373321, 0.498438587, 0.498505914, 0.498943553, 0.498392368, 0.499157834, 0.500874012, 0.501042836, 0.501676179, 0.502318273, 0.503030794, 0.503471238, 0.502898235, 0.502656403, 0.502661020, 0.502676269, 0.502228518, 0.502582901, 0.502233675, 0.501007422, 0.500464699, 0.500204300, 0.500674962, 0.500032962, 0.499992279, 0.499285530, 0.498841927, 0.499298226, 0.500000611, 0.501054800, 0.500701652, 0.500653039, 0.500564194, 0.500179971, 0.500330777, 0.500282638, 0.500706348, 0.500142870, 0.500064761, 0.500464153, 0.499491219, 0.499449655, 0.500036340, 0.500065647, 0.500046656, 0.499998485, 0.499184955, 0.500298006, 0.500377786, 0.500410474, 0.500181523, 0.500540027, 0.500876559, 0.501968763, 0.502293281, 0.502311844, 0.502768339, 0.502588605, 0.502430238, 0.502395131, 0.503409633, 0.503653918, 0.504541557, 0.504704384, 0.503866458, 0.502927539, 0.502554145, 0.501720144, 0.502383452, 0.503200941, 0.503260142, 0.502941253, 0.503449151, 0.504159157, 0.504368103, 0.503512981, 0.501908090, 0.501977405, 0.501316504, 0.501595794, 0.500293549, 0.501329821, 0.500905106, 0.500697979, 0.501226777, 0.500440397, 0.500946965, 0.501659172, 0.502380400, 0.502039393, 0.502338728, 0.502218394, 0.502546931, 0.502905343, 0.503748360, 0.503982771, 0.503429371, 0.503278233, 0.502967013, 0.503428213, 0.504242074, 0.503956933, 0.504180461, 0.504537626, 0.504397669, 0.503626066, 0.503354026, 0.503271627, 0.503587663, 0.502535431, 0.502270923, 0.502997713, 0.503336885, 0.503410056, 0.502391768, 0.502419601, 0.503023194, 0.503606030, 0.502977969, 0.503399999, 0.502915711, 0.501954078, 0.502087822, 0.500877737, 0.500969751, 0.501701539, 0.502244749, 0.502860509, 0.501817215, 0.501756798, 0.503343426, 0.503379714, 0.503695817, 0.504707236, 0.505141336, 0.504016570, 0.503724059, 0.503984880, 0.503831315, 0.503810608, 0.503445402, 0.503808606, 0.503935907, 0.503822499, 0.504047349, 0.503311284, 0.503405949, 0.502711748, 0.501147601, 0.501126656, 0.500487580, 0.499745182, 0.500679628, 0.501087204, 0.500952289, 0.501216246, 0.501938985, 0.501651992, 0.502157685, 0.503265047, 0.504104443, 0.503139003, 0.503344431, 0.504262176, 0.503601854, 0.503269201, 0.503723040, 0.503847447, 0.503629647, 0.502826526, 0.503353482, 0.503517314, 0.503004725, 0.502871189, 0.502819284, 0.502033867, 0.501867717, 0.502500911, 0.500668902, 0.501334120, 0.500964208, 0.500793934, 0.501094948, 0.501510942, 0.502198467, 0.502871043, 0.503102361, 0.502939245, 0.502817250, 0.501408910, 0.501177205, 0.501422560, 0.502197689, 0.501681871, 0.501194006, 0.501791715, 0.501853715, 0.501507238, 0.501688773, 0.501602341, 0.501363407, 0.501965172, 0.501594009, 0.501286773, 0.501210997, 0.501348312, 0.501552731, 0.501171822, 0.501201263, 0.501478405, 0.501235621, 0.501310633, 0.501550213, 0.502384658, 0.501883206, 0.501834977, 0.501982992, 0.502127558, 0.501595751, 0.500835485, 0.501785816, 0.501371363, 0.501639880, 0.501759614, 0.503086585, 0.502701246, 0.503301472, 0.503810675, 0.504758333, 0.505456279, 0.505248746, 0.505513205, 0.505962687, 0.506799489, 0.506817755, 0.506783413, 0.507370672, 0.507049181, 0.507605341, 0.507085320, 0.507785062, 0.507466023, 0.506962368, 0.506567706, 0.506531259, 0.507076036, 0.507000309, 0.506995381, 0.506660977, 0.505861261, 0.506436814, 0.506804428, 0.507567986, 0.507679113, 0.507704282, 0.506729793, 0.506405187, 0.505963277, 0.505094616, 0.504614316, 0.503820533, 0.503384316, 0.503634697, 0.503329995, 0.503245866, 0.503556175, 0.503431446, 0.502559223, 0.501850649, 0.503054596, 0.503445389, 0.504004995, 0.504058428, 0.504171227, 0.504560158, 0.503821013, 0.503824163, 0.503669081, 0.503303753, 0.503975291, 0.503748241, 0.504191450, 0.504920107, 0.505471846, 0.505435159, 0.505025521, 0.505479217, 0.505060221, 0.505152124, 0.506142929, 0.506879027, 0.506875169, 0.505737716, 0.506073600, 0.505800877, 0.506101134, 0.505961988, 0.506543464, 0.507489315, 0.507162372, 0.506314091, 0.505561710, 0.505553851, 0.505417218, 0.505344142, 0.505592730, 0.506252122, 0.506549697, 0.507048584, 0.506986516, 0.507450812, 0.507277864, 0.507790084, 0.508052809, 0.508961291, 0.508640514, 0.508085731, 0.507920646, 0.507786966, 0.507900515, 0.506888697, 0.507034064, 0.506955183, 0.506929457, 0.507203076, 0.507508714, 0.507192555, 0.506431861, 0.505966077, 0.505103389, 0.504374169, 0.504716942, 0.504340915, 0.503522505, 0.504100575, 0.504480184, 0.504919354, 0.505570374, 0.505220099, 0.505306271, 0.504788703, 0.504766716, 0.504782518, 0.503975853, 0.504284929, 0.503969720, 0.504671784, 0.504953288, 0.505404858, 0.506003246, 0.506414278, 0.506606296, 0.506734341, 0.506299692, 0.505707035, 0.506177824, 0.505780964, 0.506606897, 0.506634797, 0.506659425, 0.506939085, 0.506036386, 0.506613437, 0.505971036, 0.505647567, 0.505606376, 0.504903894, 0.504559733, 0.504493489, 0.504611041, 0.504373077, 0.504633263, 0.504916622, 0.504920886, 0.504836171, 0.504807914, 0.505575102, 0.505838505, 0.505298825, 0.505949509, 0.506097305, 0.506497981, 0.506684373, 0.506919983, 0.507135024, 0.507259402, 0.507744618, 0.507474184, 0.508162279, 0.508347159, 0.508379458, 0.508672810, 0.508662850, 0.508547120, 0.508143700, 0.507795496, 0.507037092, 0.507183624, 0.507357639, 0.508059659, 0.508081582, 0.507387637, 0.507314909, 0.507845372, 0.507263090, 0.507853675, 0.507900648, 0.506677399, 0.506407240, 0.506360718, 0.507010656, 0.506628506, 0.506746328, 0.506148196, 0.505346420, 0.505460319, 0.504800355, 0.504598893, 0.504334365, 0.505287385, 0.506095365, 0.505823994, 0.505969755, 0.506692001, 0.507538918, 0.506482657, 0.506539905, 0.506059764, 0.506968515, 0.506650869, 0.505916826, 0.504922897, 0.505247682, 0.505848489, 0.505890542, 0.506482943, 0.506066929, 0.505311640, 0.505990601, 0.506454136, 0.505923405, 0.505457222, 0.505923351, 0.506121869, 0.506112680, 0.504913753, 0.505140384, 0.504945927, 0.504930699, 0.505798905, 0.505564208, 0.505441503, 0.505666976, 0.504931732, 0.504589927, 0.505313737, 0.505098923, 0.504089595, 0.504593587, 0.503572420, 0.502508164, 0.501859478, 0.501825982, 0.501197139, 0.501026988, 0.500743157, 0.500211943, 0.500306493, 0.499830826, 0.499491575, 0.500404920, 0.500163445, 0.500075233, 0.500428975, 0.500592456, 0.498888724, 0.498874420, 0.498331939, 0.498136271, 0.498187709, 0.498487484, 0.498123115, 0.498095122, 0.497582047, 0.498405569, 0.497741814, 0.497828900, 0.497148324, 0.497135831, 0.497505035, 0.497296720, 0.497609983, 0.498082750, 0.5, 0.5 ]
b = [] # local array
seen = set() # duplicate check
seen_add = seen.add
for step in range(0, 1000):
x = 1.5*step
if step > 1000 and party==1:
ex = 2.0*float(step)
vx = float(step)/2.0
p = (ex-vx) / ex
n = ex / p
x = np.random.binomial(n, p) # (1+step*self.coef)
# probabilistic rounding
x = round(x)
#x = probRound(x)
if True or self.dupl or (x not in seen and not seen_add(x)):
b.append(x)
#print b
return b
def gen(self):
self.b = [self.genPart(0), self.genPart(1)]
def silent(self, time1, time2, lmbd):
self.lmbd = lmbd
self.startPos=[int(lmbd * time1), int(lmbd * time2)] # expected value
return self.startPos
def next(self, party, step):
return (0, int(self.startPos[party] + self.b[party][min(step, len(self.b[party])-1)]))
class PoissonStrategy(Strategy):
'''
One of best strategies proposed. Relies on assumption of a Poisson process controlling
new connections occurrence. Simulates such random process as own strategy.
For particular setting (lambda, time), coefficient is needed to be found. If coeficient
is optimized, this strategy performs very well.
'''
startPos=[0,0]
nats = None
sim = None
lmbd = 0.1
dupl = False
#coef = 1.4127
coef = 1.1772
b = [[],[]]
def init(self, params=None):
#self.reset()
self.gen()
#print self.b[0], "len=", len(self.b[0])
def reset(self, nats=[], sim=None, params=[]):
self.sim = sim
if len(nats)==2: self.nats = nats
if self.sim!=None: self.lmbd = sim.lmbd
self.gen()
pass
def coe(self, x):
#return (4.43727 * math.exp(-2.156 * x))
#return -0.928467 * math.log(0.255879 * x) # this formula is OK for lambda [0.01, 0.07], t=10
#return 1.0 / (0.161918 * math.log(65.7525 * x)) # OK for [0.04 , 0.15], t=10
# experimental
#return 1.0 / (0.165 * math.log(64 * x)) # OK for [0.04 , 0.15], t=10
#return 6.18 / (math.log(x) + 4.28) # OK for [0.04 , 0.15], t=10
#
# Usable functions below.
#
# Alternative #1 - works good.
return 1.0 / (0.163321 * math.log(64.2568 * x)) # OK for [0.04 , 0.15], t=10
# Alternative #2 - quartic interpolation.
# Very good fit for low intervals.
return 1.0 / (0.172876+1.28162*x-1.41256*x*x+0.825093*x*x*x-0.184726*x*x*x*x)
def genPart(self, party):
# lambda on both sides
lmbd = self.sim.lmbd if self.sim!=None else self.lmbd
# port scan interval from simulation
t = self.sim.portScanInterval if self.sim != None else 10
x = 0
b = [] # local array
seen = set() # duplicate check
seen_add = seen.add
for step in range(0, 3001):
x = int( np.random.poisson(lmbd * t * (1.0+step*self.coe(lmbd*t))) )
#x = int( np.random.poisson(lmbd * t * (1.0+step*self.coef)) )
#x = round( np.random.poisson(float(step) * (1.0 + lmbd*t)) )
#if party==0:
# x = round(step * (1 + lmbd*t))
#if party==1:
# x = round(step * (1 + lmbd*t))
# If unique element -> add to the b[]
# Otherwise skip this <step> iteration - specialty. For different runs
# different steps can be skipped.
if self.dupl or (x not in seen and not seen_add(x)):
b.append(x)
if len(b) > 1100: break
#b.append(x)
#b = f7(b)
return b
def gen(self):
self.b = [self.genPart(0), self.genPart(1)]
def silent(self, time1, time2, lmbd):
self.lmbd = lmbd
self.startPos=[int(lmbd * time1), int(lmbd * time2)] # expected value
self.gen()
#self.startPos=[NatSimulation.poisson(lmbd, time1), NatSimulation.poisson(lmbd, time2)]
return self.startPos
def next(self, party, step):
#return (0, int(self.startPos[party] + NatSimulation.poisson(self.lmbd, 10 * (1+step*1.77) )))
return (0, int(self.startPos[party] + self.b[party][min(step, len(self.b[party])-1)]))
#self.startPos[party] += 1+NatSimulation.poisson(self.lmbd, 10)#*(1+step*0.77))
#return (0, int(self.startPos[party]))
def getStrategy(desc, verbose=0):
'''
Returns strategy according to string identifier
'''
strategy = PoissonStrategy()
if desc == 'i2j':
if verbose>0: print "I2J Strategy: "
strategy = I2JStragegy()
elif desc == 'ij':
if verbose>0: print "IJ strategy"
strategy = IJStragegy()
elif desc == 'fibo':
if verbose>0: print "Fibonacci strategy"
strategy = FiboStrategy()
elif desc == 'their':
if verbose>0: print "Their strategy"
strategy = TheirStragegy()
elif desc == 'poisson':
if verbose>0: print "Poisson strategy"
strategy = PoissonStrategy()
elif desc == 'binom':
if verbose>0: print "Binomial strategy"
strategy = BinomialStrategy()
elif desc == 'simple':
if verbose>0: print "Simple strategy"
strategy = SimpleStrategy()
return strategy
def nfline2tuple(line):
'''
Translates nfdump line of a format "fmt:%%ts;%%td;%%pr;%%sa;%%sp;%%da;%%dp" to a tuple defined by the format
'''
tpl = [str(x).strip() for x in line.split(";")]
tstart = tpl[0]
tdur = float(tpl[1])
dtime = dparser.parse(tstart) # Parse time from nfdump to datetime format
startUtc = NatSimulation.dtimeToUtc(dtime) # convert date time string to UTC
lastData = int(startUtc + round(tdur))
tpl.append(startUtc)
tpl.append(lastData)
return (tpl, startUtc)
class NfdumpAbstract:
def deinit(self):
pass
def generator(self):
pass
class NfdumpSorter(NfdumpAbstract):
'''
Generator for reading a nfdump file by nfdump program, sorted by time of netflow
start - sorting on the fly by heap algorithm.
'''
proc = None
once = False
tout = 300*1000
def __init__(self, filename, filt=None, activeTimeout=300*1000):
'''
Initializes object for nfdumpSortedGenerator - creates a nfdump process
'''
if self.once == True: raise Exception('Generator was not de-initialized, may be still running...')
cmdLine = 'nfdump -q -r "%s" -o "fmt:%%ts;%%td;%%pr;%%sa;%%sp;%%da;%%dp" "%s"' % (filename, filt if filt!=None else "")
print "nfdump command line used: %s" % cmdLine
self.once = True
self.tout = activeTimeout
self.proc = subprocess.Popen(cmdLine, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
print "Data loaded, going to process output..."
def __enter__(self):
return self
def __exit__(self):
self.deinit()
def deinit(self):
'''
Kills subprocess if still exists
'''
if self.proc!=None:
try:
self.proc.kill()
except Exception:
pass
pass
self.proc = None
self.once = False
pass
def generator(self):
'''
Generator object producing nfdump lines for a given filename for nfdump file.
Records are sorted by first seen time in this generator since nfdump records does not have to be sorted by default.
(Probe stored records by its internal logic)
Using heap to sort by maintaining some amount of records in memory in priority queue
and taking minimal element if have enough elements and sufficient precision.
'''
if self.once == False: raise Exception('Generator is not initialized...')
cnt = 0
isDead = False # is producing program dead?
buff = []
while(True): # while there is something to run
cnt += 1
retcode = self.proc.poll() #returns None while subprocess is running
line = self.proc.stdout.readline()
# Flag determines whether queue sort is consistent with absolute sort w.r.t. whole data set.
isSorted = False
# Flag determines whether there was some line added to the queue => new event read.
lineAdded = False
# Parse output format to tuple.
# Partial sort by heap.
#
# Read line if there is something to read and process didn't finish
# in previous round.
if line!=None and len(line)>0 and isDead==False:
tpl, startUtc = nfline2tuple(line)
# Add record to the priority queue sorted by first seen time
heappush(buff, (startUtc, tpl))
lineAdded = True
# If time difference between current element and minimal one in queue is
# greater than active timeout, we have probably enough data in queue
# to be completely sorted w.r.t. whole data set since the biggest gap
# in nearly-sorted block is of size active timeout (probe added flow to file
# when active timeout was expired). The next entry cannot be smaller.
# Just to be sure - require at least 10 000 elements in priority queue.
if (startUtc - buff[0][0]) > self.tout and len(buff) >= 10000:
isSorted=True
#print "Sorted event... cnt=%d, min=%s, curr=%s, diff=%s" % (cnt, buff[0][0], startUtc, (startUtc - buff[0][0]))
# If program is dead skip the line parsing in the next iteration and set data
# in the priority queue as prepared to be processed.
if retcode is not None:
isDead=True
isSorted=True
# If line was added and queue is not ready, then continue (still can read some data,
# program is still running, so wait to fill the queue)
if lineAdded and not isSorted and retcode is None:
if (cnt % 1000) == 0:
sys.stdout.write('.')
sys.stdout.flush()
continue
# If there is nothing to process then we are done...
if len(buff)==0:
break
tplpopped = heappop(buff)
yield tplpopped
# End of the input processing
self.deinit()
class NfdumpReader(NfdumpAbstract):
'''
Generator for reading pre-processed NFdump file.
Nfdump generator reads already sorted records in format "fmt:%%ts;%%td;%%pr;%%sa;%%sp;%%da;%%dp"
Just reads the file and parses nfdump format to tuple.
'''
fo = None
once = False
def __init__(self, filename):
'''
Initializes object for generator - opens nfdump file for reading
'''
if self.once == True: raise Exception('Generator was not de-initialized, may be still running...')
self.fo = open(filename, "r+")
self.once = True
def __enter__(self):
return self
def __exit__(self):
self.deinit()
def deinit(self):
'''
Kills subprocess if still exists
'''
if self.fo!=None:
try:
self.fo.close()
except Exception:
pass
pass
self.fo = None
self.once = False
pass
def generator(self):
'''
Nfdump generator reads already sorted records in format "fmt:%%ts;%%td;%%pr;%%sa;%%sp;%%da;%%dp"
Just reads the file and parses nfdump format to tuple.
'''
if self.once == False: raise Exception('Generator is not initialized...')
while True:
line = self.fo.readline()
if not line:
break
tpl, startUtc = nfline2tuple(line)
yield (startUtc, tpl)
self.deinit()
class NatSimulation(object):
# Lambda for Poisson process generator. Time unit = 1 ms
# Intuitively: represents rate of creation of new events in given period.
# Average number of arrivals per unit time is lambda. [analysis&synthesis]
# The expected length of interarrival intervals is 1/lambda. [analysis&synthesis]
# Interarrival intervals are independent and distributed exponentially with parameter lambda. [analysis&synthesis]
#
# @see http://www.columbia.edu/~ks20/4703-Sigman/4703-07-Notes-PP-NSPP.pdf
# @see http://filebox.vt.edu/users/pasupath/papers/poisson_streams.pdf
# @see http://www.math.wsu.edu/faculty/genz/416/lect/l05-45.pdf
# @see http://preshing.com/20111007/how-to-generate-random-timings-for-a-poisson-process/
lmbd = 0.01
# Number of miliseconds for silent period to take [ms].
# Based on basic ping / round trip time it takes to communicate
# IP with another peer
silentPeriodBase = 1000
# Lambda for Pois(lmbd) for silent period variability.
# Silent period time = silentPeriodBase + Pois(lmbd) [ms]
silentPeriodlmbd = 100
# Number of rounds for simulation
simulationRounds = 1000
# how many rounds has fast simulation? Fast is extended to deep simulation if it has
# more than 50% probability of success
simulationRoundsFast = 100
# Number of errors that are handled by algorithm
errors = 1000
# number of milliseconds between consecutive port scans
portScanInterval = 10
# number of connections to establish
numCon = 1
# very compact output
compact=True
# draw dot if 1 round
dot=1
ascii=1
# process in generator
proc=None
@staticmethod
def poisson(lmbd, t):
'''
Uses Numpy package to take sample from poisson distribution
'''
return int(np.random.poisson(lmbd*t))
@staticmethod
def uniform(lmbd, t):
return random.random() * lmbd * t
@staticmethod
def poissonSample(lmbd, t):
'''
Generates number of events in Poisson process in time [0, t]
source: http://www.math.wsu.edu/faculty/genz/416/lect/l05-45.pdf
'''
u = random.random()
N = 0
p = math.exp(-lmbd * t)
F = p
while u > F:
N = N+1
p = lmbd*t*p/N
F = F + p
return N
def poissonCDF(self, lmbd, x):
'''
Returns P(X <= x), X~Poisson(lmbd)
P(X <= x) = e^{-lambda} * \sum_{i=0}^{k}{ \frac{lambda^i}{i!} }
'''
F = 1
res = 0
for i in range(0, x+1):
res = res + (math.pow(lmbd, i) / F)
F = F * (i+1)
return (math.exp(-lmbd) * res)
def getNumOfNewConnections(self, tim):
'''
Simple wrapper for poission. Returns number of new connections
created. It is assumed they are distributed according to Poisson distribution.
'''
return int(np.random.poisson(self.lmbd*tim))