比较来自两个品种的两个变量

Compare two variables from two breeds

我还没有学会如何比较两个不同品种的两个属性。

在我的例子中,我想请品种(健康商店和不健康商店)比较他们的收入。收入最少的商店类型将关闭,另一种类型的商店将在他的位置开张。 “比较收入”的命令是行不通的。我知道我不应该使用品种,但我不知道如何在不出现“勾号”错误的情况下以不同的方式表达它。

谢谢!

breed[healthy-shops healthy-shop]
breed[unhealthy-shops unhealthy-shop]

healthy-shops-own[
  total-earnings-h          
]

unhealthy-shops-own[
  total-earnings-nh 
]

to setup
create-healthy-shops 50
create-unhealthy-shops 50
ask healthy-shop [set total-earnings-n 2000 + random 2000]
ask unhealthy-shop [set total-earnings-nn 2000 + random 2000]
end

to go

compare-revenue
end

;;;;;;;;;

to compare-revenue

 let minhealth one-of healthy-shops with-min [total-earnings-h] 
 let minunhealth one-of unhealthy-shops with-min [total-earnings-nh]
    
  ask breed [ifelse (total-earnings-nh < total-earnings-h 
  [ask minunhealth [die hatch-healthy-shops 1]]
  [ask minhealth [die hatch-unhealthy-shops 1]]]
end

另一个使用let命令的版本(不知道对不对)

to compare-revenue 

    let minhealth one-of healthy-shops with-min [total-earnings-h] 
    let minunhealth one-of unhealthy-shops with-min [total-earnings-nh]
    
  if minhealth > minunhealth [ask minunhealth [die hatch-healthy-shops 1]]
  if minhealth < minunhealth [ask minhealth [die hatch-unhealthy-shops 1]]

end

当您更正拼写错误时:ask healthy-shops [set total-earnings-h 2000 + random 2000]ask unhealthy-shops [set total-earnings-nh 2000 + random 2000] 我收到错误消息“您不能在观察者上下文中使用 go,因为 go 是 turtle/link-only”。 那是因为minhealth/minunhealth的用法没有给出各自的店铺。您可以通过以下方式更正:

([total-earnings-nh] of minunhealth < [total-earnings-h] of minhealth)

这将使代码 运行 直到没有健康或不健康的商店,然后崩溃。

您可以使用

更改它
if any? unhealthy-shops and any? healthy-shops
  [compare-revenue]

此外,您必须更改 diehatch 的顺序。现在,没有创建新的商店,因为它应该孵化的商店已经被删除。

完整代码如下:

breed[healthy-shops healthy-shop]
breed[unhealthy-shops unhealthy-shop]

healthy-shops-own[
  total-earnings-h          
]

unhealthy-shops-own[
  total-earnings-nh 
]

to setup
  create-healthy-shops 50 
  [
    setxy random-xcor random-ycor
    set total-earnings-h 2000 + random 2000
  ]
  create-unhealthy-shops 50 
  [
    setxy random-xcor random-ycor
    set total-earnings-nh 2000 + random 2000
  ]
  
end

to go
if any? unhealthy-shops and any? healthy-shops
  [compare-revenue]
end

to compare-revenue

 let minhealth one-of healthy-shops with-min [total-earnings-h] 
 let minunhealth one-of unhealthy-shops with-min [total-earnings-nh]
    
  ifelse ([total-earnings-nh] of minunhealth < [total-earnings-h] of minhealth)
  [ask minunhealth [hatch-healthy-shops 1 die]]
  [ask minhealth [hatch-unhealthy-shops 1 die]]
end