我有200只海龟可以是信息的发送者或接收者,并且可以在随机网络中相互连接。我要求接收者查看是否有发件人连接到他们,如果是这样,他们的知识值是一个有3个值的3个项目的集合,用添加到他们的邻居的知识值进行更新。
问题是myself
可以很容易地用于分配给变量,但在我的情况下,它不能用于更改预定义集合中的项目,因为我遇到一个错误,说这不是我可以使用set on!
有什么建议吗?
由于
Turtles-own [
knowledgeValue
receiver?
sender?
]
;Network is already constructed and turtles are connected.
to go
ask turtles [
set knowledgeValue []
]
ask turtles [
repeat 3 [
set knowledgeValue lput random-float 1 knowledgeValue
]
]
ask turtles with [receiver?] [
ask (turtles-on neighbors) with [sender?] [
; the knowledge of receiver gets updated
set [item 0 [knowledgeValue]] of myself [item 0 [knowledgeValue]] of myself + knowledgeValue
]
]
end
答案 0 :(得分:3)
问题不在于myself
。问题是您尝试set
list
中的值set
,而replace-item
用于变量。请改为ask myself set ....
。
你需要做\B
答案 1 :(得分:2)
如果你必须这样做,这里有一个选择。使用此设置:
******************************************************************************
This program contains Ipopt, a library for large-scale nonlinear optimization.
Ipopt is released as open source code under the Eclipse Public License (EPL).
For more information visit http://projects.coin-or.org/Ipopt
******************************************************************************
This is Ipopt version 3.11.9, running with linear solver mumps.
NOTE: Other linear solvers might be more efficient (see Ipopt documentation).
Number of nonzeros in equality constraint Jacobian...: 30
Number of nonzeros in inequality constraint Jacobian.: 0
Number of nonzeros in Lagrangian Hessian.............: 23
Total number of variables............................: 13
variables with only lower bounds: 0
variables with lower and upper bounds: 13
variables with only upper bounds: 0
Total number of equality constraints.................: 8
Total number of inequality constraints...............: 0
inequality constraints with only lower bounds: 0
inequality constraints with lower and upper bounds: 0
inequality constraints with only upper bounds: 0
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
0 9.9999900e-03 1.00e+00 5.00e-04 -1.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 5.9117705e-02 1.00e+00 1.20e+02 -1.0 5.36e+07 - 1.04e-05 7.63e-06f 18
2 1.1927070e+00 1.00e+00 2.62e+06 -1.0 9.21e+05 -4.0 6.16e-15 2.29e-23H 1
3 2.9689692e-01 1.00e+00 1.80e+05 -1.0 2.24e+13 - 1.83e-07 8.42e-10f 20
4r 2.9689692e-01 1.00e+00 1.00e+03 -0.0 0.00e+00 - 0.00e+00 4.58e-07R 11
5r 2.1005820e+01 9.99e-01 5.04e+02 -0.0 6.60e-02 - 9.90e-01 4.95e-01f 2
6r 7.7118141e+04 9.08e-01 5.18e+03 -0.0 2.09e+00 - 4.21e-01 1.00e+00f 1
7r 1.7923891e+04 7.82e-01 1.54e+03 -0.0 3.63e+00 - 9.90e-01 1.00e+00f 1
8r 5.9690221e+03 5.41e-01 5.12e+02 -0.0 2.92e+00 - 9.90e-01 1.00e+00f 1
9r 4.6855625e+03 5.54e-01 1.95e+02 -0.0 5.14e-01 - 9.92e-01 1.00e+00f 1
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
10r 8.4901226e+03 5.55e-01 5.18e+01 -0.0 2.24e-01 - 1.00e+00 1.00e+00f 1
Number of Iterations....: 10
(scaled) (unscaled)
Objective...............: 8.4901225582208808e+03 8.4901225582208808e+03
Dual infeasibility......: 6.3613117039244315e+06 6.3613117039244315e+06
Constraint violation....: 5.5503677023620179e-01 5.5503677023620179e-01
Complementarity.........: 9.9999982900301554e-01 9.9999982900301554e-01
Overall NLP error.......: 6.3613117039244315e+06 6.3613117039244315e+06
Number of objective function evaluations = 43
Number of objective gradient evaluations = 6
Number of equality constraint evaluations = 71
Number of inequality constraint evaluations = 0
Number of equality constraint Jacobian evaluations = 12
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations = 10
Total CPU secs in IPOPT (w/o function evaluations) = 0.006
Total CPU secs in NLP function evaluations = 0.001
EXIT: Maximum Number of Iterations Exceeded.
这会生成一个世界,其中所有turtles-own [
knowledgeValue
receiver?
sender?
]
to setup
ca
resize-world -3 3 -3 3
set-patch-size 70
ask patches [
sprout 1 [
set receiver? false
set sender? false
ifelse random-float 1 > 0.5 [
set receiver? true
set color green
set knowledgeValue [0 0 0]
] [
set sender? true
set color red
set knowledgeValue []
repeat 3 [
set knowledgeValue lput random-float 1 knowledgeValue
]
]
]
]
reset-ticks
end
都以receivers
作为[0 0 0]
列表开始(以证明knowledgeValue
是添加值的senders
。现在,让所有接收者通过sender?
到ask
请求接收者来更新该接收者的列表。我不是非常喜欢这个解决方案,但它可以解决你所概述的同样问题。
to option-1
ask turtles with [ receiver? ] [
; ask senders to ask 'myself' (receiver)
ask ( turtles-on neighbors ) with [sender?] [
ask myself [
; note that the second myself (below) actually refers to the 'sender'
set knowledgeValue replace-item 0 knowledgeValue ( item 0 knowledgeValue + item 0 [knowledgeValue] of myself)
]
]
]
end
一种替代方法是简单地对所需的值求和并直接添加它们:
to option-2
ask turtles with [ receiver? ] [
let my-senders ( turtles-on neighbors ) with [ sender? ]
if any? my-senders [
; get the sum of their item 0 knowledgeValue
let to-add sum [ item 0 knowledgeValue ] of my-senders
; add it to the knowledgeValue
set knowledgeValue replace-item 0 knowledgeValue ( item 0 knowledgeValue + to-add )
]
]
end