1

I am trying to create a model in which a question is posed to turtles. Each turtle represents a unique domain of knowledge (1, 2, 3, 4, 5...) and each question contains a random subset of domains, e.g., (3 5 8).

I then want to ask turtles with IDs that are contained in the question to answer it. I am hoping to get some advice on:

  1. how to create the random array for the question (global variable) and;
  2. how to compare that against the turtle IDs.

This is my first time using NetLogo, so I apologise for the ignorance. And I'm definitely not asking you to write the proc for me but what I could use some advice on the right direction / commands to try.

For 2. the comparison, I have written the following but I'm unsure if this will work. My concern is if it is looking for an exact match of the question array with the turtle ID, a match will never be found. What I intend is, if a turtle ID is ANYWHERE in array, THEN [set expert? 2]:

to give-an-answer
  foreach question [
    if question = who [set expert? 2]
  ]
end

Very much appreciate any advice you can give me!

Thanks John

1
  • Thanks to Seth and King-Ink, I think I have cracked the problem. I'm just about to analyse the output to see if it makes sense, but here is the code: Commented Nov 4, 2014 at 13:40

2 Answers 2

2

Easier if you use an agentset instead of a list. Using agentsets is generally good style in NetLogo and usually easier than alternative approaches.

You can make your question with n-of:

set question n-of turtles 10

And then a turtle can check if it's in the question with member?:

if member? self question
  [ set expert? 2 ]

Though if you want all of the turtles who are part of the question to set expert? to 2, then it's easiest to do it all at once like this:

ask question [
  set expert? 2
]

Done! No need for if or member? or a loop.

A few other bits of advice:

  • In NetLogo, it's only idiomatic to end a variable with a question mark if it contains a true or false value. So set expert? 2 reads strangely. Why not set expert? true? If you really need to use an integer here, then you should remove the question mark from the variable name.

  • It's best to avoid using who numbers at all. Using who numbers in NetLogo almost always makes your code harder and more complicated to write — rarely easier.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, thank you, thank you! Not just for solving my problem but for picking up on some of the other flaws in my code. I'll post the completed code below...
1

Thank for a clear question.

Part one can be done with lput and repeat. Lput adds a member to the left side of a list. It uses the syntax

 Set mylist lput value mylist

Value would be your random domain of knowledge. Repeat repeats things n number of times thus.

 Repeat 10 [thing]

Where thing is some code you want repeated. Put it together in your context you get.

Set question lput (random number-of-domains) question

Part 2. The primitive you are looking for is member? It takes the form

 Member? Value list

It returns true if value is in the list otherwise false.

1 Comment

Thanks a million, King Ink

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.