Index out of bounds error

110 views
Skip to first unread message

Eva Geerlings

unread,
Jan 27, 2020, 10:41:44 AM1/27/20
to GAMA
Hello there, 

I'm making a model in which I simulate EV charging and driving behaviour (see attachments). I keep getting the Java error: index out of bounds, which occurs almost every time when i run the model (9 out of 10 times). I pasted the lines of code and the error report under this message. 

The situation is as follows: I made two lists for each agent: remaining_cps and distances_cps. With the creation of the EVagent, the first list is filled with all CPs (charging points) and the second list is filled with all the distances between the CP and the destination of the agent. In the reflex underneath, the CP will be removed from both lists when the CP is occupied. After that, the next_cp_index is calculated to see what the next CP is on the list with the lowest value for the distance. This index is used to select the next destination_cp, from the remaining_cps lists. 

I'm quite new to using GAMA, so any help would be much appreciated! Please let me know if I need to explain more about my issue. 

Kind regards, 
Eva

 //reflex of setting CP as target
    reflex time_to_charge
when: SoC < dSoC and going_home != true {
    searching_for_cp
<- true ;
    let tobe_removed
<- [] ;
    loop thecp over
: remaining_cps {
   
if condition: ((thecp as cp).occupied=true) {
    add item
: thecp to: tobe_removed ;
   
}
   
}
    loop thecp over
:tobe_removed {
    remove item
:(thecp as cp) from: remaining_cps ;
    remove item
:(thecp as cp) from: distances_cps ;
   
}
    next_cp_index
<- distances_cps index_of min(distances_cps);
    destination_cp
<- remaining_cps[next_cp_index];
        the_target_cp
<- point(destination_cp) ;
   
}


2 occurrences in 2 agents at cycle 26: Java error: index out of bounds
in destination_cp <- remaining_cps[next_cp_index];
IndexOutOfBoundsException: Index: 41, Size: 41
java
.util.ArrayList.rangeCheck(ArrayList.java:657)
java
.util.ArrayList.get(ArrayList.java:433)
msi
.gama.util.IList.get(IList.java:181)
msi
.gama.util.GamaList.getFromIndicesList(GamaList.java:99)
gaml
.additions.GamlAdditions.lambda$938(GamlAdditions.java:995)
msi
.gaml.expressions.BinaryOperator._value(BinaryOperator.java:77)
msi
.gaml.expressions.AbstractExpression.value(AbstractExpression.java:70)
when applying the internal_at operator on [cp(0),cp(1),cp(2),cp(3),cp(4),cp(5),cp(8),cp(9),cp(10),cp(11),cp(12),cp(13),cp(14),cp(15),cp(16),cp(17),cp(18),cp(19),cp(20),cp(21),cp(22),cp(23),cp(24),cp(25),cp(26),cp(27),cp(28),cp(29),cp(30),cp(31),cp(32),cp(33),cp(34),cp(35),cp(36),cp(37),cp(38),cp(39),cp(40),cp(41),cp(42)] and [41]
in destination_cp <- remaining_cps[next_cp_index];
in reflex time_to_charge when: (SoC < dSoC) and (going_home != true)  {
 searching_for_cp
<- true;
 unknown tobe_removed
<- [];
 loop thecp over
: remaining_cps  {
 
if cp(thecp).occupied = true  {
 tobe_removed
<+ thecp;
 
}
 
 
}



 loop thecp over
: tobe_removed  {
 remaining_cps
>- cp(thecp);
 distances_cps
>- cp(thecp);
 
}


 next_cp_index
<- distances_cps index_of min(distances_cps);
 destination_cp
<- remaining_cps[next_cp_index];
 the_target_cp
<- point(destination_cp);
}


in agents EVagent21, Simulation 0

EVmodelGeerlings.zip

Srirama Bhamidipati

unread,
Jan 27, 2020, 1:01:33 PM1/27/20
to GAMA
Hi 

Just from a quick look it seems you may be looping an extra index. Note that index of lists start at 0 in GAML. so your loop should be one less than length of your list.


Srirama 

Eva Geerlings

unread,
Jan 28, 2020, 12:35:29 PM1/28/20
to GAMA
Hi,

Thank you for your answer! I now tried to remove 1 from the loop length: 

     loop thecp from: 0 to:length(remaining_cps)-1 {

   
if condition: ((thecp as cp).occupied=true) {
    add item
: thecp to: tobe_removed ;
   
}
   
}

    loop thecp
from: 0 to:length(tobe_removed)-1 {

    remove item
:(thecp as cp) from: remaining_cps ;
    remove item
:(thecp as cp) from: distances_cps ;
   
}

But I get the same error. Am I doing this in the wrong way?

Thank you in advance!

Eva

Op maandag 27 januari 2020 19:01:33 UTC+1 schreef Srirama Bhamidipati:

Srirama Bhamidipati

unread,
Jan 29, 2020, 3:24:17 AM1/29/20
to GAMA
Hi

It is still the same error. Here is a output I generated:

error comes here at index 39 in agent EVagent(21)
because not_visited_cps has only 39 elements

