Make multiple turtles move all at the same time - parallel-processing

What is the idea of the program: We have watchdogs, sheep, wolves. My sheep are in a ford, the wolves are coming from outside, when entering the ford, watchdogs start moving. Wolves eat sheep, sheep eat grass, watchdogs should scare wolves (not yet implemented).
My problem: All the turtles are moving one at a time.
What I want: I want them to move all at once for every tick.
Code:
breed [sheeps sheep]
breed [wolves wolf]
breed [watchdogs watchdog]
turtles-own [attacked?]
globals [counter
attack
sheep-energy
wolf-energy
grass-counter
lifespan
age
num-attacks
death-countdown]
to set-variables-shapes
set lifespan 365 * 10
set-default-shape sheeps "sheep"
set-default-shape watchdogs "dog"
set-default-shape wolves "wolf"
set sheep-energy 200
set wolf-energy 400
set death-countdown 60
end
to setup
clear-all
set-variables-shapes
create-fold
spawn-animals
reset-ticks
end
to go
ask turtles [
move
]
respawn-grass
ask sheeps [
if ( pcolor != green or pcolor != brown) [
move-to one-of patches with [ pcolor = green or pcolor = brown] ;; If i change this with turn-around (procedure), all sheeps die at once.
]
eat-grass
reproduce
sheep-status
death
show sheep-energy
]
ask wolves [
attack-sheep
if (xcor < 5 and xcor > -14 and ycor < 15 and ycor > -5 ) [
ask watchdogs [
move
if ( pcolor != green or pcolor != brown ) [
move-to one-of patches with [ pcolor = green]
]
]
]
]
tick
end
to create-fold
ask patches [
if (pxcor < 6 and pxcor > -15 and pycor < 16 and pycor > -6)[
set pcolor grey]
if (pxcor < 5 and pxcor > -14 and pycor < 15 and pycor > -5) [
set pcolor green]
]
end
to sheep-status
if (pcolor = brown) [
set sheep-energy sheep-energy - (life-from-food / 2)]
if attacked?
[set color red]
end
to attack-sheep
ask other sheeps in-radius (size)[
set attacked? true
set color red
]
end
to reproduce
if (age = lifespan / 10) [
hatch-sheeps 40 [
hatch-options
]
]
end
to eat-grass
if (pcolor = green)[
set pcolor brown
set sheep-energy sheep-energy + life-from-food
]
end
to respawn-grass
ask patches [
if pcolor = brown [
ifelse grass-counter = 0 [
set pcolor green
set grass-counter grass-respawn-timer
]
[ set grass-counter grass-counter - 1]
]
]
end
to death
sheep-death-timer
if (sheep-energy <= 0)
[die]
;;if (age >= lifespan) [die]
sheep-explode
end
to sheep-death-timer
if (attacked?)[
set death-countdown death-countdown - 1
if (death-countdown <= 30)
[set color black]
if (death-countdown <= 0)
[die]
]
end
to sheep-explode
if (sheep-energy >= 10000)[
hatch-sheeps 20 [
hatch-options]
die
]
end
to hatch-options
set sheep-energy 200
set age 1
set attacked? false
end
to move
fd 1
rt random 90
lt random 90
end
to spawn-animals
create-wolves number-of-wolves [
set color red
setxy 10 -10
set size 1.3
ask patch 10 -10[
Set plabel "Wolves"]
]
create-sheeps number-of-sheeps [
if (pxcor < 5 and pxcor > -14 and pycor < 15 and pycor > -5 )[
setxy -4.5 5]
ask patch -4.5 15 [
Set plabel "Sheeps"]
set color white
set attacked? false
]
create-watchdogs number-of-dogs [
if (pxcor < 5 and pxcor > -14 and pycor < 15 and pycor > -5 )[
setxy -4.5 5
set size 1.3]
set color blue
]
end
to turn-around
fd -1
rt 180
end

