Reputation: 83
I am currently working on a project that requires the following: I want the port (a breed) to ask storage locations (another breed) whether they have a certain value. If not, that proceed to ask the next storage location etc. etc. until he finds that location with the correct value. If so, than it should take another action (like building something). I got this now, which seems to work but it is super long and over complicated I think.
to check-pipeline-26
ask storage 26
[ifelse pipeline < 1
[build-pipeline]
[check-pipeline-27]
]
end
to check-pipeline-27
ask storage 27
[ifelse pipeline < 1
[build-pipeline]
[check-pipeline-28]
]
end
to check-pipeline-28
ask storage 28
[ifelse pipeline < 1
[build-pipeline]
[check-pipeline-29]
]
end
to check-pipeline-29
ask storage 29
[ifelse pipeline < 1
[build-pipeline]
[check-pipeline-30]
]
end
Let me know if you have any tips to make this easier or simplified. Thanks in advance!
Max
Upvotes: 1
Views: 83
Reputation: 3806
Here's a recursive solution instead where I pass in the pipeline number as a parameter:
to check-pipeline [n]
if n <= max [who] of storages
ask storage n [ifelse pipeline < 1
[build-pipeline]
[check-pipeline n + 1]
]
end
Note: you probably keep track of your max-who in a variable rather than computing each time.
Alternatively, this may be a better solution where you could just use a for loop and sort the agents by their who to make them go in order:
to check-pipeline
foreach sort-on [who] storages
[astorage -> ask astorage[ if pipeline < 1 [build-pipeline stop]]]
]
end
Upvotes: 3