-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (109 loc) · 3.45 KB
/
main.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"github.com/fogleman/gg"
"gopkg.in/yaml.v3"
"image"
"image/png"
"io/ioutil"
"log"
"math/rand"
"os"
"runtime"
"time"
"wicsie/agents"
"wicsie/constants"
"wicsie/drawing"
"wicsie/heatMapDecoder"
"wicsie/simulation"
)
type config struct {
PopulationMap string `yaml:"populationMap"`
MaskMap string `yaml:"maskMap"`
Behaviour string `yaml:"behaviour"`
Steps int `yaml:"steps"`
Weight float64 `yaml:"weight"`
StatsOut string `yaml:"statsOut"`
}
type spreadingPoint struct {
X int `yaml:"x"`
Y int `yaml:"y"`
Probability float64 `yaml:"probability"`
}
func (c *config) readConfig(LOGGER *log.Logger) {
file, err := ioutil.ReadFile("config/config.yml")
if err != nil {
LOGGER.Fatalf("[Config] Error reading config file: %v", err)
}
err = yaml.Unmarshal(file, c)
if err != nil {
LOGGER.Fatalf("[Config] Error parsing config file: %v", err)
}
}
func readProbabilities(LOGGER *log.Logger) []*spreadingPoint {
file, err := ioutil.ReadFile("config/diseaseStart.yml")
if err != nil {
LOGGER.Fatalf("[Config] Error reading probabilities file: %v", err)
}
var probabilities []*spreadingPoint
err = yaml.Unmarshal(file, &probabilities)
if err != nil {
LOGGER.Fatalf("[Config] Error parsing probabilities file: %v", err)
}
return probabilities
}
func initSystem() {
image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
rand.Seed(time.Now().UnixNano())
}
func createSimulation(cfg config) (*simulation.Simulation, *agents.GridMap, int, int) {
heatMap, _, _, width, height := heatMapDecoder.LoadAndDecode(fmt.Sprintf("config/%s", cfg.PopulationMap))
legend := heatMapDecoder.ReadPredefined()
grid := agents.CreateGridMap(width, height, constants.KChunkSize)
script := agents.DecodeFile(fmt.Sprintf("config/%s", cfg.Behaviour))
movementCreation := func() agents.Movement {
return agents.CreateScriptMovement(grid, script, heatMap)
}
return simulation.CreateSimulation(simulation.Config{
Weight: cfg.Weight,
Width: float64(width),
Height: float64(height),
Movement: movementCreation,
Spreading: agents.CreateParallelGridSpread(grid),
HeatMap: heatMap,
LegendIndex: legend,
}), grid, width, height
}
func preInfectSystem(simulation *simulation.Simulation, LOGGER *log.Logger) {
points := readProbabilities(LOGGER)
for _, point := range points {
simulation.InfectAtPosition(float64(point.X), float64(point.Y), point.Probability)
}
}
func runSimulation(simu *simulation.Simulation, board *drawing.Board, grid *agents.GridMap, config config) {
dw := simulation.CreateDataWriter(fmt.Sprintf("out/%s", config.StatsOut))
for i := 0; i < config.Steps; i++ {
simu.Step()
grid.UpdateGridMap(simu.GetAgents())
board.DrawGridMap(*grid)
board.SaveBoard(fmt.Sprintf("out/raw/boardgrid%d.png", i))
dw.Write(i, *grid)
}
}
func main() {
LOGGER := log.New(os.Stdout, "[MAIN] ", log.Ltime)
LOGGER.Printf("Starting simulation\n")
LOGGER.Printf("Found %d cores\n", runtime.NumCPU())
LOGGER.Printf("Using %d cores\n", runtime.GOMAXPROCS(runtime.NumCPU()))
var config config
config.readConfig(LOGGER)
initSystem()
mask, err := gg.LoadImage(fmt.Sprintf("config/%s", config.MaskMap))
if err != nil {
LOGGER.Fatalf("Could not load mask: %v", err)
}
simu, grid, w, h := createSimulation(config)
preInfectSystem(simu, LOGGER)
board := drawing.CreateBoard(w, h, mask, 1, config.Weight)
runSimulation(simu, board, grid, config)
}