netlogo 将概率转换为线值
netlogo transforming probabilities into line values
我有一个包含 10 个概率的列表,我想将它们转换为线段,以便执行轮盘赌选择。如何转换为线段?
概率列表:
[0.17 0.15 0.14 0.14 0.11 0.1 0.06 0.06 0.04 0.03]
转化为线段时应该是:
[0.17 0.32 0.46 0.60 0.71 0.81 0.87 0.93 0.97 1.0]
现在这是我的代码:
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
当然,它只是将当前概率和前一个概率相加,所以我得到:
[0.17 0.32 0.29 0.28 etc]
我不确定你所说的线段到底是什么意思,但如果你只是想要一个 self-contained 块来创建一个列表,其中
newlist[i] = (oldlist[i] + (newlist[i - 1]))
您可以使用 foreach
遍历旧列表并生成新的求和列表,如下所示。
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
我有一个包含 10 个概率的列表,我想将它们转换为线段,以便执行轮盘赌选择。如何转换为线段?
概率列表:
[0.17 0.15 0.14 0.14 0.11 0.1 0.06 0.06 0.04 0.03]
转化为线段时应该是:
[0.17 0.32 0.46 0.60 0.71 0.81 0.87 0.93 0.97 1.0]
现在这是我的代码:
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
当然,它只是将当前概率和前一个概率相加,所以我得到:
[0.17 0.32 0.29 0.28 etc]
我不确定你所说的线段到底是什么意思,但如果你只是想要一个 self-contained 块来创建一个列表,其中
newlist[i] = (oldlist[i] + (newlist[i - 1]))
您可以使用 foreach
遍历旧列表并生成新的求和列表,如下所示。
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