如何发送每个代理/机器人在Steel的火星探险家中单独执行任务?

时间:2017-11-08 13:05:17

标签: netlogo

我正在解决这个问题(Steel&#39的火星探险家)。成就定位岩石,障碍物,母船群并创建3个机器人。

另外,我发送我的3个机器人寻找岩石并且他们总是转向他们的母船,我遇到的问题是,当3个机器人中的1个找到岩石时,所有其他机器人都回到了母船,这是错误的,因为应该是每一个人去找石头然后转向母船。

这是我的代码:

globals [ ltcord ltcord2  xaa  yaa xbb  ybb xcc  ycc candidates ]

breed [ robotsA robotA ]
breed [ obstacles obstacle ]
breed [ ships ship]

to setup

  ca
  set ltcord  (list)
  set ltcord2 (list)
  set candidates (list)

  valid-position
  create-obstacless
  create-clusters
  create-ship
  create-robotss
end

to go
    general robotsA
end

to general [robot]

    let x [xcor] of robot
    let y [ycor] of robot
    set x (item 0 x)
    set y (item 0 y)

  let bool false
  ask patch x y [ set bool (pcolor = red) ]
  while [ bool = false ]
  [
    walk 1 robot

    set x [xcor] of robot
    set y [ycor] of robot
    set x (item 0 x)
    set y (item 0 y)

    ask patch x y [ set bool (pcolor = red) ]
  ]
  ask patch x y [ set pcolor black ]

  set bool false
  while [ bool = false ]
  [
    walk 2 robot

    set x [xcor] of robot
    set y [ycor] of robot
    set x (item 0 x)
    set y (item 0 y)

    if (round x = 50 and round y = 50)
       [set bool true]
  ]
  show "done"
end



to walk [op robot]
    let x [xcor] of robot
    let y [ycor] of robot
    set x (item 0 x)
    set y (item 0 y)

  if op = 1
  [
    move-robot-to-cluster robot
  ]

  if op = 2
  [
      move-robot-to-ship robot
  ]

   ifelse is-obstacle robot = 0
    [
      ask robot [ fd 1 ]
    ]
    [
         ask patch (x - 1) y
         [  if pcolor != blue
               [ addCandidate (list (x - 1)  y) ]
         ]

         ask patch (x + 1) y
         [  if pcolor != blue
               [ addCandidate (list (x + 1)  y) ]
         ]

         ask patch x (y + 1)
         [  if pcolor != blue
               [ addCandidate (list x (y + 1)) ]
         ]

         ask patch x (y - 1)
         [  if pcolor != blue
               [ addCandidate (list x (y - 1)) ]
         ]

         let n length candidates
         let rd random n

         let candidato (item rd candidates)

         let xi (item 0 candidato)
         let yi (item 1 candidato)

         ask robot
         [
           set xcor xi
           set ycor yi
         ]
         DeleteItemsCandidates
    ]
end


to-report is-obstacle [robot]
  let x nobody

  ask robot
  [ set x min-one-of other obstacles in-radius 1 [distance myself] ]

  if (x != nobody)
     [ report 1 ]
  report 0
end

to move-robot-to-cluster [robot]
  ask robot
  [
     If any? Patches with[ pcolor = red]
      [
        set heading towards one-of patches with[ pcolor = red ]
      ]
  ]
end

to move-robot-to-ship [robot]
  ask robot
  [
    set heading towards ship 10
  ]
end


to create-robotss

   ask n-of 3 patches ;with [ not is-obstacle? ]
  [
    sprout-robotsA 1
    [
      set color white
      set size 1
    ]
  ]

我可以改进或修复什么?

的问候,

0 个答案:

没有答案