我对网络标识很陌生,我正在尝试构建一个网络,在这个网络中,节点有一个随机数,随滴答而变化,然后随机连接到其他节点。一旦建立了连接,我需要:如果两个节点之间的数目相同,那么链接就会结晶并改变颜色,否则它就会被删除,这个过程会被重复,直到所有节点都有相同的随机值。提前感谢
发布于 2021-06-23 15:14:58
在评论中作出澄清之后..。
您提到的事情可以以非常简单的方式完成,因此希望您可以使用下面的解释和关键字浏览NetLogo手册(特别是NetLogo字典),并完全熟悉正在发生的事情。
首先,让我们设置一些东西
globals [
; You'll probably want to declare these two variables directly
; in the Interface with a slider or something, but here I'll
; set them up from within the Code section.
number-of-turtles
level-of-randomness
]
turtles-own [
my-number
connected?
]
to setup
clear-all
reset-ticks
set-globals
create-turtles number-of-turtles [
setxy random-xcor random-ycor
set connected? false
]
end
to set-globals
set number-of-turtles 500
set level-of-randomness 1000
end到目前为止,你有很多海龟分散在它们的环境中,每个人都可以访问几个全局变量,还有几个属于每个海龟的变量来跟踪它们的状态。
根据你所描述的逐字逐句地说,你可以说:
to go.v1
; This first block of commands below is to get rid of the links that
; emerged in the previous iteration of 'go.v1', but that
; you don't want to keep because they link turtles with
; different 'my-number'. You will also need to include the
; stop condition that best suits you.
ask links with [color != green] [
die
]
; Insert here a stop condition.
ask turtles with [connected? = false] [
set my-number random level-of-randomness
]
; The 'let' command creates a local variable.
ask turtles [
let target one-of other turtles
create-link-with target
; The statement below containing "[who] ..." is how you need to
; call a link: 'who' is a built-in turtle-own variable reporting
; the ID number of the turtle, and to report a link you will need
; to give it the two IDs of the two turtles being connected.
if (my-number = [my-number] of target) [
ask link ([who] of self) ([who] of target) [
set color green
]
if (connected? = false) [
set connected? true
]
if ([connected?] of target = false) [
ask target [
set connected? true
]
]
]
]
tick
end上面的代码按照您的话做了:每个海龟总是与另一个海龟创建链接,然后测试条件,并基于该条件(其结果存储为链接的颜色:只有好的链接才成为green),在下面的go.v1迭代开始时保留或删除链接。
然而,尽管您可能有理由这样做,但您可能只是对一个需要较少计算的替代方案感到满意:
to go.v2
; Insert here a stop condition.
ask turtles with [connected? = false] [
set my-number random level-of-randomness
]
ask turtles [
let target one-of other turtles
if (my-number = [my-number] of target) [
create-link-with target [
set color green
ask both-ends [
if (connected? = false) [
set connected? true
]
]
]
]
]
tick
end这样,海龟在创建任何链接之前都会评估潜在的伴侣,并且只有在my-number相同的情况下才会继续创建链接。这样,就没有必要创建并消除所有不必要的链接( my-number条件无论如何都必须在go.v1中测试)。
https://stackoverflow.com/questions/68085986
复制相似问题