simulation - 仅当 netlogo 中不存在链接时如何创建链接
问题描述
我是 Netlogo 新手,边做边学,希望有人能帮我解决看似简单的操作:
在每个滴答声中,对于每个海龟 X,我想创建或加强与半径radius内的其他海龟 Y 的无向链接,并以一定的概率createlinkprob。如果X 和 Y 之间已经存在链接,我想增加该链接的多重性。(由于 Netlogo 只允许存在一个链接,我需要将多重性存储在链接变量中,以指示更强的链接。)我似乎无法弄清楚如何做到这一点。
以下代码大致说明了我想要的,但不起作用。(在向半径内的所有海龟生成事件的内部“询问”中, 我的理解是“我自己”是指调用海龟 X,而“自我”是指 Y。这部分似乎有效。我正在尝试使用who号码来识别链接,但该部分没有。)
感谢您的任何帮助,您可以提供!
links-own
[
multiplicity
]
to setup
clear-all
make-turtles
reset-ticks
end
to make-turtles
create-turtles num-nodes [ set size 3 ]
;; layout-circle turtles max-pxcor - 1
ask turtles [
;; move each turtle to a random point
setxy random-xcor random-ycor
]
end
to go
generate-event
ask turtles
[
fd random 2
rt -5 + random 10
]
ask links
[
if random 100 < removelinkprob
[ifelse multiplicity = 1 [die] [set multiplicity multiplicity - 1]]
]
tick
end
to generate-event
;; at every event - one per tick - each turtle may connect to others in radius R with probability createlinkprob/100.
;; If already connected, the multiplicity value is incremented.
;; Otherwise, the multiplicity value is set to 1 and a link is created.
ask turtles
[
ask other turtles in-radius radius
[if random 100 < createlinkprob [
ifelse in-link-neighbor? myself
[
ask link who (show [who] of myself) set multiplicity (multiplicity + 1)
]
[create-link-with myself [set multiplicity 1]]]
]
]
end
; based on http://ccl.northwestern.edu/netlogo/models/GiantComponent
; Copyright 2005 Uri Wilensky.
; Modified by Michael Frishkopf
; under Creative Commons license: https://creativecommons.org/licenses/by-nc-sa/3.0/
解决方案
Following the edit where you provided a working example...
The structure of your code is already fit for doing what you want it to do. The only reason why it does not work is because of two syntax problems.
link
requires two reporters, i.e. the two turtles at the ends of the link (see here). You wrotelink who (show [who] of myself)
. Whilewho
is a reporter reporting a turtle,show [who] of myself
is not a reporter:show
prints a value, so you have to removeshow
and just writelink (who) ([who] of myself)
(note that the round brackets here are optional, and that their setup simply is a stylistic choice for readability).ask
requires square brackets enclosing the command (see here), therefore you have to put square brackets aroundset multiplicity (multiplicity + 1)
.
This leads to a reflection on the question itself.
When you write in NetLogo the code you provided, NetLogo brings you very precisely to where the problem is: it highlights the world show
and it gives you the following error message
Expected reporter
You should have been aware of this, and should have tailored your question to address this problem you had; or, perhaps better given the foundational nature of the problem, looked up what reporters are in NetLogo - see here the Programming Guide.
The same goes with ask
: after you fix the first syntax problem, NetLogo brings you very directly to where the second problem is, it highlights ask
and it gives you the following error message
ASK expected 2 inputs, an agent or agentset and a command block
So it would have been particularly easy to check how ask
works and see that it requires the command block to be in square brackets.
Note that this is exactly what you should do to Create a Minimal, Reproducible Example (see in particular the sections Minimal and Reproducible - Eliminate any issues that aren't relevant to the problem); with the additional benefit that, by following this guidance, you would have almost certainly resolved the two syntax problems yourself.
So please, in the future make sure that you check what error messages you are receiving and that you ask a question which is pertinent to those errors and following the website's guidance.
推荐阅读
- c# - SignalR 库在应用程序在没有调试点的情况下运行并在调试时正常工作时引发错误
- django - 对象没有属性“标题”
- java - 输出中的 Java 字符串连接错误
- dictionary - Plotly express 或 Go - 地图县内的标签
- node.js - 在 JWT 身份验证过程中需要帮助
- symfony - 如何实现 dotrine 实体事件监听器
- c++ - 使用地图进行验证
- c++ - 在子类成员之间传递数据
- google-cloud-platform - 使用 google-auth-library-ruby 和 google-api-ruby-client 的 3-legged OAuth 和一次性代码流
- spring-boot - Spring Boot 配置客户端无法解析配置服务器