【问题标题】:How to code an IF command with 2 conditions in NetLogo?如何在 NetLogo 中编写具有 2 个条件的 IF 命令?
【发布时间】:2019-04-16 19:44:17
【问题描述】:

我的海龟是公司,他们拥有海龟,利润因公司而异,以及离岸?并重新安装?这是真的还是假的。

代码有些地方不对劲。我很难将 IF 和 AND 命令结合起来。如果标签的参数离岸?报告 = true 并且这些公司的利润低于报告离岸公司的利润? = false,比他们应该移动。代码的移动部分工作正常。请在下面找到我目前拥有的(错误报告)代码:

breed [ firms firm ]


firms-own [   
   profit
   offshored?   ;; reports either true or false
   reshored?   ;; reports either true or false
]

to setup
  ask firms [
    if offshored? true AND profit < [ profit ] of firms with [ offshored? = false ] [   ;; if the profit of an offshored firm is smaller than the lowest profit of firms at home, the decision to reshore is yes!
      ask one-of turtles [ move-to one-of patches with [ pcolor = 58 and not any? turtles-here ] ]  ;; the firm reshores
      AND set reshored? true ] ]   ;; the firm is now labelled as reshored
end

【问题讨论】:

    标签: if-statement conditional-statements netlogo agent-based-modeling economics


    【解决方案1】:

    上面的设置不会做任何事情,主要问题可能是您将公司的利润变量与列表进行比较([profit] of firms with [ offshored? = false ])。您不能以这种方式直接将单个值与值列表进行比较,因此您必须以不同的方式进行处理。例如,您可以使用min 来获取其他感兴趣的公司的最小利润值:

    breed [ firms firm ]
    
    firms-own [ profit offshored? reshored? ]
    
    to setup
      ca
      ask patches with [ pxcor < -10 ] [
        set pcolor red
      ]
    
      create-firms 100 [
        set color white
        set profit random 101
        set offshored? one-of [ true false ]
        set reshored? false
        while [ any? other turtles-here ] [
          move-to one-of neighbors with [ pcolor = black ]
        ]
      ]
    
      ask firms [
        if offshored? and profit < min [ profit ] of firms with [ not offshored? ] [
          move-to one-of patches with [ pcolor = red and not any? turtles-here ]
          set reshored? true
          set color yellow
          set size 2
        ]
      ]
      reset-ticks
    end 
    

    此外,您的ask firms 声明中有ask one-of turtles - 我认为您想像本例中所做的那样省略它,以便进行评估的公司是移动的代理-ask one-of turtles 将只选择一个随机任何品种的乌龟。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      • 2016-10-25
      相关资源
      最近更新 更多