Moving parameters for variable to set up - model behaviour changes - variables

This one stumping me...
I have an ABM which generates new patients arriving to a hospital unit by random-poisson on a single patch (spout procedure). Each patient is assigned an area to go to and a time to spend in the unit according to the area and other assigned variables. When I created this with the distributions embedded in the procedures or reporters it worked fine every time, but when I code the random variables into the set up to make it easier to manipulate it regularly generates grossly abnormally low/high values (ranges not seen when run in original format) and sometimes the creation of new patients doesn't happen at all though the model still ticks on... The only things changed are the placement of the variables in set up and and not in the body of the code.
I can't figure out why it would randomly have no patients entering the system which makes me mistrusting of anything else it generates. Is this just a formatting style that Netlogo doesn't like? Or am I missing something?
Thanks for any advice/help in solving this one
original code:
if ticks = 1000 [stop]
ask arrivals
[
assess
crowding-check
relocate
]
end
to assess
sprout-patients random-poisson 1.5
[set time_arrived ticks
set condition random-float 1.0
set NEWS2 random-float 7.0
set shape "person"
]
end
to-report AEC_treatment_time ;; gamma dist
let result random-gamma 3.478 0.525
if result < 2 [ report 2 ]
if result > 20 [ report 20]
report result
end
to-report AMU_treatment_time ; gamma dist
let result random-gamma 5.7716 0.3
if result < 4 [ report 4]
if result > 48 [ report 48]
report result
end
new code:
ca
set new-patients random-poisson 1.5
set AEC-los random-gamma 3.478 0.525
set AMU-los random-gamma 5.7716 0.3
reset-ticks
end
to go
if ticks = 1000 [stop]
ask arrivals
[
sprout-patients new-patients
assess
crowding-check
relocate
]
end
to assess
ask patients-here
[set time_arrived ticks
set condition random-float 1.0
set NEWS2 random-float 7.0
set shape "person"
]
end
...
to-report AEC_treatment_time ;; gamma dist
let los-AEC AEC-los
if los-AEC < 2 [ report 2 ]
if los-AEC > 20 [ report 20]
report los-AEC
end
to-report AMU_treatment_time ; gamma dist ;; reports treatment time for patients in AMU
let los-AMU AMU-los
if los-AMU < 4 [ report 4]
if los-AMU > 48 [ report 48]
report los-AMU
end
ps trying multiple iterations, it seems to be the random-poisson change that is the one causing the issues

It's pretty hard to see without the full code as it looks to be a problem with the way procedures connect, but your new structure has ask patients-here (at the start of the assess procedure) inside a loop through ask arrivals (go procedure). Is arrivals a breed?
Generally it is a bad thing to have nested ask turtles type structures because each turtle ask all turtles that satisfy the conditions so you can get subtle errors. Anyway, this will probably get you back to what you were doing before:
to go
if ticks = 1000 [stop]
ask arrivals
[ sprout-patients new-patients [assess]
crowding-check
relocate
]
end
to assess
set time_arrived ticks
set condition random-float 1.0
set NEWS2 random-float 7.0
set shape "person"
end
This structure makes the assess procedure into a procedure that runs from the turtle's perspective (or context) and has it run immediately as the turtle calling it is created.

Related

Netlogo - Turtles on more than one Patch - Item vs Who

How are you? I guess there is no way to visually represent in the Netlogo Interface a system with a "many-to-many" relationship, where turtles belong to more than one patch, and of course patches host more than one turtle. Correct?
I have a model where Banks (my turtles) operate on more than one Country (my patches). So I think my only option is to have two breeds of turtles, i.e. Banks and Countries, and connect Banks to Countries with link agents. Correct?
(I will also need interbank links, so I will need to set up a different breed of links. Correct?)
Now: I need to identify countries by name (i.e. Italy, France, Spain, etc.). I am reading a CSV file with country names and feeding it into a Netlogo list, but I am not sure how to have a country agent "be" a name. I created a country breed-specific variable called "country-name", and I am trying to use "item" to sequentially access each next name in the list of country names and assign it to the country-name variable of the next country agent in the Countries breed. However, I cannot relate "item" to "who" here, because "who" is not breed-specific. So I am thinking of setting up a separate breed-specific numbered index, but something like this has been asked in a previous question (trying to create a sequential ID variable for breeds in netlogo) and it was strongly suggested NOT to do so. Any suggestions on how to proceed?
I agree with the comment by Matteo. That said,
You don't ever need to use "who". Identify your banks and your countries with a "name" field that the breed owns. For example
banks-own [ name ]
counties-own [ name ]
;; then somewhere in your loop as you read them in...
create-banks 1 [ set name "Chase" ... ]
create-countries 1 [ set name "England" ...]
Below I use 'one-of' but there will only be one hit.
This makes the syntax work converting a list item to a specific turtle.
You never should need to know or care about the "who" value of a bank
or country.
to make-branches
;; read this list in from a csv file, etc. or hard code it
set branchlist [[ "Lloyds" "England"] ["Lloyds" "France"] [ "Chase" "USA"]["Chase" "France" ]]
;; then work the list. Again, you never need "who".
foreach branchlist [
z -> ask one-of banks with [name = item 0 z ]
[ create-link-with one-of countries with [ name = item 1 z ]]
]
end

