创建变量列表以查找最大值

时间:2014-04-20 15:46:52

标签: netlogo

我试图创建一个列表,其中包含一组变量的值,这些变量是基于其他变量计算的每个过程。在这种情况下,我试图创建一个实用程序值列表,这将导致运行该过程的代理程序决定更改其TIME变量。所有的caulculations部分都是正确的,唯一的问题是在尝试为实用程序创建列表并从中选择最大值以继续该过程时。有谁知道我能做些什么来避免这个问题?干杯

to utility-runway


  let feepKG7 [feepKG] of runway1
  let feepKG8 [feepKG] of runway2
  let feepKG9 [feepKG] of runway3
  let feepKG10 [feepKG] of runway4
  let feepKG11 [feepKG] of runway5
  let feepKG12 [feepKG] of runway6
  let feepKG13 [feepKG] of runway7
  let feepKG14 [feepKG] of runway8
  let feepKG15 [feepKG] of runway9
  let feepKG16 [feepKG] of runway10
  let feepKG17 [feepKG] of runway11
  let feepKG18 [feepKG] of runway12
  let feepKG19 [feepKG] of runway13
  let feepKG20 [feepKG] of runway14
  let feepKG21 [feepKG] of runway15
  let feepKG22 [feepKG] of runway16
  let feepKG23 [feepKG] of runway17


   set utility7 ( ( previous-business1 + previous-leisure1 + previous-business2 + previous-leisure2 ) / feepKG7 )
   set utility8 ( ( previous-business1 + previous-leisure1 + previous-business2 + previous-leisure2 + previous-business3 + previous-leisure3 ) / feepKG8 )
   set utility9 ( ( previous-business2 + previous-leisure2 + previous-business3 + previous-leisure3 + previous-business4 + previous-leisure4 ) / feepKG9 )
   set utility10 ( ( previous-business3 + previous-leisure3 + previous-business4 + previous-leisure4 + previous-business5 + previous-leisure5 ) / feepKG10 )
   set utility11 ( ( previous-business4 + previous-leisure4 + previous-business5 + previous-leisure5 + previous-business6 + previous-leisure6 ) / feepKG11 )
   set utility12 ( ( previous-business5 + previous-leisure5 + previous-business6 + previous-leisure6 + previous-business7 + previous-leisure7 ) / feepKG12 )
   set utility13 ( ( previous-business6 + previous-leisure6 + previous-business7 + previous-leisure7 + previous-business8 + previous-leisure8 ) / feepKG13 )
   set utility14 ( ( previous-business7 + previous-leisure7 + previous-business8 + previous-leisure8 + previous-business9 + previous-leisure9 ) / feepKG14 )
   set utility15 ( ( previous-business8 + previous-leisure8 + previous-business9 + previous-leisure9 + previous-business10 + previous-leisure10 ) / feepKG15 )
   set utility16 ( ( previous-business9 + previous-leisure9 + previous-business10 + previous-leisure10 + previous-business11 + previous-leisure11 ) / feepKG16 )
   set utility17 ( ( previous-business10 + previous-leisure10 + previous-business11 + previous-leisure11 + previous-business12 + previous-leisure12 ) / feepKG17 )
   set utility18 ( ( previous-business11 + previous-leisure11 + previous-business12 + previous-leisure12 + previous-business13 + previous-leisure13 ) / feepKG18 )
   set utility19 ( ( previous-business12 + previous-leisure12 + previous-business13 + previous-leisure13 + previous-business14 + previous-leisure14 ) / feepKG19 )
   set utility20 ( ( previous-business13 + previous-leisure13 + previous-business14 + previous-leisure14 + previous-business15 + previous-leisure15 ) / feepKG20 )
   set utility21 ( ( previous-business14 + previous-leisure14 + previous-business15 + previous-leisure15 + previous-business16 + previous-leisure16 ) / feepKG21 )
   set utility22 ( ( previous-business15 + previous-leisure15 + previous-business16 + previous-leisure16 + previous-business17 + previous-leisure17 ) / feepKG22 )
   set utility23 ( ( previous-business16 + previous-leisure16 + previous-business17 + previous-leisure17) / feepKG23 )


  set utility-list [ utility7 utility8 utility9 utility10 utility11 
    utility12 utility13 utility14 utility15 utility16 
    utility17 utility18 utility19 utility20 utility21 utility22 utility23 ]

  let max-utility max-one-of utility-list

  if max-utility = utility7 [set time 7]  
  if max-utility = utility8 [set time 8]  
  if max-utility = utility9 [set time 9] 
  if max-utility = utility10 [set time 10] 
  if max-utility = utility11 [set time 11] 
  if max-utility = utility12 [set time 12] 
  if max-utility = utility13 [set time 13] 
  if max-utility = utility14 [set time 14] 
  if max-utility = utility15 [set time 15] 
  if max-utility = utility16 [set time 16] 
  if max-utility = utility17 [set time 17] 
  if max-utility = utility18 [set time 18] 
  if max-utility = utility19 [set time 19] 
  if max-utility = utility20 [set time 20] 
  if max-utility = utility21 [set time 21] 
  if max-utility = utility22 [set time 22] 
  if max-utility = utility23 [set time 23] 




end

1 个答案:

答案 0 :(得分:1)

首先,我假设每个runway#都是品种runway的乌龟或链接。构造此代码的更好方法是一次对所有值应用相同的操作。为此,首先我们创建一个feepKG值列表:

let feepKGs map [ [ feepKG ] of ? ] sort runways

runways前面的排序使得feepKGs的第一项与第一项runway对应,依此类推。很遗憾,由于您sort会返回一个列表,因此您必须使用map代替of来获取feepKG的列表。

接下来,我们需要从feepKG列表中获取我们的实用程序列表。您的utility7计算可能不正确,因为它是唯一不同的计算。无论如何,要对列表中的每个项目应用相同的操作,我们使用map

let utilities map [(previous-business1 + previous-leisure1 + previous-business2 + previous-leisure2 + previous-business3 + previous-leisure3 ) / ?] feepKGs

map将该大表达式应用于feepKGs的每个元素(其中?是元素)。因此utilities与您的utility-list相同(假设utility7的表达式实际上是错误的)。要获取max实用程序的值,您只需使用max

let max-utility max utilities

然后,我们得到max-utility的索引以找出它是哪一个。由于您的实用程序编号从7开始,列表索引从0开始,我们必须在索引中添加7:

set time 7 + position max-utility utilities