vipasha shah
vipasha shah

Reputation: 19

getting "ASK expected input to be an agent or agentset but got the list [green-players" error in netlogo

This is the code of a 2 player game that I manipulated

o play-the-game
  if (any-friends-nearby?) [gain-energy]
  if (any-opponents-nearby?) [fight-opponent]
end

to-report any-friends-nearby?
  report (any? (turtles-on neighbors4) with [breed = [breed] of myself])
end

to-report any-opponents-nearby?
  report (any? (turtles-on neighbors4) with [breed != [breed] of myself])
end


to gain-energy
  set similar-nearby count ( turtles-on neighbors4 )
 with
  [color = [color] of myself]
  set total-nearby count (turtles-on neighbors)
  ;
  ;
  if (similar-nearby >= total-nearby - similar-nearby)
  [set energy energy + 5]
end

to fight-opponent
  let my-breed [breed] of green-players
  let my-color [color] of green-players
  let opponent-breed [breed] of red-players
  ;
  ;;
  ask my-breed
  [check-random-winner]

end

to check-random-winner
  let pick random-float 2
  let winner nobody
  ask turtles
  [if winner = nobody
    [ ifelse size > pick
      [set winner self ]
      [set pick pick - size] ] ]
end

to change-opponent
  ask red-players
  [ set breed green-players
      set color green ]
end

Sorry if it's a bit long but when I setup up and then press go "ASK expected input to be an agent or agentset but got the list [green-players...]"

How can I fix this? Also I'm very new to Netlogo and StackOverflow, apologies if I haven't asked my question properly.

Upvotes: 0

Views: 368

Answers (1)

Matteo
Matteo

Reputation: 2926

The error message tells you that you are passing a list (more specifically, a list of breeds) to ask, when it comes to ask my-breed.

This is because the my-breed local variable is determined by

let my-breed [breed] of green-players

Let's see what we have there:

  1. breed is a turtles-own variable: it holds the turtle's breed, and being an agents' variable can be used as a reported in the of construct (see below).

  2. of is a reporter: it takes a reporter (normally an agents' variable) on its left (in your case: breed) and either an agent or an agentset on its right (in your case: green-players). What of reports (i.e. what it outputs) is...

    • ... a single value if there is an agent on the right.
    • ... a list of values if there is an agentset on the right*.

    *Think about it: if I ask the color of your eyes (you are a single person, i.e. a single agent), you will tell me a single color. But if I ask the color of your friends' eyes (your friends are a group of people, i.e. an agentset), the only way for you to answer my question is to tell me a list of colors.

  3. green-players is an agentset: all of the agents whose breed is green-players (note that for NetLogo green-players is an agentset even if it contains 1 or 0 agents).

From this, we can see that in this case of reports a list of breeds, because it reports the breed of every agent that is part of green-players, hence it will report the list [green-players green-players green-players green-players ... ] which is as long as the number of green-players in the model. You can verify this by clicking setup and then running [breed] of green-players in the Command Center.

This is a list of breeds (which can also be seen as a list of agentsets), which is not an agent or an agentset (which are the only possible targets of ask).

((note that the exact same thing happens with let my-color [color] of green-players and let opponent-breed [breed] of red-players))

So, how do you construct an agentset based on a variable? The most common way to do it is by using with (read here why).

But how can you fix your code? I don't know because I don't understand what you want to achieve.

I am not sure how you would want to use it in the code you posted, as I'm not even sure you need to use ask in fight-opponent (let alone ask an agentset). Your fight-opponent procedure is such that, apart from the problem we just discussed, the "my-" things (i.e. my-breed and my-color) always refer to the green players while opponent-breed always refers to the red players - even if fight-opponent is run by a red player! And also, it is not clear what you want to achieve with the check-random-winner procedure and if you want this procedure to be ran by an entire breed. These things make it quite confusing to understand how you could want to fix the fight-opponent procedure.

For example: who do you want to run the check-random-winner command?

A combination of two things would be beneficial: develop your model one step at a time and make sure that every new little piece of code does exactly what you expect it to do; also, when you ask for how to fix something it is useful that you explain what you want your code to do. By doing these two things I believe it will be a lot easier to answer your questions

Upvotes: 1

Related Questions