There is no such thing as "at the same time" in NetLogo. NetLogo is single-threaded and doesn't support parallel processing.
If all the agents move during the same tick, you can think of that as "at the same time", since it all happened during the course of a tick.
But within a single tick, there is literally no way for agents to move other than one at a time.

You can use
ask-concurrent sheeps [move ]
to make each sheep agent all move at the same time, but it shouldn't matter and they actually don't recommend you use it.

Related

How to write a script file to process this kind of file? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a file like this:
"JE_TUT_M05_W1_009 [ 2 1 1 1 1 1 1 1 ] [ 18774 18773 18773 ] [ 1610 1609 ] [ 14604 14603 14603 ] [ 2598 2597 2597 2597 2597 2597 ] [ 2 1 1 1 1 1 1 1 ]
JE_TUT_M05_W1_009 SIL K_B R_I AE1_I SH_E SIL"
And each symbol like SIL, K_B, R_I, AE1_I, SH_E corresponds to a []. Now I want to calculate the number of numbers in each[]. For example, for the first SIL, the number of numbers is 1-8, for K_B, the number of numbers is 9-11 and so on. Overall, the final result is kind of like an array:SIL->1-8, K_B->9-11, R_I->12-13, AE1_I->14-16.......
I am wondering how to write script to get the result.
Use some language suited for data processing, like GNU awk.
As it is my birthday today and it seems you don't have a clue how to approach this, here is a start:
$ gawk '
BEGIN {
FPAT = "([^ ]+)|(\\[[^][]+\\])"
}
{
gsub(/^"|"$/,"")
for(i=2;i<=NF/2;i++) {
gsub(/^\[|\]$/,"",$i)
print $(i+(NF/2)) "->" c+1 "-" (c+=split($i,tmp,FS))
}
}' file
Output:
SIL->1-8
K_B->9-11
R_I->12-13
AE1_I->14-16
SH_E->17-22
SIL->23-30
def parse_line(input_line):
my_list = input_line.split(' ')
my_skip = my_list[0]
count = 0
j = -1
out_dict = {}
for elem in my_list:
if elem.isdigit():
count = count + 1
if my_skip == elem:
continue
if elem == '[':
start = count + 1
if elem == ']':
print( my_list[j] ,"->", f"{start}-{count}" )
j = j - 1
input_line = "JE_TUT_M05_W1_009 [ 2 1 1 1 1 1 1 1 ] [ 18774 18773 18773 ] [ 1610 1609 ] [ 14604 14603 14603 ] [ 2598 2597 2597 2597 2597 2597 ] [ 2 1 1 1 1 1 1 1 ] JE_TUT_M05_W1_009 SIL K_B R_I AE1_I SH_E SIL"
parse_line(input_line)
Above is python solution. Im assuming, JE_TUT_M05_W1_009 i.e first string appears twice in your input line.
Output:
SIL -> 1-8
SH_E -> 9-11
AE1_I -> 12-13
R_I -> 14-16
K_B -> 17-22
SIL -> 23-30

How to avoid nobody run time error in netlogo?

