问题
I have changed many parts of my code to improve performance, now following is one of the most time consuming procedures in my code:
to deduct [Picking_Agent C]
If label_ = "Common Food Source"
[
Let witnesses_From_Other_Village other (agents in-radius vision with [Belongs_to != [Belongs_to] of Picking_Agent and member? Picking_Agent agents in-cone vision 100 ])
if any? witnesses_From_Other_Village
[Let Penalty (0 - (C / count witnesses_From_Other_Village * 10))
ask witnesses_From_Other_Village
[
Update_link_Values Picking_Agent Penalty
]
]
]
end
to Update_link_Values [Other_Agent Value]
if self != Other_Agent
[ ask Other_Agent [set popularity popularity + Value]
ifelse out-link-neighbor? Other_Agent
[ask out-link-to Other_Agent [ set Value-Of-The-Relationship Value-Of-The-Relationship + Value set Frequency Frequency + 1 ] ] ;IF already has a link
[create-link-to Other_Agent [set Value-Of-The-Relationship Value-Of-The-Relationship + Value set Frequency Frequency + 1 hide-link] ] ;If they meet for the first time
]
end
I had to use condition if self != Other_Agent
only to make sure my experiments does not end with error "that agent can not have links to itself", even I tried to make sure that agent asks other agents to update the link and never is itself!!
Name Calls Incl T(ms) Excl T(ms) Excl/calls
DAILY-TASK-WITH-LEADER 4598209 271.667 169791.762 0.037
DEDUCT 248639 168314.428 104789.036 0.421
....
UPDATE:
If the performance problem is in Picking_Agent agents in-cone vision 100
Then I think the right question to ask is how to find agents who can see the calling agent in the most efficient way? Using this method I have up to 10 eye witnesses for most of activities that can be witnessed, if I use in-radius this could be as high as 90 agents!
回答1:
I'm pretty sure your problem is here:
member? Picking_Agent agents in-cone vision 100
This is an extraordinarily slow means of determining whether one agent is in another agent's vision cone. Why? Because it includes this subexpression:
agents in-cone vision 100
which computes the set of all agents in the cone. Doing that is very expensive; in-cone
does tons of distance and angle calculations. But you don't need to know who all of those agents in the cone are! You already have an agent, and you just need to know if that one single agent is in the cone or not.
You should avoid in-cone
and compute your yes-or-no answer directly using such primitives as distance
and towards
. I'd suggest putting that computation in a separate procedure, and testing that procedure thoroughly in isolation to make sure you've got it right, before moving on. It doesn't take very much code, but the code it requires is tricky to get right. (Probably the language ought to include a primitive that does it, instead of making you write it yourself.)
来源:https://stackoverflow.com/questions/20113396/how-can-i-improve-following-function-in-netlogo