2014-09-05 43 views
1
两个条件

我的补丁有costgain属性,我想用最小的cost和最大gain排序补丁列表。 sort-by函数用于对一个属性进行排序,但我如何排序这两个属性?排序补丁集或agentset用的NetLogo

+0

你能更准确地知道你想要的“最低成本和最大收益”是什么意思?我可以想象几种可能的含义 - 你可以从另一种中减去一种含义,或者只使用后者来打破前者中的联系,或者... – 2014-09-06 14:38:09

回答

1

排序许多属性的agentset,您可以使用sort-bysort-on:你更喜欢哪一个

patches-own [ cost gain ] 

to sort-patches 
    ca 
    ask patches [ 
    set cost random 100 
    set gain random 100 
    ] 

    let patches-sorted-by sort-by [ 
    ([ cost ] of ?1 > [ cost ] of ?2) or 
    ([ cost ] of ?1 = [ cost ] of ?2 and [ gain ] of ?1 < [ gain ] of ?2) 
    ] patches 
    show map [[ list cost gain ] of ? ] patches-sorted-by 

    let patches-sorted-on sort-on [ (cost * -1000) + gain ] patches 
    show map [[ list cost gain ] of ? ] patches-sorted-on 
end 

是你。使用sort-on需要仔细构建公式(即,如果您可以获得大于1000的收益,则以上方法将不起作用),但稍微不详细。

编辑:多项标准

确定排序的更一般的方式,这可能是您的情况矫枉过正,但我​​想出了很多更一般的:

to-report sort-by-criteria [ criteria items ] 
    ; `criteria` needs to be a task that returns a list of numbers 
    report sort-by [ 
    compare-lists (runresult criteria ?1) (runresult criteria ?2) 
    ] items 
end 

to-report compare-lists [ l1 l2 ] 
    report ifelse-value (empty? l1 or empty? l2) [ false ] [ 
    ifelse-value (first l1 = first l2) 
     [ compare-lists but-first l1 but-first l2 ] 
     [ first l1 < first l2 ] 
    ] 
end 

什么你需要通过sort-by-criteria是一个task,给定你想要排序的项目之一,将报告一个数字列表,根据该列表你的项目将被排序。

在你的情况,你会使用它想:

let sorted-patches sort-by-criteria (
    task [[ list (-1 * cost) gain ] of ? ] 
) patches 

对于两个标准,它可能不值得使用,但如果你有标准的长列表,它很可能是一个更容易和更清晰使用比其他任何方法。

+0

这假定只有成本相等才能获得收益。但是我想知道这张海报是否真的需要考虑到这一点。 – 2014-09-06 17:18:43

+0

确实如此。我没有考虑过这种选择,但我想如果海报想要某种加权排序的成本和收益,他可以使用'sort-on'版本并用'(cost * -1000)+ gain'替换一个合适的功能。 – 2014-09-06 18:08:10

+0

谢谢Nicolas,谢谢Seth,实际上我想找到'cost'和'gain'之间的最佳组合......不仅如果成本是相等的。 – delaye 2014-09-08 08:16:00

相关问题