OK, I’m trying to make a fun ai challenge between a bunch of different types of learning, but when I try to code Qlearning I am running into trouble. It will just sit still and do nothing.
here is sketch
int s = 25; // size of each block
Table pathTable,Qbrain; // table to hold path and a second for brain of Q
TableRow tr; // my tablerow
ArrayList<Wall> wall = new ArrayList<Wall>(); // my wall array which creates path
ArrayList<Qenemy> qenemy = new ArrayList<Qenemy>(); // my Q enemy array 1 at time
//ArrayList<SeeAll> Senemy = new ArrayList<SeeAll>(); // my next enemy
void setup(){
size(625,625);
pathTable = loadTable("path.csv"); // load the path i want
Qbrain = loadTable("Qbrain.csv"); // load the Q brain
for (int i = 0; i <= 324; i++){ // add walls
wall.add(new Wall(i));
}
qenemy.add(new Qenemy(26)); // add Q enemy
//Senemy.add(new SeeAll(26));
}
void draw(){
background(0);
for (Wall w: wall){ // walls
w.show(); // show walls
w.mouseCheck(); // check walls if making path
}
Qenemy q = qenemy.get(0); // getting Q enemy
q.show(); // showng it
q.think();// giving it a brain it not using
if (q.collide == true){ // check collide
qenemy.remove(0);
qenemy.add(new Qenemy(26));
}// collide
fill(255);
textSize(20);
text(qenemy.size(),5,350);
}// draw
void mouseClicked(){
tr = pathTable.getRow(0);
for (int i = 0; i < wall.size(); i++){
Wall w = wall.get(i);
if (w.check == true){
if (w.type == 0){w.type = 1;}
else if (w.type == 1){w.type = 2;}
else if (w.type == 2){w.type = 3;}
else if (w.type == 3){w.type = 0;}
tr.setInt(i,w.type);
saveTable(pathTable,"path.csv");
}// check true
}
}
void keyPressed(){
if (key == 'r'){
for (int i = 0; i < wall.size(); i++){
Wall w = wall.get(i);
w.type = 0;
tr.setInt(i,w.type);
}
saveTable(pathTable,"path.csv");
}// r
}
enemy
class Enemy{ // create enemy that others are based from
PVector pos; // position
int num; //where is it on the table
int type = 0; // which ai is it
Boolean collide = false; // has it collided with wall
Enemy(int n, int t){
num = n;// get it number
int y = 0;
while(n > 24){// placing it on map
n -= 25;
y += 1;
}
pos = new PVector(n * s, y * s);
type = t;// setting type
}
void show(){// showing enemy
fill(125);
rect(pos.x,pos.y,s,s);
fill(0);
if (type == 0){text("Q",pos.x+5, pos.y+20);}
if (type == 1){text("B",pos.x+5, pos.y+20);}
}
}
Qenemy
class Qenemy extends Enemy{ // extending enemy so can skip the common stuff
int bn = 0; // the best number from table
int br = 0; // the best table column
float reward = 0.01; // the reward given
float r,rn;// 2 randoms
int explore = 100;// explore rate
int lnum; // last pos
Qenemy(int n){
super(n,0);
}
void setPos(int n){// setPos after die
num = n;
lnum = num;
int y = 0;
while(n > 24){
n -= 25;
y += 1;
}
pos = new PVector(n * s, y * s);
}
void think(){// thiink about what to do
r = random(100);// random number
r = round(r);// rounding random
if (r < explore){explorer();}// if less then explore do explorer
if (r >= explore){checkTable();}// if greater or equal do checkTable
move();
}// think
void checkTable(){
br = 0;// set best column to 0 so wont be messed up from last loop
bn = 0;// set best number to 0 so can find best
tr = Qbrain.getRow(num); // getting correct table and row
for (int i = 0; i < 4; i++){// checking to see who is best
int n = tr.getInt(i);
if (n > bn){//if best switch the info
br = i;
bn = n;
}
}
}
void explorer(){// exploring
tr = Qbrain.getRow(num);// getting right table
rn = random(3);// find random number
rn = round(rn);// rounding number
Float n = tr.getFloat(int(rn));// grabbing number from table
////////////////////////////////////////////////////////////////// looking at it i think problem here or collide
if (rn == 0){// if 0 go up
num -= 25;
n += reward;
tr.setFloat(int(rn),n);
}
if (rn == 1){//if 1 go down
num += 25;
n += reward;
tr.setFloat(int(rn),n);
}
if (rn == 2){// if 2 go left
num -= 1;
n += reward;
tr.setFloat(int(rn),n);
}
if (rn == 3){// if 3 go right
num += 1;
n += reward;
tr.setFloat(int(rn),n);
}
explore -= 1; // -1 from explore so might use brain
}// explorer
void move(){
setPos(num);// setting position
checkCollide();// check if hitting wall
}
void checkCollide(){
collide = false;// set to false so don't have problems from last
for (int i = 0; i < wall.size(); i++){// checking walls
Wall w = wall.get(i);
if (w.pos.x == pos.x && w.pos.y == pos.y && w.type == 0){// if enemy and wall collide
tr = Qbrain.getRow(lnum);// get last num since we moved already
float n = tr.getFloat(int(rn));// get number from table
n -= reward*2;// double reward and minus
tr.setFloat(int(rn),n);// set number back in
setPos(26);// reset position
}
}
if (explore == 0){// killing enemy if can't explore
collide = true;
}
lnum = num;// setting last to position
}// check
}
wall
class Wall{
PVector pos;
int num;
Boolean check = false;
int type = 0;
Wall(int n){
num = n;
int y = 0;
while(n > 24){
n -= 25;
y += 1;
}
pos = new PVector(n * s, y * s);
}// wall
void show(){
tr = pathTable.getRow(0);
type = tr.getInt(num);
if (type == 0){fill(255,0,0);}
if (type == 1){fill(0,255,0);}
if (type == 2 || type == 3){fill(255);}
rect(pos.x,pos.y,s,s);
fill(0);
textSize(20);
if (type == 2){text("S",pos.x+5, pos.y+20);}
if (type == 3){text("E",pos.x+5, pos.y+20);}
if (check){line(pos.x,pos.y,pos.x+s,pos.y+s);}
}
void mouseCheck(){
check = false;
if (mouseX > pos.x && mouseX < pos.x + s && mouseY > pos.y && mouseY < pos.y + s){
check = true;
}
}// mouseCheck
}