Anisa Qureshi
Anisa Qureshi

Reputation: 1

Netlogo Prisoners Dilemma runtime and compiletime errors

I'm currently creating a code for the prisoner's dilemma game. In my adoption, the turtles represent logging companies and the patches are trees, the two companies are competing for the trees but can cooperate by regrowing them.

The problem I am having, is that the code below keeps creating a runtime error because of the ask turtle, it states:

Only the observer can ASK the set of all turtles. error while turtle 0 running ASK called by procedure DEFOREST

called by Button 'go'. However, if I get rid of the ask turtles segment then it creates a compile time error stating 'I can't use tricks in a turtle/patch context because tick is an observer only. So what can I do to fix this? I can temporarily take it out of my go function whilst I build on my code but at some point, I will need to invoke the code in the go function.

;; Action function to deforest

ask turtles [ ask patch-at 0 0 [ if pcolor = green [ set pcolor brown set money money + 50 set deforestation true ] ] ]
end

Upvotes: 0

Views: 78

Answers (1)

Alan
Alan

Reputation: 9610

Each procedure has a "context": which agent can run the procedure. Command are labeled with their context in the NetLogo dictionary. (Look for the little icons.) If you use an observer-only command, such as tick, then all commands must be observer commands. (Only the observer can run the procedure.) If you use a turtle-only command, then all commands must be turtle commands. (Only a turtle can run the procedure.) If you mix contexts, it is an error.

Now, any agent can run ask. However, NetLogo builds in a constraint: only the observer can ask turtles. This is because it is almost always an error to, e.g., have a turtle asking all turtles (including itself) to do something. You can however have a turtles ask other turtles. This is allowed but dangerous, in the following way. If you have 100 turtles, and each asks the other 99 to do something, you will run ask 9900 times. This is roughly the square of the number of turtles. So it is allowed, but you should think hard before you do it.

In sum, if you want to ask turtles in deforest, you need to make sure that only the observer is running deforest, not turtles. But on top of that, you almost surely do not want every turtle asking that one patch (over and over) to check for deforestation. That looks like an error in program logic.

Upvotes: 0

Related Questions