如何使一只乌龟一次走到另一只乌龟?

时间:2019-06-17 14:53:15

标签: netlogo

我正在尝试编写一个程序来模拟无人机拾取数据包并将其传送到某些平台。数据包交付后,它们就会死亡。现在,如果执行“单个步骤”,无人机将立即从数据包位置移动到平台,然后再次移动到数据包位置。但是,我希望无人机按照实际步骤移动,这样我可以包括适当的数据包等待时间,并更好地了解交付一个包裹需要多长时间。

我尝试在多个位置实现“ fd 1”(例如,立即在“ to go”功能中实现,但也分别在“ fly”中实现,或者在“ fly-empty”和“ fly-loaded”中都实现)如果在这些位置中的任何一个实施“ fd 1”,无人机仍然会跳入(对我来说似乎是)随机跳变大小,并且系统停止运行,因为它实际上没有拾取任何软件包。如果有人可以帮助我!

enter code here
breed [platforms platform]
breed [packets   packet  ]
breed [drones    drone   ]

drones-own  [charge
         mypacket
         status
         consumption
         target
         dropoff
         capacity
         transportedpackets
]

packets-own [destination
         waitingtime
         pickups
         mydrone
         lastdrone
         ]

globals [delivered
     faults
     nrdrones
     deliverydrones
     colorize
     pickedup
     destinationlist
     dronewithpackets]

to setup
  ca
  clear-all-plots
  setup-globals
  setup-platforms
  setup-drones
  setup-packets
  reset-ticks
end

to setup-globals
  set delivered 0
  set faults    0
  set deliverydrones (list)
  set destinationlist(list)
  set colorize       color?
end

to setup-platforms
  create-platforms 1 [setxy -15 6  set color green set shape "circle" set label "Platform 0"]
  create-platforms 1 [setxy -2 10  set color green set shape "circle" set label "Platform 1"]
  create-platforms 1 [setxy 18 -7  set color green set shape "circle" set label "Platform 2"]
  create-platforms 1 [setxy 9 -2   set color green set shape "circle" set label "Platform 3"]
end

to setup-drones
  create-drones 3 [
    setxy 0 0
    set color red
    set mypacket            nobody
    set status              "ready"
    set charge              MaxCharge
    set label               who  
    set transportedpackets (list)
  ]
end

to setup-packets
  create-packets 10 [
    setxy 10 10
    set color yellow
    set shape "circle"
    set size .5
    set destination         platform 1
    set waitingtime         0
    set pickups             0
    set mydrone             nobody
  ]
  create-packets 4 [
    setxy -10 -10
    set color yellow
    set shape "circle"
    set size .5
    set destination         platform 3
    set waitingtime         0
    set pickups             0
    set mydrone             nobody
  ]
    create-packets 10 [
    setxy 9 -2
    set color yellow
    set shape "circle"
    set size .5
    set destination         platform 1
    set waitingtime         0
    set pickups             0
    set mydrone             nobody
  ]
      create-packets 10 [
    setxy -2 -10
    set color yellow
    set shape "circle"
    set size .5
    set destination         platform 0
    set waitingtime         0
    set pickups             0
    set mydrone             nobody
  ]
end

to go
  ask drones    with [status = "flying"     ]                 [fly          ]
  ask drones    with [status = "charging"   ]                 [recharge     ]
  ask drones    with [status = "ready"      ]                 [pickup       ]
  ask packets   with [mydrone = nobody      ]                 [countwaitingtime  ]
  ask drones    with [status = "waiting for new packets"]     [packetstopickup   ]
end

to fly
  set charge charge - consumption
  ;print (word "Drone " who "has this much charge left: " charge)
  if charge < 0 [if capacity != 0 [ask packets with [mydrone = dronewithpackets] [die] die]]
  ifelse mypacket      = nobody  [fly-empty] [fly-loaded]
end

to fly-loaded
  ask packets with [mydrone = dronewithpackets] 
  [print (word "Thanks drone" dronewithpackets " for dropping me, packet number " who " of at platform " position max destinationlist destinationlist) die]
  move-to dropoff
  if distance dropoff = 0
  [land]
