while 条件应该检查​​两个变量的总和是否低于阈值

While condition should check whether the sum of two variables is below a threshold or not

我正在开发一种模型,我的代理人应该在其中开车上班。但是,它们的范围是有限的,这就是为什么我想使用 WHILE 函数来检查驱动的距离是否 目的地( 工作距离 )加上目的地的行驶距离(例如,使用自己的汽车进行送货服务=上班距离)低于某个阈值(这里,130 公里)。这个想法是,代理人可以开车上班和 return 如果他们只开车到 130 公里,即 上班距离 distance-at-work 应该小于 130。否则,他们会 运行 没有汽油。

距离是根据概率分布得出的。每个变量的概率分别称为 chance_distance_to_work 和 chance_distance_from_work。例如。有 3% 的概率他们开车 2 公里去上班,有 8% 的概率他们开车 4 公里去上班,等等。同样适用于上班的距离。我打算通过嵌套的 ifelse 语句推导距离。

我想重复我的 chance_distance_to/at_work 变量的 "drawing" 直到两个变量 (distance-to/at-work) 的总和等于或小于 130,这样他们就可以回到他们的状态家乡。

turtle-own [
distance-to-work
distance-at-work
]

to setup-driving-pattern

ask full-time-employees [

; the loop should continue until the sum of both, distance-to-work 
; and distance-at-work is 130 or less
while [distance-to-work + distance-at-work >= 131] [

; I created two variables for the chance of each variable,
; in order to assign a certain number (km). This variable 
; should draw a random number between 0 and 99. According 
; to this number (or probability) the respective 
, distance-to-work or distance-at-work is drawn.

let chance_distance_at_work random 100
let chance_distance_to_work random 100

; here is a nested ifelse statement which continues until 100%, 
; but this is just an example
ifelse chance_distance_to_work < 3 [set distance-to-work 2 ] [
ifelse chance_distance_to_work < 8 [set distance-to-work 4 ] ... ]

ifelse chance_distance_at_work < 4  [set distance-at-work 1 ] [
ifelse chance_distance_at_work < 12 [set distance-at-work 2 ] ... ]
 ]
]

end

我猜 WHILE 函数中的加法有问题。因为没有 while 函数,每只乌龟(这里是全职员工)都会获得自己的工作距离和工作距离变量。此外,如果条件或报告者没有说明 [distance-to-work + distance-at-work >= 131],而是 [distance-to-work + distance-at-工作 < 131] 相反,它有效。因此,如果总和大于 130,WHILE 函数 returns 是工作距离和工作距离。但是如果我只是切换符号,它就不再起作用,并且两个变量的赋值始终为 0,因此默认值。

我可以独立设置两个变量,即我可以为每个变量获取一个值。但是,如果我现在想合并两个变量并检查它们的总和是否低于某个阈值(例如 130),那么两个变量总是被确定为 0。

非常感谢。感谢您的帮助!

我不明白这个问题,但我的回复对于 "comment" 来说太长了,所以我将其作为 "an answer".

放在这里

首先,这个左括号没有右括号:"ask full-time-employees [ " 所以我假设你想要最后一个“]”。

但是,"while" 语句的语法不正确——如果测试通过,它必须用括号括住测试和 运行 的命令。也就是说,它必须始终如下所示:

同时 [ ] [ ]

你只有这个: 而 [ ]

所以小编会报错:"While expected two inputs, a TRUE/FALSE block and a command block."

同样,我假设您的意思是将其余代码放在 WHILE 循环中。

因为我没有看到他们的 "let" 声明,所以我试图假设工作距离和工作距离是全局变量。 (下次请 post 所有代码,这样我们就不需要猜测了!)但是,如果它们是全局变量,您的代码将失败,因为它们将被初始化为零,因此 WHILE 测试将总是失败。

不管怎样,你在测试这些总和是否大于130,如果是,那么继续驾驶,这对我来说毫无意义。我假设当总行驶距离超过 130 公里时,您想 停止 驾驶。

第一位员工的 WHILE 测试将为 TRUE,但这些数字会增加,直到 WHILE 测试最终失败,员工将停止驾驶。但是您编写的任何内容都不会将这些变量重置为零,因此 WHILE 测试对于所有其他驱动程序都将失败。不好。

因此,这些需要是局部变量,而不是全局变量,并且需要在“询问全职员工[]中声明(通过 "let" 语句)并初始化为零 块并靠近该块的开头。

接下来,我不明白你在做什么,你有这样的测试:

