我在链接切割和重新布线过程中遇到问题。 (计数和任何错误?)

问题描述 投票:0回答:1

在最后的代码中,在链接切割和重新布线过程中,我遇到错误**“COUNT预期输入是一个代理集,但得到的数字是0”,在“让临时链接计数计数我的-运行turtle 1时出现临时链接”。此错误由过程GO调用,由“go”按钮触发。 **

在此之前,我也尝试过以下代码,但是这次我收到了错误“ANY?预期输入是一个代理集,但得到的数字是 0”,对于“如果有的话?my-temporary-links [”这一行runningturtle 0。此错误也由程序 GO 调用,由“go”按钮触发

如果有的话?我的临时链接 [ 让选定的链接成为我的临时链接之一 让 other-turtle other [end1] of [selected-link] of selected-link ;选择链接另一端的代理 让 fikir-farki abs(Ai - get-my-link-weight other-turtle) ;代理人之间的外部理念差异

我愿意更改整个代码块,因此欢迎任何建议。


globals [
  link-weight-threshold
  nd
  maximum-loop-count
  link-weights ; list to store link weights
]

turtles-own [
  my-permanent-links ; to store permanent links
  my-temporary-links ; to store temporary links
  agents-with-majority-external
  agents-with-minority-external
]

to-report get-my-link-weight [other-turtle]
  ; Get the weight of the link on the other end of the given agent
  let link-index position other-turtle my-permanent-links
  ifelse link-index != false [
    report item link-index link-weights
  ] [
    report 0 ; Report 0 as the default if the link weight is not found
  ]
end

to create-permanent-links
  create-links-with n-of nd other turtles
  [
    ask myself [
      set my-permanent-links lput self my-permanent-links ; Create a permanent link with own agent
    ]
    set link-weights lput (random-float 0.5 + 0.5) link-weights ; add the weight to the list between 0.5 and 1
    set color red ; Set the color of permanent links to red
  ]
end

to create-temporary-links-with [other-turtle]
  ; Initiate a procedure to create a temporary link with the other turtle.
  create-link-with other-turtle [
    ; Create a new link with the other turtle.

    set color blue
    ; Set the color of the new link to blue.

    set shape "temp"
    ; Set the shape of the new link to "temp".

    let weight random-float 0.5
    ; Set the weight of the new link to a random decimal between 0 and 0.5.

    ;set my-temporary-links lput self my-temporary-links
    ; Add the new link to the list of temporary links on itself.
  ]
end

to remove-temporary-link-with [link-to-remove]
  ask link-to-remove [
    let link-index position self link-weights
    if link-index != false [
      set link-weights remove-item link-index link-weights ; remove the weight from the list
    ]
    die
  ]
end

to setup
  clear-all
  set nd 1 ; Ensure each agent has at least 1 neighbor
  set link-weight-threshold 0.5
  set maximum-loop-count 50 ; Maximum loop count
  set link-weights [] ; create an empty list to store link weights
  create-turtles 3 [
    setxy random-xcor random-ycor
    set shape "person"
    set my-permanent-links [] ; create an empty list for each agent's permanent links
    set agents-with-majority-external []
    set agents-with-minority-external []
  ]
  ask turtles [
    create-permanent-links
    set color white
  ]
  reset-ticks
end

to go
  while [ticks < maximum-loop-count] [
    ask turtles [
      ; Stage 1: Assigning internal opinion (Fixed)
      let Ai random-float 2 - 1

      ; Stage 2: Assigning a random number for external opinion
      let Bi random-float 2 - 1

      ; Updating color and status based on the agent's opinion
      ifelse Bi > 0 [
        set agents-with-majority-external fput self agents-with-majority-external
        set color green
      ]  [
        set agents-with-minority-external fput self agents-with-minority-external
        set color orange
      ]

      ; Updating External Opinion Only via Temporary Links
      let β 0.1
      let y 0.3

      if is-agentset? my-temporary-links and any? my-temporary-links [
        ask my-temporary-links [
          let other-turtle-link-neighbors link-neighbors
          ifelse(count other-turtle-link-neighbors > 0) [
            ask other-turtle-link-neighbors [
              set Bi Bi + y * abs(Bi - get-my-link-weight myself) + β * abs(get-my-link-weight myself - Bi) ; Update Bi
            ]
          ] [
            ; If there are no link-neighbors, perform the necessary operations
          ]
        ]
      ]

      ; Link Cutting and Rewiring
      let temporary-link-count count my-temporary-links
      if temporary-link-count > 0 [
        let selected-link one-of my-temporary-links
        if is-link? selected-link [
          let other-turtle other [end1] of selected-link ; Select the agent at the other end of the link
          let opinion-difference abs(Ai - get-my-link-weight other-turtle) ; External opinion difference between agents
          let threshold-value 0.2

          ; If the external opinion difference is small and there is no temporary link between the agents yet, connect the link
          if (opinion-difference <= threshold-value) [
            ; Remove the temporary link
            set my-temporary-links (my-temporary-links with [self != selected-link])
            ; remove the weight from the list
            set link-weights remove-item (position selected-link link-weights) link-weights
          ]
        ]
      ]
    ]
    tick
  ]
end
netlogo
1个回答
0
投票

一般来说,发生这样的错误是因为代理集(在本例中是链接集)尚未初始化为集。默认情况下,NetLogo 将所有变量初始化为零,这可能就是您收到错误的原因。如果在

my-temporary-links
中创建每个代理时将每个代理的
no-links
初始化为
setup
,则
my-temporary-links
将以空链接集开始,并且
count
any?
应正常运行。

查尔斯

© www.soinside.com 2019 - 2024. All rights reserved.