Netlogo comparing list of global variables to numbers - list

I apologise in advance for how simple the answer probably is to this question, I am very new to netlogo and very out of my depth.
I am trying to read a water-temperature from a file and consequently get my turtles to die/breed depending on the temperature. I have got the file to read finally, and set water-temperature as a global variable, however I am now stuck on the comparison part. It won't let me compare the variable to a number because I think the variable is a list. The following error message comes up;
The > operator can only be used on two numbers, two strings, or two agents of the same type, but not on a list and a number.
error while turtle 7 running >
called by procedure REPRODUCE
called by procedure GO
called by Button 'go'
Code is below;
globals [ year
month
water-temperature ]
extensions [ csv ]
to setup
ca
load-data
create-turtles 50
[ set size 1
set color red
setxy random-xcor random-ycor ]
reset-ticks
end
to go
ask turtles [ move
reproduce ]
run-temperature
end
to load-data
file-close-all
file-open "C:\\Users\\Hannah\\Documents\\Summer research project\\test3.csv"
end
to run-temperature
file-close-all
file-open "C:\\Users\\Hannah\\Documents\\Summer research project\\test3.csv"
while [ not file-at-end? ] [
set water-temperature csv:from-row file-read-line
tick ]
file-close
end
to move
rt random 50
lt random 50
fd 1
end
to reproduce
if water-temperature > 35 [ die ]
if water-temperature > 30 and water-temperature < 34 [ hatch 1 rt random-float 360 fd 1 ]
if water-temperature > 25 and water-temperature < 29 [ hatch 2 rt random-float 360 fd 1 ]
if water-temperature > 20 and water-temperature < 24 [ hatch 3 rt random-float 360 fd 1 ]
end
I would be so grateful for any help!
Thanks :)
Hannah

welcome to Stack Overflow. Can you please provide an example of the first few lines of your "test3.csv" file? That will help get your question sorted- if you have a header or multiple columns that could be causing your problems- multiple columns might be getting read in as a list. As well, I think you want file-read instead of file-read-line.
A few other things- your load-data procedure is unnecessary as far as I can tell (you only need the loading to occur in run-temperature).
More importantly, your code right now says something like: "All turtles, move and reproduce. Now, read the whole temperature file line by line." The problem is that your while statement is saying "until you have yet reached the end of the file, read a line, tick, and move to the next one." Additionally, your model will tick once per line, without the turtles ever doing anything- it probably is simpler to just have your tick at the very end of your go procedure. It is likely better to avoid the use of while in your go procedure in this scenario, as it will loop until the while condition is satisfied.
It might be easier to just read your whole test.csv and store it in a variable for easier access- here is one example. Using this setup:
globals [
water-temperature
water-temperature-list
]
to setup
ca
crt 50 [
setxy random-xcor random-ycor
]
First, tell Netlogo water-temperature-list is a list using set and []. Then, do the same file close/open as before to prep your file for reading. Then, use a similar while loop to read your temperatures into water-temperature-list, using lput:
set water-temperature-list []
file-close-all
file-open "test3.csv"
while [ not file-at-end? ] [
set water-temperature-list lput file-read water-temperature-list
]
file-close-all
reset-ticks
end
Now your model more simply access those values, since they are stored in a model variable directly. You can easily use the ticks value with item as an index for that list- for example, on tick 0 the first element in the list will be accessed, on tick 1 the second element, and so on. For example:
to go
set water-temperature item ticks water-temperature-list
ask turtles [
if water-temperature > 30 [
die
]
if water-temperature <= 30 [
rt random 60
fd 1
]
]
tick
end
Note that with this setup once you get to the end of your temperatures, there will be an error telling you that Netlogo can't find the next list element- you'll have to put a stop condition somewhere to prevent that.
I know that is an alternative to your approach but I hope that it's helpful. For another similar but more complicated example, check out this model by Uri Wilensky.

Related

Netlogo: Edit link attribute based on lists of linked agents

I have a model with banks. Each bank is linked to all other banks by a special breed of undirected link called "riskshares". Each bank also has links to other types of agents.
Each bank has a list of countries where it operates. The list is called "op-countries-list", which does not have a fixed length, and both its length and countries are randomly picked. For example, one bank might have "op-countries-list" of length 5 = "Australia, Italy, Spain, Canada, Brazil", and another bank might have "op-countries-list" of length 2= "Italy, France".
Now: I need to change to "TRUE" one boolean attribute of the "riskshare" link, depending on whether there is one common country in the "op-countries-list" of the two banks linked by the "riskshare".
I have (among other things):
banks-own
[op-country-list]
riskshares-own
[same-country-op?]
And I am trying this approach:
ask banks
[
foreach op-country-list
ifelse member? op-country-list of other-end
[set same-country-op? of my-out-riskshare true]
]
With this, I get an "OF expected input to be a reporter" error. I think I should come up with a to-report procedure and then use it, but I am not sure how to do it in this case. In addition, I am not sure how to specify that I want other-end to look into the "riskshare" kind of link, and not all other links that the bank has.
Do you have any suggestions?
There is definitely a way to do it with foreach as you're getting at, but you might find map a little more appropriate in this case. map will iterate over items in a list and do "something"- in this case, you could check for membership in the list of interest. More detail in comments below:
breed [ banks bank ]
undirected-link-breed [ riskshares riskshare ]
banks-own [ op-country-list ]
riskshares-own [ same-country-op? ]
to setup
ca
random-seed 1
let possible-countries [ "CANADA" "USA" "FRANCE" "JAPAN" "AUSTRALIA" "SPAIN" "RUSSIA" ]
create-banks 5 [
; Pick some random countries
let n-countries random 3 + 1
set op-country-list n-of n-countries possible-countries
]
layout-circle turtles 8
ask banks [
set label op-country-list
; Build the links
create-riskshares-with other banks [
; Determine if the banks share an operating country
; Pull the country lists from each bank associated with the link
let bank-1-countries [op-country-list] of end1
let bank-2-countries [op-country-list] of end2
; Use map to iterate over each country of one bank and check its
; membership in the op-country-list of the other bank. Check if 'true'
; is found in the list
let any-true? member? true map [ cur_bank -> member? cur_bank bank-2-countries ] bank-1-countries
print member? true map [ cur_bank -> member? cur_bank bank-2-countries ] bank-1-countries
; Set same-country-op? according to the any-true condition
ifelse any-true? [
set same-country-op? true
set color blue
] [
set same-country-op? false
set color red
]
]
]
reset-ticks
end
This toy model (with the random-seed as is) gives an output like:
Which I think meets the conditions you outline. Hopefully that gets you pointed in the right direction!

Netlogo Lists: detecting the patch in a list with max distance to one specific patch - for each turtle

I want to solve a problem in Netlogo that so far exceeds my programming skills. I want to built a list for each turtle which contains the furthest patch the turtle went on this "day". So far I tried to built a list were all patches are stored for each turtle. Now I want to calculate - for each turtle - the patch from this list that has maximum distance from its home (hide). I want to empty the list every night (thats not mandatory) Thats my code so far:
let temp-visited-patch-list lput patch-here temp-visited-patch-list
if period = night
[
[foreach [temp-visited-patch-list] [x -> set visited-patch-list lput (max x [distance hide]) visited-patch-list]]
let temp-visited-patch-list []
]
So I am not that far to extract the values for each turtle seperatly - and even the part I posted does not work. I get an "expected command" error. I would be very thankful for any suggestions to solve this problem.
Best regards
Olivia
This code has the turtles move, remember their furthest patch and turns those patches red and calculates average distance. It should help orient you how to use patches and turtle attributes to solve your question.
globals [home-patch]
turtles-own
[ farpatch
maxdistance
]
to setup
clear-all
set home-patch one-of patches
ask home-patch
[ set pcolor blue
sprout 20
]
reset-ticks
end
to go
repeat 20 [movement]
ask (patch-set [farpatch] of turtles) [set pcolor red]
type "Average max distance:" print mean [maxdistance] of turtles
tick
end
to movement
ask turtles
[ set heading random 360
forward 1
if distance home-patch > maxdistance
[ set maxdistance distance home-patch
set farpatch patch-here
]
]
end

