Netlogo:如何使用路由变量实际沿着路径移动

2023-12-24

我使用两种类型的乌龟,汽车和房屋。两者都是随机放置的。 我的目标是从组合路线向量开始为每辆车获取一条路线,并让每辆车移动并访问分配给它的每个家庭。 首先,我根据组合路线向量为每辆车创建一条路线。 我在下面展示我的代码。 但现在,我试图让汽车遵循各自的路线......

globals [ route-vector ]
breed [carr car]
breed [hous housess]
carr-own [ route ]

to setup
clear-all
create-carros
create-casas
make-routes
end

to create-carros
create-carr cars [ set color green ]
ask carr  [
set shape "car"
set size 1.5
setxy random-xcor random-ycor
 ]
end

to create-casas
create-hous house [ set color red ]
ask hous  [
set shape "house"
set size 1.5
setxy random-xcor random-ycor
]
end


to make-routes
set route-vector [ 3 4 5 6 7 0 1 2 0 1 ] ;5 10 15 20 25
let houses sublist route-vector 0 (length route-vector / 2 )
let carlist sublist route-vector (length route-vector / 2 ) (length route-
vector)
ask carr [ set route [] ]
(foreach carlist houses
 [ [the-car the-house] ->
  ask carr with [who = the-car] [ set route lput the-house route ]
 ]
  )
 end

 to go
  ask carr  [
    ;; if at target, choose a new random target
    ;        if distance route = 0
     ;          [ set route one-of route-vector
     ;            face route ]
    ;        ;; move towards target.  once the distance is less than 1,
     ;        ;; use move-to to land exactly on the target.
    ;        ifelse distance route < 1

    ;let mylist [1 2 3]
     ;foreach route
     face route
     fd 1
;print map last filter first route
    ;    face housess 3
     ;    fd 1

     ;    move-to one-of route
     ;    fd 1

      ]
     ; move-to housess 3
       ;fd 1

      ;tick
      end

我想使用路线变量来实际沿着路径移动。 但我不知道如何告知每辆车各自的路线并让他们搬到自己的家。

我尝试仅对一辆车使用“开始”按钮

做: 问车1 [ 面对路线 FD 1 但总是收到错误(“FACE 期望输入是代理,但得到了 改为列出 [4 7]。”) ] 结尾

在这种情况下,我想让 1 号车首先移动到 4 号房屋,然后移动到 7 号房屋,然后回到原来的位置...... 我尝试了多种方法,但找不到解决方案。我尝试单独执行此操作,我从每辆车的“路线”列表中选择了第一项,但我仍然无法..

如果有人可以帮助我,我真的很感激。谢谢


Using who索引海龟的数字可能会导致问题 - 在这种情况下,您将遇到无法真正动态更新列表的问题,因为hous and carr数字仅基于其创建顺序。如果可能的话,最好将海龟直接存储在列表中。使用您的设置的修改版本查看此示例:

globals [ route-vector ]
breed [carr car]
breed [hous housess]
breed [spawns spawn]
carr-own [ route route-counter spawn-target target]

to setup
  clear-all
  create-carros
  create-casas
  make-routes
  reset-ticks
end

to create-carros
  create-carr 3 [ set color green ]
  ask carr  [
    set size 1.5
    setxy random-xcor random-ycor

    ; set up a 'route-counter' to act as an index for a car's route
    set route-counter 0
    set target nobody
    set route []    
    pd
  ]

  ; hatch a 'spawn-target' turtle that can be used to return
  ; the carr back to their starting position
  ask carr [    
    hatch 1 [
      set breed spawns
      ht
    ]
    set spawn-target one-of other turtles-here with [ 
      xcor = [xcor] of myself
    ]
  ]
end

to create-casas
  create-hous 5 [ set color red ]
  ask hous  [
    set shape "house"
    set size 1.5
    setxy random-xcor random-ycor
  ]
end

现在,不再依赖who数字来索引房屋,直接使用您的房屋列表carr routes:

to make-routes
  ; Just use the car-order 
  let car-order [ 0 1 2 0 1 ] 

  ; Create a list of hous directly by sorting them
  let houses sort hous

  ; Your same foreach procedure, but in this case the carr
  ; are storing the house in the list directly to avoid
  ; indexing problems
  ask carr [  ]
  (foreach car-order houses
    [ [the-car the-house] ->
      ask carr with [who = the-car] [ set route lput the-house route ]
    ]
  )
end

然后,carr可以迭代他们的路线来根据索引值选择一个新目标route-counter(他们有一个小休息时间spawn-target).

