0

我创建了许多“向量”,每个向量都有自己的“健身”,健身是使用健身功能获得的(已经完成)。
例如:

Vector v1 = new Vector();
Vector v2 = new Vector();
V1 = // some data 
V2 = // some other data
double fitness1 = fitness(v1);
double fitness2 = fitness(v2);

现在我有数百个Vectors,每个都有自己的适应度,我正在尝试创建一个名为的函数globalbest,它返回Vector具有最佳适应度但并不总是得到最好的!谁能帮帮我?这是代码:

   public Vector globalbest(Vector allPopulation) {
    Vector global = new Vector();
    double max = 0;
    for (int i = 0; i < allPopulation.size(); i++) {
        double value = fitness((Vector) allPopulation.get(i));
        if (value > max) {
            global = (Vector) allPopulation.get(i);
            max = value ;
        }
    }
    return global;
}
4

1 回答 1

0

If you want the item with the max value from a set of items, something like this will suffice:

Item maxItem = null;
int maxValue = 0;
for( Item item : items ) {
    if( item.value() > maxValue ) {
        maxItem = item;
        maxValue = item.value();
    }
}
return maxItem;
于 2013-09-20T00:25:34.853 回答