2017-04-10 4 views
1

웜 입자 알고리즘을 사용하여 모바일 리소스를 최적화하는 Android Studio 프로젝트가 있습니다. 내 피트니스 기능에서이 오류가 계속 발생하여 내 주요 활동이 실행되지 않거나이 때문에 결과가 표시됩니다. 이 내가있는 비트로 사용하는 int 값이 입력을 주요 활동에서 사용자의 입력을 받아 할당하는 해시 맵을 구현, 기본적으로 내가에 점점 문제를 계속 내 수업 ...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; 
    } 

} 

입니다 비트 [] 배열. 이들은 당신이 입력/출력 매개 변수로 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 []'는 분명히 길이가 0 인 배열입니다. – john16384

+0

@ john16384 hashmap 값을 배열에 전달하고 싶습니다. 그러나 boolean 배열은 사용자가 입력 한 해시 맵의 서비스가 켜져 있는지 (true) 또는 꺼져 있는지 (false) 여부를 기반으로합니다. –

답변

0

그것은 나에게 보인다 ... 내 오류입니다. 그러나 배열을 만든 후에는 배열의 크기를 변경할 수 없습니다. 그래서 당신은 몇 가지 옵션이 있습니다 :

1) 배열은 당신이 그것을 통과 serviceNames와 같은 크기를 가지고 있는지 확인

2)는 BitSet 또는 List<Boolean> 같은 크기를 조정할 수있는 개체를 (사용). 배열 대신.

3) getGoodness 내부 어레이를 만들고 doublegetGoodness의 반환 값으로서 생성 bits 배열을 갖는 합성 값을 반환한다.

+0

atm에 아무것도 없는데 배열의 serviceNames와 동일한 크기인지 확인하려면 어떻게해야합니까? 원래 알고리즘 구현 (Goodness 인터페이스)이 선의 함수에 대한 매개 변수로 부울 [] 위치를 취하고이를 변경할 수 없기 때문에 배열이되어야합니다. 도와 주셔서 감사합니다 :) –

+0

음,'serviceMap.size()'는'bits' 배열이 있어야하는 크기로 보입니다. 'getGoodness'를 호출하는 호출자 코드를 제어 할 수 없다면 호출자는 길이가 0 인 배열을 넘겨주고있는 것 같습니다. – john16384

+0

getGoodness는 'bpso'라는 실제 알고리즘에 의해 호출되고 있는데 변경할 수 없습니다. 따라서 boolean [] 비트는 getGoodness 함수의 매개 변수 여야합니다. 나는 이것을 일종의 코드로 만들려고 노력해 왔지만, final static int로 선언하지 않으면 여전히 비트로 서비스를 전달할 수 없다. 나는 서비스의 수와 서비스 이름이 사용자가 입력 한 것에 따라 달라지기 때문에 그렇게 할 수 없다. 어쨌든 도와 주셔서 감사합니다! –