Netlogo-如何让乌龟转向规格?

时间:2015-08-11 17:08:00

标签: netlogo

请参阅以下Netlogo文件。我希望海龟在WS3右转,来自“缓冲存储”,海龟在WS5左转,来自“QC缓冲存储”。但是,我收到了一个错误。我在网上发现了附加的代码并且不理解它100%。有人可以帮助我吗?

非常感谢, 约翰

globals [ 
  IN-free-cabinet 
  IN-free-drum-a
  IN-free-drum-b
  new_directions
  distance-to-jump
  WM-output
  WM-output-bad
  drum-output
  wip
  mlt-aux
  avg-mlt
  ]

patches-own[
  directions
  velocity
  processing-time
  is-processing?
  malfunction
  output
  ID
  bifurcation
]

breed [ machine ]
breed [ drum ]

machine-own [
  birth-time
  death-time
  process-plan
  processing
  time-of-processing
  QC-check
  ]

drum-own [
  birth-time
  process-plan
  processing
  time-of-processing
  ]

to create-WM [ machine-type ]
;********** Create Cabinet **********
 set-default-shape machine "square"
 ask patch -12 0 [
   ifelse count machine-here = 0 [ set IN-free-cabinet TRUE ] [ set IN-free-cabinet FALSE ] 
 ]
 if IN-free-cabinet 
 [
   create-machine 1 [
    setxy -12 0
    set size 1   
    set heading 90
    set color Blue
    set birth-time ticks
    set process-plan ["WS1" "WS2" "WS3" "WS4" "WS5" "WS6" "OUT" ]
    set processing FALSE
    set QC-check "GOOD"
    ] 
 ]
;********** Create Cabinet **********
;********** Create Drums **********
 set-default-shape drum "drum"
 ask patch -7 -3 [
   ifelse count drum-here = 0 [ set IN-free-drum-a TRUE ] [ set IN-free-drum-a FALSE ] 
 ]
  ask patch -3 3 [
   ifelse count drum-here = 0 [ set IN-free-drum-b TRUE ] [ set IN-free-drum-b FALSE ] 
 ]
 if IN-free-drum-a
 [
   create-drum 1 [
    setxy -7 -3
    set size 1   
    set heading 0
    set color Pink
    set birth-time ticks
    set process-plan ["WS3"]
    set processing FALSE
   ]
 ]
  if IN-free-drum-b
 [
   create-drum 1 [
    setxy -3 3
    set size 1   
    set heading 180
    set color Pink
    set birth-time ticks
    set process-plan ["WS5"]
    set processing FALSE
   ]
 ]
;********** Create Drum **********
end

to setup
  ;; (for this model to work with NetLogo's new plotting features,
  ;; __clear-all-and-reset-ticks should be replaced with clear-all at
  ;; the beginning of your setup procedure and reset-ticks at the end
  ;; of the procedure.)
  __clear-all-and-reset-ticks
  set WM-output 0
  set drum-output 0
  set avg-mlt 0
  set mlt-aux 0
  set WM-output-bad 0
