在一条线上创建重叠代理
Creating overlapping agents in a line
使用下面的代码我得到的代理是这样的:
to setup
ask breadth-patches [sprout-walls wall-agents[set color 2]]
ask length-patches [sprout-walls wall-agents[set color 2]]
ask gap-patches [sprout-walls wall-agents[set color 2]]
ask length-patches[align-inside-at-top]
ask breadth-patches [align-inside-at-right-left]
ask gap-patches[align-inside-at-top]
end
to align-inside-at-top ;; patch procedure
let counter count walls-here ;; we will use this as a count-down, after using it in some calculations
if counter > 0 ;; could assume there are turtles, but we are not.
[ let gap1 1 / counter ;; size of turtles, gap between turtles
let half-gap gap1 / 2 ;; half-size of turtles
let ytop 0
if-else(pycor < 0)[set ytop pycor - .5 - half-gap]
[set ytop pycor + .5 - half-gap]
let xleft pxcor - .5 - half-gap
ask walls-here
[ set size gap1
set ycor ytop
set xcor xleft + gap1 * counter
set counter counter - 1 ;; so we're placing them from right to left
; set ycor ycor + 0.125
]
]
end
to align-inside-at-right-left ;; patch procedure
let counter count turtles-here ;; we will use this as a count-down, after using it in some calculations
if counter > 0 ;; could assume there are turtles, but we are not.
[ let gap1 1 / counter ;; size of turtles, gap between turtles
let half-gap gap1 / 2 ;; half-size of turtles
let ytop pycor + .5 + half-gap
let xleft 0
if-else (pxcor < 0)[
set xleft pxcor + .5 - half-gap]
[ set xleft pxcor - .5 + half-gap
]
ask turtles-here
[ set size gap1
set ycor ytop - gap1 * counter
set xcor xleft ;+ gap * counter
set counter counter - 1 ;; so we're placing them from right to left
]
]
end
注意:矩形中的空隙是由于以下代码
ask patches with [pxcor > (gap * (-1)) and pxcor < gap and pycor =(breadthrec - 1)][ask walls-here[die]]
此处,gap = 1,即宽度为 1 个补丁。
因此输入参数是 wall-agents
,它指定沿长度和宽度补丁每个补丁创建的代理数。
我希望更改为创建重叠代理,如下图所示(抱歉,该图不是那么完美,但我希望它能解释清楚)。请帮助了解如何实现这一目标。
代码很多,请大家调试。
我建议先解决问题的简单版本。您是否有代码可以在单个补丁中创建 wall-agents
只海龟,沿一条线均匀分布?一旦您有了执行该操作的工作代码,您就可以尝试将其推广到更复杂的问题。
如果您 运行 在编写更简单的版本时遇到困难,您可以在 Stack Overflow 上提出一个较小的问题,这比您当前的非常大的问题更容易回答。
如果您能够编写更简单的版本,请不要丢弃它 — 保留它,以便在需要时可以返回使用它。然后解决更大的问题。
您甚至可以采用更简单的版本,将其放入过程中,然后从较大的解决方案中调用该过程。制作有效的小程序,然后从其他程序调用这些较小的程序,通常是将问题分解为可管理部分的好方法。
使用下面的代码我得到的代理是这样的:
to setup
ask breadth-patches [sprout-walls wall-agents[set color 2]]
ask length-patches [sprout-walls wall-agents[set color 2]]
ask gap-patches [sprout-walls wall-agents[set color 2]]
ask length-patches[align-inside-at-top]
ask breadth-patches [align-inside-at-right-left]
ask gap-patches[align-inside-at-top]
end
to align-inside-at-top ;; patch procedure
let counter count walls-here ;; we will use this as a count-down, after using it in some calculations
if counter > 0 ;; could assume there are turtles, but we are not.
[ let gap1 1 / counter ;; size of turtles, gap between turtles
let half-gap gap1 / 2 ;; half-size of turtles
let ytop 0
if-else(pycor < 0)[set ytop pycor - .5 - half-gap]
[set ytop pycor + .5 - half-gap]
let xleft pxcor - .5 - half-gap
ask walls-here
[ set size gap1
set ycor ytop
set xcor xleft + gap1 * counter
set counter counter - 1 ;; so we're placing them from right to left
; set ycor ycor + 0.125
]
]
end
to align-inside-at-right-left ;; patch procedure
let counter count turtles-here ;; we will use this as a count-down, after using it in some calculations
if counter > 0 ;; could assume there are turtles, but we are not.
[ let gap1 1 / counter ;; size of turtles, gap between turtles
let half-gap gap1 / 2 ;; half-size of turtles
let ytop pycor + .5 + half-gap
let xleft 0
if-else (pxcor < 0)[
set xleft pxcor + .5 - half-gap]
[ set xleft pxcor - .5 + half-gap
]
ask turtles-here
[ set size gap1
set ycor ytop - gap1 * counter
set xcor xleft ;+ gap * counter
set counter counter - 1 ;; so we're placing them from right to left
]
]
end
注意:矩形中的空隙是由于以下代码
ask patches with [pxcor > (gap * (-1)) and pxcor < gap and pycor =(breadthrec - 1)][ask walls-here[die]]
此处,gap = 1,即宽度为 1 个补丁。
因此输入参数是 wall-agents
,它指定沿长度和宽度补丁每个补丁创建的代理数。
我希望更改为创建重叠代理,如下图所示(抱歉,该图不是那么完美,但我希望它能解释清楚)。请帮助了解如何实现这一目标。
代码很多,请大家调试。
我建议先解决问题的简单版本。您是否有代码可以在单个补丁中创建 wall-agents
只海龟,沿一条线均匀分布?一旦您有了执行该操作的工作代码,您就可以尝试将其推广到更复杂的问题。
如果您 运行 在编写更简单的版本时遇到困难,您可以在 Stack Overflow 上提出一个较小的问题,这比您当前的非常大的问题更容易回答。
如果您能够编写更简单的版本,请不要丢弃它 — 保留它,以便在需要时可以返回使用它。然后解决更大的问题。
您甚至可以采用更简单的版本,将其放入过程中,然后从较大的解决方案中调用该过程。制作有效的小程序,然后从其他程序调用这些较小的程序,通常是将问题分解为可管理部分的好方法。