Netlogo Land-use change model: foreach list command

I have made a code for a land use change model but one part does not work properly. I'm not very experienced with Netlogo and and can't manage to find my mistake(s).
problem:
the foreach part will not work although I copied it from the NETLOGO dictionary.
Netlogo dictionary (http://ccl.northwestern.edu/netlogo/docs/dict/foreach.html) gives:
(foreach list (turtle 1) (turtle 2) [3 4]
[ [the-turtle num-steps] -> ask the-turtle [ fd num-steps ] ])
;; turtle 1 moves forward 3 patches
;; turtle 2 moves forward 4 patches
I re-wrote this to my own models needs but Netlogo then reports " expected an anonymous command here, rather than a list or block"
my code:
to go
;; Sets Willingness to change true if patches are with more fellow patches than the scenario describes
(foreach list (Land-use = 1) (Land-use = 2) (Land-use = 3) (Land-use = 4) (Land-use = 5) (Land-use = 6) (Land-use = 7) [Senario1N Senario1L Senario1A Senario1B Senario1I Senario1R Senario1W]
[ [the-Land-use the-Scenario] - > ask patches [if count patches with [the-Land-use] > the-Scenario [ set Willingstochange True ] ] ])
;; Gives a score to atractivenesstochangein based on the ratio patches vs scenario
(foreach list (Land-use = 1) (Land-use = 2) (Land-use = 3) (Land-use = 4) (Land-use = 5) (Land-use = 6) (Land-use = 7) [Senario1N Senario1L Senario1A Senario1B Senario1I Senario1R Senario1W]
[ [the-Land-use the-Scenario] - > set atractivenesstochangein (count patches with [the-Land-use]/the-Scenario) ]
end
But also when I use the exact Netlogo dictionary example Netlogo reports the same problem
There are multiple problems with this code. StackOverflow procedure is that you ask a separate question for each error that you are trying to fix. But this is more than just a procedure to assist other people trying to find answers to their problems, it is also related to good programming practice.
You need to code much more gradually. Write a piece of code, test it does what you want, fix it, then move on only once it works properly. It is much more difficult to debug when you have added a lot of code because it is no longer easy to work out where the error was introduced. This is even more important in NetLogo, where the interactions between elements can lead to subtle errors.
Having said that, I can at least identify some syntax problems.
1/ I have no idea what you mean by the 'iterations' part
2/ change procedure
ask patches [set Land-use (Land-use of Atractiveneigbor]
should be
ask patches [set Land-use ([Land-use] of Atractiveneigbor]
3/ setup-patches
Not sure, but I suspect this is about the ordering in your setup procedure. You run the load-gis procedure later than the setup-patches procedure. Your load-gis procedure starts with a clear-all command, which deletes everything you have already done.

Creation of a flexible but on variables depending hierarchical structure in Netlogo

i am trying to model the following hierarchical structur in Netlogo:
Imagine a typical company or a a brach of public administration. There is one boss (turtle) on top and a number of employees (turtles) below him/her. There are two variables: span-of-control soc (how many employees can one Boss overwatch) and depth-of-control doc (how many hierarchical levels do exist in the structure). The total number of employees equals soc^doc. The total number of turtles equals 1+soc^doc (1 is the boss).
There are two Choosers in the Netlogo-Interface: soc and doc (ranging from 1 to 4).
What I imagine to code: Depending on the Variables chosen, the structure should arrange itself automatically by the following rule: Create as many links to employees as there have been employees on the higher hierarchical level until the doc is reached.
Example: doc:3 soc:3 1 Boss (always 1 so it can be used like an anchor)
1. Level: 3 links (1*3)
2. Level: 9 Links (3*3)
3. Level: 27 Links (9*3)
4. Level: Over as doc is reached
To realise this, I need to make the turtles kind of read the doc and soc variables and make them create links accordingly but I dont know how.
Here is my code so far:
globals [
information
]
undirected-link-breed [ Informationflows Informationflow ]
breed [ Employees Employee ]
breed [ tasks task ]
breed [ Bosses Boss ]
;#########SETUP########
to setup
clear-all
create-Bosses 1 [ set color red
set size 2 ]
set-default-shape Bosses "person"
ask Bosses [ setxy 0 15 ]
ask patches [ set pcolor white ]
set-default-shape Employees "person"
create-Employees ( span-of-control ^ depth-of-control) [set color blue
set size 2 ] ; absolute Number of Employees
;ask Boss 0 [ create-Informationflow-with random Employee 8] ; IDEA
;ask Employees [ create-Informationflow-with one-of other Employees] ; IDEA
;ask Employees [ create-Informationflow-with Boss 0 ] ; IDEA
repeat 100 [ layout ]
ask Employees [
setxy 0.95 * xcor 0.95 * ycor ]
end
to-report value-of-span-of-control? ; Just an idea
report span-of-control
end
;##########LAYOUT##########
to layout
; layout-radial Employees Informationflows (Boss 0) ;Problem: Boss is fixed in the Center
layout-spring Employees Informationflows 0 10 2
end
If anybody could hint me in the right direction, I would be very thankful.
Kind regards,
Jon
Okay, general coding advice first - do ONE thing, test it and fix it before moving on to the next thing.
Your question about how to 'read' the doc and soc variables makes me think you are very new to NetLogo. If so, please go and do the tutorials that are at the NetLogo site. Creating a chooser with the name 'XYZ' creates a global variable named 'XYZ', there is no additional step to read it. Since you are choosing numbers, you might want to use a slider instead of a chooser.
Try replacing your commented out ask Boss line with this:
ask one-of Boss [ create-Informationflow-with n-of soc Employees ]
It's generally bad practice to use who numbers because turtles can die or be generated in a different order because you change some code and then the who numbers are different. So I used one-of to randomly select any boss (of which there is only one anyway). I similarly used n-of to select Employees to create links with and specified the number n in the n-of to be the value from the soc variable.

NETLOGO: Distribution of variable on my-links

I have network with number of nodes connected with links. I need to set variable (lets call it "trust") to links so that sum of variables from links from one agent to others makes 100. Example: Agent have 3 links to another agents. Their variables are "34 13 53" or "23 61 16" or " 37 16 47". Sum is always 100. Hope it makes sense. Is there some easy way to do it netlogo?
Just give them the values then normalise. Something like (not tested) ask agents [ ask my-links [ set trust [ 100 * trust / sum [ trust ] of my-links ] ]
The problem you are going to have is that the value will need to be different each way. For example, if A and B have a link between them, then trust might need to be 25 at A's end (because the other links total 75) and 50 at B's end. So you actually need the link to have two values - how much A trusts B and how much B trusts A.

pseudo randomization in loop PsychoPy

I know other people have asked similar questions in past but I am still stuck on how to solve the problem and was hoping someone could offer some help. Using PsychoPy, I would like to present different images, specifically 16 emotional trials, 16 neutral trials and 16 face trials. I would like to pseudo randomize the loop such that there would not be more than 2 consecutive emotional trials. I created the experiment in Builder but compiled a script after reading through previous posts on pseudo randomization.
I have read the previous posts that suggest creating randomized excel files and using those, but considering how many trials I have, I think that would be too many and was hoping for some help with coding. I have tried to implement and tweak some of the code that has been posted for my experiment, but to no avail.
Does anyone have any advice for my situation?
Thank you,
Rae
Here's an approach that will always converge very quickly, given that you have 16 of each type and only reject runs of more than two emotion trials. #brittUWaterloo's suggestion to generate trials offline is very good--this what I do myself typically. (I like to have a small number of random orders, do them forward for some subjects and backwards for others, and prescreen them to make sure there are no weird or unintended juxtapositions.) But the algorithm below is certainly safe enough to do within an experiment if you prefer.
This first example assumes that you can represent a given trial using a string, such as 'e' for an emotion trial, 'n' neutral, 'f' face. This would work with 'emo', 'neut', 'face' as well, not just single letters, just change eee to emoemoemo in the code:
import random
trials = ['e'] * 16 + ['n'] * 16 + ['f'] * 16
while 'eee' in ''.join(trials):
random.shuffle(trials)
print trials
Here's a more general way of doing it, where the trial codes are not restricted to be strings (although they are strings here for illustration):
import random
def run_of_3(trials, obj):
# detect if there's a run of at least 3 objects 'obj'
for i in range(2, len(trials)):
if trials[i-2: i+1] == [obj] * 3:
return True
return False
tr = ['e'] * 16 + ['n'] * 16 + ['f'] * 16
while run_of_3(tr, 'e'):
random.shuffle(tr)
print tr
Edit: To create a PsychoPy-style conditions file from the trial list, just write the values into a file like this:
with open('emo_neu_face.csv', 'wb') as f:
f.write('stim\n') # this is a 'header' row
f.write('\n'.join(tr)) # these are the values
Then you can use that as a conditions file in a Builder loop in the regular way. You could also open this in Excel, and so on.
This is not quite right, but hopefully will give you some ideas. I think you could occassionally get caught in an infinite cycle in the elif statement if the last three items ended up the same, but you could add some sort of a counter there. In any case this shows a strategy you could adapt. Rather than put this in the experimental code, I would generate the trial sequence separately at the command line, and then save a successful output as a list in the experimental code to show to all participants, and know things wouldn't crash during an actual run.
import random as r
#making some dummy data
abc = ['f']*10 + ['e']*10 + ['d']*10
def f (l1,l2):
#just looking at the output to see how it works; can delete
print "l1 = " + str(l1)
print l2
if not l2:
#checks if second list is empty, if so, we are done
out = list(l1)
elif (l1[-1] == l1[-2] and l1[-1] == l2[0]):
#shuffling changes list in place, have to copy it to use it
r.shuffle(l2)
t = list(l2)
f (l1,t)
else:
print "i am here"
l1.append(l2.pop(0))
f(l1,l2)
return l1
You would then run it with something like newlist = f(abc[0:2],abc[2:-1])