In my model I want to control the amount of energy that goes into the system. Each turtle has its own set amount and I have an estimate of what the total average should be.
However, in my current code, the energy always exceeds this amount, often by a lot. I don't mind that this occurs occasionally but I'd like it to average out at each run of the model.
It's the line while [sum [energy] of turtles < 1752] that I'm trying to use to limit the energy.
globals [energy]
breed[ones one]
breed[twos two]
breed[threes three]
breed[fours four]
breed[fives five]
breed[sixes six]
breed[sevens seven]
to setup
clear-all
ask patches [
while [sum [energy] of turtles < 1752] [
let choose-size random-float 1
if choose-size < 0.493 [ sprout-ones 1 ]
if choose-size >= 0.493 and choose-size < 0.861 [ sprout-twos 1 set energy 75 ]
if choose-size >= 0.861 and choose-size < 0.921 [ sprout-threes 1 set energy 216 ]
if choose-size >= 0.921 and choose-size < 0.988 [ sprout-fours 1 set energy 700]
if choose-size >= 0.988 and choose-size < 0.994 [ sprout-fives 1 set energy 2500]
if choose-size >= 0.994 and choose-size < 0.998 [ sprout-sixes 1 set energy 8500]
if choose-size >= 0.998 [ sprout-sevens 1 set energy 25000]
]]
end
You are using while in your code. While means that the code will be repeated until the condition is no longer valid. This means that the first patch will keep sprouting turtles until the limit is exceeded, and all other patches will not sprout any turtles at all.
ask patches [
while [sum [energy] of turtles < 1752] [
<...>
]
]
If you replace your while with an if instead, each patch will only try once to sprout a turtle. At that point, the total number of turtles that could be sprouted is dependent on the number of patches that you have.
Related
How i can make a random ycor higher/lower than a value?I have traffic 2 sample and i added some sheeps on the grass but i want them to respawn and only on the grass randomly not on the road.And what should i change so that sheeps can`t never reach the streets?
breed [sheeps sheep]
to setup
clear-all
draw-road
setup-vans
create-or-remove-cars
set selected-car one-of turtles
ask selected-car [ set color red ]
set selected-van one-of turtles
ask selected-van [ set color red ]
reset-ticks
set-default-shape turtles "sheep"
create-sheeps number-of-sheeps [
set color white
set xcor random-pxcor
set ycor -4
set energy random 10
]
end
to-report free [ road-patches ] ; turtle procedure
let this-car self
report road-patches with [
not any? turtles-here with [ self != this-car ]
]
end
to go
ask turtles [ move-forward ]
ask turtles with [ ycor != target-lane ] [ move-to-target-lane ]
ask sheeps
[
move
forward 1
]
tick
end
to move
rt random 100
lt random 100
fd 20
set energy energy - 0.5
end
You have two questions here. Let me answer the first.
set ycor some-value + random-float (max-pycor + 0.5 - some-value)
where some-value is the value you want pycor to be greater than. Similarly,
set ycor some-value - random-float (some-value - min-pycor - 0.5)
where some-value is the value you want pycor to be less than.
max-pycor + 0.5 is the maximum possible value of ycor and min-pycor - 0.5 is the minimum possible value.
I'm fairly new to Netlogo but I want to build a model where an agent (a car driver) will leave his home at a certain hour with a certain probability. Let's say he leaves Monday morning at 1 am (if have linked the ticks to the time so one tick is one hour).
I tried to work with ifelse-statements combined with a second statement which has to be verified in order for the entire statement to become true. In the example below the car / agent should leave with a probability of 7.7% its house and drive to a patch called underway-patches. Since one week has 168 hours, I tried to link the hour via the mod ticks (hence, mod ticks = 1 is equal to 1 am on a Monday morning).
This alone works:
ifelse ticks mod 168 = 1 and random-float 100.0 < 7.7
[ ask turtles [ move-to one-of underway-patches ] ]
[ ask turtles [ move-to one-of home-patches] ]
This works fine. So I have always about 7 out of 100 turtles moving to the underway-patch.
But If I now add the second hour, so 2 am, the first function does not work anymore (there are no turtles moving at all at 1 am - only at 2 am). I expect about 7 out of 100 turtles to move at 1 am to the underway-patch and then I expect about 5 out of 100 turtles to move at 2 am to the underway-patch (and the other 7 of the first hour should go back to the home-patches).
This does not work anymore:
; Monday, 1 am
ifelse ticks mod 168 = 1 and random-float 100.0 < 7.7
[ ask turtles [ move-to one-of underway-patches ] ]
[ ask turtles [ move-to one-of home-patches] ]
; Monday, 2 am
ifelse ticks mod 168 = 2 and random-float 100.0 < 5.1
[ ask turtles [ move-to one-of underway-patches] ]
[ ask turtles [move-to one-of home-patches] ]
I appreciate every help! Thanks in advance.
First, congratulations on an extremely clear question despite your newness to the site.
The problem is not that you have multiple ifelse statements, that is fine. The issue is that your ifelse statement is applying a single test to all turtles. Just look at the first example with one statement:
ifelse ticks mod 168 = 1 and random-float 100.0 < 7.7
[ ask turtles [ move-to one-of underway-patches ] ]
[ ask turtles [ move-to one-of home-patches] ]
Imagine that it is tick number 1. The computer runs the random number generator and gets 2. Great, the condition is true so the first block gets run. That will have ALL turtles move to the underway-patches. Similarly, if the random number generator returns 10, then the condition is false and ALL turtles move to home-patches.
You probably want something more like (you don't have to do the brackets on multiple lines, I did it so you can see the logical blocks of the structure):
ifelse ticks mod 168 = 1
[ ask turtles-on home-patches
[ if random-float 100.0 < 7.7
[ move-to one-of underway-patches
]
]
]
[ ask turtles-on underway-patches [ move-to one-of home-patches] ]
Or if you want exactly the correct proportion of turtles to move:
ifelse ticks mod 168 = 1
[ let num-to-move 0.077 * count turtles-on home-patches
ask n-of num-to-move turtles-on home-patches
[ move-to one-of underway-patches
]
]
[ ask turtles-on underway-patches [ move-to one-of home-patches] ]
Just a general observation, if you are going to code this for every possible starting time, you are going to have a lot of code that is identical except for the tick and the proportion. You state that you are new to NetLogo, so I don't want to jump too quickly to more advanced concepts, but come back when you're a little further along with having thought through your model and we can probably help you create a procedure that reduces the need to duplicate code.
UPDATE: One approach to reusing the code
This isn't quite right because I'm not really clear what sort of movement you want, but here is a complete model where the proportions are stored in a list and ticks is used to identify the correct item in the list. That proportion is then passed to a piece of code that moves turtles.
globals
[ home-patches
underway-patches
proportions
]
to setup
clear-all
set proportions [0 0.077 0.05 0.15]
set home-patches patches with [abs pxcor <= 3 and abs pycor <= 3]
ask home-patches [ set pcolor white ]
set underway-patches patches with [not member? self home-patches]
ask underway-patches [ set pcolor yellow ]
create-turtles 100
[ set color red
]
reset-ticks
end
to go
move-turtles item (1 + ticks mod 3) proportions
tick
end
to move-turtles [#prop]
print #prop
ask turtles
[ ifelse member? patch-here home-patches
[ if random-float 1 < #prop
[ move-to one-of underway-patches
]
]
[ move-to one-of home-patches
]
]
end
I want x% of turtles, called pholders, to change their choice from a good 1 to a good 2.
The code is as follows:
ask pholders [ifelse random-float 1 <= probkauf
[ask (n-of (count pholders with [choice-num = 1] * 0.01) pholders with[choice-num = 1]) [set choice-num 2]]
[ifelse random-float 1 < 0.5[imitation set typeofchoice 1][beratung set typeofchoice 4]]
]
Initially 100% of the pholders chose good 1. The Problem is as follows: When i rise the number of pholders above something between 102 and 108 n-of doesn't calculate a 1%-fraction anymore, it calculates 10%. The higher the number of pholders the bigger the fraction: for 200 pholders the code calculates 60%. When i leave the number of pholders constant and below 108 but change the percentage from 0.01 to 0.02 it calculates something like 55% or 58%. Is the problem probably coming from ask n-of in an ask environment?
Thank you very much in advance.
Your problem is that you are running the probabilistic code multiple times. Your code has this structure:
ask pholders
[ ifelse random-float 1 <= probkauf
[ ask (n-of (count pholders with [choice-num = 1] * 0.01) pholders with [choice-num = 1])
[ set choice-num 2]
]
[ <do something else> ]
]
If you have 500 pholders, then there will be 500 times that a pholder selects a random number and, if the number is lower than your value probkauf, it instructs a number of pholders with choice-num of 1 to change it to choice-num 2. 500 potential occasions of 1% conversion is why you have so many being converted.
Based on the description in your comments, I think you want this:
globals [probkauf]
turtles-own [choice-num]
to setup
clear-all
set probkauf 0.5
create-turtles 1000
[ setxy random-xcor random-ycor
set color blue
set choice-num 1
]
reset-ticks
end
to go
update-choices
tick
end
to update-choices
ifelse random-float 1 < probkauf
[ ask turtles with [choice-num = 1]
[ if random-float 1 < 0.01
[ set choice-num 2
set color red
]
]
]
[ ; whatever happens with other part of probability
]
end
Morning everybody
After editing my model, by adding a random spawn rate:
to migrate
if random-float 100 < random-spawn-rate
[create-turtles 2 [rt random-float 360 fd 1]]
end
i get the problem that after one kind of turtles (the boats) die out, even their random hatching seems to die out?
i tried other ways, but nothing really works
Please help me
thank you
Full code:
breed [fish a-fish]
breed [boats boat]
boats-own [profit]
to setup
clear-all
ask patches [set pcolor blue]
set-default-shape fish "fish"
create-fish initial-number-fish
[
set color grey
set size 1.0
setxy random-xcor random-ycor
]
set-default-shape boats "boat"
create-boats initial-number-boats
[
set color black
set size 1.5
set profit random (1 * profit-per-fish)
setxy random-xcor random-ycor
]
reset-ticks
end
to go
if not any? turtles [stop]
ask fish
[
move
fish-reproduce
]
ask boats
[
move-boats
catch-fish
death
reproduce-boats
migrate
]
tick
end
to move
rt random 50
lt random 50
fd 1
end
to fish-reproduce
if random-float 100 < fish-growth
[hatch 1 [rt random-float 360 fd 1]]
end
to move-boats
rt random 50
lt random 50
fd 1
set profit profit - 0.1
end
to catch-fish
let prey one-of fish-here
if prey != nobody
[ask prey [die]
set profit profit + profit-per-fish]
end
to death
if profit < 0 [die]
end
to reproduce-boats
if profit > 1
[
set profit (profit / 2)
hatch 1 [rt random-float 360 fd 1]]
end
to migrate
if random-float 100 < random-spawn-rate
[create-turtles 2 [rt random-float 360 fd 1]]
end
If there are no boats to hatch new boats, then no more boats will be created. Your hatch is dependent on how many boats there are.
Essentially, for a boat:
if profit > 1
[
set profit (profit / 2)
hatch-boat 1 ...
]
You cut the profit in half for every boat spawned. Unless your profit grows or remains the same, your boats will eventually die out since no more will be hatching.
im doing an evacuation model which will compare the time to take if ull take the nearest exit or go with the crowd. What i have done so far is to simulate the" nearest exit"
to go
ask turtles [sideway]
end
to sideway
if pxcor < 0 [ set heading 270 ]
if pxcor > 0 [ set heading 90 ]
end
this is just apseudocode. now my question is that how to count the number of turtles in pxcor >0 and pxcore<0?
example
if pxcor > 0
counter ++;
count turtles with [ pxcor > 0 ]
turtles is just all the turtles. turtles with [ pxcor > 0 ] creates a new set that contains just the turtles from turtles for which pxcor > 0 is true. count just counts the number of agents in a set.