;********** Main line **********
  ask patch -11 0 [
    set pcolor White
    set directions [-1 90 -1 -1] 
    set velocity 1
    set processing-time WS1-Processing-Time
    set is-processing? FALSE
    set malfunction FALSE
    set ID "WS1"
    set bifurcation FALSE
  ]
  ask patch -10 0 [
    set pcolor Yellow
    set directions [-1 90 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]
  ask patch -9 0 [
    set pcolor White
    set directions [-1 90 180 -1]
    set velocity 1
    set processing-time WS2-Processing-Time
    set is-processing? FALSE
    set malfunction FALSE 
    set ID "WS2"  
    set bifurcation FALSE
  ]
  ask patch -8 0 [
    set pcolor Yellow
    set directions [-1 90 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]
  ask patch -7 0 [
    set pcolor White
    set directions [-1 90 -1 -1]
    set velocity 1
    set processing-time WS3-Processing-Time
    set is-processing? FALSE
    set malfunction FALSE
    set ID "WS3" 
    set bifurcation FALSE
  ]
  ask patch -6 0 [
    set pcolor Yellow
    set directions [-1 90 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]
  ask patch -5 0 [
    set pcolor White
    set directions [-1 90 -1 -1]
    set velocity 1
    set processing-time WS4-Processing-Time
    set is-processing? FALSE
    set malfunction FALSE
    set ID "WS4" 
    set bifurcation FALSE
  ]
  ask patch -4 0 [
    set pcolor Yellow
    set directions [-1 90 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]
  ask patch -3 0 [
    set pcolor White
    set directions [-1 90 -1 -1]
    set velocity 1
    set processing-time WS5-Processing-Time
    set is-processing? FALSE
    set malfunction FALSE
    set ID "WS5" 
    set bifurcation FALSE
  ]
  ask patch -2 0 [
    set pcolor Yellow
    set directions [-1 90 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]
  ask patch -1 0 [
    set pcolor Yellow
    set directions [-1 90 -1 -1]
    set velocity 1
    set processing-time 0
    set is-processing? FALSE
    set malfunction FALSE
    set ID "WS6" 
    set bifurcation FALSE
  ]
  ask patch 0 0 [
    set pcolor Yellow
    set directions [-1 90 180 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation TRUE
  ]
  ask patch 1 0 [
    set pcolor Yellow
    set directions [-1 90 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]
  ask patch 2 0 [
    set pcolor Yellow
    set directions [-1 90 -1 -1]
    set velocity 1
    set processing-time 0
    set is-processing? FALSE
    set malfunction FALSE
    set ID "WS7" 
    set bifurcation FALSE
  ]
  ask patch 3 0 [
    set pcolor Yellow
    set directions [-1 90 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]
  ask patch 4 0 [
    set pcolor Yellow
    set directions [-1 90 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]  
  ask patch 5 0 [
    set pcolor Yellow
    set directions [-1 90 -1 -1]
    set velocity 1
    set processing-time 0
    set is-processing? FALSE
    set malfunction FALSE
    set ID "WS8" 
    set bifurcation FALSE
  ]
  ask patch 6 0 [
    set pcolor Yellow
    set directions [-1 90 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]
  ask patch 7 0 [
    set pcolor Yellow
    set directions [-1 90 -1 -1]
    set velocity 1
    set processing-time 0
    set is-processing? FALSE
    set malfunction FALSE
    set ID "WS9" 
    set bifurcation FALSE
  ]
  ask patch 8 0 [
    set pcolor Yellow
    set directions [-1 90 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]
  ask patch 9 0 [
    set pcolor Yellow
    set directions [-1 90 -1 -1]
    set velocity 1
    set processing-time 0
    set is-processing? FALSE
    set malfunction FALSE
    set ID "WS10" 
    set bifurcation FALSE
  ]  
  ask patch 10 0 [
    set pcolor Yellow
    set directions [-1 90 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]
  ask patch 11 0 [
    set pcolor Green
    set directions [-1 -1 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set output TRUE
    set ID "OUT"
    set bifurcation FALSE
  ]  
 ;********** Main line **********
 ;********** Drum buffer **********
 ask patch -3 1 [
    set pcolor Yellow
    set directions [180 -1 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ] 
  ask patch -3 2 [
    set pcolor Yellow
    set directions [180 -1 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ] 
   ask patch -3 3 [
    set pcolor Green
    set directions [180 -1 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]  
  ask patch -7 -1 [
    set pcolor Yellow
    set directions [0 -1 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ] 
  ask patch -7 -2 [
    set pcolor Yellow
    set directions [0 -1 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ] 
  ask patch -7 -3 [
    set pcolor Green
    set directions [0 -1 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]  
 ;********** Drum buffer **********
 ;********** Alternative line **********
  ask patch 0 -1 [
    set pcolor Black
    set directions [-1 -1 180 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ] 
  ask patch 0 -2 [
    set pcolor Black
    set directions [-1 90 -1 -1]
    set velocity 2
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]  
  ask patch 1 -2 [
    set pcolor Black
    set directions [-1 90 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ] 
  ask patch 2 -2 [
    set pcolor Black
    set directions [-1 90 -1 -1]
    set velocity 2
    set processing-time 20
    set is-processing? FALSE
    set malfunction FALSE
    set ID "WS11" 
    set bifurcation FALSE
  ]   
  ask patch 3 -2 [
    set pcolor Black
    set directions [-1 90 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]   
  ask patch 4 -2 [
    set pcolor Black
    set directions [0 -1 -1 -1]
    set velocity 2
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]   
  ask patch 4 -1 [
    set pcolor Black
    set directions [0 -1 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ] 
 ;********** Alternative line **********
 ;********** Line labels **********  
 ask patch -11 1 [
    set plabel "WS1"
  ]
  ask patch -9 1 [
    set plabel "WS2"
  ]
  ask patch -7 1 [
    set plabel "WS3"
  ]
  ask patch -5 1 [
    set plabel "WS4"
  ]
  ask patch -3 1 [
    set plabel "WS5"
    set plabel-color 25
  ]
; ask patch -1 1 [
;    set plabel "WS6"
;  ]
;;  ask patch 2 1 [
;    set plabel "WS7"
;]
;  ask patch 5 1 [
;    set plabel "WS8"
;  ]
;  ask patch 7 1 [
;    set plabel "WS9"
;  ]
;  ask patch 9 1 [
;    set plabel "WS10"
;  ]  
;;  ask patch 2 -1 [
;;    set plabel "WS11"
;;  ]  
  ask patch 11 0 [
    set plabel "OUT"
  ]   
  ask patch -6 0 [
    set plabel "SCANNER"
    set plabel-color 25
  ]
 ;********** Line labels ***********  
 ;********** Input Buffer ***********  
  ask patch -12 0 [
    set pcolor Green
    set directions [-1 90 -1 -1]
    set velocity 1
    set is-processing? FALSE
    set malfunction FALSE
    set bifurcation FALSE
  ]

 ;********** Input Buffer *********** 
end

to execute-random 
;********** Machine movement *********** 
 ask machine [
    ask patch-here[
       set distance-to-jump velocity
       set new_directions remove -1 directions ]
    set heading first new_directions
    ifelse not bifurcation [    
    if not processing and first process-plan != "WS3"[
    if not any? machine-on patch-ahead distance-to-jump and not [malfunction] of patch-ahead distance-to-jump[ jump distance-to-jump ]
    if first process-plan = ID [ 
        set process-plan remove-item 0 process-plan
        set processing TRUE 
        set is-processing? TRUE
        set time-of-processing processing-time
      ]
    ]
      if not processing and first process-plan = "WS3" and [ID] of patch-ahead distance-to-jump = "WS3" and 
;      not [malfunction] of patch-ahead distance-to-jump and 
      not any? machine-on patch-ahead distance-to-jump[
        if any? drum-on patch-ahead distance-to-jump [
          jump distance-to-jump
          set process-plan remove-item 0 process-plan
          set processing TRUE 
          set is-processing? TRUE
          set time-of-processing processing-time
          set shape "WM"
        ]
      ]  
      if not processing and first process-plan = "WS3" and [ID] of patch-ahead distance-to-jump != "WS3" and 
;      not [malfunction] of patch-ahead distance-to-jump and 
      not any? machine-on patch-ahead distance-to-jump[  jump distance-to-jump ]  
 ][ ifelse not any? machine-on patch-ahead distance-to-jump and not [is-processing?] of patch-ahead 2 and not [malfunction] of patch-ahead distance-to-jump [ jump distance-to-jump 
   set process-plan remove-item 0 process-plan
   set process-plan fput "WS7" process-plan
   ][
 if not any? machine-on patch-right-and-ahead 90 2 and ([malfunction] of patch-ahead 2 or any? machine-on patch-ahead 2) [
;   if use-W11 [ 
     set heading 180
    jump 2 
   set process-plan remove-item 0 process-plan
   set process-plan fput "WS11" process-plan ] ] ]
 ]
; ]
;********** Machine movement ***********  
;********** Drum movement *********** 
 ask drum [
   if not processing and [ID] of patch-here != "WS3"[
     ask patch-here[
       set distance-to-jump velocity
       set new_directions remove -1 directions ]
     if not any? drum-on patch-ahead distance-to-jump and ID != "WS3" and not [is-processing?] of patch-ahead distance-to-jump [ jump distance-to-jump ]
     ]
   if  any? machine-on patch-here [ die ]
   ]
;********** Drum movement ***********  
end

to resource-processing
  ask patches [
    if ID = "WS7" [ set processing-time 0 ]
    if ID = "WS11" [ set processing-time 0 ]
    ]

  ask machine [
    if processing [
      set time-of-processing time-of-processing - 1
      ask patch-left-and-ahead 90 1 [ set plabel-color Green]
      ]
    ]
  ask machine [
    if processing [
      if time-of-processing <= 0 [
        ask patch-here [
          set is-processing? FALSE
          ]
        ask patch-left-and-ahead 90 1 [ set plabel-color White]
        set processing FALSE
        if  [ID] of patch-here = "WS5" [ ifelse random-float 1 < 0.1 [ set QC-check "BAD" ] [ set QC-check "GOOD" ] ]
        ]
      ]
    ] 
;   ifelse malfunction-WS7  [ ask patch 2 0 [ set malfunction TRUE ] ]
;   [ ask patch 2 0 [ set malfunction FALSE ] ]
end

to count-output 
  ask machine [
        if plabel = "OUT" [
         set death-time ticks
         ifelse QC-check = "GOOD" [ set WM-output  WM-output + 1 ] [ set WM-output-bad WM-output-bad + 1 ]
         set mlt-aux (mlt-aux + (death-time - birth-time))
         set avg-mlt (mlt-aux / (WM-output + WM-output-bad))
                    die ]
        ]
end

to count-drums
  ask machine [
    if plabel = "SCANNER"
    [set drum-output drum-output + 1]
  ]
  end 

to resource-ocupation
  ask patches [
    ifelse any? machine-here [ set is-processing? TRUE ] [ set is-processing? FALSE ]
    ]

end

to statistics
  set-current-plot-pen "WIP"
  set wip count machine-on patches + count drum-on patches 
  plot wip
  set-current-plot-pen "avg-mlt"
  plot avg-mlt / 10
end

to execute
  create-WM[]
  resource-ocupation
  resource-processing
  execute-random
  tick
  count-drums
  count-output
  statistics
end

这是我的错误消息:

REMOVE expected input to be a string or list but got the number 0 instead.
error while patch -3 -1 running REMOVE
  called by procedure EXECUTE-RANDOM
  called by procedure EXECUTE
  called by Button 'Run'

org.nlogo.nvm.ArgumentTypeException: REMOVE expected input to be a string or list but got the number 0 instead.
 at org.nlogo.prim.etc._remove.report(_remove.java:57)
 at org.nlogo.prim._asm_procedureexecuterandom_setobservervariable_61.perform()
 at org.nlogo.nvm.Context.runExclusive(Context.java:119)
 at org.nlogo.nvm.ExclusiveJob.run(ExclusiveJob.java:57)
 at org.nlogo.nvm.Context.runExclusiveJob(Context.java:162)
 at org.nlogo.prim._asm_procedureexecuterandom_ask_59.perform(:1)
 at org.nlogo.nvm.Context.runExclusive(Context.java:119)
 at org.nlogo.nvm.ExclusiveJob.run(ExclusiveJob.java:57)
 at org.nlogo.nvm.Context.runExclusiveJob(Context.java:162)
 at org.nlogo.prim._asm_procedureexecuterandom_ask_57.perform(:1)
 at org.nlogo.nvm.Context.stepConcurrent(Context.java:91)
 at org.nlogo.nvm.ConcurrentJob.step(ConcurrentJob.java:82)
 at org.nlogo.job.JobThread.org$nlogo$job$JobThread$$runPrimaryJobs(JobThread.scala:143)
 at org.nlogo.job.JobThread$$anonfun$run$1.apply$mcV$sp(JobThread.scala:78)
 at org.nlogo.job.JobThread$$anonfun$run$1.apply(JobThread.scala:76)
 at org.nlogo.job.JobThread$$anonfun$run$1.apply(JobThread.scala:76)
 at scala.util.control.Exception$Catch.apply(Exception.scala:88)
 at org.nlogo.util.Exceptions$.handling(Exceptions.scala:41)
 at org.nlogo.job.JobThread.run(JobThread.scala:75)

NetLogo 5.2.0
main: org.nlogo.app.AppFrame
thread: JobThread
Java HotSpot(TM) 64-Bit Server VM 1.8.0_51 (Oracle Corporation; 1.8.0_51-b16)
operating system: Linux 3.13.0-24-generic (amd64 processor)
Scala version 2.9.2
JOGL: (3D View not initialized)
OpenGL Graphics: (3D View not initialized)
model: headsline

06:45:24.666 SwitchedTabsEvent (org.nlogo.app.Tabs) AWT-EventQueue-0
06:45:24.661 RuntimeErrorEvent (org.nlogo.app.App$$anon$1 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0
06:45:24.659 JobRemovedEvent (org.nlogo.app.App$$anon$1 (org.nlogo.window.GUIWorkspace)) JobThread
06:45:24.657 InterfaceGlobalEvent (org.nlogo.app.InterfacePanel$2 (org.nlogo.window.SliderWidget)) AWT-EventQueue-0
06:45:24.657 InterfaceGlobalEvent (org.nlogo.app.InterfacePanel$2 (org.nlogo.window.SliderWidget)) AWT-EventQueue-0
06:45:24.657 InterfaceGlobalEvent (org.nlogo.app.InterfacePanel$2 (org.nlogo.window.SliderWidget)) AWT-EventQueue-0
06:45:24.657 InterfaceGlobalEvent (org.nlogo.app.InterfacePanel$2 (org.nlogo.window.SliderWidget)) AWT-EventQueue-0
06:45:24.657 InterfaceGlobalEvent (org.nlogo.app.InterfacePanel$2 (org.nlogo.window.SliderWidget)) AWT-EventQueue-0
06:45:24.657 PeriodicUpdateEvent (org.nlogo.app.App$$anon$1 (org.nlogo.window.GUIWorkspace)) AWT-EventQueue-0
06:45:24.549 AddJobEvent (org.nlogo.window.ButtonWidget) AWT-EventQueue-0

1 个答案:

答案 0 :(得分:1)

J.Chen,我对你提出的问题没有答案,但你真正需要的帮助是关于如何开始调试NetLogo程序。 David Merino是对的,我们没有足够的信息为您调试程序,但您应该先采取更多步骤。 StackOverflow不是“调试我的程序”站点。这是您尝试解决问题后无法找到解决方案的问题网站。此时,您可以发布更具体的问题。

以下是调试您正在尝试的程序的一些提示:

错误消息开始:

REMOVE expected input to be a string or list but got the number 0 instead.
error while patch -3 -1 running REMOVE
  called by procedure EXECUTE-RANDOM
  called by procedure EXECUTE
  called by Button 'Run'

这意味着错误是由remove函数的实例引起的。在NetLogo中,您可以使用“编辑”菜单上的“查找”命令来搜索“删除”。更好的是,搜索“删除”,即在其后面有一个空格。程序中有remove命令的两个实例,因此您知道其中一个导致问题。

接下来,错误消息告诉您remove函数已由过程execute-random调用。这将是一个用户定义的过程。其定义将以to execute-random开头。事实证明,remove的两个实例都在execute-random过程中,因此您现在必须弄清楚哪一个导致问题。如果remove中有一个在execute-random,而另一个不在,那么你现在可以忽略另一个。

现在,错误消息显示“将预期输入删除为字符串或列表,但改为使用数字0”。如果您查看NetLogo字典中remove的文档,您会看到它的第二个参数应该是列表或字符串。源代码中的remove命令都尝试从名为-1的内容中删除数字directions。由于-1不是可以在字符串中的字符,因此directions可能应该是列表。但是,错误消息告诉您,有时direction包含数字0而不是列表。

我现在要做的可能是在包含print directions的行之前的源代码中插入remove语句。添加print self以打印补丁也可能很有用。我不知道接下来要做什么,因为我不懂程序,没有GUI元素我就无法运行它。

但是,这里有一个提示并不明显:所有变量都以值0开头。directions在源代码顶部附近定义为patches-own变量。我怀疑的是,至少有一个补丁,这个变量没有初始化,在执行remove行之一时将列表作为其值,这就是为什么directions的值为0的原因