imantha
imantha

Reputation: 3828

Netlogo, Finding the closest agent based on link distance

enter image description here

Hi everyone, So I am trying to solve a netlogo model where there are nodes linked to each other. Its a search and rescue based model where there are two types of agents population-agents and rescue-agents, whom are placed on a node. Similar to the image below, where orange and purple nodes represent nodes on which there is an pop/resc agent. The Rescue-agents need to find the closest population agent based on the links distance, and travel to them. The link distances for example are the numbers shown on the image below. (Note the idea is not to use min-one-of in-radius command which will give the closest distance as to the closest link distance) Any idea on how to find the closest population agent to each resc-agent?

Upvotes: 0

Views: 559

Answers (2)

imantha
imantha

Reputation: 3828

Yep managed to solve the problem. But it involves making many list. If ANY ONES GOT A BETTER SOLUTION OR IMPROVING THE FOLLOWING CODE. PLS DO LET ME KNOW. THANKS :)

I managed it to solve it by creating 3 lists, 1. a list for all the population agents (N-pop-list) 2. a list for all the nodes the population agents are on (N-pop-node-list) 3. and a list for distances, where each node will calculate the distance from itself to the node the pop-agents on. (N-pop-distance-list)

globals [G-target-agent G-node-target-agent]
breed [pop-agents pop-agent]
breed [resc-agents resc-agent]
breed [nodes node]

pop-agents-own [node-pop (;node the pop agents on)]
resc-agents-own [node-resc (;node resc-agents on) target-agent (;agent they want to 
travel to) target-pop-agent-at (;node target-pop-agents on)]
nodes-own [N-pop-list N-pop-node-list N-pop-distance-list]
links-own [dist (pretty much length/distance of the link !)]

...
to setup-lists

ask nodes [
let temp-pop-list []
let temp-pop-node-list []
set N-pop-distance-list []

ask pop-agents[
  set temp-pop-list lput self temp-pop-list
  set temp-pop-node-list lput node-pop temp-pop-node-list
]
set N-pop-list temp-pop-list
set N-pop-node-list temp-pop-node-list

foreach N-pop-node-list
[[i] ->
  let dist-to-node nw:weighted-distance-to i dist
  set N-pop-distance-list lput dist-to-node N-pop-distance-list
]
]
end

to search-pop-agent
ask resc-agent[
ask node-resc[
let min-val min N-pop-distance-list
let min-val-pos position min-val N-pop-distance-list
set G-target-agent item min-val-pos N-pop-list 
set G-node-pop-agent-at item min-val-pos N-pop-node-list
]
set target-agent G-target-agent
set node-pop-agent-at G-node-pop-agent-at
]
end

Upvotes: 2

JenB
JenB

Reputation: 17678

This is a job for the networks extension, which contains the primitives nw:turtles-in-radius and nw:weighted-distance-to to calculate distances along links.

Upvotes: 3

Related Questions