【问题标题】:Creating conditional links between two breeds在两个品种之间创建条件链接
【发布时间】:2020-11-06 22:20:05
【问题描述】:

我正在编写一个关于房地产市场及其政治影响的 NetLogo 模型。模型中有两个品种:家庭和房屋。在我的开发中,我遇到困难的一个早期步骤是让家庭通过嵌套条件语句定义的两种类型的链接(拥有或租用)之一与房屋匹配。这导致了我至今无法克服的两个困难。

  1. 在命令setup-market 命令中,我试图为每个家庭定义一组可能购买的房屋,如果满足一组条件,则家庭购买(并创建link)。如果它买不起,它就会尝试租用。如果租不起的家庭会die

我的代码不断导致以下错误:

IFELSE expected input to be a TRUE/FALSE but got the turtle (house XXX) instead.

  1. 我在后面的代码中也遇到了另一个问题(在用“;”注释掉的两行中),我尝试将变量 owner-occupiedrenter 设置为 1,基于存在适当的链接(它们应保持为 0,如果保持未链接,则该家庭应死亡)。

完整的代码如下。带有“;; 这是给我带来麻烦的行”的行表示错误似乎发生在哪里。

更新: 代码已使用 JenB 的解决方案进行了更新。产生的错误现在是: CREATE-LINK-WITH expected input to be a turtle but got NOBODY instead. 出现在以下行:create-link-with one-of potentialHomes [ set color red

undirected-link-breed [own-links own-link]
undirected-link-breed [rent-links rent-link]
breed [city-centers city-center]
breed [households household]
households-own 
[ 
  age 
  money 
  income 
  monthly-income 
  consumption 
  monthly-consumption 
  hh-size race 
  preference 
  net-income
  net-monthly-income
  myHouse
]
breed [houses house]
houses-own 
[ 
  cost 
  down-payment 
  mortgage-payment 
  rent 
  rent-premium 
  rooms 
  onMarket
  owner-occupied
  rental
  onMarket?
]
patches-own [ 
  seed?  ;;district seed
  district  ;;district number
  full? ;;is the district at capacity?
  quadrant 
]

to setup
  clear-all
  reset-ticks
  setup-patches
  set-default-shape households "person"
  create-households num-households [ setxy random-xcor random-ycor ]
  set-default-shape houses "house"
  create-houses num-houses [ setxy random-xcor random-ycor ]
  setup-households
  setup-houses
  setup-market
  generate-cities
end

to generate-cities
  let center-x random-xcor / 1.5 ;;keep cities away from edges
  let center-y random-ycor / 1.5
end

to setup-patches
  ask patches with [pxcor > 0 and pycor > 0] [set quadrant 1 set pcolor 19 ]
  ask patches with [pxcor > 0 and pycor < 0] [set quadrant 2 set pcolor 49 ]
  ask patches with [pxcor < 0 and pycor < 0] [set quadrant 3 set pcolor 139 ]
  ask patches with [pxcor < 0 and pycor > 0] [set quadrant 4 set pcolor 89 ]
end

to setup-households
  ask households
    [ set age random-poisson 38
      set money random-exponential 30600
      set income random-exponential 64324
      set monthly-income income / 12
      set consumption .5 * income
      set monthly-consumption consumption / 12 
      set hh-size random 6 + 1
      set net-income income - consumption
      set net-monthly-income monthly-income - monthly-consumption
    ]
end

to setup-houses
  ask houses
    [ set cost random-normal 300000 50000
      set down-payment cost * down-payment-rate
      set mortgage-payment (cost - down-payment) / 360 
      set rooms random-exponential 3
      set onMarket 1
      set rent mortgage-payment + mortgage-payment * .25
      set owner-occupied 0
      set rental 0
    ]
end

to setup-market
  ask houses
    [ set onMarket? TRUE ]
  ask households 
    [ ifelse any? houses with [ [money] of myself > down-payment and [net-monthly-income] of myself > mortgage-payment ]
      [ let potentialHomes houses with [[money] of myself > cost and onMarket? ] 
        create-link-with one-of potentialHomes [ 
          set color red
        ]
      ] 
      [ 
        ifelse any? houses with [ [net-monthly-income] of myself > rent]
[ let potentialRentals houses with [ [net-monthly-income] of myself > rent and onMarket? ] 
  create-link-with one-of potentialRentals [ set color blue ]
]
[ die ]
      ]
  ]
  ask houses 
    [ if any? link-neighbors [set onMarket FALSE ] 
      ;if any? link-neighbors and color red [ set owner-occupied 1 ]
      ;if any? link-neighbors and color blue [ set rental 1 ]
  ]
end

to go
  move-households
  tick
end

to move-households
  ask households [
    move-to myHouse
  ]
end

【问题讨论】:

    标签: netlogo agent-based-modeling


    【解决方案1】:

    您无需“怀疑”问题出在哪里,NetLogo 指向问题所在。运行你的代码,问题其实是ifelse one-of houses with [ [net-monthly-income] of myself &gt; rent]。看着那条线,你从游泳池中随机选择了一个租金低于收入的房子。但是你没有条件让ifelse去测试。

    在以前的构造中,您在末尾有!= nobody,但您在这一行中忘记了这一点。这将修复错误,但如果您使用 any? 代替,您的代码将不太容易出错。您似乎在使用one-of .... != nobody 来测试是否有满足条件的海龟。这就是any? 的用途。

    所以而不是:

    ifelse one-of houses with [ [net-monthly-income] of myself > rent] != nobody
    [ let potentialRentals houses with [[money] of myself > rent and onMarket = 1 ] 
      create-link-with one-of potentialRentals [ set color blue ]
    ]
    [ die ]
    

    你可以拥有:

    ifelse any? houses with [ [net-monthly-income] of myself > rent]
    [ let potentialRentals houses with [[money] of myself > rent and onMarket = 1 ] 
      create-link-with one-of potentialRentals [ set color blue ]
    ]
    [ die ]
    

    我应该补充一点,这里存在潜在的逻辑问题。假设有租金低于收入的房屋,代码将执行第一个(真实)操作。但不保证有满足新条件的房子,是不一样的。

    此外,NetLogo 具有truefalse 的概念,因此您不需要使用 1 和 0。按照惯例(但不是必需的),布尔变量名称以问号结尾。所以你可以有set onMarket? true 而不是set onMarket 1。你为什么要这样做?它使逻辑运算符更清晰、更易于阅读(从而减少了错误)。您的线路:

    let potentialRentals houses with [[money] of myself > rent and onMarket = 1 ]
    

    看起来像:

    let potentialRentals houses with [[money] of myself > rent and onMarket? ]
    

    您可以使用if not onMarket? 代替if onMarket? = falseif onMarket = 0 之类的操作

    【讨论】:

    • 感谢您的回复。我对any? 命令不是很熟悉,所以您的反馈非常有帮助。我也习惯了 NetLogo 语法(我通常在 R 中编写代码),所以记住使用 ? 作为布尔运算符很有帮助。
    • 我不太确定你发现的逻辑问题,希望你能澄清一下。我要完成的是逻辑:(1)家庭寻找可以从储蓄中支付首付的房子(money&gt;down-payment)和net-monthly-income&gt;mortgage-payment;如果没有满足(1)的房屋,则(2)家庭寻找可以负担每月租金的房屋;如果没有房屋满足(2),则(3)家庭死亡。我意识到我在 OP 中的至少一个位置错误地识别了 net-monthly-income 变量;我会发布一个更正。再次感谢您的指导。
    • 进一步评论:调整后的代码现在产生错误:CREATE-LINK-WITH expected input to be a turtle but got NOBODY instead. 这发生在以下行:create-link-with one-of potentialHomes [ set color red。也许这与您确定的逻辑问题有关? (虽然我仍然不太确定这个问题。)
    • @Abe 请不要在提供答案后用后续问题更新您的帖子 - 这不是 SO 的工作方式;如果答案解决了最初报告的问题,请接受它并使用新问题打开一个新问题(如有必要,请在此处链接)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    相关资源
    最近更新 更多