-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
227 lines (202 loc) · 7.1 KB
/
run.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"database/sql"
"fmt"
"log"
"math"
"math/rand"
"os"
"path/filepath"
"time"
"github.com/aliaksei135/abs-specific/hist"
"github.com/aliaksei135/abs-specific/sim"
"github.com/aliaksei135/abs-specific/util"
"runtime"
"strings"
_ "github.com/mattn/go-sqlite3"
"github.com/urfave/cli/v2"
)
func simulateBatch(batch_size int, chan_out chan []int64, bounds [6]float64, alt_hist, track_hist, vel_hist, vert_rate_hist hist.Histogram, timestep, target_density, own_velocity float64, path [][3]float64, conflict_dists [2]float64, surfaceEntrance bool) {
for i := 0; i < batch_size; i++ {
seed := rand.Int63()
traffic := sim.Traffic{Seed: seed, AltitudeDistr: alt_hist, VelocityDistr: vel_hist, TrackDistr: track_hist, VerticalRateDistr: vert_rate_hist, SurfaceEntrance: surfaceEntrance}
traffic.Setup(bounds, target_density)
ownship := sim.Ownship{Path: path, Velocity: own_velocity}
ownship.Setup()
sim := sim.Simulation{Traffic: traffic, Ownship: ownship, ConflictDistances: conflict_dists, TimeStep: timestep}
sim.Run()
sim.End()
pos_sum := 0.0
samples := int(math.Min(600, float64(len(sim.Traffic.Positions.RawMatrix().Data)-1)))
for i := 0; i < samples; i++ {
pos_sum += sim.Traffic.Positions.RawMatrix().Data[i]
}
chan_out <- []int64{int64(pos_sum), seed, int64(float64(sim.T) * sim.TimeStep), int64(sim.ConflictLog)}
}
}
// func parseBounds(boundStr string) [6]float64 {
// tokens := strings.Split(boundStr, ",")
// var out [6]float64
// for i, t := range tokens {
// out[i], _ = strconv.ParseFloat(t, 64)
// }
// return out
// }
// func parseConflictDists(conflictStr string) [2]float64 {
// tokens := strings.Split(conflictStr, ",")
// var out [2]float64
// for i, t := range tokens {
// out[i], _ = strconv.ParseFloat(t, 64)
// }
// return out
// }
func main() {
log.SetFlags(0)
start := time.Now()
app := &cli.App{
Version: "0.1a",
Usage: "Specific Traffic ABS",
Description: "Agent Based Traffic MAC Simulation",
Flags: []cli.Flag{
&cli.Float64SliceFlag{
Name: "bounds",
Usage: "W,E,S,N,B,T bounds in metres",
Required: true,
},
&cli.Float64Flag{
Name: "target-density",
Usage: "Target background traffic density in ac/m^3",
Required: true,
},
&cli.PathFlag{
Name: "altDataPath",
Usage: "Path to altitude data in metres as CSV",
Required: true,
},
&cli.PathFlag{
Name: "velDataPath",
Usage: "Path to velocity data in m/s as CSV",
Required: true,
},
&cli.PathFlag{
Name: "trackDataPath",
Usage: "Path to track data in deg as CSV",
Required: true,
},
&cli.PathFlag{
Name: "vertRateDataPath",
Usage: "Path to vertical rate data in m/s as CSV",
Required: true,
},
&cli.PathFlag{
Name: "ownPath",
Usage: "Path for ownship. Should be a nx3 CSV",
Required: true,
},
&cli.Float64Flag{
Name: "ownVelocity",
Usage: "Speed of the ownship along the defined path in m/s",
Value: 60.0,
},
&cli.IntFlag{
Name: "simOps",
Usage: "The total number of simulation runs to be done.",
Value: 1e2,
},
&cli.Float64SliceFlag{
Name: "conflictDists",
Usage: "X,Y distances in metres which define a conflict",
Value: cli.NewFloat64Slice(15.0, 6.0),
},
&cli.PathFlag{
Name: "dbPath",
Usage: "A path to the SQLite3 DB the results should be written to",
Value: "./results.db",
},
&cli.Float64Flag{
Name: "timestep",
Usage: "The number of real seconds per simulation timestep. Can be less than 1. Must be greater then 0.",
Value: 1.0,
},
&cli.BoolFlag{
Name: "surfaceEntrance",
Usage: "Boolean flag indicating whether traffic should only spawn at simulation volume surfaces",
Value: false,
},
},
Action: func(ctx *cli.Context) error {
bounds := (*[6]float64)(util.CheckSliceLen(ctx.Float64Slice("bounds"), 6))
target_density := ctx.Float64("target-density")
alt_hist := hist.CreateHistogram(util.GetDataFromCSV(util.CheckPathExists(ctx.Path("altDataPath"))), 50)
track_hist := hist.CreateHistogram(util.GetDataFromCSV(util.CheckPathExists(ctx.Path("trackDataPath"))), 50)
vel_hist := hist.CreateHistogram(util.GetDataFromCSV(util.CheckPathExists(ctx.Path("velDataPath"))), 50)
vert_rate_hist := hist.CreateHistogram(util.GetDataFromCSV(util.CheckPathExists(ctx.Path("vertRateDataPath"))), 50)
own_path := util.GetPathDataFromCSV(util.CheckPathExists(ctx.Path("ownPath")))
own_velocity := ctx.Float64("ownVelocity")
conflict_dist := (*[2]float64)(util.CheckSliceLen(ctx.Float64Slice("conflictDists"), 2))
dbPath := ctx.Path("dbPath")
simOps := ctx.Int("simOps")
timestep := ctx.Float64("timestep")
surfaceEntrance := ctx.Bool("surfaceEntrance")
if strings.HasPrefix(strings.ToLower(dbPath), "s3://") {
dbPath = filepath.Join(os.TempDir(), "results.db")
}
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec("CREATE TABLE IF NOT EXISTS sims(id, seed, timesteps, n_conflicts)")
if err != nil {
log.Fatal(err)
}
fmt.Println("Created/Opened output database")
result_chan := make(chan []int64)
n_batches := runtime.NumCPU()
batch_size := int(simOps / n_batches)
fmt.Printf("Running %v batches of %v simulations\n", n_batches, batch_size)
pathLength := util.GetPathLength(own_path)
expectedSteps := pathLength / own_velocity
simulatedHours := (expectedSteps * float64(n_batches) * float64(batch_size)) / 3600
fmt.Printf("Simulating %v hrs, with %v hrs per simulation\n", simulatedHours, expectedSteps/3600)
for i := 0; i < n_batches; i++ {
go simulateBatch(batch_size, result_chan, *bounds, alt_hist, track_hist, vel_hist, vert_rate_hist, timestep, target_density, own_velocity, own_path, *conflict_dist, surfaceEntrance)
}
sim_results := make([][]int64, n_batches*batch_size)
result_count := 0
for results := range result_chan {
sim_results[result_count] = results
result_count++
if result_count >= n_batches*batch_size {
break
}
}
fmt.Printf("Formatting %v results for database insertion\n", len(sim_results))
value_fmt := "(%v, %v, %v, %v)"
string_results := make([]string, len(sim_results))
for idx, row := range sim_results {
string_results[idx] = fmt.Sprintf(value_fmt, row[0], row[1], row[2], row[3])
}
values_str := strings.Join(string_results, ",")
fmt.Println("Inserting results into database")
_, err = db.Exec("INSERT INTO sims VALUES " + values_str)
if err != nil {
log.Fatal(err)
return err
}
_, S3Upload := os.LookupEnv("S3_UPLOAD_RESULTS")
if S3Upload {
fmt.Println("Uploading results to S3...")
util.UploadToS3(dbPath)
fmt.Println("Uploaded results to S3")
}
elapsed := time.Since(start).Seconds()
fmt.Printf("Completed successfully in %v seconds.\n %v ms per simulation.\n %v secs per simulated hour.\n", elapsed, elapsed/float64(1000*n_batches*batch_size), elapsed/simulatedHours)
fmt.Print("Exiting...\n")
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}