Netlogo: How to format number on output - netlogo

I would like to format a number in NetLogo in a way that trailing zeros after the decimal point are not supressed, i.e. have the output 0.200 for the following command instead of 0.2:
observer> print 0.200
0.2
Is there a way to achieve this in NetLogo?

There is not in netlogo setup to my knowledge. Here is a dandy procedure that does the trick
to-report pad-zeros [in sig]
let out (word in)
if not member? "." in [set out word out "."]
while [ length out - position "." out < sig][set out (word out "0")]
report out
end
The drawback is that it reports a string. This is minimally annoying because you are only using it for output.

Related

netlogo: Minimum patch coordinate unit and monitoring availability?

I would like to change the minimum coordinate unit of the patch space, which is set to exist only one turtle per patch space, to less than one, and monitor it. We want to set the minimum spatial unit to the same tick size n as in tick-advance (e.g. n=0.1). Here is a piece of code, but it doesn't work. Can anyone help me? Thanks in advance.
globals [n] ; n<=1 i.e. n=0.1
; omitted
ask turtles [forward n]
tick-advance n
I find myself in the same boat as Matteo- I don't know that what you're looking to do is possible / makes sense in the context of NetLogo. The coordinates of patches are fixed (ie, one patch is one unit) but arbitrary (1 in Netlogo can mean 1 m or 1 km, depending on the model). In other words, a patch's coordinates are discrete, while turtles can move around in continuous space. So you can, of course, have a turtle wander around in step sizes of 1/10:
globals [n]
to setup
ca
set n 0.1
crt 10
reset-ticks
end
to go
ask turtles [
forward n
]
tick-advance n
end
After one run of go above, you could conceivably have a turtle at coordinates [xcor = 0.1, ycor = 0.1], although it would still be on patch 0 0 since pxcor values are integers.
It seems that what you are actually needing to do is not coming across as needed- can you edit your question to provide a little more detail / context? Perhaps knowing the why behind your need to model in this way will help askers get you pointed in the right direction. I personally am curious about:
Why you are using tick-advance instead of just tick
How you have implemented your one-turtle-per-patch restrictions- in other words, can you show a Minimal Reproducible Example? That may prompt other ways to approach what you're after.
Here is an example world with time tied to ticks:
globals [ seconds ]
to setup
ca
set seconds 0
resize-world 0 50 0 50
ask patches with [
floor (pxcor / 10) mod 2 + floor (pycor / 10) mod 2 = 1
] [
set pcolor white
]
crt 10
reset-ticks
end
to go
ask turtles [
fd 1
]
set seconds precision (seconds + 0.1) 2
if seconds mod 1 = 0 [print ( word "It has been " seconds " seconds.")]
tick
end

how to use to-report in multiple procedures in netlogo

I'm writing a NetLogo program and I'm facing an issue.
I have 'to-report' the procedure and it reports a value. In this report, I take input from the user.
But I want to use the reported value(output) in multiple other procedures.
to-report initial-goat
let goats-number read-from-string user-input "How many goats are there ?"
while [goats-number > 100] [set goats-number read-from-string user-input "Please enter a value below or equal to 100 "]
report goats-number
end
to goat-create
create-turtles initial-goat
end
to calculate
let x initial-goat
let alive count turtles
let dead (x - alive)
end
Here when I use the procedures to calculate and goat-create the program asks the user two times to give input. how to avoid this?
thanks.
best regards,
In fact, assuming that the calculate procedure runs each time step, your code will ask for the initial goats many times. As this information never changes, you should store it in a variable. There are two options.
If you want to use a question format, then:
globals [initial-goat]
to get-initial-goat
set goats-number read-from-string user-input "How many goats are there ?"
while [goats-number > 100] [set goats-number read-from-string user-input "Please enter a value below or equal to 100 "]
end
to goat-create
create-turtles initial-goat
end
to calculate
let alive count turtles
let dead (initial-goat - alive)
end
But the more usual way to do something like this is to simply have a slider on the interface rather than ask a question. You would have a slider (with upper limit 100) and then you only need:
to goat-create
create-turtles initial-goat
end
to calculate
let alive count turtles
let dead (initial-goat - alive)
end

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

Accumulating per-step mortality model procedure in Netlogo

Kindly advise as to what I might be doing wrong in the following netlogo procedure for per-step mortality (I am implementing the "Pass-Away" procedure as a step in a "GO" procedure):
to Pass-Away
ask turtles [
let chances 1 - exp( -1 * mortality * ticks )
let dead-count 0
if chances >= 1 [die]
set dead-count dead-count + 1
]
end
Is the above code correct? Even if it is correct, is there a modified or better alternative means to achieve a constant (cummulative) per-time-step turtle mortality as they move through the world? Finally, how do I get the variable "dead-count" to report to a monitor on the netlogo interface?
This will mostly work, except that your dead-count will always be 1. But it will probably not work as you intend. You should embark on an exploration of what you have written. The first thing to do when you get confused is to break things into smaller pieces. The second thing is to add some helpful visuals. In this case, you will learn a lot by plotting your representation of chances. I'm going to have to guess that your mortality attribute is a random float, since you did not say so. The following is a partial fix of your code, which provides enough clues to continue. You will need to add a plot in the interface tab -- see https://subversion.american.edu/aisaac/notes/netlogo-basics.xhtml#core-concepts-plotting -- and you can add a monitor in the same way if you find monitoring with print too clunky.
globals [dead-count]
turtles-own [mortality]
to setup
ca
set dead-count 0
crt 100 [set mortality random-float 1]
reset-ticks
end
to go
ask turtles [pass-away]
print (word "dead count = " dead-count) ;easiest monitor
clear-plot ;you'll need to have added a plot
foreach sort [chances] of turtles [
[?] -> plot ?
] ;keep an eye on the largest value in this plot ... get it?
tick
end
to-report chances ;turtle proc
report 1 - exp( -1 * mortality * ticks )
end
to pass-Away ;turtle proc
if chances >= 1 [
set dead-count dead-count + 1 ;*inside* the conditional! must come first!
print (word "chances: " chances)
die
]
end

Exporting data to CSV file

In my model I have option to choose agents (1,2,3 or 15). Each of them has same variables and I need to print them into CSV files(depending on number of agents, they may be 2 files or 15 files). What is the best way to do it? I have been looking in dictionary & StackOverflow, but I didn't found this case.
Thank you for your help.
So ... it is certainly possible but perhaps not convenient. For example,
extensions [csv]
turtles-own [var1 var2 att-file]
to setup
ca
crt 15 [setup-att-file]
end
to setup-att-file ;turtle proc
set att-file (word "turtle" who ".csv")
carefully [file-delete att-file] []
end
to-report get-att-vals ;turtle proc
report (list var1 var2)
end
to export-att-vals ;turtle proc
ask turtles [
file-open att-file
file-print csv:to-row get-att-vals
file-close
]
end
looks I don't need to use csv extension - just need to plot variables and then export plots.
ask turtles [
create-temporary-plot-pen (word who)
set-plot-pen-color color
plotxy xcor ticks
]
This works pretty much, answer was replied by Seth Tisue in this topic How to properly plot variables