Spaces:
Sleeping
Sleeping
Create src/world.rs
Browse files- src/world.rs +60 -0
src/world.rs
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
use crate::vm::BCNVM;
|
| 2 |
+
use crate::programs::CREDIT_REQUEST;
|
| 3 |
+
use rand::Rng;
|
| 4 |
+
|
| 5 |
+
pub struct World {
|
| 6 |
+
pub agents: Vec<BCNVM>,
|
| 7 |
+
pub tick: u64,
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
impl World {
|
| 11 |
+
pub fn new(n: usize) -> Self {
|
| 12 |
+
let mut agents = Vec::new();
|
| 13 |
+
|
| 14 |
+
for i in 0..n {
|
| 15 |
+
let id = format!("node-{}", i);
|
| 16 |
+
let vm = BCNVM::new(CREDIT_REQUEST.to_vec(), id);
|
| 17 |
+
agents.push(vm);
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
Self { agents, tick: 0 }
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
pub fn step(&mut self) {
|
| 24 |
+
self.tick += 1;
|
| 25 |
+
let mut rng = rand::thread_rng();
|
| 26 |
+
|
| 27 |
+
for agent in self.agents.iter_mut() {
|
| 28 |
+
let _ = agent.run();
|
| 29 |
+
|
| 30 |
+
// Crisis injection
|
| 31 |
+
if rng.gen_bool(0.01) {
|
| 32 |
+
agent.state.trust -= 20;
|
| 33 |
+
agent.state.default_risk += 0.2;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
// Productivity growth
|
| 37 |
+
agent.state.productivity += rng.gen_range(0..3);
|
| 38 |
+
|
| 39 |
+
// Flow redistribution
|
| 40 |
+
agent.state.flow_total += rng.gen_range(0..50);
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
// Peer mesh gossip
|
| 44 |
+
for i in 0..self.agents.len() {
|
| 45 |
+
let snapshot = self.agents[i]
|
| 46 |
+
.mesh
|
| 47 |
+
.local_snapshot(&self.agents[i].state);
|
| 48 |
+
|
| 49 |
+
for j in 0..self.agents.len() {
|
| 50 |
+
if i != j {
|
| 51 |
+
self.agents[j].mesh.ingest_peer(snapshot.clone());
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
for agent in self.agents.iter_mut() {
|
| 57 |
+
agent.mesh.reconcile(&mut agent.state);
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
}
|