根据乌龟的属性创建链接
Create links according to turtle's attributes
我有两个海龟品种(products
和 consumers
),每个品种都有一个 3 维列表,用于定义它们的 needs
(消费者)和它们的 attributes
(产品).
我想要的是让每个消费者 (i) 寻找满足他们所有需求的产品,并与他们一起创造 link。如果该产品不存在,我希望他们 (ii) 放弃价值最低的需求,并寻找满足其他两个需求的产品。如果该产品不存在,那么我希望他们 (iii) 寻找另一种只满足最高价值需求的产品。
所以说 consumer 20
有 needs [0.2 0.5 0.3]
。如果他们找到 product
与 attributes [0.2 0.5 0.3]
完全相同的列表,我希望 link 发生。如果没有这样的产品,那么我希望消费者忽略最低值(示例中的 0.2
)并寻找具有 attributes [xx 0.5 0.3]
的产品,其中 xx
代表 "whatever".
使用 SO 中其他地方的示例,我拼凑了以下代码(几乎!)完成了技巧 (i) 的第一部分,但无法做到 (ii) 和 (iii)尽管多方努力。有人知道如何做吗?
breed [consumers consumer]
breed [products product]
consumers-own [
needs
]
products-own [
attributes
number-buyers
]
to setup
ca
create-consumers 100 [ setxy random-xcor random-ycor]
create-products 100 [ setxy random-xcor random-ycor]
set-default-shape consumers "person"
set-default-shape products "box"
ask consumers [
set needs n-values 3 [ precision (random-float 1) 1 ]
]
ask products [
set attributes n-values 3 [ precision (random-float 1) 1 ]
]
reset-ticks
end
to go
buy
tick
end
to buy
ask links [ die ]
ask consumers [
carefully [ create-link-with one-of products with [reduce and (map = attributes [ needs ] of myself)] ] [ show "how do I find a sub-optimal product by ignoring my need with the lowest value ?" ]
]
ask products [
set number-buyers count link-neighbors
]
end
您对完整匹配想得太多了 - 只需检查两个列表是否相同即可。然而,几乎匹配有点复杂。这是一个完整的示例模型,它找到列表中最低的位置,然后检查其他项目是否相同。
breed [consumers consumer]
breed [products product]
consumers-own [
needs
]
products-own [
attributes
number-buyers
]
to setup
clear-all
ask patches [set pcolor white]
create-consumers 10
[ setxy random-xcor random-ycor
set shape "person"
set color blue
set needs n-values 3 [ one-of [1 2 3] ]
]
create-products 10
[ setxy random-xcor random-ycor
set shape "box"
set color red
set attributes n-values 3 [ one-of [1 2 3] ]
]
reset-ticks
end
to go
ask consumers [buy]
tick
end
to buy ; set up as turtle procedure for testing purposes
ask my-links [ die ]
let candidates products with [attributes = [needs] of myself]
ifelse any? candidates
[ create-link-with one-of candidates ]
[ type self type " attributes: " type needs print " no matches"
let lowpoint position (min needs) needs ; note will take first if two equal min
set candidates products with
[ ((item 0 attributes = item 0 [needs] of myself) or lowpoint = 0) and
((item 1 attributes = item 1 [needs] of myself) or lowpoint = 1) and
((item 2 attributes = item 2 [needs] of myself) or lowpoint = 2)
]
ifelse any? candidates
[ create-link-with one-of candidates ]
[ print "no almost-match available" ]
]
end
我为 link 创建了潜在产品的代理集(称为候选人),然后创建了 link。这使代码更具可读性。如果未找到匹配项,它还允许 if any?
构造。而且它还使调试更容易,因为您可以放置打印语句来报告匹配项数和类似项。如果您正在应用某种限制选择的条件,我建议您始终这样做。
另外,您有一个包含三个项目的列表,每个项目有 10 个可能的值。这意味着将有 1000 种组合。您只有 100 个消费者和 100 个产品,因此匹配将相当罕见。
我有两个海龟品种(products
和 consumers
),每个品种都有一个 3 维列表,用于定义它们的 needs
(消费者)和它们的 attributes
(产品).
我想要的是让每个消费者 (i) 寻找满足他们所有需求的产品,并与他们一起创造 link。如果该产品不存在,我希望他们 (ii) 放弃价值最低的需求,并寻找满足其他两个需求的产品。如果该产品不存在,那么我希望他们 (iii) 寻找另一种只满足最高价值需求的产品。
所以说 consumer 20
有 needs [0.2 0.5 0.3]
。如果他们找到 product
与 attributes [0.2 0.5 0.3]
完全相同的列表,我希望 link 发生。如果没有这样的产品,那么我希望消费者忽略最低值(示例中的 0.2
)并寻找具有 attributes [xx 0.5 0.3]
的产品,其中 xx
代表 "whatever".
使用 SO 中其他地方的示例,我拼凑了以下代码(几乎!)完成了技巧 (i) 的第一部分,但无法做到 (ii) 和 (iii)尽管多方努力。有人知道如何做吗?
breed [consumers consumer]
breed [products product]
consumers-own [
needs
]
products-own [
attributes
number-buyers
]
to setup
ca
create-consumers 100 [ setxy random-xcor random-ycor]
create-products 100 [ setxy random-xcor random-ycor]
set-default-shape consumers "person"
set-default-shape products "box"
ask consumers [
set needs n-values 3 [ precision (random-float 1) 1 ]
]
ask products [
set attributes n-values 3 [ precision (random-float 1) 1 ]
]
reset-ticks
end
to go
buy
tick
end
to buy
ask links [ die ]
ask consumers [
carefully [ create-link-with one-of products with [reduce and (map = attributes [ needs ] of myself)] ] [ show "how do I find a sub-optimal product by ignoring my need with the lowest value ?" ]
]
ask products [
set number-buyers count link-neighbors
]
end
您对完整匹配想得太多了 - 只需检查两个列表是否相同即可。然而,几乎匹配有点复杂。这是一个完整的示例模型,它找到列表中最低的位置,然后检查其他项目是否相同。
breed [consumers consumer]
breed [products product]
consumers-own [
needs
]
products-own [
attributes
number-buyers
]
to setup
clear-all
ask patches [set pcolor white]
create-consumers 10
[ setxy random-xcor random-ycor
set shape "person"
set color blue
set needs n-values 3 [ one-of [1 2 3] ]
]
create-products 10
[ setxy random-xcor random-ycor
set shape "box"
set color red
set attributes n-values 3 [ one-of [1 2 3] ]
]
reset-ticks
end
to go
ask consumers [buy]
tick
end
to buy ; set up as turtle procedure for testing purposes
ask my-links [ die ]
let candidates products with [attributes = [needs] of myself]
ifelse any? candidates
[ create-link-with one-of candidates ]
[ type self type " attributes: " type needs print " no matches"
let lowpoint position (min needs) needs ; note will take first if two equal min
set candidates products with
[ ((item 0 attributes = item 0 [needs] of myself) or lowpoint = 0) and
((item 1 attributes = item 1 [needs] of myself) or lowpoint = 1) and
((item 2 attributes = item 2 [needs] of myself) or lowpoint = 2)
]
ifelse any? candidates
[ create-link-with one-of candidates ]
[ print "no almost-match available" ]
]
end
我为 link 创建了潜在产品的代理集(称为候选人),然后创建了 link。这使代码更具可读性。如果未找到匹配项,它还允许 if any?
构造。而且它还使调试更容易,因为您可以放置打印语句来报告匹配项数和类似项。如果您正在应用某种限制选择的条件,我建议您始终这样做。
另外,您有一个包含三个项目的列表,每个项目有 10 个可能的值。这意味着将有 1000 种组合。您只有 100 个消费者和 100 个产品,因此匹配将相当罕见。