Error in Netlogo code once I turn on my boolean variable. Does anyone see the error? - netlogo

I am trying to develop an agent-based model of a Rock-Paper-Scissors game but want to know how this dynamic shifts once spatial interactions are turned on (spatial-interactions? is my boolean variable). The model runs fine when the boolean switch is turned off however, once I turn it on, NetLogo throws out an error (pasted below). There seems to be an error in the play-game procedure but the error is not obvious to me. I suspect that I am not using the netlogo primitive "with" correctly but I have looked at many examples and I dont see the problem with how I have it written in the code. Any help y'all can provide would be appreciated! I have also pasted the code below.
WITH expected input to be an agentset but got NOBODY instead.
error while turtle 8 running WITH
called by procedure PLAY-GAME
called by procedure GO
called by Button 'go'
to play-game
let orange-morphs count co-players with [strategy = "orange"]
let yellow-morphs count co-players with [strategy = "yellow" ]
let blue-morphs count co-players with [strategy = "blue" ]
if strategy = "orange" [
set payoff payoff + (0 * yellow-morphs) + (1 * orange-morphs) + (win-payoff * blue-morphs)
]
if strategy = "yellow" [
set payoff payoff + (1 * yellow-morphs) + (win-payoff * orange-morphs) + (0 * blue-morphs)
]
if strategy = "blue" [
set payoff payoff + (win-payoff * yellow-morphs) + (0 * orange-morphs) + (1 * blue-morphs)
]
end

With your spatial dynamics, I assume that you get cases when your agents are not close enough to any other agents so that they have nobody to play against. This error happens when there are no agents grouped in the agentset co-players.
You can circumvent this by adding if (any? co-players) [ ... ] at the start of this procedure, with the entire rest of the procedure within the square brackets. That causes them not to try playing if there is nobody around to play with.

Related

Selecting patch from an agentset with highest value in netlogo