to go 
  ask carr [

    ; If a car has no target, set the target to the
    ; item indexed by 'route-counter'
    if target = nobody [
      set target item route-counter route
    ]

    ; Movement chunk
    face target
    ifelse distance target > 1 [
      fd 1
    ] [ 
      move-to target

      ; Only advance the route counter if the current target
      ; was not the original spawn point
      if target != spawn-target [
        set route-counter route-counter + 1
      ]
      set target nobody

    ]

    ; If the route counter would index outside of the 
    ; list boundaries, reset it to 0
    if route-counter > length route - 1 [
      set route-counter 0
      set target spawn-target
    ]
  ]  

  tick  
end

这仍然不是超级程序化,因为您依赖于您的汽车订单与您的房屋数量相同的长度,但我不确定您实际上想要做什么,所以也许它会起作用。

EDIT

As per 你的评论 https://stackoverflow.com/questions/47730424/netlogo-how-using-the-route-variable-to-actually-move-along-the-path/47735178?noredirect=1#comment82473779_47735178- 如果你must使用who数字,您仍然可以使用“生成目标”示例让海龟返回到其起始位置 - 只需让它在carr and hous已经产生。再说一遍,这绝对不理想,因为如果您不小心生成顺序、数量,您的模型可能会“崩溃”carr / house, etc.

如此基础setup and create-casas程序如上所述,以此作为您的新程序create-carros程序:

to create-carros
  create-carr 3 [ set color green ]
  ask carr  [
    set size 1.5
    setxy random-xcor random-ycor

    ; set up a 'route-counter' to act as an index for a car's route
    set route-counter 0
    set target nobody
    set route []
    pd
  ]
end

现在,你的make-routes可以包含spawn目标海龟(此示例具有您评论中的乱序房屋):

to make-routes
  set route-vector [4 7 6 3 5 0 1 2 0 1]
  let houses sublist route-vector 0 (length route-vector / 2 )
  let carlist sublist route-vector (length route-vector / 2 ) (length route-vector)

  (foreach carlist houses
    [ [the-car the-house] ->
      ask car the-car [ 
        set route lput ( housess the-house ) route 
      ]
    ]
  )

  ; hatch a 'spawn-target' turtle that can be used to return
  ; the carr back to their starting position
  ask carr [
    hatch 1 [
      set breed spawns
      ht
    ]
    set spawn-target one-of other turtles-here with [
      xcor = [xcor] of myself
    ]
  ]
end

然后,go上面的过程应该可以正常工作,无需任何更改。

EDIT 2

让你的 carr 停止的一个简单方法是设置一个逻辑标志,以便只有满足特定条件的 carr 才会移动。考虑这个修改car-own and create-carros setup:

carr-own [ route route-counter spawn-target target route-complete? ]

to create-carros
  create-carr 3 [ set color green ]
  ask carr  [
    set size 1.5
    setxy random-xcor random-ycor

    ; set up a 'route-counter' to act as an index for a car's route
    set route-counter 0
    set target nobody
    set route []
    set route-complete? false
    pd
  ]
end

在这里,我们现在有一个名为的布尔(逻辑)变量route-complete?,设置为false对于所有新车。现在,您可以将一行添加到go程序说“只有具有route-complete?设置为 false,执行这些操作。”

to go
  ; ask carr with route-complete set to false
  ask carr with [ not route-complete? ] [

    ; If a car has no target, set the target to the
    ; item indexed by 'route-counter'
    if target = nobody [
      set target item route-counter route
    ]

    face target
    ifelse distance target > 1 [
      fd 1
    ] [
      move-to target

      ; Only advance the route counter if the current target
      ; was not the original spawn point. ADDITIONALLY,
      ; if the target is the starting target, set route-complete?
      ; to true for that carr
      ifelse target != spawn-target [
        set route-counter route-counter + 1
      ] [
        set route-complete? true
      ]
      set target nobody
    ]

    ; If the route counter would index outside of the
    ; list boundaries, reset it to 0
    if route-counter > length route - 1 [
      set route-counter 0
      set target spawn-target
    ]
  ]
  tick
end

你会注意到有一个修改过的位move-to块如果carr正在移回到其起始位置,它也设置了它的route-complete? to true, so that the next timegois called, that卡尔不会动。

