I exported a landscape from NetLogo. I imported this same landscape in Netlogo. From this landscape, I added another global variable specific to patches "residency-time":
globals [
list-of-polygons ]
patches-own [
area-ha
residency-time
]
I created a function to assign a value of residency time to each polygon in my landscape
to assign-residency-times
set list-of-polygons ([ID-polygon] of patches)
set list-of-polygons remove-duplicates list-of-polygons
set list-of-polygons remove 0 list-of-polygons
print sort list-of-polygons
foreach list-of-polygons [
ask patches with [ ID-polygon = ? ] [
if all? patches [pcolor = green] [
set residency-time exp(1.02 * area-ha) ]
.... ] ]
I don't understand why I have this error message : Nothing named area-ha has been defined. However, I defined area-ha dans patches-own.
In addition, when I inspect a patch, all global variables specific to patches disappeared (see figure below) !
Thanks in advance for your help.
I only have 45 reputation, so I can't just post a comment. But: It looks like you haven't defined ID-polygon as a patch-variable. This may be causing a problem earlier on in your code, and that may cause the code never to compile which would mean that patch vars are never actually added to patches in the "Interface" view.
This is assuming that you haven't defined a patch reporter called ID-polygon, of course. It's hard to tell without the full code.
Related
I was trying to personB's own variable to update in personA's own list type variable. when I tried to run this code i recived an error message "PERSONB breed does not own variable LEDGER
error while personbs 1 running LEDGER, called by procedure UPDATE-LEDGER...."
I cant change the ownership(If I make it global others can alter). how can I solve this?
My code is:
breed [personAs personA]
breed [personBs personB]
personAs-own [
wallet
ledger
]
personBs-own [
packid
batch-num
]
to start
ca
create-personAs 1 [
setxy random-xcor random-ycor
set shape "person"
set ledger []
]
create-personBs 1 [
setxy random-xcor random-ycor
set shape "person"
set packid 10
set batch-num who
]
update-ledger
end
to update-ledger
ask personBs [
set ledger fput (list packid batch-num) ledger
]
end
[Before starting: please note that in NetLogo breeds are defined first in their collective form and later in their individual form, e.g. breed [cats cat] instead of breed [cat cats], so you should change your breeds declarations accordingly. I'll give my answer as if you used the correct declaration, e.g. breed [personAs personA]. Also, note that breeds in NetLogo are not sensible to lower/upper case, so in general you might also want to give more meaningful names to make it easier to read]
--
It is possible to change turtles' breed by just asking them to
set breed newbreed
This will change their breed and, accordingly, all their breed-own variables (i.e. the turtle will lose its previous breed's variables and will gain its new breed's variables).
However your problem seems to be more fundamental: why did you make ledger a personAs-own variable if you want personBs to use set ledger? This is the crucial point.
There are two possibilities here:
You want each turtle to have a ledger to operate on. In that case, ledger must not be a personAs-own variable; it has to be a turtles-own variable (or, if in your full model you have other breeds who do not need ledger, then make it both a personAs-own and personBs-own variable). In any case, no global variables involved (a global variable only has one value for everyone, so that's not what you need).
You want personBs to be able to operate on the ledger of personAs. In that case, personBs will have to ask personAs to modify their ledger; or, alternatively, some personBs can operate on the ledger of some personAs by indeed using the of reporter (see here); for example:
; Solution with 'ask':
ask one-of personBs [
ask one-of personAs [
set ledger 10000
]
]
; Solution with 'of':
ask one-of personBs [
set [ledger] of one-of personAs 10000
]
Important update
Your expression "If I make it global others can alter" suggests me that you're thinking in terms of other programming languages, where you classify variables based on the level of access to that variable that other parts of the programme have.
Note that this is not the case in NetLogo: all parts of the programme (in this case: all agents) have access (even if indirect) to every variable through ask and/or of. No matter whether a variable is turtles-own, links-own, breed-own or patches-own. Every agent can read any other agent's variables. But also: every agent can modify any other agent's variables (see the code examples that I made regarding point 2 above). Therefore, making a variable something-own simply means that those are the agents carrying that variable.
I got 2 large sets of agentbreeds1 (villagers) and an equal amount of agentbreeds2 (houses). I am attempting to link these two together on a one-to-one ratio. So far I tried to do this by the following line, but I am not sure whether it does so correctly:
ask villagers [
create-link-with one-of other houses
]
Just for clarity, if I have villager1 it may only connect to one house that does not have any link to another villager already.
Hopefully one of the bright folks here can help me further!
Thanks in advance.
That's pretty close, but since you have no constraint saying something like "you may only create a link with a house that has no villager already" you will end up with some houses that have multiple villagers linked and some houses that have no villagers linked. All you have to do to correct that is to include that constraint, such that villagers may only link to a house that is not already linked:
ask villagers [
create-link-with one-of houses with [ not any? link-neighbors ]
]
To check that it worked, you can either visually inspect the links or run some variation of the lines below:
ask turtles [
if count link-neighbors != 1 [
show ("I am not linked to just one turtle.")
]
]
If any turtle is not linked to another, or is linked to more than one, it will print the line in quotes.
The netlogo docs give the following example
show count turtles-here
=> 10
show count other turtles-here
=> 9
and the documentation says the "other" command excludes "this" agent. My question is ... WHICH agent? Seems like this command can be run in the observer context and so no agent. Or at least in this example, the context could be a patch context in which case the "other" would exclude ALL the turtles? Is there some mechanism to set the context for a particular agent? Maybe:
ask agent [
show count other turtles-here
]
in which case why didn't the NetLogo code snippet include that?
The agent excluded is the agent being asked. ask, ask-concurrent, and of set the context. For example,
ask turtle 0 [ show count other turtles ]
counts all turtles except for turtle 0.
ask turtles [ show count other turtles ]
iterates over each turtle individually. In each iteration, other excludes the current turtle.
other never excludes agents of a different type. That is,
ask patch 0 0 [ show count other turtles ]
will just count all the turtles since none of the turtles are patch 0 0.
The agent of the current context can be referred to with self. The agent that other excludes will always be self. Thus,
ask agents [ show count other agents ]
is exactly equivalent to
ask agents [
let this-agent self
show count agents with [ self != this-agent ]
]
(note that this can be expressed more succinctly using myself, but since myself is way more confusing, and way worse named, than other I'm avoiding it here)
Seems like this command can be run in the observer context and so no agent.
This is in fact a bug! I've created an issue for it here: https://github.com/NetLogo/NetLogo/issues/757
From each of the turtle's perspectives, I have to run a function for a turtle to decide which turtle it will assign as it's "buddy".
Right now I have the code below but it doesn't achieve the effect.
foreach sort other turtles [
ask ? [
if Smin < Sim myself ? and self != ? [
]
]
]
In C/Java it would've been simple, just a simple for loop and then that's it. Apparently I am having a hard time understanding NetLogo's foreach function and the integration of '?' in looping. How can I do this?
It's unclear from the code sample you posted what exactly you are trying to do.
Some things that may help:
Unless you want to address your turtles in a specific order, it's usually not necessary to use foreach. Just doing ask other turtles [ ... ] can replace the whole foreach sort other turtles [ ask ? [ ... ] ].
Given that you are inside an ask ? block, self != ? will always be false, and thus, so will the and clause of your if. The code inside your inner block is never reached.
myself refers the agent from an "outer" ask block (e.g., in ask x [ ask y [ ... ] ], self would be y and myself would be x). Neither myself nor self is affected by foreach, and ? is not affected by ask.
My guess is that maybe you just want:
ask other turtles [
if Smin < Sim myself self [
]
]
But I can't know for sure, especially since I have no idea what Smin and Sim are. If you post more detail, maybe we can help you further.
Finally: NetLogo code usually ends up being much simpler than the equivalent C/Java code, but you must learn to embrace the "NetLogo way". Thinking in Java/C and then trying to translate in NetLogo will usually lead one astray.
In netlogo I have a procedure that calls another procedure. How can I go about getting the value
for example, I have two breeds of agents, a hub and a link. A hub has a local variable called 'budget' and I'm trying to modify its value.
hubs-own [
budget
]
to go
ask hub 0 [
do-ivalue
]
end
to do-ivalue
ask links [
;; I'm trying to set the local variable budget of the hub that's calling this link
set self.budget newvalue ;; this is obviously wrong, how can I fix this?
]
end
what you want to do is use is 'myself', it refers to the caller (asker): the one who asked to run the code where the 'myself' is located.
to do-ivalue
ask links [
ask myself [set budget 10] ]
end
The 'self' refers to the agent running the code. It is similar to 'this' in Java.
hmm.
not sure why u want to do it this way..
what u can do for now is
ask links[
let new_value new_value_from_link
ask hubs[
set budget new_value
]
]