向列表项添加属性
Adding attributes to items of a list
我想知道如何为列表的每个元素添加属性。
特别是,我有一个列表,通过在每个刻度处添加项目来制作。每个项目都有两个属性。这些属性对我很重要,因为我想 select 基于其属性值的项目。
我做了这样的事情:
ask one-of turtles ;; turtles have cars
[ set attribute_1 ;; cars' attribute
set attribute_2 ;; cars' attribute
set mylist fput car mylist
]
我想要这样的东西[car1 attribute_1 attribute_2, car2 attribute_1 attribute_2, car3...]
。
目前我有 [car1 car2 car3...]
。
一旦将属性与项目相关联,我就需要从列表中选择具有最高价值的项目。用户Omarito 给我提供了一个可能的解决方案:,但我不太清楚如何拾取物品。
我想问你的是,是否可以有类似[car1 attribute_1 attribute_2, car2 attribute_1 attribute_2, car3...]
的东西,或者我是否只能有类似
的东西
[car1 car2 car3...]
或
[[attribute_1 attribute_2] [attribute_1 attribute_2] ...]
。
如果我写 set mylist fput [(list attribute_1 attribute_2)]
,我会收到错误消息:需要一个文字值。
希望你能帮助我。谢谢
更新:在尼古拉斯的回答之后,我将代码编辑如下:
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
大部分代码用于创建海龟和显示结果,但关键的两行是:
set my-list fput (list car attribute-1 attribute-2) my-list
用于将子列表添加到主列表,以及:
let best-by-1 last sort-by [ [a b] -> item 1 a < item 1 b ] my-list
用于根据从子列表中获取的特定属性对主列表进行排序。然后您可以使用 item 0 best-by-1
来获取汽车本身。
也就是说,如果您想充分发挥 NetLogo 的潜力,您可能应该创建一个汽车品种:
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
max-one-of
原语可以轻松选择最好的汽车,如果您使用代理而不是列表,其他一切也可能会更容易。
我想知道如何为列表的每个元素添加属性。 特别是,我有一个列表,通过在每个刻度处添加项目来制作。每个项目都有两个属性。这些属性对我很重要,因为我想 select 基于其属性值的项目。 我做了这样的事情:
ask one-of turtles ;; turtles have cars
[ set attribute_1 ;; cars' attribute
set attribute_2 ;; cars' attribute
set mylist fput car mylist
]
我想要这样的东西[car1 attribute_1 attribute_2, car2 attribute_1 attribute_2, car3...]
。
目前我有 [car1 car2 car3...]
。
一旦将属性与项目相关联,我就需要从列表中选择具有最高价值的项目。用户Omarito 给我提供了一个可能的解决方案:
我想问你的是,是否可以有类似[car1 attribute_1 attribute_2, car2 attribute_1 attribute_2, car3...]
的东西,或者我是否只能有类似
[car1 car2 car3...]
或
[[attribute_1 attribute_2] [attribute_1 attribute_2] ...]
。
如果我写 set mylist fput [(list attribute_1 attribute_2)]
,我会收到错误消息:需要一个文字值。
希望你能帮助我。谢谢
更新:在尼古拉斯的回答之后,我将代码编辑如下:
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
大部分代码用于创建海龟和显示结果,但关键的两行是:
set my-list fput (list car attribute-1 attribute-2) my-list
用于将子列表添加到主列表,以及:
let best-by-1 last sort-by [ [a b] -> item 1 a < item 1 b ] my-list
用于根据从子列表中获取的特定属性对主列表进行排序。然后您可以使用 item 0 best-by-1
来获取汽车本身。
也就是说,如果您想充分发挥 NetLogo 的潜力,您可能应该创建一个汽车品种:
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
max-one-of
原语可以轻松选择最好的汽车,如果您使用代理而不是列表,其他一切也可能会更容易。