NetLogo updation error approximation issue - netlogo

Please check the NetLogo code below with output.
CODE:
to create-files
let i 0.10
while[i < 1]
[
set i i + .05
show i
]
end
OUTPUT:
observer: 0.15000000000000002
observer: 0.2
observer: 0.25
observer: 0.3
observer: 0.35
observer: 0.39999999999999997
observer: 0.44999999999999996
observer: 0.49999999999999994
observer: 0.5499999999999999
observer: 0.6
observer: 0.65
observer: 0.7000000000000001
observer: 0.7500000000000001
observer: 0.8000000000000002
observer: 0.8500000000000002
observer: 0.9000000000000002
observer: 0.9500000000000003
observer: 1.0000000000000002
I wonder the why is updation error? Is there some approx. taking place? How to avoid it?

Take a look at the section on floating-point accuracy in the NetLogo programming guide. Virtually every widely used programming language has the same problem. Essentially in order to be able to represent both really small number and really big numbers, you get problems like this.
A simple fix is to use precision to round the numbers off to the decimal that you want. In your case, you probably want:
to create-files
let i 0.10
while[i < 1]
[
set i precision (i + .05) 2
show i
]
end

What Bryan said. But note also:
to create-files
let i 2
while [i < 20] [
set i i + 1
show i / 20
]
end
prints:
observer: 0.15
observer: 0.2
observer: 0.25
observer: 0.3
observer: 0.35
...

Related

Simulink misses data points in a from-workspace block for discrete simulation

I have a simulation running at 50 Hz, and some data that comes in at 10 Hz. I have extra 'in-between' points with dummy data at the following 50 Hz time points, and interpolation set to off. This should in theory ensure that between 10 Hz time steps, the dummy data is being held and only at the 10 Hz steps is the actual data present. For example, my data vector would be
[0.0 0.8 0.1 0.12 0.2 0.22 0.3 0.32 0.4 0.42 0.5 0.52 ...
-1 -1 1 -1 2 -1 3 -1 4 -1 5 -1 ...]
However, with a scope attached directly from the 'from-workspace' block, simulink is returning this:
[0.0 0.8 0.1 0.12 0.2 0.22 0.3 0.32 0.34 0.4 0.42 0.5 0.52...
-1 -1 -1 -1 2 -1 3 3 -1 4 -1 5 5...]
where some values are skipped and others are repeated in a consistent pattern. Is there something with simulinks time-step algorithms that would cause this?
Edit: A solution I ended up finding was to offset the entire time vector by 1/100th of a second so that the sim was taking data between points rather than on points, and that seemed to fix it. Not ideal, but functional.

Netlogo Diffusion Confusion

I've been playing around with the diffuse keyword.
Consider the following 3x3 world where there's 3 chemical gradients at the top left corner and no chemical elsewhere. Also, there's no wrap on the edges.
[3 0 0 ]
[0 0 0 ]
[0 0 0 ]
If I have a diffusion rate of .5, I would expect that 3 (gradient) * .5 (diffusion rate) / 3 (#neighbors) = .5of the gradient would be given to its 3 neighbors. I would also expect that the original patch has 1.5 units remaining.
However, when I run the diffuse code, it seems that 3 (gradient) * .5 (diffusion rate) / 8 (#neighbors) = .1875 of the gradient is being set to the 3 neighbors. The original patch then has 2.4375 remaining units which isn't .5 of the original gradient. What's going on here? Is this an error or is my understanding incorrect?
See below:
patches-own [value]
to setup
cp
ask patch 0 2 [ set value 3]
diffuse value .5
ask patch 1 1 [ show value]
end
observer: show [value] of patches
observer: [0.1875 0.1875 0 2.4375 0 0 0 0.1875 0]
observer> ask patch 0 2 [ show count neighbors]
(patch 0 2): 3
One quick edit to your code is if you want the top left patch to have a value of 3, you need to ask patch 0 2. You're currently asking the bottom right patch.
Now, your issue is coming from the fact that when you diffuse, it tried to give 1.5 value away spread out over 8 patches, giving each neighboring patch 0.1875. Since your starting patch is in a corner is is only able to spread out across 3 patches and only gives away 0.5625 (3*.1825).
This leaves the original patch with 2.4375.
Note you do get your expected result if you allow the world to wrap around.

netlogo transforming probabilities into line values

I have a list of 10 probabilities that I want to transform to line segments so I can perform roulette wheel selection. How can I transform to line segments?
Probability list:
[0.17 0.15 0.14 0.14 0.11 0.1 0.06 0.06 0.04 0.03]
When transformed into line segments should be:
[0.17 0.32 0.46 0.60 0.71 0.81 0.87 0.93 0.97 1.0]
Right now this is my code:
to calculate-s
let i 1 ;; previously added a 0 to list using set p-list fput 0 p-list
while [i < 11] [
ask turtles [
if [p] of self = item i p-list [
let s (p + item (i - 1) p-list)
]
]
set i (i + 1)
]
end
But of course it just sums the current probability and the previous one so I get:
[0.17 0.32 0.29 0.28 etc]
I'm not sure exactly what you mean by line segments, but if you just want a self-contained block that creates a list where
newlist[i] = (oldlist[i] + (newlist[i - 1]))
You can use foreach to step through the old list and generate a new list of summed values, as below.
to make-segments
let oldlist [0.17 0.15 0.14 0.14 0.11 0.1 0.06 0.06 0.04 0.03]
let newlist []
let n 0
foreach oldlist [
[x]->
ifelse n < 1 [ ;;; if index is 0 you can't index 0 -1, so just use item 0
set newlist lput item 0 oldlist newlist
]
[ ;;; else, add the item n from the old list to the
;;; previous item in the new list.
set newlist lput (precision (item n oldlist + item (n - 1) newlist) 2) newlist
]
set n n + 1
]
print newlist
end

How to add label on Distance File - Orange

How to add label to each object/ row in distance file in Orange?
I've tried as the example in this link
4
john 0.1
joe 0.5 0.3
jack 0.7 0.9 0.2
jane 0.2 0.8 0.6 0.5
but Orange give the following error:
Error while reading the file: 'invalid literal for int() with base 10:'john"
Example is broken. It should be like this:
4 labelled
john 0.1
joe 0.5 0.3
jack 0.7 0.9 0.2
jane 0.2 0.8 0.6 0.5

Estimating two matrix using Hidden markov model toolbox in MATLAB

I am trying to apply Hidden Markov Model to improve my detection accuracy.
In my program, there are two states, 1 and 0. I used Bayes detector to generate the probability for each instance to be in class 1 and 0. For example, I have a sequence Actual states: 1 1 1 1 1 0 0 0 0 0
probability in class 1: 0.5 0.6 0.7 0.8 0.9 0.2 0.3 0.4 0.5 0.5
probability in class 0: 0.5 0.4 0.3 0.2 0.1 0.8 0.7 0.6 0.5 0.5
I tried to "use probability in class 1" as the "seq" and "Actual states" as "states" in function hmmestimate ([estimateTR, estimateE] = hmmestimate(seq,states);)
But it seems that "seq" must be integers, and I do not understand what is the requirement for the input arguments.
Thanks in advance!