1

I have a problem in Netlogo, where i should check if a neighbor patch of a specific patch is already colored black.

This is how i thought it could work:

ifelse [ask patch xcoord-temp ycoord-temp [ask patches in-radius rnd_radius [pcolor = black]]]?
  [print "true"]
  [print "false"]

Error Message is: Expected a TRUE/FALSE here, rather than a list or block.

xcoord-temp and ycoord-temp are calculated coordinates.

rnd_radius is a random radius between 1 and 15.

So does someone has an idea how to solve this?

Thanks!

Regards, John

David Merinos
  • 1,195
  • 1
  • 14
  • 36
John
  • 11
  • 1

2 Answers2

0

Okay first, the reporter patch receives an identificator that's linked to a patch. If you want to ask something to a patch at a certain position you want to use [patch-at]1.

Second, I don't understand the use of question mark in your code ?.

Third, by using [pcolor = black] the compiler understands you want to set rather than get.

Lastly, you can use the word of to access values of an agent.

So I would put it out like this:

to myprocedure
  ask patch-at xcoord-temp ycoord-temp 
      [
        check-color
      ]
end
to check-color
      ifelse [pcolor] of (patches in-radius rnd_radius) = black
      [print "true"]
      [print "false"]
end

Note than I'm using an extra procedure that may not be complete, perhaps it would have to receive the temporal coordinates you use; unless these coordinates are global variables that change overtime, then you can use the procedures like that.

Please tell us if this helps you, I don't have NetLogo installed right now, I did that based on the dictionary.

Edit: As stated in the comments

[pcolor] of (patches in-radius rnd_radius) will return a list, thus it can never equal black.

You'll have to ask for an specific neighbor of the patch or perhaps use one-of in the list resulting from the previous statement..

David Merinos
  • 1,195
  • 1
  • 14
  • 36
  • `[pcolor] of patches in-radius ...` will return a list of colors, and a list can never equal `black`, so this will always print `false`. – Seth Tisue Oct 20 '15 at 17:26
  • You're right, I was only translating his code into mine. – David Merinos Oct 20 '15 at 18:13
  • Thanks for your fast answer! The problem is, that your solution makes another error: `You can't use XXX in an observer context, because XXX is turtle/patch-only.` I think it's because of the patch-at? – John Oct 21 '15 at 10:58
  • Your original code had `patch`, not `patch-at`. If you want absolute coordinates (not relative offsets), then `patch` is correct; perhaps David's substitution of `patch-at` was inadvertent. – Seth Tisue Oct 21 '15 at 13:32
  • @John of course, to use my code you'd have to fix yours so that only patches call `myprocedure` – David Merinos Oct 21 '15 at 22:18
0

The simplest and least verbose way would be to write a reporter procedure like this

to-report any-black-neighbors-in-radius? [r]
  report any? patches in-radius r with [pcolor = black]
end

You can then ask a patch to run this procedure using r as a parameter, and either print the value, or use it for something else. Like this:

ask one-of patches [print any-black-neighbors-in-radius 5]

If you want to ask a particular patch to run it, just ask that patch using patch, like so:

ask patch 5 -5 [print any-black-neighbors-in-radius 3]

Arthur Hjorth
  • 889
  • 4
  • 10
  • And if you want to test whether they are all black (rather than at least one), replace `any?` with `all?` – JenB Oct 21 '15 at 11:03
  • Thanks for the fast answer! Yes, this works, it's not exactly doing what i thought, but I think it's another problem i have to look about. So thanks very much! – John Oct 21 '15 at 11:14