我有两个乌龟品种(products
和consumers
),每个品种都有一个3维列表,其中定义了它们的needs
(消费者)和attributes
(产品)。
我想要的是让每个消费者(i)寻找一种能够满足他们所有需求的产品,并与他们建立联系。如果该产品不存在,我希望他们(ii)放弃具有最低价值的一种需求,并寻找一种满足其他两种需求的产品。如果该产品不存在,那么我希望他们(iii)寻找另一种仅能满足最高价值需求的产品。
所以说consumer 20
有needs [0.2 0.5 0.3]
。如果他们找到product
且与attributes [0.2 0.5 0.3]
的列表完全相同,我希望链接发生。如果没有这样的产品,那么我希望消费者忽略最低值(示例中的0.2
),并寻找具有attributes [xx 0.5 0.3]
的产品,其中xx
代表“任何”。
使用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
答案 0 :(得分:1)
您正在考虑全盘比赛-只需检查两个列表是否相同即可。但是,几乎匹配要复杂一些。这是一个完整的示例模型,该模型查找最低列表中的位置,然后检查其他项目是否相同。
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
我创建了要链接到的潜在产品的代理集(称为候选项),然后创建了链接。这使代码更具可读性。如果没有找到匹配项,它也允许if any?
的构造。而且,它还使调试变得更容易,因为您可以放置打印语句来报告匹配项和类似项的数量。如果您要应用某种限制选择的条件,我建议您始终这样做。
此外,您有一个三项列表,每个项有10个可能的值。这意味着将有1000种组合。您只有100个消费者和100种产品,因此匹配非常少见。