Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
715 views
in Technique[技术] by (71.8m points)

netlogo - 如何在不初始化的情况下更新变量(How to update variables without being initialised)

following my previous post, I would like to ask the reason why a variable is being initialised when I try to update its value.

(在上一篇文章之后,我想问一下为什么在尝试更新其值时初始化变量的原因。)

I am trying to find a way to fix this issue.

(我正在尝试找到一种解决此问题的方法。)

What I am wishing to do is updating the variable's original value by adding a small random quantity q = random-float 0.1 , only when I add this-item to the neighbours' lists.

(我希望做的是仅当我this-item添加到邻居的列表中时,通过添加一个小的随机量q = random-float 0.1来更新变量的原始值。)

My expected output would be:

(我的预期输出将是:)

(object 16) with original attribute 0.147 neigh: 0 (object 16) with updated attribute 0.167 with random-float 0.02

((对象16)具有原始属性0.147,neigh:0(对象16)具有更新属性0.167,且具有随机浮点数0.02)

ie without initialising the original value:

(即不初始化原始值:)

(object 16) with original attribute 0.147 neigh: 0 (object 16) with updated attribute 0.02 with random-float 0.02

((对象16)具有原始属性0.147,neigh:0(对象16)具有更新属性0.02且具有随机浮点数0.02)

The latter is my current output, and this is wrong.

(后者是我当前的输出,这是错误的。)

I do not know how to fix this issue within my code.

(我不知道如何在我的代码中解决此问题。)

I am still trying to understand what is going wrong, but I have not found yet a way to fix it, so I hope you can help me to understand more.

(我仍在尝试了解问题所在,但尚未找到解决问题的方法,因此希望您能帮助我了解更多。)

My code is the following:

(我的代码如下:)

       if breed = breed1 [
          set selected people

          hatch-items 1 [
            hide-turtle
            set chosen selected
            set attribute1 random-float 1
            set attribute2 attribute1

            set this-item self
            print (word " attribute1 " attribute1 " attribute2 " attribute2)
            ask myself [
              set my-list fput this-item my-list
            ] ;; close ask myself
         ] ;; close hatch

           ; ADD NEW ITEM TO NEIGHBOURS' LISTS ;
            print (word " Before updating value, attribute1 " attribute1 " attribute2 " attribute2)
            ask link-neighbors with [breed = breed1] [
                   let q random-float 0.1
                   set attribute1 (attribute1 + q)
            print (word " After updating value, attribute1 " attribute1 " attribute2 " attribute2 " q " q)
   ] ;; close ask link-neighbors
] ;; close if

I put some print function to see the outputs:

(我放置了一些打印功能以查看输出:)

  • the first print returns me attribute1 0.85 and attribute2 0.85

    (第一次打印返回我attribute1 0.85 and attribute2 0.85)

    print (word " attribute1 " attribute1 " attribute2 " attribute2)

  • the second output returns me attribute1 0 and attribute2 0

    (第二个输出返回我attribute1 0 and attribute2 0)

    print (word " Before updating value, attribute1 " attribute1 " attribute2 " attribute2)

  • the third output returns me attribute1 0 attribute2 0 and q 0.08

    (第三个输出返回我attribute1 0 attribute2 0 and q 0.08)

    print (word " After updating value, attribute1 " attribute1 " attribute2 " attribute2 " q " q)

What is happening is that attribute1 is initialised within the ask link-neighbors , so I am adding the quantity q to 0 , and not to the original value of attribute, defined in the hatch.

(发生的事情是, attribute1ask link-neighbors初始化,所以我将数量q添加到0 ,而不是添加到填充中定义的attribute的原始值。)

Could you please help me to update it, without initialising the value?

(您能否在不初始化值的情况下帮助我进行更新?)

Many thanks

(非常感谢)

  ask by a.rimbaud translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...