codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Genetic Algorithm Help  behrk2 at 00:56 on Friday, November 25, 2005
 

Suppose you have an altered chess board:

1 1 1 1 0 0 0 0
1 1 1 1 1 0 0 0
1 1 1 1 1 1 0 0
1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1

where the 0's are spaces where nothing can be placed.

Now suppose you have four tetris shapes (each a 4x4 2D array)

Using a genetic algorithm, I need to come up with the best fitness (# spaces filled/total # of spaces)

Pseudo code for the genetic algorithm:

-------------------------------------
initialize population randomly;
Evaluate;
while (best fitness < 1)
{
Evaluate;
Discard bottom 2 solutions;
Add a new solution as crossover of top 2 solutions;
Add a new solutation as a mutation of the top solution;
}
-------------------------------------

Here is the code I have so far, I'm having trouble staying in bounds with my arrays:



import java.util.*;

public class BEASTga {

public static void main (String[] args) {

int board[][] = new int[8][8];

for(int i=0;i<8;i++)
{
//System.out.println();

for(int j=0;j<8;j++)
{
board[j] = 1;
board[0][4] = 0;
board[0][5] = 0;
board[0][6] = 0;
board[0][7] = 0;

board[1][5] = 0;
board[1][6] = 0;
board[1][7] = 0;

board[2][6] = 0;
board[2][7] = 0;

board[3][7] = 0;

//System.out.print(board[j]);
}
}

//Box1();
//Box2();
//Box3();
//Box4();

//Line1();
//Line2();
//Line3();
//Line4();

//L1();
//L2();
//L3();
//L4();

//Cyclops1();
//Cyclops2();
//Cyclops3();
//Cyclops4();

Random generator = new Random();

int shapeUse[][] = new int[8][8];

int shape = generator.nextInt(16);

int xCoord1 = generator.nextInt(7);
int yCoord1 = generator.nextInt(7);

int xCoord2 = generator.nextInt(7);
int yCoord2 = generator.nextInt(7);

int xCoord3 = generator.nextInt(7);
int yCoord3 = generator.nextInt(7);

int xCoord4 = generator.nextInt(7);
int yCoord4 = generator.nextInt(7);

switch (shape) {
case 1: shapeUse = Box1();
break;
case 2: shapeUse = Box2();
break;
case 3: shapeUse = Box3();
break;
case 4: shapeUse = Box4();
break;

case 5: shapeUse = Line1();
break;
case 6: shapeUse = Line2();
break;
case 7: shapeUse = Line3();
break;
case 8: shapeUse = Line4();
break;

case 9: shapeUse = L1();
break;
case 10: shapeUse = L2();
break;
case 11: shapeUse = L3();
break;
case 12: shapeUse = L4();
break;

case 13: shapeUse = Cyclops1();
break;
case 14: shapeUse = Cyclops2();
break;
case 15: shapeUse = Cyclops3();
break;
case 16: shapeUse = Cyclops4();
break;

default: System.out.println("Not a shape!");
break;
}

int FITNESS = 0;
int FILLED = 0;
int TOTAL = 0;

int count = 0;
int bad = 0;

FITNESS = FILLED / 54;

while (FITNESS < 1) {

//EVALUATE
//================================

board[xCoord1][yCoord1] = shapeUse[0][0];
board[xCoord1][yCoord1 + 1] = shapeUse[0][1];
board[xCoord1][yCoord1 + 2] = shapeUse[0][2];
board[xCoord1][yCoord1 + 3] = shapeUse[0][3];

board[xCoord1 + 1][yCoord1] = shapeUse[1][0];
board[xCoord1 + 1][yCoord1 + 1] = shapeUse[1][1];
board[xCoord1 + 1][yCoord1 + 2] = shapeUse[1][2];
board[xCoord1 + 1][yCoord1 + 3] = shapeUse[1][3];

board[xCoord1 + 2][yCoord1] = shapeUse[2][0];
board[xCoord1 + 2][yCoord1 + 1] = shapeUse[2][1];
board[xCoord1+ 2][yCoord1 + 2] = shapeUse[2][2];
board[xCoord1+ 2][yCoord1 + 3] = shapeUse[2][3];

board[xCoord1 + 3][yCoord1] = shapeUse[3][0];
board[xCoord1 + 3][yCoord1 + 1] = shapeUse[3][1];
board[xCoord1 + 3][yCoord1 + 2] = shapeUse[3][2];
board[xCoord1 + 3][yCoord1 + 3] = shapeUse[3][3];



board[xCoord2][yCoord2] = shapeUse[0][0];
board[xCoord2][yCoord2 + 1] = shapeUse[0][1];
board[xCoord2][yCoord2 + 2] = shapeUse[0][2];
board[xCoord2][yCoord2 + 3] = shapeUse[0][3];

board[xCoord2 + 1][yCoord2] = shapeUse[1][0];
board[xCoord2 + 1][yCoord2 + 1] = shapeUse[1][1];
board[xCoord2 + 1][yCoord2 + 2] = shapeUse[1][2];
board[xCoord2 + 1][yCoord2 + 3] = shapeUse[1][3];

board[xCoord2 + 2][yCoord2] = shapeUse[2][0];
board[xCoord2 + 2][yCoord2 + 1] = shapeUse[2][1];
board[xCoord2 + 2][yCoord2 + 2] = shapeUse[2][2];
board[xCoord2 + 2][yCoord2 + 3] = shapeUse[2][3];

board[xCoord2 + 3][yCoord2] = shapeUse[3][0];
board[xCoord2 + 3][yCoord2 + 1] = shapeUse[3][1];
board[xCoord2 + 3][yCoord2 + 2] = shapeUse[3][2];
board[xCoord2 + 3][yCoord2 + 3] = shapeUse[3][3];



board[xCoord3][yCoord3] = shapeUse[0][0];
board[xCoord3][yCoord3 + 1] = shapeUse[0][1];
board[xCoord3][yCoord3 + 2] = shapeUse[0][2];
board[xCoord3][yCoord3 + 3] = shapeUse[0][3];

board[xCoord3 + 1][yCoord3] = shapeUse[1][0];
board[xCoord3 + 1][yCoord3 + 1] = shapeUse[1][1];
board[xCoord3 + 1][yCoord3 + 2] = shapeUse[1][2];
board[xCoord3 + 1][yCoord3 + 3] = shapeUse[1][3];

board[xCoord3 + 2][yCoord3] = shapeUse[2][0];
board[xCoord3 + 2][yCoord3 + 1] = shapeUse[2][1];
board[xCoord3+ 2][yCoord3 + 2] = shapeUse[2][2];
board[xCoord3+ 2][yCoord3 + 3] = shapeUse[2][3];

board[xCoord3 + 3][yCoord3] = shapeUse[3][0];
board[xCoord3 + 3][yCoord3 + 1] = shapeUse[3][1];
board[xCoord3 + 3][yCoord3 + 2] = shapeUse[3][2];
board[xCoord3 + 3][yCoord3 + 3] = shapeUse[3][3];



board[xCoord4][yCoord4] = shapeUse[0][0];
board[xCoord4][yCoord4 + 1] = shapeUse[0][1];
board[xCoord4][yCoord4 + 2] = shapeUse[0][2];
board[xCoord4][yCoord4 + 3] = shapeUse[0][3];

board[xCoord4 + 1][yCoord4] = shapeUse[1][0];
board[xCoord4 + 1][yCoord4 + 1] = shapeUse[1][1];
board[xCoord4 + 1][yCoord4 + 2] = shapeUse[1][2];
board[xCoord4 + 1][yCoord4 + 3] = shapeUse[1][3];

board[xCoord4 + 2][yCoord4] = shapeUse[2][0];
board[xCoord4 + 2][yCoord4 + 1] = shapeUse[2][1];
board[xCoord4+ 2][yCoord4 + 2] = shapeUse[2][2];
board[xCoord4+ 2][yCoord4 + 3] = shapeUse[2][3];

board[xCoord4 + 3][yCoord4] = shapeUse[3][0];
board[xCoord4 + 3][yCoord4 + 1] = shapeUse[3][1];
board[xCoord4 + 3][yCoord4 + 2] = shapeUse[3][2];
board[xCoord4 + 3][yCoord4 + 3] = shapeUse[3][3];

//================================


// while (EXPRESSION) {

for (int i = 0; i <= 7; i++){

for (int j = 0; j <=7; j++){

if (board[j] == 1) {

count = count + 1;
}
else{
bad = bad + 1;
}
}
}

// }
FILLED = count;
System.out.println(FITNESS);
}


}

public static int[][] Box1(){

int box1[][] = new int[4][4];

for(int i=0;i<4;i++)
{
//System.out.println();

for(int j=0;j<4;j++)
{
box1[j] = 1;
box1[0][0] = 0;
box1[0][1] = 0;
box1[1][0] = 0;
box1[1][1] = 0;

//System.out.print(box1[j]);

}

}
//System.out.println();
return box1;
}

public static int[][] Box2(){

int box2[][] = new int[4][4];

for(int i=0;i<4;i++)
{
//System.out.println();

for(int j=0;j<4;j++)
{
box2[j] = 1;
box2[0][0] = 0;
box2[0][1] = 0;
box2[1][0] = 0;
box2[1][1] = 0;

//System.out.print(box2[j]);

}

}
//System.out.println();
return box2;
}

public static int[][] Box3(){

int box3[][] = new int[4][4];

for(int i=0;i<4;i++)
{
//System.out.println();

for(int j=0;j<4;j++)
{
box3[j] = 1;
box3[0][0] = 0;
box3[0][1] = 0;
box3[1][0] = 0;
box3[1][1] = 0;

//System.out.print(box3[j]);

}

}
//System.out.println();
return box3;
}

public static int[][] Box4(){

int box4[][] = new int[4][4];

for(int i=0;i<4;i++)
{
//System.out.println();

for(int j=0;j<4;j++)
{
box4[j] = 1;
box4[0][0] = 0;
box4[0][1] = 0;
box4[1][0] = 0;
box4[1][1] = 0;

//System.out.print(box4[j]);

}

}
//System.out.println();
return box4;
}

public static int[][] Line1(){

int line1[][] = new int[4][4];

for(int i=0;i<4;i++)
{
//System.out.println();

for(int j=0;j<4;j++)
{
line1[j] = 1;
line1[0][0] = 0;
line1[1][0] = 0;
line1[2][0] = 0;
line1[3][0] = 0;

//System.out.print(line1[j]);

}

}
//System.out.println();
return line1;
}

public static int[][] Line2(){

int line2[][] = new int[4][4];

for(int i=0;i<4;i++)
{
//System.out.println();

for(int j=0;j<4;j++)
{
line2[j] = 1;
line2[0][0] = 0;
line2[0][1] = 0;
line2[0][2] = 0;
line2[0][3] = 0;

//System.out.print(line2[j]);

}

}
//System.out.println();
return line2;
}

public static int[][] Line3(){

int line3[][] = new int[4][4];

for(int i=0;i<4;i++)
{
//System.out.println();

for(int j=0;j<4;j++)
{
line3[j] = 1;
line3[0][0] = 0;
line3[1][0] = 0;
line3[2][0] = 0;
line3[3][0] = 0;

//System.out.print(line3[j]);

}

}
//System.out.println();
return line3;
}

public static int[][] Line4(){

int line4[][] = new int[4][4];

for(int i=0;i<4;i++)
{
//System.out.println();

for(int j=0;j<4;j++)
{
line4[j] = 1;
line4[0][0] = 0;
line4[0][1] = 0;
line4[0][2] = 0;
line4[0][3] = 0;

//System.out.print(line2[j]);

}

}
//System.out.println();
return line4;
}

public static int[][] L1(){

int L1[][] = new int[4][4];

for(int i=0;i<4;i++)
{
//System.out.println();

for(int j=0;j<4;j++)
{
L1[j] = 1;
L1[0][0] = 0;
L1[1][0] = 0;
L1[2][0] = 0;
L1[2][1] = 0;

//System.out.print(L1[j]);

}

}
//System.out.println();
return L1;
}

public static int[][] L2(){

int L2[][] = new int[4][4];

for(int i=0;i<4;i++)
{
//System.out.println();

for(int j=0;j<4;j++)
{
L2[j] = 1;
L2[0][1] = 0;
L2[1][1] = 0;
L2[2][1] = 0;
L2[2][0] = 0;

//System.out.print(L2[j]);

}

}
//System.out.println();
return L2;
}

public static int[][] L3(){

int L3[][] = new int[4][4];

for(int i=0;i<4;i++)
{
//System.out.println();

for(int j=0;j<4;j++)
{
L3[j] = 1;
L3[1][0] = 0;
L3[1][1] = 0;
L3[1][2] = 0;
L3[0][2] = 0;

//System.out.print(L3[j]);

}

}
//System.out.println();
return L3;
}

public static int[][] L4(){

int L4[][] = new int[4][4];

for(int i=0;i<4;i++)
{
//System.out.println();

for(int j=0;j<4;j++)
{
L4[j] = 1;
L4[0][0] = 0;
L4[0][1] = 0;
L4[0][2] = 0;
L4[1][0] = 0;

//System.out.print(L4[j]);

}

}
//System.out.println();
return L4;
}

public static int[][] Cyclops1(){

int Cyclops1[][] = new int[4][4];

for(int i=0;i<4;i++)
{
//System.out.println();

for(int j=0;j<4;j++)
{
Cyclops1[j] = 1;
Cyclops1[1][0] = 0;
Cyclops1[1][1] = 0;
Cyclops1[1][2] = 0;
Cyclops1[1][3] = 0;
Cyclops1[0][1] = 0;
Cyclops1[0][2] = 0;

//System.out.print(Cyclops1[j]);

}

}
//System.out.println();
return Cyclops1;
}

public static int[][] Cyclops2(){

int Cyclops2[][] = new int[4][4];

for(int i=0;i<4;i++)
{
//System.out.println();

for(int j=0;j<4;j++)
{
Cyclops2[j] = 1;
Cyclops2[0][0] = 0;
Cyclops2[0][1] = 0;
Cyclops2[0][2] = 0;
Cyclops2[0][3] = 0;
Cyclops2[1][1] = 0;
Cyclops2[1][2] = 0;

//System.out.print(Cyclops2[j]);

}

}
//System.out.println();
return Cyclops2;
}

public static int[][] Cyclops3(){

int Cyclops3[][] = new int[4][4];

for(int i=0;i<4;i++)
{
//System.out.println();

for(int j=0;j<4;j++)
{
Cyclops3[j] = 1;
Cyclops3[0][1] = 0;
Cyclops3[1][1] = 0;
Cyclops3[2][1] = 0;
Cyclops3[3][1] = 0;
Cyclops3[1][0] = 0;
Cyclops3[2][0] = 0;

//System.out.print(Cyclops3[j]);

}

}
//System.out.println();
return Cyclops3;
}

public static int[][] Cyclops4(){

int Cyclops4[][] = new int[4][4];

for(int i=0;i<4;i++)
{
//System.out.println();

for(int j=0;j<4;j++)
{
Cyclops4[j] = 1;
Cyclops4[0][0] = 0;
Cyclops4[1][0] = 0;
Cyclops4[2][0] = 0;
Cyclops4[3][0] = 0;
Cyclops4[1][1] = 0;
Cyclops4[2][1] = 0;

//System.out.print(Cyclops4[j]);

}

}
//System.out.println();
return Cyclops4;
}

}



  Re: Genetic Algorithm Help  crwood at 06:17 on Saturday, November 26, 2005
 

You initialize the indices for the board array with statements like these

int xCoord1 = generator.nextInt(7);
int yCoord1 = generator.nextInt(7);

which assigns a random int in the range 0 <= x <= 7.
Then you increment the indices in statements like these

board[xCoord1][yCoord1] = shapeUse[0][0];
board[xCoord1][yCoord1 + 1] = shapeUse[0][1];
board[xCoord1][yCoord1 + 2] = shapeUse[0][2];
board[xCoord1][yCoord1 + 3] = shapeUse[0][3];

If an index is close to 7 the increment (1, 2, 3) will send it out of bounds.








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: sorting and Linked list
•  Re: need help linked list
•  Re: Help with arrays
•  Re: Reading from a file
•  Re: Why Use Method?
•  Re: Help with a simple program
•  Re: need help with quiz
•  Re: Help with filesystem object & displaying in a table
•  Re: Genetic Algorithm Help


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005