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



我正在开发一个模型,我的代理应该在该模型中开车上班。然而,他们的覆盖范围是有限的,这就是为什么我想使用WHILE功能来检查目的地的行驶距离(上班距离)加上到达目的地的驾驶距离(例如,使用自己的汽车提供送货服务=工作距离工作的距离在工作的距离之和应该小于130。否则,他们的汽油就会用完。

距离是根据概率分布绘制的。每个变量的概率分别称为chance_dinstance_to_work和chance_dstance_from_work。例如,他们开车2公里上班的概率为3%,开车4公里上班的几率为8%,等等。上班时开车的距离也是如此。我打算通过嵌套的ifelse语句来推导距离。

我想重复我的chance_dinstance_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函数,每只乌龟(这里是全职员工)都有自己的工作距离和工作距离变量。此外,如果条件或报告人没有说明[到工作的距离+工作的距离>=131],而是说明[到上班的距离+上班的距离<113],则它有效。因此,如果总和大于130,WHILE函数将返回工作距离和工作距离。但如果我只是切换符号,它就不再起作用了,而且两个变量的赋值总是0,所以默认值。

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

非常感谢。我感谢你的帮助!

我不理解这个问题,但我的回复太长了,无法给出"评论",所以我将其作为"答案"放在这里。

首先,这个开放式没有括号:"询问全职员工[",所以我认为你最后还想要一个"]"。

但是,您没有正确的"while"语句语法——它必须在测试和测试通过时要运行的命令周围都有括号。也就是说,它总是这样:

而[][]

你只有这个:而[]

因此,编辑器会给你一个错误:"虽然预期有两个输入,一个TRUE/FALSE块和一个命令块。">

同样,我认为您的意思是让代码的其余部分位于WHILE循环中。

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

无论如何,对我来说,你正在测试这些总和是否大于130是没有意义的,如果是,则继续驾驶。我假设当总行驶距离超过130公里时,您想停止驾驶。

第一个员工的WHILE测试将为TRUE,但这些数字将增加,直到WHILE测试最终失败,员工将停止驾驶。但你所写的任何东西都不会将这些变量重置为零,所以WHILE测试对其他所有驱动程序都会失败。不好。

因此,这些需要是局部变量,而不是全局变量,并且需要在"ask full-time employees[]"中声明(通过"let"语句)并初始化为零街区和街区起点附近。

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

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

你为什么要做这样的事?我认为你想做的只是简单地将新的chance_distance_to_work添加到旧的跑步总"工作距离"中,然后将新的chance_distance_at_work添加到老的跑步总的"工作距离"中,然后结束块。然后以(工作距离+工作距离)结束块,该距离等于到目前为止行驶的总距离,您可以在WHILE循环的下一次循环中进行测试。正确的

我认为这是正确的,这是代码中所做的更改。这段代码将运行。我写了很多"印刷"声明来展示它在做什么。我希望这能回答你的问题。

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

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。。。。。。工作机会距离今天=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]是否见过面。如果报告程序报告为false,则退出循环。否则,运行命令并重复。问题是到工作的距离和工作时的距离的默认值是0。因此,默认情况下,报告程序报告false并退出循环,甚至不运行命令一次。

我首先必须以这样的方式定义变量的默认值,即报告器报告true,从而运行命令。因此,修订版本如下:

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 ] ... ]
]
]

相关内容

最新更新