Reputation: 67
I create 3 cars, but I only have routes for the car with ID 0 and 1:
Car 0 -> route [4 3]
Car 1 -> route [7 6 5]
Car 2 -> route [] because I want this car stopped and it's spare
I want create a crash? variable for the cars, ie, IF reach 25 ticks, the variable crash? It is TRUE, and one of the cars in route stops ...
In my go procedure i try put this, but one-of only :
to go
ask carr with [ not route-complete? and not crash?] [
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
if route = [] [
set target spawn-target
]
if target = nobody [
set target item route-counter route
]
face target
ifelse distance target > 1 [
fd 1
] [
move-to target
ifelse target != spawn-target [
set route-counter route-counter + 1
] [
set route-complete? true
]
set target nobody
]
if route-counter > length route - 1 [
set route-counter 0
set target spawn-target
]
]
tick
end
I should only stop one-of the cars that has a route and not count the car with ID 2 that has already stopped. because I think this code accounts for all the cars and choose one to stop, but I want to just stop or the car 0 or 1. How to fix?
Then, how can I define a car to be the spare car (car with ID 2) to take the stopped car route to visit the missing homes? Because at 25 ticks if one of the cars 0 or 1 stop, there will surely be houses that have not been visited ...
If anyone can help me in two tasks that can not develop, would greatly. Thank you
Upvotes: 1
Views: 99
Reputation: 10301
I think easiest might be to just get the car that 'crashes' to pass on the remainder of its route on to the spare car using sublist
to index the route
variable- check out the dictionary definition for more detail on that. In the interest of changing your code as little as possible, try inserting the following code right at the beginning of your go
procedure (code comments give more detail, full go
procedure below):
to go
; only run this chunk if ticks = 25
if ticks = 25 [
ask one-of carr with [ not crash? and not route-complete? ] [
; get one of the still moving cars to:
; set crash? to true, change color to yellow
; for easy visibility
set crash? true
set color yellow
; make a temporary variable to hold the remaining house
; targets using sublist
let remaining-route sublist route route-counter ( length route )
ask one-of carr with [ route = [] ] [
; have the 'spare' care take over the remaining-route,
; set route-complete to false, and set the current
; target to 'nobody' so that the other conditions for
; movement are satisfied
set route remaining-route
set route-complete? false
set target nobody
]
]
]
;; ask carr with route-complete set to false
ask carr with [ not route-complete? and not crash? ] [
;for the spare car not to move
if route = [] [
set target spawn-target
]
if target = nobody [
set target item route-counter route
]
face target
ifelse distance target > 1 [
fd 1
] [
move-to target
ifelse target != spawn-target [
set route-counter route-counter + 1
] [
set route-complete? true
]
set target nobody
]
;; If the route counter would index outside of the
; list boundaries, reset it to 0
if route-counter > length route - 1 [
set route-counter 0
set target spawn-target
]
]
tick
end
Note that as-is, the active carr
can technically (rarely) complete their route before ticks
= 25. To avoid this, either stop the model when the routes are complete or work around some other way.
Upvotes: 2