NetLogo: Neet to set the variable's value to a random number within a range - range

Dear NetLogo Community,
I am aiming to set a variable's value to a number between -1 and 1. I have tried the following code but in vain.
to xyz
[
set probability-of-wom compute-wom [-1 1]
if probability-wom > 0 [...]
]
end
to-report compute-wom -1 1
report -1 + random-float (1 - -1)
end
probablity-wom is a global variable in this case.
Appreciate your support in advance.
Thank you.
Regards,
Shreesha

Shreesha
Let's say you want to generate a random number between lower and upper. Then your compute-wom would be
to-report compute-wom [lower upper]
report upper - random-float (upper - lower)
end
In your case, you would
set probability-of-wom compute-wom -1 1
But a couple of comments. First, what you are generating here is a random number between two limits (as your title suggests), so calling it a probability could be misleading to anyone reading your code. Probabilities will normally be in the range from zero to one. If you really are just looking to do something with a 50% probability, you can simply say
if random-float 1 >= 0.5 [...]
Second, reporters should generally take variable arguments if they are to have arguments at all. Note that since you hard code -1 and 1 in the body of your to-report compute-wom reporter, passing them as arguments is redundant, and possibly misleading to anyone reading your code.

Related

How to create condition in NetLogo that 2 slider values cannot exceed defined value?

I created a model which as 2 different sliders, namely ratio1 and ratio2. They are located on the interface and their values should add up to 1 (here: labour-unit), and also cannot exceed this value. For now, NetLogo let's me exceed the condition.
I tried this:
to setup
create-turtles number-of-turtles ;; number of firms to be defined through slider
set labour-unit ratio1 + ratio2
set labour-unit 1
end
Therefore, my question is: How to create a condition in the setup that 2 slider values cannot exceed a defined value?
Is there any reason you actually need two sliders if the values always add to 1? Could you just have one slider called "proportion with labor-type x" or whatever you're modelling? Then, you can just have reporters to return the values for the actual proportion you're after- for example:
to-report ratio1
report proportion-slider
end
to-report ratio2
report precision ( 1 - proportion-slider ) 2
end
Then on your interface you could have the slider (and monitors if needed):

WITH expected a TRUE/FALSE block, but got a TRUE/FALSE instead?

I have the code
sum [plant-energy] of (patches-with-ash with (pycor > 0 and pxcor > 0)))
for a monitor in my model. plant-energy is a defined patch variable and patches-with-ash is a defined agentset. I'm trying to get a sum of all plant energies for the patches in patches-with-ash in the top-right half of the space, but this returns a weird error.
WITH expected this input to be a TRUE/FLASE block, but got a TRUE/FALSE instead
Any help would be much appreciated!
EDIT:
I'm just using the monitor as a test for my code. I'm trying to sum the plant energy of all patches in the agentset with xcor less than and ycor greater than a turtle (i.e. all patches of this agentset to the upper left of the turtle). I think this is the right avenue to go down but if anyone knows a better way I would appreciate that as well!
try it like this:
sum [plant-energy] of (patches-with-ash with [pycor > 0 and pxcor > 0]))
The [] basically tells NetLogo to do the test within the [] and return a true or false, which is then passed to the with

Netlogo How can I avoid the "dividing by 0" error during the dividing process compiling with Behavior space?

I get an error saying "It is dividing by 0". How can I avoid the "dividing by 0" error during the dividing process compiling with Behavior space of Netlogo? This is a Run-time Error popup message. " It is divided by 0. Error while observer running / Called by procedure GO, Called by button 'go' " The following is sample program. The variable "number-of-stop" means that number of ticks while stopping the turtles stopped on the road, and "number-of-dead" means that the number of dead turtles. Behavior space collects the data by every tick. When the variable is 0, the divide-by-zero error occurs. Are there any ideas? Thank you.
globals [ Ans number-of-stop number-of-dead ]
set Ans precision((number-of-stop) / (number-of-dead))3
just set a test before doing the calculation, for example (not tested):
set Ans ifelse-value (number-of-dead = 0)
[ -1 ]
[ precision((number-of-stop) / (number-of-dead))3 ]
This sets the value to -1 if the denominator is 0 and only does the division if the test fails (that is, if number-of-dead is not 0)
Another approach to this problem is to use carefully, which allows you to take an alternate action if an error occurs. It can be useful particularly if there are conditions that could cause and error that are difficult or tedious to check for in advance.
globals [ Ans number-of-stop number-of-dead ]
to go
set number-of-stop 10
set number-of-dead 0
carefully [set Ans precision((number-of-stop) / (number-of-dead))3] [set Ans -1]
show Ans
end
You can also as NetLogo to tell you what the error was, if that is useful.

Storing / recalling the value of a variable in Netlogo

Is there a way to store / recall values of a variable in Netlogo from previous ticks?
I need a way to recall what a variable was from previous ticks. If my turtle variable R was equal to 0 the last 3 ticks, I need it to set another variable back to zero.
Here is what that I was thinking.
to regression
ask turtles [if (R = 0 from last 3 ticks [Set Oin 0 ]]
end
How about making a list of variables, then limiting the length of that list to how far back you'd like the agent to "remember"
set memory []
then add to the list each tick and get rid of old memories with
set memory lput value memory
if length memory >= 4 [set memory but-first memory]
and if zero is on the list, have that alter behavior in some way
if member? 0 memory [blah]

error message "Min expected input to be a list but got the number 324 instead" when try to plot graph in Netlogo

really need advice on this, i want to plot graph where the x-axis is evacuation time (should start from 0 until max time required for people evacuate) and y-axis is N# turtles. below is code, when all peoples leave the room, graph will plot later, however when the simulation is done, it pop-up this error message "Min expected input to be a list but got the number 324 instead. 324 is max time-to-evacuate. anywhere in the code i did wrong?please advice. thanks
globals [
time-to-exits
time-to-evacuate]
to setup
set time-to-exits []
set time-to-exits lput time-to-evacuate time-to-exits
end
to go
if (flag-active-alarm )[active-alarm]
if all? turtles [ pcolor = red ] ;stops simuation
[plot-time-to-exits
stop ]
end
to plot-time-to-exits
set-current-plot "Escape-Time"
set-plot-x-range min time-to-evacuate max time-to-evacuate
set-current-plot-pen "evacuated"
plot-pen-down
plot count turtles
end
When you wrote:
min time-to-evacuate max time-to-evacuate
I think you must have meant:
min time-to-exits max time-to-exits
time-to-exits is a list of values, so it makes sense to use min or max with it. In contrast, time-to-evacuate is just a single number.
I had a similar problem with the command min, I got it to read the list by including the list primitive.
min (list time-to-evacuate)