请注意,您可以更改它route-complete?如果你愿意的话,可以到计数器而不是真/假carr跑过他们的路线一定次数。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Netlogo:如何使用路由变量实际沿着路径移动 的相关文章

  • Next.js 嵌套动态文件夹路由

    Update 由于 category slug index js 导致此错误getServerSideProps 我尝试做index js在产品文件夹下 它可以工作 这意味着它可以与 category slug 一起使用getServerS
  • 基于角色在同一路径上延迟加载模块

    我正在尝试根据我的角色加载 Angular 模块 当我登录时 我用 Angular Guard 尝试过 但这不起作用 当它失败时 它不会转到下一条路线 const routes Routes path loadChildren gt Aut
  • 如何使用Python中的or-tools解决累积旅行商问题?

    累积旅行商问题 CTSP 的目标是最小化到达客户的时间总和 而不是总旅行时间 这与最小化总旅行时间不同 例如 如果一个人拥有无限的车辆 车辆与位置数量相同 并且目标是最大限度地减少到达位置的总时间 则可以为每个位置发送一辆车 因为这是满足所
  • Angular 2 错误:无法解析“RouteParams”的所有参数

    尝试使用 RouteParams 获取查询字符串参数 但我只是收到错误 无法解析 RouteParams 的所有参数 确保所有 参数用 Inject 修饰或具有有效类型 注释并且 RouteParams 用 Injectable 修饰 an
  • 使用express.js动态加载路线

    我使用express js作为网络服务器 并且想要一种简单的方法来将所有 app get 和 app post 函数分开以分隔文件 例如 如果我想为登录页面指定 get 和 post 函数 我希望在动态加载的路由文件夹中有一个 login
  • MVC Ajax.ActionLink 找不到 POST 方法

    我在控制器中声明了一个 POST 方法 AcceptVerbs HttpVerbs Post public ActionResult UpdateComments int id string comments 在我看来 还有一个 Actio
  • 如何找到所有未包含在代理集中的代理?

    我有一个名为 Giant Component 的代理集 并将所有代理的颜色设置为红色 ask giant component set color red ask my links set color red 现在我需要将所有其他海龟的颜色设
  • Symfony2 / 路由 / 使用参数作为控制器或操作名称

    是否可以使用给定参数路由到控制器 操作 例如 my custom route pattern controller action defaults controller AcmeDemoBundle controller action 我希
  • 参数绑定到 ASP.NET MVC Core 中的路由或查询字符串

    我正在将 ASP NET MVC NET Framework Web 应用程序迁移到 ASP NET MVC Core 3 1 该应用程序是公司内部的 我们正在借此机会清理一些 API 路由 使它们更加 RESTful 例如 api Val
  • GET 和 POST 方法的单独 Flask 路由

    在 Flask 中定义路由时 最好的做法是使用由多个 HTTP 方法定义的单个路由 并在该单个路由中使用显式逻辑处理不同的 HTTP 方法 例如 app route api users methods GET POST def users
  • orchard cms路由问题

    我创建了一些自定义内容类型 其中包括路线部分 以便我的内容管理员可以编辑项目的别名 我没有运气配置一条路线 使我自己的控制器能够为这些项目的请求提供服务 核心 Routable 模块中到 ItemController 的路径的路由优先级为
  • Angular 2 路由器导航无法使用相同的 url 第二次工作

    我有一个页面 单击按钮后 它将使用下面的路由器导航进行重定向 router navigate search data test 但是 当我第二次单击同一按钮而不更改值时 router navigate 将无法工作 我怎样才能覆盖它 欢迎所有
  • 当约束失败时从路由重定向

    我想在路由约束失败时重定向到不同的网址 路由 rb 匹配 u gt user signin constraints gt 黑名单域 黑名单域 rb class BlacklistDomain BANNED DOMAINS domain1 c
  • 在 Laravel 4 路由中接受编码的 URL

    我正在 Laravel 4 Beta 5 中开发一个网站 我试图将编码的 URL 传递到路由器 问题是 编码的 URL 中包含百分比等 因此它被 Laravel 阻止 URL 使用 Javascript 函数进行编码encodeURICom
  • Slim3 从 CSRF 中间件中排除路由

    我正在建立一个基于 slim3 框架的网上商店 我需要处理服务器到服务器的 POST 请求以确认付款是否成功 我将 csrf 添加到容器中 如下所示 container csrf function container return new
  • asp.net Web Api 路由不起作用

    这是我的路由配置 config Routes MapHttpRoute name ActionApi routeTemplate api controller action id defaults new id RouteParameter
  • 如何使用NetLogo 6.2公平分配海龟?

    我有一个问题 我在这里寻求帮助 如何使用 NetLogo 6 2 为每种配置文件类型均匀分配海龟 https stackoverflow com questions 70748349 how to make an equal distrib
  • Spring 集成入站网关 当队列为空时触发事件

    我是新手 但我会尽量简洁 INPUT QUEUE gt INBOUND GATEWAY 1 gt ROUTER gt ACTIVATOR lt gt HOLD QUEUE gt INBOUND GATEWAY 2 我遇到的情况是 我必须像前
  • AngularJS:从控制器内读取路由参数

    如何在 AngularJS 控制器中读取 URL 中的参数 假设我有一个像这样的网址http localhost var value我希望将该值存储在控制器内的变量中 var value URL 我尝试过使用 routeParams val
  • ASP.NET MVC - 重写 FormMethod.Get 查询字符串?

    我有一个简单的表单 只有一个文本框和一个提交按钮 该表单基本上将文本框中的值作为查询字符串发送到不同的页面 当我单击提交按钮时 查询字符串采用以下格式 例如 mysite com TargetCode Test1 我希望它以这种格式显示 m

随机推荐