upgraedd commited on
Commit
bbd2a75
·
verified ·
1 Parent(s): f209a91

Create consensus_verification.py

Browse files
Files changed (1) hide show
  1. consensus_verification.py +85 -0
consensus_verification.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ consensus_verification.py
3
+ A deterministic model for consensus emergence in multi-agent systems.
4
+ """
5
+ import hashlib
6
+ import numpy as np
7
+ from dataclasses import dataclass
8
+ from typing import Dict, List
9
+
10
+ @dataclass(frozen=True)
11
+ class Claim:
12
+ """Represents a proposition with a unique identifier and a clarity score."""
13
+ identifier: str # Hash of the canonical statement
14
+ clarity: float # 0.0 (ambiguous) to 1.0 (clear)
15
+
16
+ class ConsensusEngine:
17
+ """
18
+ Simulates a network of agents converging on belief states through
19
+ iterative local updates.
20
+ """
21
+ def __init__(self, agent_count: int = 10):
22
+ self.agent_count = agent_count
23
+ self.agents = [{"id": i, "beliefs": {}} for i in range(agent_count)]
24
+
25
+ def process(self, claims: List[Claim]) -> Dict[str, float]:
26
+ """
27
+ Executes the consensus process.
28
+ Returns a mapping from each claim identifier to its final acceptance ratio.
29
+ """
30
+ # 1. Initialize agent beliefs stochastically based on claim clarity.
31
+ for agent in self.agents:
32
+ for claim in claims:
33
+ accept_prob = 0.1 + (0.9 * claim.clarity)
34
+ agent["beliefs"][claim.identifier] = 1 if np.random.random() < accept_prob else 0
35
+
36
+ # 2. Iterate: agents adjust beliefs towards network average.
37
+ for _ in range(5):
38
+ network_avg = self._average_beliefs(claims)
39
+ for agent in self.agents:
40
+ for claim in claims:
41
+ current = agent["beliefs"][claim.identifier]
42
+ social_influence = network_avg[claim.identifier] - current
43
+ agent["beliefs"][claim.identifier] = np.clip(current + social_influence * 0.3, 0, 1)
44
+
45
+ # 3. Calculate and return final consensus.
46
+ final_state = {}
47
+ for claim in claims:
48
+ total_acceptance = sum(a["beliefs"][claim.identifier] for a in self.agents)
49
+ final_state[claim.identifier] = round(total_acceptance / self.agent_count, 3)
50
+ return final_state
51
+
52
+ def _average_beliefs(self, claims: List[Claim]) -> Dict[str, float]:
53
+ avg = {}
54
+ for claim in claims:
55
+ total = sum(a["beliefs"].get(claim.identifier, 0) for a in self.agents)
56
+ avg[claim.identifier] = total / self.agent_count
57
+ return avg
58
+
59
+ def run_demonstration():
60
+ """
61
+ Demonstrates the model's convergence on a small set of claims.
62
+ """
63
+ # Define claims with varying clarity.
64
+ claim_data = [
65
+ ("Water boils at 100°C at sea level.", 0.99),
66
+ ("Democracy requires informed participation.", 0.75),
67
+ ("Abstract concepts influence material outcomes.", 0.40),
68
+ ]
69
+ claims = [
70
+ Claim(identifier=hashlib.sha256(text.encode()).hexdigest()[:16], clarity=score)
71
+ for text, score in claim_data
72
+ ]
73
+ print("Claims:")
74
+ for c in claims:
75
+ print(f" ID:{c.identifier} | Clarity:{c.clarity}")
76
+
77
+ # Run the engine.
78
+ engine = ConsensusEngine(agent_count=10)
79
+ result = engine.process(claims)
80
+ print("\nConsensus Results (Acceptance Ratio):")
81
+ for claim_id, ratio in result.items():
82
+ print(f" {claim_id}: {ratio}")
83
+
84
+ if __name__ == "__main__":
85
+ run_demonstration()