我遇到了在NetLogo中逻辑组织而和 ifelse 条件的麻烦。
我有一笔目标款项,我需要在一年内(1个刻度)花费。此外,我有目标木材价值,我需要在今年内收获。因此,可能出现3种可能性:
money saved, timber meet
money spent, timber not meet
然而,我的while循环没有按预期工作,即当我正确地花费我的金钱并遇到木材golas时,由于某种原因我得到条件为“money spent, timber not meet
”,这不是真的。
请问,您能否在而和 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
答案 0 :(得分:2)
如果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"
]