如何 select 列表中具有最高价值的项目

How to select the item with highest value in a list

我在模型中考虑的每只乌龟都有一个列表。我想要 select 列表中的一个项目,特别是质量最高的项目。质量是 [0,1] 范围内的参数。 我的问题是如何将参数分配给每个项目,然后 select 具有最高参数值的项目。

为了更好的解释:list的一个例子是(item 4, item3, item2, item1)。我想要的是:质量# 的项目 4,质量# 的项目 3,依此类推。 当我创建列表的项目时,它们有一个质量参数(它是乌龟自己的):(quality random-float 1)。 然后,我应该有这样的东西: item4 0.2, item3 1, item2 0.2, item1 0.5。 我想要 select 的是质量最高的项目,即 item3 质量等于 1

为了给参数赋值,我考虑过:

ask one-of turtles
     [
      ifelse empty? mylist
        [
          set quality random-float 1
          ...
        ]
     ]

我不知道这是否是在 Netlogo 中将属性分配给列表项的正确方法。

select购买一个项目的步骤是:

  1. Select一只乌龟
  2. 检查其列表是否为空
  3. Select 其列表中质量最高的项目

基于它们,我会这样写:

let mylist [ item4 item3 item2 item1 item0 item6] 
let max-value max mylist 
let max-index position max mylist 

问题是我不确定我是否select正在使用最高质量的项目,因为我不完全确定是否正确地为项目分配了质量。

希望你能帮助我。谢谢

考虑到您有一个将所有项目属性(ItemID、ItemName、...、ItemValue)和项目数组打包在一起的结构。

Item(ItemID, ItemName, ..., ItemValue) // these are the proprieties of our object Item
Item arr_items[NB_ITEMS] //  the array (list) of items you have

您可以使用以下用伪代码编写的算法,并将其转换为您使用的语言来选择具有最高价值的项目。

伪代码:

// Lets assume your array of items is called "arr_items"
// Lets also assume indexing start from 0
index_max  = 0 // first index
for i = 0 to length(arr_items) - 1: // loop though all items
    // if the current ith item have ItemValue bigger than index_max's items ItemValue 
    if arr_items[item_max].ItemValue > arr_items[i].ItemValue then 
        index_max = i // then change index_max to be i
return index_max  // return the index of the item with the highest value