Netlogo: Nested if/ifelse statements

Building my first ABM using Netlogo and have a problem involving ifelse statements and how to use them. I'm modelling agents response to flooded properties. The concepts are as follows:
If an agent is flooded, they will consider adopting protective measures (if they haven't already).
If an agent has adopted protective measures, and are flooded, the success of the measure is calculated.
My code is as follows:
to process-property
let $random-flood-number random-float 1
ask properties [
set flood-damage-list-consequent replace-item 1 flood-damage-list-consequent (item 1 flood-damage-list-initial * (1 - PLP-reduction))
set flood-damage-list-consequent replace-item 2 flood-damage-list-consequent (item 2 flood-damage-list-initial * (1 - PLP-reduction))'
ifelse $random-flood-number < probability-flooding
[
set flooded? TRUE
set number-of-times-flooded (number-of-times-flooded + 1)
if plp-adopted? != TRUE [
calculate-adoption-intention
]
]
[
set flooded? FALSE
]
]
ask properties with [plp-adopted? = TRUE] [
plp-reliability-analysis
]
end
to plp-reliability-analysis
if plp-abandoned? = TRUE [stop]
if flooded? = TRUE [
if number-of-times-flooded > 1 [
let plp-reliability-factor 0.77 ;;This variable represents the probability of success that Manual PLP will offer full reduction in damage. Taken from JBA (2012;2014).
ifelse random-float 1 < plp-reliability-factor
[
set plp-deployed-successful? TRUE
set PLP-reduction 0.25
set successful-flood-damage-reduction (sum flood-damage-list-initial * PLP-reduction)
]
[
set plp-deployed-successful? FALSE
set PLP-reduction 0.9
set unsuccessful-flood-damage-reduction (sum flood-damage-list-initial * PLP-reduction)
calculate-abandonment-intention
]
]
]
end
I have written the following code as an error check, which i keep getting:
if flooded? = FALSE and plp-deployed-successful? = TRUE [error["Properties should only deploy PLP when they are flooded"]]
I believe the problem lies in the ifelse statements in "plp-reliability-analysis" procedure. I'm new to Netlogo and am confused as to when to use an 'if' or 'ifelse' statement. If someone could explain and have a look at the above code i'd be very grateful.
Thanks,
David
The difference between if and ifelse is that:
if is used when you want to run some piece of code only under certain conditions
ifelse is used when you want to run some piece of code under some condition and a different piece of code if the condition is not met.
Have a look at this shortened version of your code. Note that I moved the opening bracket to the beginning of the line to line up the start and end of code blocks. I also put end bracket on the same line for very short code blocks, but the bracketing is the same as yours.
to process-property
let $random-flood-number random-float 1
ask properties
[ ifelse $random-flood-number < probability-flooding
[ set flooded? TRUE ]
[ set flooded? FALSE ]
]
ask properties with [plp-adopted? = TRUE]
[ plp-reliability-analysis
]
end
to plp-reliability-analysis
if flooded? = TRUE
[ if number-of-times-flooded > 1
[ let plp-reliability-factor 0.77
ifelse random-float 1 < plp-reliability-factor
[ set plp-deployed-successful? TRUE ]
[ set plp-deployed-successful? FALSE ]
]
]
end
You draw a random number and assign it to the variable $random-flood-number. Then you ask every property agent to compare that number to the value of probability-flooding. However, you never draw a new random number. So if it's true for one property, it will be true for all the properties. Given it is a flood model, that is presumably intentional as all houses are equally affected by flooding.
Imagine that a low number is drawn and they all get flooded. All the ones with plp-adopted? are then sent to the plp-reliability-analysis procedure. For all of them, the variable flooded? is true so the code block is run.
The first line is if number-of-times-flooded > 1. The first time a flood occurs, the number-of-times-flooded is changed from 0 to 1. That will fail the test (did you mean to use >= instead of > ?) and the remainder of the code will not be run. It will simply jump to the end bracket.
[ let plp-reliability-factor 0.77
ifelse random-float 1 < plp-reliability-factor
[ set plp-deployed-successful? TRUE ]
[ set plp-deployed-successful? FALSE ]
]
But for the second and later code, it will run and 77% of the properties will have the plp recorded as successful, and the others set to unsuccessful.
So, how do you end up with some properties having the combination of false flood? and true plp-deployed-successful?.
Now jump forward in time and 2 (or more) floods have occurred. A flood has just happened, so 77% of the properties with plp-adopted? have true plp-deployed-successful? This time, there is not a flood and all properties have flooded? set to false. Those with plp-adopted? are sent to the plp-reliability-analysis procedure. However, flooded? is now false so the code block does not run and they retain their values of plp-deployed-successful? from the previous run through.

IFELSE [Compare the length of two separate lists] [ACT]

Following my question yesterday, I was trying to implement the code with filter and length. I think I achieved the filter but now I need the agents to compare their list length with other agent (activities) list. Here is my code:
ask agentes [
set culture-tags n-values 17 [random 2]
set shared-culture (filter [ i -> i = 0 ] culture-tags) ;count 0s and
create shared-culture
]
end
to move-agentes
ask agentes [
let nearest-activity min-one-of (activities) [ distance myself ]
ifelse any? activities in-radius sight-radius with
[ length t-shared-culture = length shared-culture ] ;as many 0s in t-shared-culture (specified later in the setup for activities) as in shared-culture
[ face nearest-activity ]
[ rt random 40
lt random 40
fd 0.3
]
]
Netlogo prints the next error: "ACTIVITIES breed does not own variable SHARED-CULTURE error while activity 176 running SHARED-CULTURE".
I imagine the mistake must be here: [ length t-shared-culture = length shared-culture ] but I don't know how to make both lists to interact.

Netlogo : inequality doesn't work properly under foreach

It supposes to be a simple code, but I don't know why it doesn't work properly.
I want to change a color of non-white turtle back into white if a condition is fulfilled. I put inequality as the condition.
for example, if the amount of red turtle > = 5, then [ do something ].
No error message for the code, but I found that [ do something ] codes are executed before the condition is fulfilled. For example, it is executed when the amount of turtle is 1 or 4. And I also found that there are moments when it reach > = 5, [ do something] code is not executed.
Below is the code
to seize-value
ask consumers [set type-of-value ( list blue red green) ]
foreach type-of-value [
if count consumers with [color = ?] > = 5 [
let z consumers with [color = ?]
ask z [ set color white ]
ask consumers with [color = white] [set value? false]
ask one-of cocreation-patches [ sprout 1 [gen-prevalue]]
]]
end
I've tried using a single color, instead of a list of color (without - foreach) it doesn't work either.
Can anyone help with this?
Thank you
You have something like the following at the top of your code to set up type-of-value as an agent variable:
breed [ consumers consumer ]
consumers-own [ type-of-value ]
However, you are treating it as a global variable in your code. First you say ask consumers [set type-of-value ( list blue red green) ] to set the AGENT variable called type-of-value to the list of colours. But you end that ask [] statement before starting the foreach.
Unless the consumers have different lists of colours, what you really want is something more like this (untested). Note that I have also removed your multiple creations of the same agentset (for efficiency):
globals [ type-of-value ]
to setup
clear-all
...
set type-of-value ( list blue red green)
...
reset-ticks
end
to seize-value
*type "seize-value on tick " print ticks
foreach type-of-value
[ let changers consumers with [color = ?]
*print ?
*print count changers
if count changers >= 5
[ ask changers
[ set color white
set value? false
]
ask one-of cocreation-patches [ sprout 1 [gen-prevalue] ]
]
]
end
UPDATE for debugging I have added three lines that will output key information for debugging. They are marked with an asterisk (*). Add those lines (without the *) and look at the output.