NetLogo:正确的 while 和 ifelse 条件?
NetLogo: correct while and ifelse conditions?
我很难在 NetLogo 中逻辑组织我的 while 和 ifelse 条件。
我有一个目标资金,我需要在一年内花掉 (1 tick)。另外,我有目标木材价值,我需要在今年内采伐。因此,可以出现3种可能性:
- 1:我有足够的钱来实现我的木材目标
- 2:我有更多的钱
我需要花钱来实现我的收获目标 - 我存钱 -
money saved, timber meet
- 3:我
实现收割目标所需的资金较少 - 我
收获不够
money spent, timber not meet
但是,我的 while 循环没有按预期工作,即当我正确地花钱并遇到 timber golas 时,出于某种原因我得到条件为“money spent, timber not meet
”,这是不正确的。
拜托,你能在我的 while 和 ifelse 条件中看到我如何安排它们作为输出我的 3 个预期输出?或者我怎样才能更好地编写代码?
谢谢!
while 和 ifelse 循环:
while [ real_money_year >= 0 ] [ ;and real_timber_year <= timber_target_year
ifelse real_money_year > 0 ; continue if you have enough money
[
ifelse real_timber_year < timber_target_year ; is your timber goal meet?
[ ; timber goal is not meet and DR have enough money to harvest
move-to one-of patches with [patch_timb_stock > 0]
pen-down ; see the path
set real_timber_year patch_timb_stock + real_timber_year ; increase timber gain after each harvest
set real_money_year real_money_year - patch_harvest_cost ; decrease money after each harvest
set pcolor magenta
print "harvest"
]
[;timber value is meet, however we have more money to spend
print "money saved, timber meet"
stop
]
]
[ ; there is not enough money to meet timber goals
print "money spent, timber not meet"
stop
]
]
完整代码:
globals [
;timber_target_year ; what is defined timber target per year?
;money_target_year ; how much money do I have to spend per year
; values that DR is able to spend within a year
real_timber_year ; how much do I really harvest in one year
real_money_year ; how much money did I really spend in one year?
]
patches-own [
patch_harvest_cost ; what is the cost of harvest of specific patch?
patch_timb_stock ; what is the timber stack of the patch?
]
to setup
clear-all
setup-rangers ; create DR
setup-patches ; create forest
reset-ticks
end
to setup-rangers
crt 1 [
set color red ]
end ; setup ranger
to setup-patches
ask patches [
set pcolor green
set patch_harvest_cost p_harvest_cost
set patch_timb_stock p_timber_stock
]
set real_money_year money_target_year
end ; setup patches
to go
ask turtles [
harvest
]
tick
end
to harvest
while [ real_money_year >= 0 ] [ ;and real_timber_year <= timber_target_year
ifelse real_money_year > 0 ; continue if you have enough money
[
ifelse real_timber_year < timber_target_year ; is your timber goal meet?
[ ; timber goal is not meet and DR have enough money to harvest
move-to one-of patches with [patch_timb_stock > 0]
pen-down ; see the path
set real_timber_year patch_timb_stock + real_timber_year ; increase timber gain after each harvest
set real_money_year real_money_year - patch_harvest_cost ; decrease money after each harvest
set pcolor magenta
print "harvest"
]
[;timber value is meet, however we have more money to spend
print "money saved, timber meet"
stop
]
]
[ ; there is not enough money to meet timber goals
print "money spent, timber not meet"
stop
]
]
end
你应该只有在 real_money_year = 0
的情况下才能达到该条件,因此请对此进行测试。更重要的是,你似乎想要一个更像这样的逻辑:
while [(real_money_year > 0) and (real_timber_year < timber_target_year)] [
move-to one-of patches with [patch_timb_stock > 0] ;NOTE: assumes such a patch exists!
set real_money_year real_money_year - patch_harvest_cost
set real_timber_year (patch_timb_stock + real_timber_year)
]
ifelse (real_timber_year >= timber_target_year) [
print (word "timber goal met, money saved = " real_money_year) ;possibly 0
][
print "money spent, timber goal not met"
]
我很难在 NetLogo 中逻辑组织我的 while 和 ifelse 条件。
我有一个目标资金,我需要在一年内花掉 (1 tick)。另外,我有目标木材价值,我需要在今年内采伐。因此,可以出现3种可能性:
- 1:我有足够的钱来实现我的木材目标
- 2:我有更多的钱
我需要花钱来实现我的收获目标 - 我存钱 -
money saved, timber meet
- 3:我
实现收割目标所需的资金较少 - 我
收获不够
money spent, timber not meet
但是,我的 while 循环没有按预期工作,即当我正确地花钱并遇到 timber golas 时,出于某种原因我得到条件为“money spent, timber not meet
”,这是不正确的。
拜托,你能在我的 while 和 ifelse 条件中看到我如何安排它们作为输出我的 3 个预期输出?或者我怎样才能更好地编写代码?
谢谢!
while 和 ifelse 循环:
while [ real_money_year >= 0 ] [ ;and real_timber_year <= timber_target_year
ifelse real_money_year > 0 ; continue if you have enough money
[
ifelse real_timber_year < timber_target_year ; is your timber goal meet?
[ ; timber goal is not meet and DR have enough money to harvest
move-to one-of patches with [patch_timb_stock > 0]
pen-down ; see the path
set real_timber_year patch_timb_stock + real_timber_year ; increase timber gain after each harvest
set real_money_year real_money_year - patch_harvest_cost ; decrease money after each harvest
set pcolor magenta
print "harvest"
]
[;timber value is meet, however we have more money to spend
print "money saved, timber meet"
stop
]
]
[ ; there is not enough money to meet timber goals
print "money spent, timber not meet"
stop
]
]
完整代码:
globals [
;timber_target_year ; what is defined timber target per year?
;money_target_year ; how much money do I have to spend per year
; values that DR is able to spend within a year
real_timber_year ; how much do I really harvest in one year
real_money_year ; how much money did I really spend in one year?
]
patches-own [
patch_harvest_cost ; what is the cost of harvest of specific patch?
patch_timb_stock ; what is the timber stack of the patch?
]
to setup
clear-all
setup-rangers ; create DR
setup-patches ; create forest
reset-ticks
end
to setup-rangers
crt 1 [
set color red ]
end ; setup ranger
to setup-patches
ask patches [
set pcolor green
set patch_harvest_cost p_harvest_cost
set patch_timb_stock p_timber_stock
]
set real_money_year money_target_year
end ; setup patches
to go
ask turtles [
harvest
]
tick
end
to harvest
while [ real_money_year >= 0 ] [ ;and real_timber_year <= timber_target_year
ifelse real_money_year > 0 ; continue if you have enough money
[
ifelse real_timber_year < timber_target_year ; is your timber goal meet?
[ ; timber goal is not meet and DR have enough money to harvest
move-to one-of patches with [patch_timb_stock > 0]
pen-down ; see the path
set real_timber_year patch_timb_stock + real_timber_year ; increase timber gain after each harvest
set real_money_year real_money_year - patch_harvest_cost ; decrease money after each harvest
set pcolor magenta
print "harvest"
]
[;timber value is meet, however we have more money to spend
print "money saved, timber meet"
stop
]
]
[ ; there is not enough money to meet timber goals
print "money spent, timber not meet"
stop
]
]
end
你应该只有在 real_money_year = 0
的情况下才能达到该条件,因此请对此进行测试。更重要的是,你似乎想要一个更像这样的逻辑:
while [(real_money_year > 0) and (real_timber_year < timber_target_year)] [
move-to one-of patches with [patch_timb_stock > 0] ;NOTE: assumes such a patch exists!
set real_money_year real_money_year - patch_harvest_cost
set real_timber_year (patch_timb_stock + real_timber_year)
]
ifelse (real_timber_year >= timber_target_year) [
print (word "timber goal met, money saved = " real_money_year) ;possibly 0
][
print "money spent, timber goal not met"
]