# Simulation Documentation
###### tags: `others`
[TOC]
## Finite-state Machine
```graphviz
digraph Emodel {
node [shape=box] S, F, R, E, I, D
S -> R
S -> F
S -> E
F -> E
E -> I
I -> R
I -> D
I -> F
{rank = "same"; S; F}
{rank = "same"; R; D}
rankdir = "LR"
}
```
### State
- $S,F,E,I,R,D$ or more
- a tmp queue, update to the state at the end of a period
### Expiring state
- $E,I$
- maintain a *heap* where key is the expiration period/day
- `while(top() is expird) pop()`
- iterating
- $I$
### Infectious State
- $I$
### Susceptible State
- $S,F$
- maintain $A$ balanced trees for each contact group
- $A$ is the number of age groups
- support `insert`, `remove`
- support *randomly choose $k$ elements*
## Simulation
### simulation of a period
```python
def simulation_period(day, period):
for U in infectious_states:
for V in susceptible_state:
U.infect(V, period)
for U in expiring_states:
U.expire()
vaccination()
for U in states:
U.end_of_period()
```
### infect
```python
def infect(V, period): # U.infect(V, period)
for u in U:
for g in group(u, period):
sieve_algorithm(u, g, V, period)
```
### expiring
```python
def expire(): # U.expire()
while u = U.heap.top() is expired:
U.remove(u)
U.leave(u) # choose which state u is gonna be in
```
### at the end of a period
```python
def end_of_period(): # U.end_of_period()
for u in U.new_nodes_added_in_this_period:
U.add_new_node(u)
```
## Random
- $U(0,1)$
- randomly choose node
- $\mbox{Exp}(\lambda)$
- determine period
## I/O
- one file discribe the whole graph
- input and output in the same format
## Implementation
- `cpp`
- SIR model with naive algorithm
## Scalability
- population
- days and periods to simulate
- size of age group
- contact matrix
- finite-state machine
- $I_{hosp}, I_{presym}$
- infecting vaccined group in lower rate