我曾经有过以下代码,它可以工作:
to build-pipeline-32
ask storage 32
[ifelse subsidy-port - pipeline-cost-extensible > 0
[set pipeline 1]
[set pipeline 0]
]
ask storage 32
[ifelse pipeline > 0
[set subsidy-port subsidy-port - pipeline-cost-extensible]
[set subsidy-port subsidy-port]
]
ask storage 32
[if pipeline = 1
[create-link-from port 25]
]
end
to build-pipeline-33
ask storage 33
[ifelse subsidy-port - pipeline-cost-extensible > 0
[set pipeline 1]
[set pipeline 0]
]
ask storage 33
[ifelse pipeline > 0
[set subsidy-port subsidy-port - pipeline-cost-extensible]
[set subsidy-port subsidy-port]
]
ask storage 33
[if pipeline = 1
[create-link-from port 25]
]
end现在我试着缩短它,因为这只需要太多的代码行:
to build-pipeline
foreach sort-on [who] storages
[ifelse subsidy-port - pipeline-cost-extensible > 0
[set pipeline 1]
[set pipeline 0]
]
foreach sort-on [who] storages
[ifelse pipeline > 0
[set subsidy-port subsidy-port - pipeline-cost-extensible]
[set subsidy-port subsidy-port]
]
foreach sort-on [who] storages
[if pipeline = 1
[create-link-from port 25]
]
end出于某种原因,这完全扰乱了补贴的价值。上面部分(设置流水线值)和下面部分(创建链接)工作。我该如何解决这个问题呢?
另一个问题:由于某种原因,堆栈溢出使我使用问题帮助向导,这非常烦人,因为我不能只选择一段复制的代码并将其格式化为问题中的代码(使用{}选项)。对于这个问题,我必须为每一行手动缩进4个空格...花了很长时间。Cmd+K也不能工作。我可以禁用此问题向导吗?谢谢!!
最大值
发布于 2019-01-21 22:15:10
你的模型扣除12倍流水线成本的原因是,一旦有资金可用,它就会构建12个流水线扩展。在第二个foreach之前,您不会扣除成本,但是您可以指定是否在第一个foreach中构建扩展。
我认为你想要这样(它将成本变化放在相同的位置,这样在测试下一个可能的扩展之前就会发生):
to build-pipeline
foreach sort-on [who] storages
[ ifelse subsidy-port - pipeline-cost-extensible > 0
[ set pipeline 1
set subsidy-port subsidy-port - pipeline-cost-extensible
create-link-from port 25
[ set pipeline 0
set subsidy-port subsidy-port ; has no effect, can be deleted
]
]
end此外,如果管道变量的唯一用途是1或0来指示是否要构建扩展,这就更容易了:
to build-pipeline
foreach sort-on [who] storages
[ if subsidy-port - pipeline-cost-extensible > 0
[ set subsidy-port subsidy-port - pipeline-cost-extensible
create-link-from port 25
]
end在您的评论中,您指出了流水线扩展的优点顺序。在NetLogo中使用who几乎总是糟糕的编码。变量who只是创建存储的顺序,将其与优点绑定会消除所有的灵活性。如果以后你想要一个不同的优点计算,会发生什么?无法更改who。您可能希望为每个存储分配一个名为merit的变量,并对该变量进行排序。
https://stackoverflow.com/questions/54255165
复制相似问题