Netlogo 向列表的项目添加属性
我想知道如何为列表的每个元素添加属性。 特别是,我有一个列表,通过在每个勾号处添加项目来创建。每个项目都有两个属性。这些属性对我来说很重要,因为我想根据其属性的值选择一个项目。 我是这样做的:Netlogo 向列表的项目添加属性,netlogo,Netlogo,我想知道如何为列表的每个元素添加属性。 特别是,我有一个列表,通过在每个勾号处添加项目来创建。每个项目都有两个属性。这些属性对我来说很重要,因为我想根据其属性的值选择一个项目。 我是这样做的: ask one-of turtles ;; turtles have cars [ set attribute_1 ;; cars' attribute set attribute_2 ;; cars' attribute set mylist fput car mylist ] 我想要这样的
ask one-of turtles ;; turtles have cars
[ set attribute_1 ;; cars' attribute
set attribute_2 ;; cars' attribute
set mylist fput car mylist
]
我想要这样的东西[car1-attribute\u-1-attribute\u-2,car2-attribute\u-1-attribute\u-2,car3…]
。
目前我有[car1 car2 car3…]
。
将属性与项目关联后,我需要从列表中选择值最高的项目。用户Omarito为我提供了一个可能的解决方案:,但我并不完全清楚如何挑选物品
我想问你的是,是否可以有类似于[car1 attribute\u 1 attribute\u 2,car2 attribute\u 1 attribute\u 2,car3…]的东西,或者如果我只能有类似的东西
[car1 car2 car3...]
或
[[attribute\u 1 attribute\u 2][attribute\u 1 attribute\u 2]…]
。
如果我写入set mylist fput[(list attribute\u 1 attribute\u 2)]
,我会收到错误消息:预期为文本值
我希望你能帮助我。谢谢
更新:在Nicolas的回答之后,我对代码进行了如下编辑:
ask one-of turtles
[
hatch-cars 1[ ;; buy a new car
set attribute_1 random-float 1
set attribute_2 random-float 1
]
let this-car self
set my-list fput this-car my-list
; ask turtles [show my-list]
set new_car? true
set old_car? false
set new_car new_car + 1 ;; how could I update it after the change?
]
我遗漏了什么吗?解决这个问题的方法有很多,但我只给你两种
让我们从一个最接近你目前尝试做事情的方式开始
turtles-own [ my-list ]
to setup
clear-all
create-turtles 3 [
set my-list [] ; start with empty list
foreach range 5 [ car -> ; let's pretend cars are just numbers
let attribute-1 precision (random-float 1) 2
let attribute-2 precision (random-float 1) 2
set my-list fput (list car attribute-1 attribute-2) my-list
]
]
ask turtles [
show my-list
print " Best car by attribute 1:"
let best-by-1 last sort-by [ [a b] -> item 1 a < item 1 b ] my-list
print word " Sublist: " best-by-1
print word " Car number: " item 0 best-by-1
print " Best car by attribute 2:"
let best-by-2 last sort-by [ [a b] -> item 2 a < item 2 b ] my-list
print word " Sublist: " best-by-2
print word " Car number: " item 0 best-by-2
print "--------"
]
end
用于将子列表添加到主列表,以及:
let best-by-1 last sort-by [ [a b] -> item 1 a < item 1 b ] my-list
如果您与代理商合作,而不是与列表合作,primitive可以让您轻松挑选最佳汽车,其他一切可能也会变得更容易。非常感谢Nicolas。你的回答很好,而且非常有帮助,也很清楚。我本想创造一个新的品种,只包括属性。我还有一个问题:如果我想创建/分配汽车的属性,不是在我创建海龟的时候,而是在我调用其中一个海龟的时候(问海龟中的一个
),我如何正确地更改代码?t是否通过调用hatch
?很抱歉,这一点我还不完全清楚,因为我在使用Netlogo方面没有太多经验。这不会有太大的不同:问一位代理[hatch cars 1[set attribute-1 random float 1让这辆车自问[set my list fput this car my list]]]
我在你回答后更新了这个问题,显示我编辑的内容,询问其是否正确。感谢您的帮助Nicolas记住,海龟
指的是所有种类的海龟,因此如果您创建一个汽车
品种,海龟
将包括汽车
。你应该为你的其他经纪人创建一个单独的品种(你可以把它命名为经纪人
,买家
,人
或其他什么),然后使用该品种,而不是,例如,问一只海龟
你需要它的地方。
let best-by-1 last sort-by [ [a b] -> item 1 a < item 1 b ] my-list
breed [ cars car ]
cars-own [
attribute-1
attribute-2
]
breed [ agents an-agent ]
agents-own [ my-list ]
to setup
clear-all
create-agents 3 [
set my-list [] ; start with empty list
hatch-cars 5 [
set attribute-1 precision (random-float 1) 2
set attribute-2 precision (random-float 1) 2
let this-car self
ask myself [ set my-list fput this-car my-list ]
]
]
ask agents [
show my-list
print " Best car by attribute 1:"
let best-by-1 max-one-of turtle-set my-list [ attribute-1 ]
print (word " " best-by-1 ", attribute-1 = " [ attribute-1 ] of best-by-1)
print " Best car by attribute 2:"
let best-by-2 max-one-of turtle-set my-list [ attribute-2 ]
print (word " " best-by-2 ", attribute-1 = " [ attribute-2 ] of best-by-2)
]
end