I have an agent set named "open patches", which I found using the following, in which "number_open_patches" is a specific radius depending on "length":
`ask turtles ['
let my_patch self
set number_open_patches ((50 * (length ^ 2)) / 100) / resolution
let open_patches patches in-radius number_open_patches
I found the distance of these patches to a particular turtle "my_patch" using:
let distance_patch [distance my_patch] of open_patches
I then calculate "patch attractiveness", which is just the distance of each of the patches ("distance_patch") in "open_patches" plugged into the equation below:
let patch_attractiveness ( map [[x] -> exp ((x / open_patches) * (log e (.2 / 1)))] distance_patch)
I would finally like to select the patch within "open_patches" that has the greatest value of "patch_attractiveness". I am using the code below:
let destination max-one-of open_patches [patch_attractiveness]
I get an error as it returns 'nobody'. Why would this be? How do I know that I would actually be selecting the right patch as "distance_patch" is just a list in random order?
EDIT: I also tried the code below, but am getting a "division by zero" error when I try to calculate patch attractiveness.
ask turtles [
let my_patch self
set number_open_patches ((50 * (length ^ 2)) / 100) / resolution
let open_patches patches in-radius number_open_patches
ask open_patches [
set distance_patch [distance self] of my_patch
set patch_attractiveness exp ((distance_patch / number_open_patches) * (log e (.2 / 1)))
]
]
You didn't give enough code to see what the problems are. Your line with the exponential dividing by an agent set doesn't seem syntactically correct. You don't show the code for calculating open_patches_in_radius and I suspect there are none and the rest of the line using that is just a distraction.
That said, I'd suggest going the opposite way.
let the patches own a distance and an attractiveness variable
but "distance" is a reserved word so it needs a different name.
ask the open-patches to determine their distance to your "my_patch" turtle and store that in their distance slot. ( instead of asking your patch )
compute whatever global things you need for your formula
ask open-patches to set their attractiveness based on their distance ( already computed) and some exponential decay formula that apparently includes some global factor.
then finally let your destination be the open-patch with the max attractiveness, which now has a much simpler formula using
"max-one-of agentset [reporter]" syntax, such as
let dest max-one-of open-patch [ attractiveness ]
or possibly you want only open-patches within a given radius, but again depending on the scattering of open-patches and the size of the radius, there may be none. Check for none when setting open-patches-in-radius because that's a valid subcase.

Error: this isn't something you can use set on netlogo

I am trying to write a piece of code which asks some of the turtles to
calculate 2 parameters, compare them and then if one is lower than the
other, subtracts a characteristic of turtles by 1.
here is the code I wrote but I receive this error: "this isn't something you
can use set on netlogo"
set monthly-electricity-consumption random-float((monthly-electricity-demand * 1.2) - (monthly-electricity-demand * 0.8)) + (monthly-electricity-demand *
0.8)
ask turtles with [shape = "triangle"] [
if monthly-electricity-consumption > monthly-electricity-demand [
set [company-loyalty-level] of self company-loyalty-level - 1]]
Do you have any advice?
I'm assuming that the line
set [company-loyalty-level] of self company-loyalty-level - 1]]
is the line that generates the error. NetLogo does not allow one turtle to directly set the variables of another turtle by using the of construction. E.g.,
ask turtle 1 [set [company-loyalty-level] of turtle 2 (company-loyalty-level - 1)]
breaks that rule. In your case, by using self, turtle 2 and turtle 1 are the same turtle, but NetLogo will still throw that error. The line that you want is simply,
set company-loyalty-level company-loyalty-level - 1]]
Once you are within an ask, the variable is understood to be the one associated with the turtle being asked. The of self is not necessary.

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

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
]
]

Netlogo error when using procedure from code example Many Regions Example procedure

I'm new in Netlogo . I'm trying to use the code included in the Many Regions Example of the library. But I get one error in this procedure
to keep-in-region [ which-region ]
if region != which-region [
let region-min-pxcor first item (which-region - 1) region-boundaries
let region-max-pxcor last item (which-region - 1) region-boundaries
let region-width (region-max-pxcor - region-min-pxcor) + 1
ifelse xcor < region-min-pxcor [
set xcor xcor + region-width ]
[if xcor > region-max-pxcor [
set xcor xcor - region-width
]
]
]
The error I get is .... any advices? Thanks in advance
-1 no esta mas que o iqual a cero.
error while a-seller 31 running ITEM
called by procedure KEEP-IN-REGION
called by procedure ADJUST
called by procedure GO
called by Botón 'go'
The error you are getting ("-1 isn't greater than or equal to zero.", once translated to English) is caused by passing -1 as an index for the item primitive.
There are two lines in the code that make use of item:
let region-min-pxcor first item (which-region - 1) region-boundaries
let region-max-pxcor last item (which-region - 1) region-boundaries
As you can see (which-region - 1) is the expression passed to item as index. If you are getting -1, it must be because which-region = 0.
In the "Many Regions Example" model, region 0 is reserved for the patches that are not part of any regions, namely, the patches that act as region boundaries. The regions themselves are numbered from 1.
Look in your code for the place where keep-in-region is called and make sure that you are using a region number that is between 1 and the number of regions you have (inclusively).
Disclaimer: I originally wrote the "Many Regions" code example. Maybe I could have used a value like nobody for the patches outside of any region and number the actual regions starting from 0 instead of one, but I don't remember if I actively decided against it or just didn't think of it.

netlogo: send message to all but one agent

I'm new at NetLogo. I'm not new at agent-based modeling. Now and then I see people saying NetLogo is so fun and easy that I'm giving it a try.
I'm stuck at square 1, can't figure out code for messaging between agents, scheduling actions for subsets of agents, other basic ABM things.
I'm working on a majority rule election model in which the turtles are voters with randomly placed x,y coordinates. Candidates (a new breed) make promises in x,y plane, the winning candidate is the one preferred by (closer to) more agents.
The patches play no role at all, except the spatial graph seems to require use of a grid
I'm having trouble in the updating of candidate positions. I conceived this as follows: Remove a 'winner' agent from the candidate agentset, tell the other candidates to adjust, then put the winner back in the agent list and see who wins in the next time.
I believe the following is close to minimum reproducible example. Please look at adjustOfferings. (Sorry, I left in a lot of show statements, otherwise can't tell what's going on.)
; Paul Johnson
; 20160801
breed [candidates candidate]
globals[
N
D
]
; turtles are voters, Euclidean-based preferences. Use
; built-in 2d coordinates for ideal points for now,
; need to learn how to create weighted multi-dimensional
; preferences later
turtles-own
[
name
]
to setup
ca ; clear all
reset-ticks ; time start 0
set N 100
; N voters with random x,y ideal points
create-turtles N
[
setxy random-xcor random-ycor
]
set D 2
create-candidates D
[
setxy random-xcor random-ycor
set shape "square"
set size 2
]
end
; collect up the votes. Don't worry about invading agent
; privacy, just measure for them
to-report election2 [choices]
let fave [min-one-of choices [distance myself]] of turtles
report fave
end
; Ask all non winners to adjust their offering
to adjustOfferings [winner]
show (word "in adjustOfferings, winner was: " winner)
let losers candidates with [self != winner]
show (word "adjustOfferings losers are: " losers)
ask losers
[
setxy random-xcor random-ycor
]
end
; ask each voter for its favorite,
; use modes to calculate the winner
to electionCycle
let theVotes election2 candidates
show (word "electionCycle: " theVotes)
let winner modes theVotes
show (word "electionCycle Winner: " winner)
adjustOfferings winner
end
to go
tick
if ticks > 1000
[
stop
]
electionCycle
end
The setup works fine, I see agents and candidates. When I hit the "Step button", I can see the election works, a winner is selected, but all candidates adjust (not just losers). Here's the console:
observer: "electionCycle: [(candidate 100) (candidate 101)
(candidate 101) (candidate 101) (candidate 100) (candidate 101)
[snip]
(candidate 100) (candidate 101)]"
observer: "electionCycle Winner: [(candidate 100)]"
observer: "in adjustOfferings, winner was: [(candidate 100)]"
observer: "adjustOfferings losers are: (agentset, 2 turtles)"
Appears to me this is the source of error:
let losers candidates with [self != winner]
The winner is still in losers after that.
I realize now you can't actually run this without the rest of the NetLogo file. What is your custom here for providing the fully runable example? Can I point you at a file on a website? http://pj.freefaculty.org/scraps/majority.nlogo. I could put this in GitHub or someplace else if you prefer.
I appreciate your advice.
The problem is indeed with the line you suggested. Both agents report true for the condition [self != winner], but why is it the case. Look at how you create and pass the winner variable to the function. You create it by using the modes primitive, which is a good idea. But modes will report you a list with one element. The problem is that you pass this list directly to your adjustofferings function. This means, the agents than compare [self != [(candidatexy)]]instead of [self != (candidatexy)] (mind the additional brackets in the first example, indicating the list).
To solve the problem, you have to extract the winner agent from the list by using the first primitive, which reports you the first item of a list. You only have to add this to the line, where you define the winnervariable: let winner first modes theVotes