ifelse chance_distance_to_work < 3 [set distance-to-work 2 ] [ ...]

你为什么要这么做?我认为你想要做的只是将新的 chance_distance_to_work 添加到旧的 运行ning 总数 "distance-to-work",然后将新的 chance_distance_at_work 添加到旧的 [=65] =]ning总计"distance-at-work",然后结束块。然后,您将以 (distance-at-work + distance-at-work) 等于到目前为止行驶的总距离来结束该块,您可以在 WHILE 循环的下一次传递中测试它。对吗?

我会假设这是正确的,这是其中进行了这些更改的代码。此代码将 运行。我放入了许多 "print" 语句来展示它在做什么。我希望这能回答你的问题。

这是它生成的代码和示例输出。

breed [full-time-employees full-time-employee]

to setup
  clear-all
  create-full-time-employees 2  ;; 2 is enough for testing
  reset-ticks
end

to go
ask full-time-employees [
    print word "Analyzing employee number " who
    let distance-to-work 0       ;; this is a running sum
    let distance-at-work 0       ;; this is a running sum 
    let total-distance-so-far 0  ;; this will be distance-to-work + distance-at-work

    ; the loop should continue until the sum of both, distance-to-work and distance-at-work is 131 or more
    while [total-distance-so-far <= 130] [

          ; drive one more day
          let chance-distance-at-work random 50
          let chance-distance-to-work random 50

          print word "......chance-distance-to-work today = " chance-distance-to-work
          print word "......chance-distance-at-work today = " chance-distance-at-work


          ;; calculate the total distance driven so far
          set distance-to-work ( distance-to-work + chance-distance-to-work)
          set distance-at-work ( distance-at-work + chance-distance-at-work)

          set total-distance-so-far distance-to-work + distance-at-work      
          print word "...At the end of this pass of the WHILE loop, total distance so far = " total-distance-so-far
     ]

     print word "......After we exit the WHILE loop, total distance so far = " total-distance-so-far
     print word " ----------- done with employee " who
     print " "
  ]   
  tick
end

和示例输出:

  • 正在分析员工编号 0 ......今天上班的机会距离 = 15 ......今天工作的机会距离 = 39 ......在这个过程结束时 WHILE 循环的总距离 = 54 ......今天的工作距离 = 8 ......工作距离 today = 37 ...在 WHILE 循环的这一遍结束时,总计 到目前为止的距离 = 99 ......今天上班的机会距离 = 10 ......今天工作的机会距离 = 35 ......在这个过程结束时 在 WHILE 循环中,到目前为止的总距离 = 144 ......在我们退出之后 WHILE 循环,到目前为止的总距离 = 144 ---------- 完成员工 0 ... 分析员工编号 1 ......今天的工作机会距离 = 42 ......今天工作的机会距离 = 11 ......在这一轮结束时 WHILE 循环的总距离 = 53 ......今天上班的机会距离 = 23 ......今天工作的机会距离 = 14 ......在这个过程结束时 WHILE 循环的总距离 = 90 ......今天上班的机会距离 = 17 ......今天工作的机会距离 = 31 ......在这个过程结束时 WHILE 循环的总距离 = 138 ......我们退出后 WHILE 循环,到目前为止的总距离 = 138 ---------- 完成员工 1

我不知道这是否是处理这些问题的常用方法,但我发现我一直 运行 犯的错误,我想分享它以防其他人可能 运行 陷入类似的问题。

所以我想看看有没有遇到记者[上班距离+上班距离<131]。如果报告者报告错误,则退出循环。否则 运行 命令并重复。问题是 distance-to-work 的默认值和 distance-at-work 的默认值是 0。因此,默认情况下,报告者报告 false 并退出循环,甚至没有 运行ning 命令一次。

我首先必须以这种方式定义变量的默认值,报告者报告真实,因此,命令是 运行。所以,修改后的版本如下:

ask full-time-employees [
    set distance-to-work 100
    set distance-at-work 100

    while [distance-to-work + distance-at-work > 131] [
    let chance_distance_to_work random 100
    let chance_distance_at_work random 100

ifelse chance_distance_to_work < 3 [set distance-to-work 2 ] [
ifelse chance_distance_to_work < 8 [set distance-to-work 4 ] ... ]

ifelse chance_distance_at_work < 4  [set distance-at-work 1 ] [
ifelse chance_distance_at_work < 12 [set distance-at-work 2 ] ... ]
 ]
]