SagittariusA
SagittariusA

Reputation: 5427

Access attributes between linked agents in NetLogo

In a project I've created, for example, 1000 humans. Now, if a probability is satisfied, then I create a dog and I link him to a human. Here we go:

to setup-agents
 set-default-shape humans "person"
 set-default-shape dogs "default"

create-humans people [
 setxy random-xcor random-ycor
 set size 1.5 
 set color green
 set sick? false
]

let i 1
while [i <= people] [

  ifelse random 100 < 43 [

    create-dogs 1 [
     set size 0.5
     set color green
     set sick_dog? false
     create-link-from one-of humans [set tie-mode "fixed" tie hide-link]
   ]
  ] []

  set i i + 1  
]

   move

end

then , after the link is created I'd need to access an attribute of humasn (has_dog?) and set it to true.

humans-own [
  has_dog?
  sick?
]

How can I ask to THAT just linked human to set his attribute to true?

Upvotes: 3

Views: 371

Answers (1)

Nicolas Payette
Nicolas Payette

Reputation: 14972

I edited my answer to your previous question before I saw this one. You'll probably find most of what your looking for over there. That being said:

You probably don't need a has_dog? variable, as you can easily figure out if a human has a dog with a reporter:

to-report has-dog? ; human reporter
  report any? out-link-neighbors  
end

You might still need to access the owner of a dog, though. Here is a reporter for that:

to-report my-owner ; dog reporter
  report one-of in-link-neighbors
end

(Using links make it theoretically possible for a dog to have multiple owners, hence the one-of in the expression. But the way your model is set up, that should never happen. This code would also report nobody if a dog had no owner, which makes sense.)

Then, supposing your humans still had a has_dog? variable, you could do:

ask my-owner [ set has_dog? true ]

Some other quick points:

  • I notice you have an ifelse statement with an empty else clause. Why not just use if?

  • You're using a while loop with a dummy index just to repeat something a number of times. NetLogo has repeat for that.

  • The logic of your loop is such that one human could have multiple dogs, because one-of could report the same human many times. That's not implausible, but it might not be what you want.

  • The previous issue could be avoided by using ask humans instead of a while loop, and have the human hatch a dog with some probability. In general, it's very rare that while is the proper solution in NetLogo. You should try to think in terms of agentsets and use things like while and indexes as a last resort.

Upvotes: 2

Related Questions