Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java PSO的适应度函数在我的代码中不起作用?_Java_Android_Particle Swarm - Fatal编程技术网

Java PSO的适应度函数在我的代码中不起作用?

Java PSO的适应度函数在我的代码中不起作用?,java,android,particle-swarm,Java,Android,Particle Swarm,我有一个Android Studio项目,使用swarm粒子算法优化移动资源。我的健身功能中不断出现这个错误,我的主要活动因此从未运行或显示结果。这是我的班级,我不断地遇到问题 public class CustomService implements Goodness { public static int numOfServices = MainActivity.servicenames.size(); public static final int NUM_DIMENSIONS = 1

我有一个Android Studio项目,使用swarm粒子算法优化移动资源。我的健身功能中不断出现这个错误,我的主要活动因此从未运行或显示结果。这是我的班级,我不断地遇到问题

public class CustomService implements Goodness {


public static int numOfServices = MainActivity.servicenames.size();
public static final int NUM_DIMENSIONS = 1 + numOfServices;
public static HashMap<String, Integer> serviceMap = new HashMap<String, Integer>(); //hashmap to store values
ArrayList<String> serviceNames = MainActivity.servicenames;
ArrayList<Double> costData = MainActivity.costDATA;
ArrayList<Double> costWlan = MainActivity.costWLAN;
ArrayList<Double> costUtilities = MainActivity.costUTILITY;
double batteryCost;

public void setBatteryCost(double batteryCost) {
    this.batteryCost = batteryCost;
}

public CustomService(double batteryCost, ArrayList<Double> costData, ArrayList<Double> costWlan,
                     ArrayList<Double> costUtilities) {
    if (costUtilities == null || costUtilities.size() < 1 || costData.size() < 1 || costWlan.size() < 1) {
        throw new RuntimeException("Please add atleast 1 cost to Data, WLAN and Utility");
    }
    this.batteryCost = batteryCost; //make sure you add battery field to UI, user enters battery level
    this.costData = costData;
    this.costWlan = costWlan;
    this.costUtilities = costUtilities;
}


public double getGoodness(boolean[] bits) {
    double utility = 0.0;
    double rcost = 0.0;
    ArrayList<Double> resourceCost = new ArrayList<Double>();
    Collections.sort(costUtilities);
    double maxValue = Collections.max(costUtilities);
    int NumOfDimensions = serviceMap.size();
    serviceMap.put("DATA", 0);
    serviceMap.put("WLAN", 1);
    int data = serviceMap.get("DATA");
    int wlan = serviceMap.get("WLAN");
    for (int i = 2; i <= NumOfDimensions; i++) { //hashmap that stores the service names with a bit value i
        for (int k = 0; k < numOfServices; k++) {
            serviceMap.put(serviceNames.get(k), i); //if i = 2, k = 1, put(2, Facebook), put(3, Twitter)
        }

        if (bits[data] && bits[wlan]) {
            return -500;
        }
        if (!bits[data] || bits[wlan]) {
            return -1000; //particle goodness always returns this??
        }
        if (bits[data]) {
            resourceCost = costData;
        } else if (bits[wlan]) {
            resourceCost = costWlan;
        }


        for (int n = 2; n <= numOfServices; n++) {
            if (bits[serviceMap.get(n)]) {
                utility += costUtilities.get(n - 1);
                rcost += resourceCost.get(n - 1);
            }
        }
        if (rcost < batteryCost) {
            return utility;
        }
    }
        return utility * 0.50;
    }

}

在我看来,您似乎想使用
数组作为输入/输出参数。但是,不能在创建数组后更改其大小。因此,您有几个选择:

1) 当您传入该数组时,请确保该数组的大小与
serviceNames
相同

2) 使用可以调整大小的对象(如
位集
列表
)而不是数组


3) 在
getGoodness
内创建数组,并返回一个复合值,该值具有
double
和创建的
bits
数组作为
getGoodness

的返回值。在我看来,您似乎想使用
bits
数组作为输入/输出参数。但是,不能在创建数组后更改其大小。因此,您有几个选择:

1) 当您传入该数组时,请确保该数组的大小与
serviceNames
相同

2) 使用可以调整大小的对象(如
位集
列表
)而不是数组


3) 在
getGoodness
内部创建数组,并返回一个复合值,该复合值具有
双精度
和创建的
数组作为
getGoodness

的返回值,错误还有什么不清楚的地方<代码>位[]显然是一个零长度数组。@john16384我想把hashmap值传递到数组中。它是一个布尔数组,但是它是基于用户输入的hashmap中的服务是开(true)还是关(false)的。关于错误还有什么不清楚的<代码>位[]显然是一个零长度数组。@john16384我想把hashmap值传递到数组中。它是一个布尔数组,但是它是基于用户输入的hashmap中的服务是开(true)还是关(false)。如果数组中没有任何内容,我如何确保数组的大小与serviceNames相同?我需要它是一个数组,因为原始算法实现(Goodness接口)将boolean[]位置作为Goodness函数的参数,我无法更改它。感谢您的帮助:)嗯,
serviceMap.size()
在我看来是
数组应该具有的大小。如果您无法控制调用方代码(谁在调用
getGoodness
?),那么这将永远不会起作用——调用方似乎在传递一个零长度数组。getGoodness正在由名为“bpso”的实际算法调用,我无法更改该算法。因此,布尔[]位必须是GetGoods函数的参数。我已经试着编写代码好几天了,我仍然不能以比特的形式传递服务,除非我将它们声明为最终的静态int。我也不能这样做,因为服务的数量和服务名称将根据用户输入的内容而变化。无论如何,谢谢你的帮助!如果阵列中没有任何内容,如何确保阵列的大小与ServiceName相同?我需要它是一个数组,因为原始算法实现(Goodness接口)将boolean[]位置作为Goodness函数的参数,我无法更改它。感谢您的帮助:)嗯,
serviceMap.size()
在我看来是
数组应该具有的大小。如果您无法控制调用方代码(谁在调用
getGoodness
?),那么这将永远不会起作用——调用方似乎在传递一个零长度数组。getGoodness正在由名为“bpso”的实际算法调用,我无法更改该算法。因此,布尔[]位必须是GetGoods函数的参数。我已经试着编写代码好几天了,我仍然不能以比特的形式传递服务,除非我将它们声明为最终的静态int。我也不能这样做,因为服务的数量和服务名称将根据用户输入的内容而变化。无论如何,谢谢你的帮助!
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                  at test_classes.CustomService.getGoodness(CustomService.java:60)
                  at bpso.BinaryPso.optimize(BinaryPso.java:45)
                  at android_tests.CustomUseCase.test(CustomUseCase.java:56)
                  at ai69.psoui.ParticleActivity.runTest(ParticleActivity.java:108)
                  at ai69.psoui.ParticleActivity$runTests.doInBackground(ParticleActivity.java:59)
                  at ai69.psoui.ParticleActivity$runTests.doInBackground(ParticleActivity.java:56)
                  at android.os.AsyncTask$2.call(AsyncTask.java:295)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)