如何绘制由两个不同品种创造的孵化品种

时间:2019-11-14 12:35:06

标签: netlogo

在绘制使用剖面线创建的对象时遇到一些麻烦。 我想分别绘制工程师A和工程师B创建的对象。 在JenB回答之后,我用于此目的的代码是

sk one-of turtles with [breed = engineersA or breed = engineersB]

    [ if empty? my-list
      [ set size random-float 1
      ]


        hatch-objects random 10
          [ hide-turtle
            ask myself
            [ set my-list fput self my-list
            ]
          ]
        ]

我想做的是

ask engineersA [plotxy ticks objects]

与工程师B相同。

不幸的是,由于对象是一个品种,所以它不起作用。

希望你能帮助我。

1 个答案:

答案 0 :(得分:2)

填充对象代码中的上下文存在问题,尤其是“ self”和“ myself”值的含义。编写的代码永远不会在my-lists上放置任何对象。解释起来很困难,所以我用冗长的代码编写了(运行的)代码?设置中的变量。如果设置为冗长?确实,打印命令将打印出代码每一步中发生的事情。 (如果将其设置为false,则打印命令将不会打印任何内容。)

代码的末尾是我用来获取每种工程师创建的对象数量图的命令。在那里的代码中对此进行了记录,并且可以正常工作。

我决定在每个对象中存储文本值“ A”或“ B”,以显示哪个品种创建了该对象。将品种存储在该变量中并对其进行测试是一个问题,所以我只使用了有效的文本。

我把海龟变大了,这样它们很容易点击检查。

这足以作为一个示例,让您了解如何计算和存储要执行的其他操作。

globals [
verbose?  ;; set this true to print out debugging info
count-type-a  ;; count of objects created by engineersA
count-type-b  ;; count of objects created by engineersB
]

breed [engineersA engineerA]
engineersA-own [ my-list]

breed [engineersB engineerB]
engineersB-own [my-list]

breed [objects object]
objects-own[
creator-breed  ;; set to either "A" or "B"
]

to setup
  clear-all
  set verbose? true   ;; turns on and off debugging print statements

  create-engineersA 3 [
    setxy random-xcor random-ycor
    set label who set size 3
    set my-list []
    ]

 create-engineersB 3 [
    setxy random-xcor random-ycor
    set label who set size 3
    set my-list []
    ]


  reset-ticks
end

to go
  let breed-picked "none"

  ask one-of turtles with [breed = engineersA or breed = engineersB]

    [
    ;;set breed-picked breed   ( produces a result that looks like a string but isn't)

    if-else ( breed = engineersA) [
      if verbose? [ print " this is breed engineersa "]
      set breed-picked "A" ]
    [ set breed-picked "B" ]

    if verbose? [ print ( word  "we are looking at turtle " who  " now, which is breed " breed-picked) ]

    if   my-list = []
      [
        if verbose? [print "my-list is empty"]
        ; set size random-float 1
      ]


        hatch-objects 3
          [
            set size 3 set color yellow set shape "circle"
            set creator-breed breed-picked
            if verbose? [
             print ( word " in hatch-objects, myself = " myself)
             print ( word " in hatch-objects, self = " self)
            ]

            let object-tag self ;; so this will persist into ask myself
            hide-turtle
            ask myself
            [     
                 if verbose? [
                  print ( word " in ask myself, self = " self )
                  print ( word " in ask myself, object-tag = " object-tag)
                  print ( word " who of object-tag " [who] of object-tag )
                 ]

                  set my-list fput object-tag my-list
        if verbose? [ print ( word   "hatched an object,  my-list is now " my-list)]
            ]


          ]
     ]
  if verbose? [ ask objects [ print creator-breed]]

   set count-type-a count objects with[ creator-breed = "A" ]
   set count-type-b count objects with[ creator-breed = "B" ]
   print ( word "count of objects created by engineersA is now " count-type-a)
   print ( word "count of objects created by engineersB is now " count-type-b)

   ;;  The following code works. I'm sure there are better ways to do it.
   ;;  The interface has a plot called "plot 1"
   ;;  Within that plot object, two pens were created, called "type-a" and "type-b"
   ;;      and the pens were given different colors.
   ;;  The check box for "show legend" was checked.
   ;;  Everything else was left at default values. The plot commands were left blank.

   set-current-plot "plot 1"   
   set-current-plot-pen "type-a"
   plot count-type-a

   set-current-plot-pen "type-b"
   plot count-type-b

end