如何选择列表中价值最高的项目 [英] How to select the item with highest value in a list

查看:39
本文介绍了如何选择列表中价值最高的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我要在模型中考虑的每只海龟的清单.我想选择一个列表项,特别是质量最高的项.质量是[0,1]范围内的参数. 我的问题涉及到如何为每个项目分配参数,然后选择具有最高参数值的项目.

I have a list for each turtle I am considering in my model. I would like to select an item of a list, specifically, the item with highest quality. Quality is a parameter in the range [0,1]. My issues regards how to assign the parameter to each item, then select the item with the highest parameter value.

为了更好地解释:列表的一个示例是(item 4, item3, item2, item1).我想要的是:具有质量#的项目4,具有质量#的项目3,依此类推. 创建列表项时,它们具有质量参数(由乌龟拥有):(quality random-float 1). 然后,我应该有这样的东西: item4 0.2, item3 1, item2 0.2, item1 0.5. 我要选择的是质量最高的商品,即item3质量等于1的商品.

To better explain: an example of list is (item 4, item3, item2, item1). What I would like is: item 4 with quality #, item 3 with quality #, and so on. When I create the items of a list, they have a quality parameter (it's turtle-own): (quality random-float 1). Then, I should have something like this: item4 0.2, item3 1, item2 0.2, item1 0.5. What I would like to select is the item with highest quality, i.e. item3 with quality equal to 1.

要分配参数,我考虑了:

To assign the parameter, I considered:

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

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

I do not know if this is the right way to assign an attribute to an item of a list in Netlogo.

选择项目的步骤是:

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

基于它们,我将编写如下:

Based on them, I would write as follows:

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

问题是我不确定我选择的是质量最高的商品,因为我不确定完全正确地为商品分配了质量.

The problem is that I am not sure that I am selecting the item with the highest quality, because I am not completely sure am assigning correctly the quality to an item.

希望您能帮助我.谢谢

推荐答案

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

Considering you have a structure that packs all the item proprieties together (ItemID, ItemName, ..., ItemValue) and an array of Items.

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

您可以使用以下用伪代码编写的算法,并将其转换为用于选择具有最高价值的商品的语言.

You can use the following algorithm, written in pseudo code, and transfer it to the language you use to pick the item with the highest value.

伪代码:

// 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

这篇关于如何选择列表中价值最高的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