Writing an if statement depending on an attribute of an agent to command agents of a different agentset - netlogo

I am trying to make an agentset do something, if an agent(of a different agentset) has a particular shape.
Here, if the shape of a particular
ghost (say Ghost 1) is circle,
then all rabbits are supposed to move forward 1. (<-This is the
intended behavior)
where
ghosts are agentset A
rabbits are agentset B
I have tried along these lines:
ask rabbits
[
if (shape ghost 1 = "circle")
[
forward 1
]
]
For this code I get,
"Expected a closing paranthesis here."
with a highlighter on ghost.
I'm aware that this code is wrong but I can't think of how else this should be written to get the desired result.

This will (I think - can't test) get the syntax correct:
ask rabbits
[
if ([shape] of ghost 1 = "circle")
[
forward 1
]
]
but you also have an ordering error and will have every rabbit check the shape of chost 1. I think what you really want is:
if ([shape] of ghost 1 = "circle")
[ ask rabbits
[ forward 1
]
]

Related

Operator > and combining it with number and a string in NetLogo

I want a little help. I want turtles to cooperate with 50% probability if their land parcels are > 5.
I am writing as
ifelse random 1 = 0 and land > 5
[set cooperate? true]
[set cooperate? false]
But it gives error that; The > operator can only be used on two numbers, two strings, or two agents of the same type, but not on a string and a number.
how to correct it?
Thanks
There's not quite enough information to diagnose the problem. Is this code inside an ask turtles block with the variable 'land' as a turtles-own attribute? Also, you might want to print off some values of 'land' to make sure you actually have numbers in it.
As you can see from the working example below, there is no error in the code you have provided.
turtles-own [ land cooperate? ]
to testme
clear-all
create-turtles 10
[ set land random 10
ifelse random 1 = 0 and land > 5
[ set cooperate? true ]
[ set cooperate? false ]
]
type "Cooperating: " print count turtles with [cooperate?]
type "Not cooperating: " print count turtles with [not cooperate?]
end

How to give a score when an element is pulled out from a list?

I would like to implement a part of code where agents can get a score when they pick an element from a list generated from a specific turtle.
I set
breed [playersA playerA]
breed [playersB playerB]
breed [balls ball]
playersA-own[
my-list
new_ball
score
]
playersB-own[
my-list
new_ball
score
]
to setup
clear-all
create-playersA 10
ask playerA 0 [ create-links-with other playersA ]
ask playerA 2 [ create-link-with playerA 1 ]
create-playersB 10
ask playerB 0 [ create-links-with other playersB ]
ask playerB 2 [ create-link-with playerA 1 ]
ask playersA[
set my-list []
set score 0
]
ask playersB[
set my-list []
set score 0
]
end
to go
let selected nobody
let team-player nobody
set selected one-of turtles with [breed=playersA or breed=playersB]
ifelse [breed = playersA] of selected[
ask selected [
set size [count link-neighbors] of self
show size
]
create-balls 1[
hide-turtle
]
]
[ ask selected [
set size [count link-neighbors] of self
show size
]
create-balls 1[
hide-turtle
]
]
set team-player link-neighbors with [breed = playersA]
ask team_player [
set my-list lput my-ball my-list
]
end
The above code should select on random turtle and add a new ball to its neighbours list. What I would need is probably a counter that can compute how many balls were shared between players.
Could you please help me to figure out with it?
Thanks
The code you posted has many problems that prevent it from passing error-checking in the editor. Some of these produce surprising error messages that don't even make sense, and they happen because the logic mixes contexts -- that is, some commands make sense for the "observer" level, some require being in a "turtle" context, etc.
I think you are trying to do too much at once, and trying to add a counter to code that already does not work. First you have to fix the code you have and then you can see where to add a counter.
You absolutely must understand how the unique agent id number "who" works. Each turtle has a unique who number assigned, starting with zero. It doesn't matter whether the breed of turtle is playerA or playerB or a ball, it will have a unique number. Once you create your first 10 turtles, of the PlayerA breed, they will have who numbers 0 through 9. Then, when you create the next 10 turtles, of PlayerB breed, they will get assigned who numbers of 10 through 19. If you then create a ball, say, it will have a who number of 20.
So there will never be a PlayerB with a who number of 0 or 1 or 2. Those numbers will already be used by PlayerA. Your setup will crash with the error:
playera 0 is not a PLAYERB error while observer running PLAYERB
called by Command Center
Even with just PlayerA, it is not clear what kind of network you want to build in the setup code. Why would everyone link to player 0, but then also add a single link between player 1 and player 2? Since players only "see" their linked team-mates, only player zero will see everyone else. Other players will have only one or two link-neighbors, so they will never update everyone else's my-lists.
create-playersA 10
ask playerA 0 [ create-links-with other playersA ]
ask playerA 2 [ create-link-with playerA 1 ]
Anyway, I would suggest that you get this much working correctly before trying to add counting.
I don't think you can do that by just looking at the code. You need to get rid of as much complexity as you can, and then use shapes, colors, and numerous print statements to see whether each command is doing what you think it should do. Complex working code almost always evolves from simple working code.
So get rid of PlayersB entirely ( comment out the code ), only create 5 players A, and change the colors and shapes as you process each step to confirm that it is working. The editor lets you use ctrl-; to comment out entire blocks of code, or un-comment them at once, so comment out everything you possibly can while you are getting one step to work, then uncomment the next section, get that to work, etc.
When you finally get everything working, you can comment out all your print statements that you used in development.
Anyway, I refactored your code, added many comment, and added many print statements, and finally got it to run. If you run just setup and look at the view, you will see what I mean about the network. ( I shut off wrapping in the view so the network looks right.)
Here's my revision of your code. It prints out what is in each player's my-list after each step, so you can see at a glance if it is doing what you want, or not. ( It's not.)
I added the who numbers as a label to each player in the view so you can see what I mean.
It produces helpful output like:
let's confirm that the lists have been updated. Here's the my-lists
for playersA [[5 5 5 5 10] [0 0 0 0 0] [0 8 8] [0 0] [0 9]]
Get the setup step to work correctly and generate the network you want before you even try to fix the go section.
breed [playersA playerA]
breed [playersB playerB]
breed [balls ball]
playersA-own[
my-list
new_ball
score
]
playersB-own[
my-list
new_ball
score
]
to setup
clear-all
;; for debugging, only create 3 players and inspect the results to confirm it's working as you intended
;; use labels to see player numbers in the view
create-playersA 5 [ set size 1 set color blue set shape "square" setxy random-xcor random-ycor set label who]
ask playerA 0 [ create-links-with other playersA [set color blue]]
ask playerA 2 [ create-link-with playerA 1 [set color red]]
create-playersB 5 [ set size 2 set color yellow set shape "person" setxy random-xcor random-ycor set label who]
; comment out this code until create-playersA is working properly
; ask playerB 0 [ create-links-with other playersB ]
; ask playerB 2 [ create-link-with playerA 1 ] ;; copy-and-paste error? link with playerB intended?
ask playersA[
set my-list []
set score 0
]
ask playersB[
set my-list []
set score 0
]
reset-ticks
end
to go
let selected nobody
let team-players nobody
let hot-ball nobody
set selected one-of turtles with [
breed = playersA
;; or breed = playersB ;; always select one of playersA for debugging this code
]
print ( word "At point 1, we selected turtle " [who] of selected " with breed " [breed] of selected)
;; we're still in the observer context here
ifelse [breed = playersA] of selected [ ;; by mentioning breed, we shift into a turtle context silently
print ( word " entering the TRUE part of the if-else statement " )
ask selected [
set size [count link-neighbors] of self
print ( word "at point 2 we set selected player's size to " size )
]
create-balls 1 [
set shape "circle" set size 3 set color blue set label who
set hot-ball who
; hide-turtle ;; for debugging show it so you can click on it and inspect it
print ( word "at point 3 we set created a blue hot-ball with who= " hot-ball )
]
;; it seems you want to update the selected turtle's my-ball variable here with a reference to the ball just created??
print " at point 4 we should set selected agent's my-ball to the ball we just made..."
ask selected [
set new_ball hot-ball
]
print (word " Confirming that selected player got the hot-ball " [new_ball] of selected )
;; ask ball hot-ball [ set hidden? true ]
;; this set of code seems to apply only when selected turtle is one of playersA, so it was moved INSIDE that ask-selected code
;; and put inside another ask selected [ ] context
ask selected [
set team-players link-neighbors with [breed = playersA]
print (word "At point 5, For selected player " who ", here is the team-players agent set :" )
print (sort team-players) ;; using "sort" here just to convert an agent set to a list for display
]
print " ------------- about to ask team-players to update their my-lists and change to triangles ---"
ask team-players [
set shape "triangle" set size 3 ;; to see at a glance that everyone was processed
set my-list lput new_ball my-list
print "... updated one my-list"
]
print " let's confirm that the lists have been updated. Here's the my-lists for playersA "
print map [ i -> [my-list] of i ] sort playersA ;; using "sort" to convert agent-set to a list
print (word "At the end of the go step, we have this many balls: " count balls)
]
;; else we should have breed != playersA
[
error " we should only be looking at one of playersA here for testing" ;; for debugging
]
;; tick
end

Netlogo IF statement, Excepted TRUE/FALSE here, rather than a list or block

I have an error message in the if statement says "Excepted TRUE/FALSE here, rather than a list or block".
I'm curious about why it gives this error and how to solve it !
I want to compare the position of the ball on the field with a position to do something.
My code is
ask balls [
fd 5
if[ [xcor] of ball 1 = 4 ] [
;;do something
]
]
You need
ask balls [
fd 5
if [xcor] of ball 1 = 4 [
;;do something
]
]
You don't need the [] around the condition you are testing and the [] is a signal to NetLogo that the contents are a list or a block, hence the error message.
Just looking at your code generally, I think you really want:
ask balls [
fd 5
if xcor = 4 [
;;do something
]
]
This will test the xcor of each ball, rather than the xcor of ball number 1. The way you have the code set up, then all balls will 'do something' when ball 1 is at the specified x-coordinates and no ball will 'do something' when it's not.

Netlogo if statement with myself and other

I'm fairly new to Netlogo and struggling with how to set up a somewhat complicated if statement. The statement is for turtles and the condition is that other turtles live in the same region and have a house.
I've tried the iterations of the following, but have not yet been successful:
if (one-of other turtles with [region = [region] of myself and house? = True]) []
if (other turtles with [region = [region] of myself and house? = True]) []
Thank you for any insights!
If you need to insert code in a question, check out the "Code Sample" button in the toolbar. You can highlight your code and click the button- super handy.
Your second try is extremely close. The quick fix is to add the any? primitive in order to tell Netlogo that you want to evaluate the agentset "other turtles" in this case. As it was, you weren't actually evaluating anything with if- kind of like saying "If the turtles in my region who have a house," as opposed to "If there are any turtles in my region who have a house."
to check-region
ask one-of turtles [
if any? other turtles with [ region = [region] of myself and house? = true ] [
set color white
]
]
end
If you need to evaluate more specific numbers, you can use something like count to set a threshold - for example:
to check-region-count
ask one-of turtles [
if count other turtles with [ region = [region] of myself and house? = true ] > 3 [
set color white
]
]
end

Logical Statement Determining if the intersection between two agentsets is empty

I have seen on here how to create an intersection or union of two agentsets, but I am trying to say if any turtle in agentset a is in agentset b, return true. I was trying
ifelse (member? (one-of my-intersections) destination-intersections)
but I am pretty sure this is just testing if one element in my-intersections is in destination -intersections instead of testing every element. Is there some way to use a for each? Or is there another functionality I am unaware of?
Again, I have already referenced
NetLogo two agentsets operations
Thank you!!
The most straightforward way to write such a test is:
to-report test [ as bs ]
report any? as with [ member? self bs ]
end
To try it:
create-turtles 5
show test (turtle-set turtle 0 turtle 1 turtle 2) (turtle-set turtle 3 turtle 4)
show test (turtle-set turtle 0 turtle 1 turtle 2) (turtle-set turtle 0 turtle 3 turtle 4)
will show:
observer: false
observer: true
It's not the most efficient way, however, because the with clause builds an intermediate agentset that's not really needed.
A faster test would be:
to-report test [ as bs ]
let result false
ask as [
if member? self bs [
set result true
stop
]
]
report result
end
Edit:
I spoke too fast. As per the NetLogo wiki page on compiler architecture, the combination of any? and with does get optimized to exit early. Bottom line: you should use the first version (i.e., any? as with [ member? self bs ]).