0

When I tested the below code it was running well with one dataset but when i test it with another dataset , it gave me this thread

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at psofc2.FeatureConstruction.classify(FeatureConstruction.java:192)
    at  pofc2.FeatureConstruction.calacc_binary(FeatureConstruction.java:180)
    at psofc2.FeatureConstruction.getOp(FeatureConstruction.java:97)
    at psofc2.FeatureConstruction.fitness(FeatureConstruction.java:44)
    at psofc2.Swarm.iterate(Swarm.java:87)
    at psofc2.Main.main(Main.java:160)

I have searched in this website for all matched cases but really I did not find my answer.

anas
  • 3
  • 4
  • What does your `Problem` look like? Also, if you split `p_i.getPosition()` and `getProblem().fitness(whatever)`, it'll make it obvious which instruction your exception is coming from. Like so: `double temp = p_i.getPosition(); getProblem().fitness(temp);` – MikaelF Feb 15 '17 at 03:53
  • thanks @MikaelF for yr reply. the problem is just a class that has some set and get methods. for yr suggestion I have spilt them and still get the same error like this (List temp = p_i.getPosition(); double new_fitness = getProblem().fitness(temp);) – anas Feb 15 '17 at 04:15
  • Yes, but which line throw the exception? (Instead of line 84) – MikaelF Feb 15 '17 at 05:19
  • actually there are six lines throw the exception and I have update them in the post. but i think i should start with the line 87 as the first error must be fixed if i am right. – anas Feb 15 '17 at 05:34
  • No, your error happens at `FeatureConstruction.java:192` – OneCricketeer Feb 15 '17 at 05:47
  • thanks@cricket_007. I have update the post with class FeatureConstruction that contain the most errors. Plz if someone can help because I am running of time. – anas Feb 15 '17 at 06:04

2 Answers2

0

Try using p_i.getSize() - 1; for the loop condition.
You seem to be looping through 0,1,2 because size will return 2 but the array will only contain 0,1.

Irf
  • 4,285
  • 3
  • 36
  • 49
J. Knight
  • 131
  • 9
  • thanks@j.knight. I tried it and still have the same error. – anas Feb 15 '17 at 04:24
  • just to inform that the code is running well with a dataset and it gave this error when used another dataset. I do not know why? – anas Feb 15 '17 at 04:41
0

Update

FeatureConstruction has a logic error in classify. You're assuming that the parameter 'training' has no more than 2 elements in it.

   public double classify(Dataset training, Dataset testing) {
        // initial the threshold and two classes
         Object[] clzz = new Object[training.classes().size()]; // <--- Init the array to the size of training
        int index = 0;
        for (Object o : training.classes()) {
            clzz[index] = o; //<-- Now everything in training has a place to be.
            index++;
        }

From the stacktrace you're providing the problem initializes on line 87 of your class; however, the Exception is being thrown from line 192 of the class FeatureConstruction within the same package. It seems like there is an implied dependency between the Particle.getPosition() and Problem.fitness(position) that is not being satisfied.

I would guess that Problem extends FeatureConstruction, and that the issue is related to state management and expectations between the Problem implementation and the parent class.

I would recommend inspecting the variables in the call stack down to FeatureConstrction.classify(). Determine what the state of the Problem is as it pertains to that specific invocation, and build a JUnit test to replicate the condition. That can help to isolate the problem and accelerate testing modifications to correct.

The Junit test will assist in understanding the state of the classes, and possibly allow checks to be put in place to prevent that state. At the end of the day I expect that at least one of three things is going to be the culprit:

  1. FeatureConstruction has a logic issue in the classify function.
  2. Problem (assumed extension of FeatureConstruction) is not accounting for state of the parent class, and is missing some kind of initialization or update call to keep the parent data model in sync.
  3. The process that controls the dependency between the Problem and the Particle has a logic error in one case, and the setup is misconfiguring the data association.

Hope that helps.

Jeremiah
  • 1,145
  • 6
  • 8
  • thank you very much for such good explanation@Jeremiah. Definitely these hints will help me to fix the error not in this code only but in all my study. – anas Feb 15 '17 at 06:57