2017-04-10 67 views
1

我有一個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) 
+0

什麼是不清楚的錯誤? 'bits []'顯然是一個零長度的數組。 – john16384

+0

@ john16384我想將hashmap值傳遞給數組。它是一個布爾數組,但它基於用戶輸入的hashmap中的服務是(true)還是off(false)。 –

回答

0

在我看來,你要使用的bits數組作爲輸入/輸出參數。但是,創建後無法更改數組的大小。所以,你有幾種選擇:

1)確保陣列具有傳遞時,它的尺寸爲serviceNames相同

2)使用一個對象,可以調整(如BitSetList<Boolean>)。而不是陣列。

3)創建所述陣列內部getGoodness和返回具有double和所創建的bits陣列的getGoodness返回值的複合值。

+0

如何確保數組的大小與serviceNames相同(如果atm中沒有任何內容)?我需要它是一個數組,因爲原始算法實現(Goodness接口)需要布爾[]位置作爲善良函數的參數,我無法改變它。謝謝你的幫助:) –

+0

好吧,'serviceMap.size()'在我看來應該是'bits'數組的大小。如果你不能控制調用者代碼(誰調用'getGoodness'?),那麼這將永遠不會工作 - 調用者傳遞一個零長度的數組。 – john16384

+0

getGoodness被稱爲'bpso'的實際算法調用,我無法改變。所以布爾[]位必須是getGoodness函數的參數。我一直試圖編碼這個好幾天,除非我將它們聲明爲final static int,否則我仍然無法傳遞這些服務。我不能這樣做,因爲服務的數量和服務名稱會根據用戶輸入的內容而有所不同。無論如何,謝謝你的幫助! –