Reputation: 23
I want to add the values of a variable ( let's say transportation cost) for specific turtles which are in a group. The transportation cost depends on the distance between a specific patch and the location assigned to the turtle. As the value of the said cost is different for each turtle, I want to sum the total cost for turtle which are in a group. To clarify, let's suppose there are 7 turtles in total and only 4 are in a group.
The values of transportation cost of each turtle is assigned as tcost.
to calculate-ttcost
set ttcost 0
let cnt 0
ask turtles [
if in-group? [
set ttcost (tcost + tcost)
set cnt cnt + 1
]
]
end
Upvotes: 1
Views: 136
Reputation: 4168
With the correction of one typo, the code you have should work, assuming that tcost
is declared as a turtles-own
variable with that turtle's transportation cost (or a reporter that gives the transportation cost for the turtle that calls it), and assuming that those turtles that are in the group in which you are interested have their turtles-own variable in-group?
set to true. The typo is in the line
set ttcost (tcost + tcost)
which should be
set ttcost (ttcost + tcost)
But there is a more straight-forward coding that will accomplish the same task.
let cnt count turtles with [in-group?]
let ttcost sum [tcost] of turtles with [in-group?]
with
limits the set of turtles to those for which in-group
is true. of
creates a list of the values of tcost
for each of those turtles, which can then be summed up.
Upvotes: 3