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.
Related
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.
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
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.
I am attempting to reclassify continuous data to categorical data using Matlab. The following script takes a 4-band (Red, Green, Blue, nIR) aerial image and calculates the normalized difference vegetation index (i.e. a vegetation index showing healthy green vegetation). The script then rescales the values from (-1 to 1) to (0 - 255). This is the matrix I am trying to reclassify in the third section of the script %% Reclassify Imag1 matrix. I am attempting to use conditional statements to perform the reclassification, although this may be the wrong approach. The reclassification step in the script does not have any apparent effect.
How can I reclassify continuous values (0 - 255) to categorical values (1, 2, 3, 4) on a cell by cell basis?
file = 'F:\path\to\naip\image\4112107_ne.tif';
[Z R] = geotiffread(file);
outputdir = 'F:\temp\';
%% Make NDVI calculations
NIR = im2single(Z(:,:,4));
red = im2single(Z(:,:,3));
ndvi = (NIR - red) ./ (NIR + red);
ndvi = double(ndvi);
%% Stretch NDVI to 0-255 and convert to 8-bit unsigned integer
ndvi = floor((ndvi + 1) * 128); % [-1 1] -> [0 256]
ndvi(ndvi < 0) = 0; % not really necessary, just in case & for symmetry
ndvi(ndvi > 255) = 255; % in case the original value was exactly 1
Imag1 = uint8(ndvi);
%% Reclassify Imag1 matrix
if (150 <= Imag1)
Imag1 = 1;
elseif (150 > Imag1) & (140 < Imag1)
Imag1 = 2;
elseif (140 > Imag1) & (130 < Imag1)
Imag1 = 3;
elseif (130 >= Imag1)
Imag1 = 4;
end
%% Write the results to disk
tiffdata = geotiffinfo(file);
outfilename = [outputdir 'reclass_ndvi' '.tif'];
geotiffwrite(outfilename, Imag1, R, 'GeoKeyDirectoryTag', tiffdata.GeoTIFFTags.GeoKeyDirectoryTag)
disp('Processing complete')
Try this:
Imag1 = [ 62 41 169 118 210;
133 158 96 149 110;
211 200 84 194 29;
209 16 15 146 28;
95 144 13 249 170];
Imag1(find(Imag1 <= 130)) = 4;
Imag1(find(Imag1 >= 150)) = 1;
Imag1(find(Imag1 > 140)) = 2;
Imag1(find(Imag1 > 130)) = 3;
Result:
Imag1 =
62 41 169 118 210
133 158 96 149 110
211 200 84 194 29
209 16 15 146 28
95 144 13 249 170
Imag1 =
4 4 1 4 1
3 1 4 2 4
1 1 4 1 4
1 4 4 2 4
4 2 4 1 1
I can go into the logic in detail if you like, but I wanted to confirm that this gives your expected results first.
Some updates based on comments on the follow-up question to eliminate the unnecessary find and make the code more robust and independent of execution order.
Imag2 = zeros(size(Imag1));
Imag2(Imag1 >= 150) = 1;
Imag2((Imag1 > 140) & (Imag1 < 150)) = 2;
Imag2((Imag1 > 130) & (Imag1 < 141)) = 3;
Imag2(Imag1 <= 130) = 4;
Note that the results are now in Imag2 instead of overwriting Imag1.
I want to randomly generate points. Well at least there should be a limitation on the y-axis. Later I connect the points to a line which should proceed in a simple animation. You can imagine this as a random walk of a drunken person, going uphill and downhill.
This sounds very simple. I searched around the web and found that this could be accomplished using the markov chain. I think this idea is really interesting.
You can create the first state of your scene by yourself and pass this state as input to the markov chain algorithm. The algorithm randomly changes this state and creates a walk.
However I cannot find any example of that algorithm and no source code. I just found an applet that demonstrates the markov chain algorithm: http://www.probability.ca/jeff/java/unif.html
Please suggest some code. Any other ideas how to accomplish this are appreciated too.
I painted an example
So I want the line to proceed in a similar way. There are valleys, slopes ... they are random but the randomness still apply to the initial state of the line. This is why I found makrov chain so interesting here: http://www.suite101.com/content/implementing-markov-chains-a24146
Here's some code in Lua:
absstepmax = 25
ymin = -100
ymax = 100
x = 0
y = 5
for i = 1, 20 do
y = y + (math.random(2*absstepmax) - absstepmax - 1)
y = math.max(ymin, math.min(ymax, y))
x = x + 5
print (x,y)
end
absstepmax limits the size of a y step per iteration
ymin and ymax limit the extent of y
There is no bias in the example, i.e., y can change symmetrically up or down. If you want your "drunk" tending more "downhill" you can change the offset after the call to random from absstepmax - 1 to absstepmax - 5 or whatever bias you like.
In this example, the x step is fixed. You may make this random as well using the same mechanisms.
Here are some sample runs:
> absstepmax = 25
> ymin = -100
> ymax = 100
> x = 0
> y = 5
> for i = 1, 20 do
>> y = y + (math.random(2*absstepmax) - absstepmax - 1)
>> y = math.max(ymin, math.min(ymax, y))
>> x = x + 5
>> print (x,y)
>> end
5 4
10 22
15 37
20 39
25 50
30 40
35 21
40 22
45 12
50 16
55 16
60 12
65 -1
70 -8
75 -14
80 -17
85 -19
90 -25
95 -37
100 -59
> absstepmax = 25
> ymin = -100
> ymax = 100
> x = 0
> y = 5
> for i = 1, 20 do
>> y = y + (math.random(2*absstepmax) - absstepmax - 1)
>> y = math.max(ymin, math.min(ymax, y))
>> x = x + 5
>> print (x,y)
>> end
5 -2
10 -15
15 -7
20 1
25 1
30 12
35 23
40 45
45 43
50 65
55 56
60 54
65 54
70 62
75 57
80 62
85 86
90 68
95 76
100 68
>
Painted result added from OP: