---
# System prepended metadata

title: Demo and Problems encountered
tags: [others]

---

# Demo and Problems encountered
###### tags: `others`

[TOC]

## Initialization

Done

## Simulation

SIR model

### 進度
- simulation unit

```cpp
void simulate() {
    for (auto& ts : tm) {
        simulate_unit(ts);
    }
}
```

```cpp
Nodes s2i;
for (uint i = 0; i < I.size(); ++i) {
    Node u = I[i];  
    for (auto cgp : u.getGroups()[ts.getPeriod()]) {         
        Nodes tmp = S.infected(u.getAge(), cgp, prob_transmission);
        while (tmp.size()) {
            s2i.push_back(tmp.back());
            S.erase(tmp.back());
            tmp.pop_back();
        }
    }
}

Nodes i2r = I.expire();
```

- Seive algorithm

```cpp
auto& vagp = std::vector<VectorAgedGroup>::at(cgp.getID());
if (vagp.size() == 0) return Nodes();

double pmax = vagp.getPmax(src) * ptrans;
double k = std::ceil(pmax * vagp.size());

Nodes ch = vagp.choose(k), re;    
for (auto v : ch) { 
    double p = vagp.getContactGroup().getContactMatrix().getRate(src, v.getAge()) * ptrans;
    if (Random::trail(p / pmax)) {
        re.push_back(v);
    }
}
return re;
```

- main function

```cpp
SIRModel model;

std::ifstream fin_graph("./data/graph_10x10.txt");
std::ifstream fin_param("./data/param_sir.conf");
std::ifstream fin_init("./data/init_sir.conf");

model.load(fin_graph, fin_param, fin_init);

model.simulate();
```

- a testing result

```cpp
void statistic(const Nodes& s2i, const Nodes& i2r) {
    std::cout << "statistic " << s2i.size() << ' ' << i2r.size() << '\n';
}
```

![](https://i.imgur.com/e9EJDLc.png)



### TODO

- red black tree
    - maintain size of tree for all nodes(as root)
```cpp
Node& random_choose_one(Node& cur) {
    if (prob(1 / cur.size()) {
        return cur;
    }
    
    if (cur.has_left() && prob(cur.left.size() / (cur.size() - 1))) {
        return random_choose_one(cur.left);
    }
    return random_choose_one(cur.right);
}
```

- statistic module

## Visualization and Statistics
https://covid-19-vaccine-simulator.herokuapp.com/
- ![](https://i.imgur.com/RYdyFaE.png)
- csv columns
    - town_stat.csv
    **key**: Period, Town
    
    | Period | Town | num_infection | num_dead | ratio_infection | ratio_dead |
    | ------ | ---- | ------------- | -------- | ---------------- | ----------- |
    - country_stat.csv
    **key**: Period, Country

    | Period | Country | num_infection | num_dead | ratio_infection | ratio_dead |
    | ------ | ------- | ------------- | -------- | ---------------- | ----------- |