-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment_setup.nls
More file actions
297 lines (236 loc) · 8.44 KB
/
environment_setup.nls
File metadata and controls
297 lines (236 loc) · 8.44 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
;; netlogo code to define the map environment
breed [waypoints waypoint] ; objects to represent navigation waypoints
waypoints-own [
waypoint-number
]
breed [mines mine] ; mine agents
mines-own [
target-strength-coefficient
]
breed [obstacles obstacle] ;; agent representing an obstacle
obstacles-own [
target-strength-coefficient
]
globals [
nav-vector-data
nav-vector-data-v2
object-position-data
waypoint-data
minefield-data
]
to setup-world [file-name]
; programmatically resize the world based on the mission profile
ifelse ( file-exists? file-name) [
file-open file-name
while [ not file-at-end? ] [
resize-world file-read file-read file-read file-read
set-patch-size 0.2
]
file-close
] [
resize-world 0 99 0 99
]
end
to load-vector-data [file-name]
; We check to make sure the file exists first
ifelse ( file-exists? file-name)
[
; We are saving the data into a list, so it only needs to be loaded once.
set nav-vector-data []
; This opens the file, so we can use it.
file-open file-name
; Read in all the data in the file
while [ not file-at-end? ]
[
; file-read gives you variables. In this case numbers.
; We store them in a double list (ex [[1 1 9.9999] [1 2 9.9999] ...
; Each iteration we append the next three-tuple to the current list
set nav-vector-data sentence nav-vector-data (list (list file-read file-read file-read file-read))
]
; user-message "Navigation loading complete!"
; Done reading in patch information. Close the file.
file-close
]
[ user-message "Navigation file does not exist in current directory!" ]
;; define the mission vector field from the loaded data
ifelse ( is-list? nav-vector-data )
[ foreach nav-vector-data [ four-tuple -> ask patch first four-tuple item 1 four-tuple [ set behavior_x item 2 four-tuple set behavior_y item 3 four-tuple]]]
[ user-message "You need to load in patch data first!" ]
display
end
;; load vector data version 2
to load-vector-data-v2 [mission-directory]
; build filename /mission-directory/mission_leg_X.txt
; mission-data-container
; start at leg_0 and increas number while file-exists? store num_legs
; read leg-data and add to mission-data-container
; write behavior_x behavior_y for all legs to each patch
let file-name word mission-directory "mission_profile.txt"
; We check to make sure the file exists first
ifelse ( file-exists? file-name)
[
; We are saving the data into a list, so it only needs to be loaded once.
set nav-vector-data-v2 []
; This opens the file, so we can use it.
file-open file-name
; Read in all the data in the file
while [ not file-at-end? ]
[
; file-read gives you variables. In this case numbers.
; We store them in a double list (ex [[1 1 9.9999] [1 2 9.9999] ...
; Each iteration we append the next three-tuple to the current list
set nav-vector-data-v2 sentence nav-vector-data-v2 (list read-from-string (word "[" file-read-line "]"))
]
; user-message "Navigation loading complete!"
; Done reading in patch information. Close the file.
file-close
] [ user-message "Navigation file does not exist in current directory!"]
;; define the mission vector field from the loaded data
ifelse ( is-list? nav-vector-data-v2 )
[ foreach nav-vector-data-v2 [ four-tuple -> ask patch first four-tuple item 1 four-tuple
[
set behavior_x []
set behavior_y []
let i 0
while [i < (count waypoints - 1)] [
set behavior_x lput (item (2 + (2 * i)) four-tuple) behavior_x
set behavior_y lput (item (2 + (2 * i) + 1) four-tuple) behavior_y
set i (i + 1)
]
]
]
] [ user-message "You need to load in patch data first!" ]
display
end
; https://stackoverflow.com/questions/10244850/read-file-lines-with-spaces-into-netlogo-as-lists
;to-report read-file-into-list [file-name]
; file-open file-name
; let xs []
; while [ not file-at-end? ] [
; set xs lput file-read xs
; ]
; file-close
; report xs
;end
;
;to-report split-into-n-lists [ n xs ]
; let lists n-values n [[]]
; while [not empty? xs] [
; let items []
; repeat n [
; if not empty? xs [
; set items lput (first xs) items
; set xs but-first xs
; ]
; ]
; foreach (n-values length items [ ? ]) [
; set lists replace-item ? lists (lput (item ? items) (item ? lists))
; ]
; ]
; report lists
;end
to place-objects-from-file [objs-file]
;; read a file with the location of objects in the environment
; We check to make sure the file exists first
ifelse ( file-exists? objs-file )
[
; We are saving the data into a list, so it only needs to be loaded once.
set object-position-data []
; This opens the file, so we can use it.
file-open objs-file
; Read in all the data in the file
while [ not file-at-end? ]
[
set object-position-data sentence object-position-data (list (list file-read file-read))
]
;user-message "Obstacle file loading complete!"
; Done reading in patch information. Close the file.
file-close
]
[ user-message "Obstacle file does not exist in current directory!" ]
ifelse ( is-list? object-position-data )
[ foreach object-position-data [obs-coords -> create-obstacles 1 [
setxy first obs-coords last obs-coords
set shape "rock"
set size 3
set color gray
] ] ]
[ user-message "You need to load in obstacle data first!" ]
end
to place-random-objects [ number_of_objects xmin ymin xmax ymax ]
create-obstacles number_of_objects [
setxy (xmin + (random-float (xmax - xmin))) (ymin + (random-float (ymax - xmin)))
set shape "rock"
set size 5
set color gray
]
end
to load-mission-waypoints [waypoints-file]
;; read a file with the location of waypoints
; We check to make sure the file exists first
ifelse ( file-exists? waypoints-file )
[
; We are saving the data into a list, so it only needs to be loaded once.
set waypoint-data []
; This opens the file, so we can use it.
file-open waypoints-file
; Read in all the data in the file
while [ not file-at-end? ]
[
set waypoint-data sentence waypoint-data (list (list file-read file-read file-read file-read)) ; leg-number x-coord y-coord z-coord
]
;user-message "Waypoint file loading complete!"
; Done reading in patch information. Close the file.
file-close
]
[ user-message "Waypoint file does not exist in current directory!" ]
ifelse ( is-list? waypoint-data )
[ foreach waypoint-data [waypoint-coords -> create-waypoints 1 [
setxy item 1 waypoint-coords item 2 waypoint-coords
set waypoint-number item 0 waypoint-coords
set shape "triangle 2"
set color blue] ]
]
[ user-message "You need to load in waypoint data first!" ]
end
to lay-random-mines [number_of_mines xmin ymin xmax ymax ]
; lay a number_of_mines mines randomly in the box between (xmin, ymin) and (xmax, ymax)
create-mines number_of_mines [
setxy (xmin + (random-float (xmax - xmin))) (ymin + (random-float (ymax - xmin)))
set shape "target"
set color orange
] ; place the mines randomly
end
to lay-mines-from-file [minefield-file]
; We check to make sure the file exists first
ifelse ( file-exists? minefield-file )
[
; We are saving the data into a list, so it only needs to be loaded once.
set minefield-data []
; This opens the file, so we can use it.
file-open minefield-file
; Read in all the data in the file
while [ not file-at-end? ]
[
set minefield-data sentence minefield-data (list (list file-read file-read)) ; x-coord y-coord
]
;user-message "Minefield file loading complete!"
; Done reading in patch information. Close the file.
file-close
]
[ user-message "Minefield file does not exist in current directory!" ]
ifelse ( is-list? minefield-data )
[ foreach minefield-data [mine-coords -> create-mines 1 [
setxy item 0 mine-coords item 1 mine-coords
set shape "target"
set color orange] ]
]
[ user-message "You need to load minefield data first!" ]
end
to setup-mission-plan
;; read a file that defines the objective potential field
;; https://github.com/NetLogo/models/blob/master/Code%20Examples/File%20Input%20Example.nlogo
end
to setup-currents
;; read a file that defines the currents in the environment
end