end


to fly-empty
  ifelse any? packets with [mydrone = nobody]
    [set target one-of packets with [mydrone = nobody]
      move-to target
     if distance target = 0 
      [pickup]]
  [set status "waiting for new packets"
   print "No more packets to transport at the moment"]
end

to land
    set delivered delivered + capacity
  set status "charging"
end

to pickup
  ifelse any? packets-here with [mydrone = nobody]
    [ set destinationlist (list)
      set destinationlist lput (count packets-here with [destination = platform 0 and mydrone = nobody])  destinationlist
      set destinationlist lput (count packets-here with [destination = platform 1 and mydrone = nobody])  destinationlist
      set destinationlist lput (count packets-here with [destination = platform 2 and mydrone = nobody])  destinationlist
      set destinationlist lput (count packets-here with [destination = platform 3 and mydrone = nobody])  destinationlist
      ;print destinationlist 
      ;print (word "Platform " position max destinationlist destinationlist "is my next destination")
      ifelse max destinationlist >= 5 [set capacity 4][set capacity max destinationlist]
      set dronewithpackets who
      ask n-of capacity packets-here with [destination = platform (position max destinationlist destinationlist) and mydrone = nobody] [
        set mydrone dronewithpackets
        print (word "I am packet " who "and I am transported by drone " mydrone "to platform " position max destinationlist destinationlist)
        ;print (word "I have been waiting to be pickedup for " waitingtime )
  ]
      set dropoff platform (position max destinationlist destinationlist)
      set mypacket capacity
      set consumption capacity
      set pickedup pickedup + capacity
      ;print (word "I picked up " capacity " packet(s)")
      set transportedpackets lput capacity  transportedpackets
      ;print (word "I am drone " who " and I have transported " sum transportedpackets " packets today" )
      set status "flying"
      ]
   [set mypacket nobody              set consumption 1]
  set status "flying"
end

to recharge
  set charge charge + RechargePower
  if  charge > MaxCharge [set status "ready" ]
end

to countwaitingtime
  set waitingtime waitingtime + 1
end

to packetstopickup
  ifelse any? packets with [mydrone = nobody]
    [pickup] [fly-empty]
end

2 个答案:

答案 0 :(得分:1)

将您所有的move-to替换为:

face dropoff
forward 1

face告诉乌龟改变航向,朝着它试图到达的方向前进。这样,每次执行go迭代时,它将向前移动一个单位距离。

您应该考虑通过在tick过程的末尾添加go来增加模型的时间,该过程将使内部计数器增加。您可以通过例如增加waitingtime计数器来隐式使用时间,但是实际上没有时间在进步。

一旦您同时拥有tickforward,海龟将在每个滴答声中移动1个单位,并且同时发生的所有事情都可能在一个滴答声中发生。如果您有一个go按钮,则该按钮指示模型运行一次go过程,并且您可以编辑该按钮以检查“永远”,以便继续调用go过程

答案 1 :(得分:0)

下面的代码应该做到这一点。 ifelse循环中的while语句合并了JenB注释,即以小于1的步长永远不会到达目标,而距离小于1。

请检查以下ifelse:如果距离小于1,则无人机会移动到目标。如果距离更大,它会向目标移动一步,等待0.1秒(以显示离散步长),并再次执行while循环,直到无人机到达目标为止。

breed [targets target]
breed [drones drons]

to setup
  ca

  create-targets 5 [
    set shape "circle"
    set color white
    setxy random-xcor random-ycor

  ]

  create-drones 1 [
    set color red
  ]

end 

to go

ask one-of drones [

    ;; assuming you have multiple targets to choose from
    ;; if there is just one target, remove the line below
    let temp-target one-of targets 

    face temp-target

    while [distance temp-target != 0] [
      ifelse distance temp-target < 1 
      [ move-to temp-target ]
      [ wait 0.1 fd 1 ]
    ]
    show "target reached"
  ]

end