I'm checking the interaction among the properties of three types of patches by using 'one-of neighbors' command. Problem occurs when the two patches are not neighboring to each other in some cases and entity NOBODY emerges. Is there any way to avoid this. It is necessary for me to use 'one-of neighbors' command. Can I give a command stating that if Nobody is detected then the value of the property of that patch be 0.1. In my code give below the problem occurs in the interaction b/w yellow patch and red patch as in some places red is not a neighbor of yellow.
I tried writing a Nobody command it was not correct.I appreciate your advice.
globals [ k ] ; interaction constant
patches-own [ a b c d' e' eeep deep ] ; state variables of properties
; a is the Proportion and variety of Blend of land
use
; b is the Land uses with supportiveness for
complimentary activities
; c is the Vehicular and Pedestrian Intensity
; d is the Intensity of Nodes in urban web
; e' is the Frequency of Enforced Vigilance
to setup
clear-all
set k initial-k
setup-patches
reset-ticks
end
to setup-patches
ask patches [ set pcolor yellow ] ; defines the patches as built up in an area
; to define yellow patches without a and b
ask patches [ if (pycor > 1) and (pycor < 8) and (pxcor = -3 ) [ set pcolor 46 ] ]
ask patches [ if (pycor > 1) and (pycor < 8) and (pxcor = -2 ) [ set pcolor 46 ] ]
ask patches [ if (pycor < -1) and (pxcor = -10 ) [ set pcolor 46 ] ]
ask patches [ if (pycor < -1) and (pxcor = -9 ) [ set pcolor 46 ] ]
ask patches [ if (pycor < -1) and (pxcor = -5 ) [ set pcolor 46 ] ]
ask patches [ if (pycor < -1) and (pxcor = 2 ) [ set pcolor 46 ] ]
ask patches [ if (pycor < -2) and (pxcor = 6 ) [ set pcolor 46 ] ]
ask patches [ if (pycor > 1) and (pycor < 8) and (pxcor = 2 ) [ set pcolor 46 ] ]
ask patches [ if (pycor = 2) and (pxcor > 8 ) [ set pcolor 46 ] ]
ask patches [ if (pycor = -2) and (pxcor > 2 ) [ set pcolor 46 ] ]
ask patches [ if (pycor = -3) and (pxcor > 6 ) [ set pcolor 46 ] ]
ask patches [ if (pycor = -4) and (pxcor > 6 ) [ set pcolor 46 ] ]
ask patches [ if (pycor = -8) and (pxcor > 6 ) [ set pcolor 46 ] ]
ask patches [ if (pycor = -9) and (pxcor > 6 ) [ set pcolor 46 ] ]
ask patches [ if (pycor = 7) and (pxcor > 8 ) [ set pcolor 46 ] ]
ask patches [ if (pycor = 6) and (pxcor > 8 ) [ set pcolor 46 ] ]
ask patches [ if (pycor = 2) and (pxcor > 2 ) and (pxcor < 6) [ set pcolor 46 ] ]
; to define road patches (horizontal)
ask patches [ if pycor = 0 [ set pcolor grey ] ]
ask patches [ if pxcor = 0 [ set pcolor grey ] ]
ask patches [if pycor = 9 [ set pcolor grey ] ]
ask patches [ if (pycor = 6) and (pxcor < -4 )[ set pcolor grey ] ]
ask patches [ if (pycor = 3) and (pxcor < -4 ) [ set pcolor grey ] ]
ask patches [ if (pycor = 4) and (pxcor > 3 ) [ set pcolor grey ] ]
ask patches [ if (pycor = -6) and (pxcor > 7 ) [ set pcolor grey ] ]
; to define road patches (vertical)
ask patches [ if (pycor > 0) and (pxcor = -10 ) [ set pcolor grey ] ]
ask patches [ if (pycor > 0) and (pxcor = -5 ) [ set pcolor grey ] ]
ask patches [ if (pycor < 0) and (pxcor = -7 ) [ set pcolor grey ] ]
ask patches [ if (pycor < 0) and (pxcor = -3 ) [ set pcolor grey ] ]
ask patches [ if (pycor < -3) and (pxcor = 4 ) [ set pcolor grey ] ]
ask patches [ if (pycor > 3) and (pxcor = 4 ) [ set pcolor grey ] ]
ask patches [ if (pycor > 0) and (pxcor = 7 ) [ set pcolor grey ] ]
; to define nodes as patches
ask patches [ if pxcor = 0 and pycor = 0 [ set pcolor red ] ]
ask patches [ if pxcor = 7 and pycor = 0 [ set pcolor red ] ]
ask patches [ if pxcor = -3 and pycor = 0 [ set pcolor red ] ]
ask patches [ if pxcor = -5 and pycor = 0 [ set pcolor red ] ]
ask patches [ if pxcor = -7 and pycor = 0 [ set pcolor red ] ]
ask patches [ if pxcor = -10 and pycor = 0 [ set pcolor red ] ]
ask patches [ if pxcor = 4 and pycor = 4 [ set pcolor red ] ]
ask patches [ if pxcor = 7 and pycor = 4 [ set pcolor red ] ]
ask patches [ if pxcor = 7 and pycor = 9 [ set pcolor red ] ]
ask patches [ if pxcor = 4 and pycor = 9 [ set pcolor red ] ]
ask patches [ if pxcor = 0 and pycor = 9 [ set pcolor red ] ]
ask patches [ if pxcor = -5 and pycor = 9 [ set pcolor red ] ]
ask patches [ if pxcor = -10 and pycor = 9 [ set pcolor red ] ]
ask patches [ if pxcor = -10 and pycor = 6 [ set pcolor red ] ]
ask patches [ if pxcor = -10 and pycor = 3 [ set pcolor red ] ]
ask patches [ if pxcor = -5 and pycor = 6 [ set pcolor red ] ]
ask patches [ if pxcor = -5 and pycor = 3 [ set pcolor red ] ]
; to set intial values of properties for patches
ask patches with [pcolor = yellow] [ set a random-float 0.9] ; initial a
ask patches with [pcolor = yellow] [ set b random-float 0.9] ; initial b
ask patches with [pcolor = grey] [ set c random-float 0.9] ; initial c
ask patches with [pcolor = red] [ set d' random-float 0.9] ; initial d'
ask patches with [pcolor = grey] [ set e' random-float 0.9] ; initial e'
end
to go
tick
if ticks >= 52 [ stop ]
ask patches with [pcolor = yellow]
[
let fc [c] of one-of neighbors with [pcolor = grey] ; reports c of any
one grey patch of neighbours
let fe' [e'] of one-of neighbors with [pcolor = grey] ; reports e' of any one grey patch of neighbours
let fd' [d'] of one-of neighbors with [pcolor = red]; reports d' of any one red patch of neighbours
if a < 0.1 [ set a 0.1
if a > 0.9 [ set a 0.9 ] ]
if b < 0.9 [ set b b + (k * a) + (k * fc) + (k * fd')
if b > 0.9 [ set b 0.9 ] ]
if b > 0.1 [ set b b - (k * fe')
if b < 0.1 [ set b 0.1 ] ]
]
end
I am guessing that the part of the code throwing the error is (for future reference, it is helpful if you try to focus on the part of the code that is causing the problem):
ask patches with [pcolor = yellow]
[ let fc [c] of one-of neighbors with [pcolor = grey]
let fe' [e'] of one-of neighbors with [pcolor = grey]
let fd' [d'] of one-of neighbors with [pcolor = red]
...
]
end
The primitive you want is any?. So you could rewrite:
let fc [c] of one-of neighbors with [pcolor = grey]
as
let greys neighbors with [pcolor = grey]
let fc 0.1
if any? greys
[ set fc [c] of one-of greys ]
Alternatively
let greys neighbors with [pcolor = grey]
let fc ifelse-value any? greys
[ [c] of one-of greys ]
[ 0.1 ]
Note that neither of these pieces of code is tested.

