我有一個Android Studio項目,它使用羣體粒子算法優化移動資源。我一直在我的健身功能中遇到這個錯誤,我的主要活動從未運行或因此顯示出結果。這是我的課,我不斷獲取問題...PSO的健身功能在我的代碼中不起作用?
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;
}
}
基本上,我實現了一個HashMap是需要從主活動的用戶輸入,並分配這些輸入與被用作位的int值bits []數組。這是我的錯誤...
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)
什麼是不清楚的錯誤? 'bits []'顯然是一個零長度的數組。 – john16384
@ john16384我想將hashmap值傳遞給數組。它是一個布爾數組,但它基於用戶輸入的hashmap中的服務是(true)還是off(false)。 –