java - ArrayIndexOutOfBoundsException:-1 -
i'm coding rl behaviors on pacman bot , messed things 1 of lists in 1 of functions arg_allmax or chooseaction
here code of class:
package rl; import java.util.arraylist; import java.util.hashtable; public class qlearn { private double epsilon = 0.1; // epsilon parameter epsilon greedy strategy private double alpha = 0.2; // alpha parameter: used influence o refresh of q private double gamma = 0.9; // used notice or not feedback of next action ; if =0 -> no feed private int actions[]; private hashtable< tuple<integer,integer>, double> q; // q(s,a) : hashtable : <state,action> -> value of q public qlearn(int[] actions) { this.actions = actions; q = new hashtable< tuple<integer,integer>, double>(); } public qlearn(int[] actions, double epsilon, double alpha, double gamma) { this.actions = actions; this.epsilon = epsilon; this.alpha = alpha; this.gamma = gamma; q = new hashtable< tuple<integer,integer>, double>(); } public double getq(int id_state, int id_action) { // value of q state of id_state , action id_action ( return 0 if value not in hashtable ) tuple<integer,integer> t = new tuple<integer,integer> (id_state, id_action); // creatte new integer object tubple value of id_state , id_action double v = q.get(t); if(v != null) return v; else return 0.0; } // argmax of list public int argmax(double[] list) { int arg=-1; double max= 0; ( int = 0; i<list.length; i++){ if ( list[i]>max ){ max = list[i]; arg = i; } } return arg; } // argmax if argmax has several iterations public arraylist<integer> arg_allmax(double[] list) { arraylist<integer> args = new arraylist<integer>(); int = argmax(list); ( int = 0; i< list.length; i++){ if (list[i] == list[a]){ args.add(i); } } return args; } // max of list public double max(double[] list) { double max_ = -1e20; int = argmax(list); max_ = list[a]; return max_; } /* * fonction updates hashtable * action id_action , state id_state * if q(s,a) had old value, allocate new value+ alpha(value - old_value) * if q(s,a) had not old value : allocate reward */ public void learnq(int id_state, int id_action, double reward, double value) { tuple<integer,integer> t = new tuple<integer,integer>(id_state,id_action); double oldv = q.get(t); if(oldv == null) { q.put(t, reward); } else { q.put(t, oldv+alpha*(value-oldv)); } } /* * here epsilon greedy strategy * proba epsilon :we choose random action * avec proba 1-eps : choose favorable action in fonction of q(s,a) */ public int chooseaction(int id_state) { int action = -1; if(math.random() < epsilon) { int = (int)(math.random()*actions.length); action = actions[i]; } else { double[] tab = new double[actions.length]; arraylist<integer> argmaxarray = new arraylist<integer>(); ( int i=0; i>actions.length; i++){ tab[i]=actions[i]; } argmaxarray=arg_allmax(tab); int i=(int)(math.random()*argmaxarray.size()); action=argmaxarray.get(i); } return action; } /* * learning after occurence of move * 1) profitable potential action q(s',a) * 2) call learnq */ public void learn(int id_state1, int id_action1, double reward, int id_state2) { int futureaction = 0; futureaction = chooseaction(id_state2); double maxqnew = 0; // remplir maxqnew = getq(futureaction, id_state2); learnq(id_state1, id_action1, reward, reward + gamma*maxqnew); } // affiche q(s,a) private void printqvalue(int id_state) { for(int action : actions) { tuple<integer,integer> t = new tuple<integer,integer>(id_state,action); double v = q.get(t); system.out.print(v+" "); } system.out.println(); }
here eclipse tells me :
exception in thread "awt-eventqueue-0" java.lang.arrayindexoutofboundsexception: -1 @ rl.qlearn.arg_allmax(qlearn.java:54) @ rl.qlearn.chooseaction(qlearn.java:108) @ rl.qlearn.learn(qlearn.java:138)
i think comes somewhere in else of chooseaction method using all_argmax fonction cannot find exact error!
here 2 involved methods (so it's more readable you):
all_argmax :
public arraylist<integer> arg_allmax(double[] list) { arraylist<integer> args = new arraylist<integer>(); int = argmax(list); ( int = 0; i< list.length; i++){ if (list[i] == list[a]){ args.add(i); } } return args; }
chooseaction :
public int chooseaction(int id_state) { int action = -1; if(math.random() < epsilon) { int = (int)(math.random()*actions.length); action = actions[i]; } else { double[] tab = new double[actions.length]; arraylist<integer> argmaxarray = new arraylist<integer>(); ( int i=0; i>actions.length; i++){ tab[i]=actions[i]; } argmaxarray=arg_allmax(tab); int i=(int)(math.random()*argmaxarray.size()); action=argmaxarray.get(i); } return action; }
your indexoutofboundsexception
occurs because of argmax([])
method, either because of empty array or because doubles in list negative.
in either of these cases int arg = -1
variable never set value -1
, out of bounds in scenario since -1
not valid array position.
the best course of action either check if array empty before passing argmax
or checking if return value valid (not -1
) before doing it. , changing double max = 0
double max = double.negative_infinity
Comments
Post a Comment