How to crop a multipage pdf using ghostscript with an array of page specific cropboxes

This post is a follow-up of "How do I crop pages 3&4 in a multipage pdf using ghostscript", but it changes the input to an array of data.
The quest: I have a pdf file (a set of drawings) where all pages have the same size and I want to crop some pages in one way and others differently. The following screenshot shows how I generated the data below for cropping:spreadsheet The "left,bottom,right,top" are to be handed over to the postscript /CropBox [934 350 3318 2034] command. Pagenumbers are just consecutive numbers, so they may not rally be needed.
page#,left,bottom,right,top
1 0 0 4252 2384
2 0 0 4252 2384
3 0 0 4252 2384
4 0 0 4252 2384
5 934 350 3318 2034
6 934 350 3318 2034
7 441 0 3811 2384
8 441 0 3811 2384
With the solution in the above mentioned question I was able to crop a specific page in an multipage pdf and it probably is a good starting point for a solution to this question. I just didn't figure it out on my own.
The relevant postscript code which tried to use as a base to solve this problem is (thanks to KenS):
<<
/EndPage {
0 eq {
pop /Page# where {
/Page# get
3 eq {
(page 3) == flush
[/CropBox [0 0 1612 1792] /PAGE pdfmark
true
}
{
(not page 3) == flush
[/CropBox [500 500 612 792] /PAGE pdfmark
true
} ifelse
}{
true
} ifelse
}
{
false
}
ifelse
}
>> setpagedevice
I guess we need some test numbers for "realistic" page sizes for my crazy page size dictionary... just for some fun testing.
/MyCrazyPageSizeDictionary begin
/PageSizeArray [
[0 0 595 842] % original A4 portrait
[87 123 508 719] % cut to A5
[149 210 446 631] % cut to A6
[192 272 403 570] % cut to A7
[223 316 372 526] % cut to A8
] def
I'd suggest you place the cropping information for each page in an array, and then add each array to an enclosing array. The problem is likely to be retaining the information.
The best way to do this, probably, is to create the array of page information as a named object in a specific dictionary. If you don't create your own dictionary, then userdict will be used instead.
Then in your EndPage procedure you simply pull the relevant index of the enclosing array, which gives you an array of crop sizes:
So, for example;
%!
/MyCrazyPageSizeDictionary 1 dict def
/MyCrazyPageSizeDictionary begin
/PageSizeArray [
[ 0 0 4252 2384]
[ 0 0 4252 2384]
[ 0 0 4252 2384]
[ 0 0 4252 2384]
[ 934 350 3318 2034]
[ 934 350 3318 2034]
[ 441 0 3811 2384]
[ 441 0 3811 2384]
] def
end
<<
/EndPage {
0 eq {
pop /Page# where {
/Page# get % stack - pagenum
/MyCrazyPageSizeDictionary /PageSizeArray get % stack - pagenum [[]]
exch % stack - [[]] pagenum
get % stack - []
[ /CropBox % stack - [] [ /CropBox
3 -1 roll % stack - [ /CropBox []
/Page pdfmark
true
}{
true
} ifelse
}
{
false
}
ifelse
}
>> setpagedevice
If you put that in a file (named eg crop.ps) then run your PDF file through Ghostscript but put 'crop.ps' as one of the input files before your PDF file:
gs <options....> crop.ps input.pdf
Then it should do what you want. With the caveat that I haven't tested this program in any way......
[Edit, added the corrected program]
/MyCrazyPageSizeDictionary 1 dict def
MyCrazyPageSizeDictionary begin
/PageSizeArray [
[ 0 0 4252 2384]
[ 0 0 4252 2384]
[ 0 0 4252 2384]
[ 0 0 4252 2384]
[ 934 350 3318 2034]
[ 934 350 3318 2034]
[ 441 0 3811 2384]
[ 441 0 3811 2384]
] def
end
<<
/EndPage {
0 eq {
pop /Page# where {
/Page# get % stack - pagenum
1 sub % array index is 0 based, page numbers start at 1
MyCrazyPageSizeDictionary /PageSizeArray get % stack - pagenum [[]]
exch % stack - [[]] pagenum
1 index length mod % get array, find length, clamp page number to length
get % stack - []
[ /CropBox % stack - [] [ /CropBox
3 -1 roll % stack - [ /CropBox []
/PAGE pdfmark
true
}{
true
} ifelse
}
{
false
}
ifelse
}
>> setpagedevice

Draw a random-beta distribution in netlogo

I'm attempting to generate a breed-owned variable which draws it's values from a random beta distribution in Netlogo.
I've found the "bc" code example online but am struggling to adapt it to what I need. Right now, for convenience, I've generate my distribution from a random-normal distribution using
create-breed 500
[
set target_factor random-normal 0.9 0.05
if target_factor > 1 [set target_factor 0.9999999999]
if target_Factor < 0.5 [set target_factor 0.5000000001]
]
So basically I'd like to get a reporter that can replace the "random-normal 0.9 0.05" part with a random beta distribution
I got so far as:
to-report random_beta
set asocial_alpha 2
set asocial_beta 2
set asocial_min_eps 0
set asocial_max_eps 0.25
let x random-gamma asocial_alpha 1
let asocial_eps (x / (x + random-gamma asocial_beta 1))
set asocial_eps asocial_min_eps + (asocial_eps * (asocial_max_eps - asocial_min_eps))
foreach ( n-values 99 [ (? + 1) / 100 * (asocial_max_eps - asocial_min_eps) + asocial_min_eps] )
[report ? ( ((? - asocial_min_eps) ^ (asocial_alpha - 1) * (asocial_max_eps - ?) ^ (asocial_beta - 1)) / ( asocial_max_eps - asocial_min_eps ) ^ (asocial_alpha + asocial_beta - 1) )]
end
all of the 'set' variables here are global factors
I'm honestly out of my depth with this one mathematically. If anyone can help me fix this reporter (or if a procedure would work that's fine too). so my final code for breed creation is:
create-breed 500
[
set target_factor random-beta
if target_factor > 1 [set target_factor 0.9999999999]
if target_Factor < 0.5 [set target_factor 0.5000000001]
]
I was asked this offsite as apparently the rngs extension hasn't been updated for awhile and thought I should post here too. Here is the code to draw beta using the built-in gamma distribution generators.
to-report random-beta [ #alpha #beta ]
let XX random-gamma #alpha 1
let YY random-gamma #beta 1
report XX / (XX + YY)
end

netlogo, ticks is slower if setting a variable

NORMAL TICKS SPEED:
ask turtles with [seated? = 0] [
fd speed
if (pxcor > -5 and pxcor < 10) and (pycor > 5 and pycor < 10) [facexy (87 + random 4) (6 + random 4) fd speed]
if (pxcor > 25 and pxcor < 28) and (pycor > 5 and pycor < 12) [
let choice random 2
fd speed
if choice = 0 [leftbench]
if choice = 1 [facexy 75 (6 + random 5)]
]
if (pxcor > 73 and pxcor < 79) and (pycor > 5 and pycor < 15) [rightbench fd speed]
if pcolor = brown and not any? other turtles-here
[ move-to patch-here
set seated? true
set pcolor orange
]
]
SLOW TICKS SPEED:
ask turtles with [seated? = 0] [
let gate patches with [(pxcor >= 88 and pxcor <= 90) and (pycor >= 5)]
fd speed
if (pxcor > -5 and pxcor < 10) and (pycor > 5 and pycor < 10) [face one-of gate fd speed]
if (pxcor > 25 and pxcor < 28) and (pycor > 5 and pycor < 12) [
let choice random 2
fd speed
if choice = 0 [leftbench]
if choice = 1 [facexy 75 (6 + random 5)]
]
if (pxcor > 73 and pxcor < 79) and (pycor > 5 and pycor < 15) [rightbench fd speed]
if pcolor = brown and not any? other turtles-here
[ move-to patch-here
set seated? true
set pcolor orange
]
]
the problem is, I want to make the speed normal every time I set a variable to make coding a lot easier and more organized.. please help
If you have a chunk of code you want to use twice, put it in a procedure.
Then whenever you need that chunk of code, you don't have to repeat it using copy-and-paste; you just call the procedure.
If the procedure needs to be able to behave a little differently, depending on where it's being used, you might have the procedure take inputs, and then have it inspect those inputs to decide what to do.
See http://ccl.northwestern.edu/netlogo/docs/programming.html#procedures .
I hope this helps? I'm not entirely sure I'm addressing your question, but if I'm not, maybe you can clarify what it is you're trying to do.

Resources