File size: 1,657 Bytes
7e6c02a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
MPVR v1 - Multi-Path Vector Routing
Preuve de concept MTTV-flp
Simule Raft vs MPVR avec routage énergétique et mémoire (4e dimension)
"""
import random, itertools

def simulate(rounds=100, failure=50):
    nodes=[0,1,2,3,4]
    failed=1
    memory={n:1.2 for n in nodes}
    results=[]
    random.seed(101)
    for r in range(1,rounds+1):
        alive=[n for n in nodes if not (n==failed and r>=failure)]
        followers=[n for n in [1,2,3,4] if n in alive]
        costs={}
        for i,j in itertools.combinations(nodes,2):
            if i in alive and j in alive:
                c=random.uniform(0.5,2.0)
                costs[(i,j)]=costs[(j,i)]=c
        def best(a,b):
            best=costs.get((a,b),9)
            for m in alive:
                if m in (a,b): continue
                c=costs.get((a,m),9)+costs.get((m,b),9)
                if c<best: best=c
            return best
        raft=sum(costs.get((0,f),0) for f in followers)+sum(sorted(costs.get((f,0),0) for f in followers)[:3])
        cand=sorted([(best(0,t),t) for t in followers])
        v3=sum(c for c,_ in cand[:3])
        for t in followers: memory[t]=0.8*memory[t]+0.2*best(0,t)
        avg=sum(memory[t] for t in followers)/len(followers)
        quorum=2 if avg<1.25 else 3
        chosen=[]
        for c,t in cand:
            if c<=memory[t]*1.15: chosen.append((c,t))
            if len(chosen)>=quorum: break
        if len(chosen)<quorum: chosen=cand[:quorum]
        v4=sum(c for c,_ in chosen)
        results.append((r,raft,v3,v4))
    return results

if __name__=="__main__":
    for r,raft,v3,v4 in simulate()[:5]:
        print(r, raft, v3, v4)