I think you should carefully look at your add/remove items sequence.  I know it is frustrating, but it is a common error, always check your list before running a loop. So in the output I generated, it would have worked if next_cp_index was 38, because not_visited_cps is length 39.


Srirama

Youcef Sklab

unread,
Feb 3, 2020, 4:57:26 AM2/3/20
to GAMA
Hi, 
Should these two lists: not_visited_cps  and distances_cps, have with the same size throughout the simulation?

Youcef

Eva Geerlings

unread,
Feb 4, 2020, 8:28:39 AM2/4/20
to GAMA
Thank you for your help! I now understand the issue with the index better. Unfortunately, I keep running into other problems.

The lists not_visited_cps and distances_cps should indeed have the same size. I figured that the code I posted before removes the occupied CPs in the not_visited_cps list, however the remove statement for the distances_cps list doesn't work. So the not_visited_cps list keeps decreasing, while the distances_cps list keeps the same size. Is there a way to remove the cp from the distances_cps list, maybe based on the index? 

I tried many things, like this (the name of the not_visited_cps list is changed in remaining_cps): 

reflex cp_list {
let thecp type:cp ;
    loop thecp over: remaining_cps {
    if ((thecp as cp).occupied=true) and ((thecp as cp) in tobe_removed) != true {
    add (thecp as cp) to: tobe_removed ;
    }
    }
    let thecpi type:int ;
    loop thecp over: tobe_removed {
    remove thecp from: remaining_cps ;
    thecpi <- (remaining_cps index_of thecp) ;
    remove index: [thecpi] from: distances_cps ;
     }

Thank you in advance!

Eva


Op maandag 3 februari 2020 10:57:26 UTC+1 schreef Youcef Sklab:

Youcef Sklab

unread,
Feb 4, 2020, 8:59:05 AM2/4/20
to GAMA
From your last model, I understand that remaining_cps, which is a list of cp;  and distances_cps which is a list of float,  are built together, step by step.
Based on that,  to remove an element (thecp) from remaining_cps and it’s corresponding element (index) in the list distances_cps, you can try this part of code : 


  loop thecp over:tobe_removed {

     

    int ind <- remaining_cps index_of thecp;

    

    remove index: ind from: distances_cps; 

    remove thecp from: remaining_cps ;

      

    }



Youcef


Eva Geerlings

unread,
Feb 4, 2020, 9:26:19 AM2/4/20
to GAMA
Thank you Youcef! This code succesfully removes the values from both lists. However, i'm now getting this error:

2 occurrences in 2 agents at cycle 18: Java error: index out of bounds
in distances_cps[] >- ind;
ArrayIndexOutOfBoundsException: null
in agents EVagent0, Simulation 0

It works when I put a if statement in the loop: 

 reflex cp_list {
 let thecp type
:cp ;
    loop thecp over
: remaining_cps {
   
if ((thecp as cp).occupied=true) and ((thecp as cp) in tobe_removed) != true {
    add
(thecp as cp) to: tobe_removed ;
   
}
   
}

    loop thecp over
: tobe_removed {
   
int ind <- remaining_cps index_of thecp ;

 
if ind >= 0 {

    remove index
: ind from: distances_cps ;
     
}
    remove thecp
from: remaining_cps ;
   
} }

Is that the right way to solve it? 

Eva

Op dinsdag 4 februari 2020 14:59:05 UTC+1 schreef Youcef Sklab:

Youcef Sklab

unread,
Feb 4, 2020, 9:36:26 AM2/4/20
to GAMA
The variable ind is supposed to be all the time:  ind>=0, and the instructions :   remove index: ind from: distances_cps ; and  remove thecp from: remaining_cps; have to be both under the same condition. 

I suggest to share the last version of your code, so that, I run it and check. 

Eva Geerlings

unread,
Feb 4, 2020, 10:05:32 AM2/4/20
to GAMA
Thank you for your help Youcef! I included the model. 

Op dinsdag 4 februari 2020 15:36:26 UTC+1 schreef Youcef Sklab:
Model0204.zip

Youcef Sklab

unread,
Feb 4, 2020, 11:31:54 AM2/4/20
to GAMA
I see. You are getting that error because the list  tobe_removed has to be updated at every new cycle. Otherwise, you will cumulate the current elements to delete with those of previous cycles. That is why you get ind = -1. 


Try this updated reflex : 

reflex cp_list {

tobe_removed <- [];

    loop thecp over: remaining_cps {

    

    if (thecp.occupied=true) and (thecp in tobe_removed != true ) {

    add thecp to: tobe_removed ;

    }

      }

    

    loop elt over: tobe_removed {

    int ind <-  remaining_cps index_of elt ;

    remove index: ind from: distances_cps ;

    remove elt from: remaining_cps ;

Eva Geerlings

unread,
Feb 5, 2020, 5:30:07 AM2/5/20
to GAMA
Thank you for your help! It works now. 

Op dinsdag 4 februari 2020 17:31:54 UTC+1 schreef Youcef Sklab:
Reply all
Reply to author
Forward
0 new messages