我的海龟计算了几个变量:gain-stay
,gain-move
,gain-grow
,gain-shrink
。我希望它们能够根据这些变量中具有最高值的变量保持,移动,增长或缩小。我有,to stay
,to move
等程序
为简单起见,如果几个变量与最大值相关联,我并不关心他们采取了哪种策略。如果仅仅为shrink
,stay
等命名变量会更容易,那就没问题了。
似乎我有两个步骤要解决:
runresult
并且不会比第一步更难以理解。非常感谢任何帮助。非常感谢你。
答案 0 :(得分:2)
此代码通过将找到的最大值与每个变量的内容进行比较并构建所有匹配的名称列表来选择最佳策略。然后它随机选择一个名称。这是一个完整的程序,您可以打开一个新模型,然后将其复制以查看会发生什么。
turtles-own [s1 s2 s3 s4]
to setup
clear-all
create-turtles 20
[ setxy random-xcor random-ycor
set s1 random 10
set s2 random 10
set s3 random 10
set s4 random 10
]
reset-ticks
end
to go
ask turtles
[ type "my values are " type s1 type s2 type s3 print s4
let maxval max (list s1 s2 s3 s4)
let strategy-list []
if maxval = s1 [set strategy-list fput "s1" strategy-list]
if maxval = s2 [set strategy-list fput "s2" strategy-list]
if maxval = s3 [set strategy-list fput "s3" strategy-list]
if maxval = s4 [set strategy-list fput "s4" strategy-list]
let chosen one-of strategy-list
run (word "do-" chosen)
]
tick
end
to do-s1
print "I chose s1"
end
to do-s2
print "I chose s2"
end
to do-s3
print "I chose s3"
end
to do-s4
print "I chose s4"
end