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:
  Creating a Fitness Function, Need Help ASAP!  behrk2 at 17:46 on Wednesday, October 26, 2005
 

--------------------------------------------------------------------------------

Suppose you have an altered chess board:

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

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

Now suppose you have four tetris shapes, where each shape contains only four 'units'

I need to create a fitness function where all four of the shapes are continuously and randomly added onto the board until no additional shapes can fit. The fitness function should return the # of spaces filled divided by the total number of spaces.

(Without using a GUI).

I was told to do this in such a way where each shape has the function:

f (shape, x coord., y coord., orientation) x 10 (So each shape has 4 ints)

Then, I was told to, using a 1-Dimensional Array of size 40 ints, add the shapes into the array for ten different iterations. For each iteration, the x and y coordinates as well as the orientation of each shape should be random.

After each iteration, the number of spaces filled should be printed.

I have the idea down, but have NO idea how to code this. Remember, it should return the # of spaces filled over the total number of spaces. Please help me! I have no idea how to represent the shapes, their coordinates, and their orientations. Thanks!

(This has to be done in java.)

  Re: Creating a Fitness Function, Need Help ASAP!  crwood at 03:14 on Thursday, October 27, 2005
 


import java.util.Random;

public class Tetris
{
Random seed;
char[][] grid;
char empty, filled, block;
int[] buffer;
final int
FLAT = 0,
VERT = 1,
TO_SE = 2,
TO_SW = 3;

public Tetris()
{
seed = new Random();
empty = '0';
filled = '*';
block = '1';
initGrid();
buffer = new int[40];
}

private void makeShapes()
{
int count = 0;
while(getEmptySpaces() > 0)
{
for(int j = 0; j < 10; j++)
{
int orientation = seed.nextInt(4);
int limit = grid.length * grid[0].length;
switch(orientation)
{
case FLAT:
limit -= 4;
break;
case VERT:
limit -= 3 * grid[0].length - 1;
break;
case TO_SE:
limit -= 3 * grid[0].length + 3;
break;
case TO_SW:
limit -= 3 * grid[0].length;
}
int pos = seed.nextInt(limit+1);
int y = pos / grid.length;
int x = pos - y * grid[0].length;
int[] shape = getShape(x, y, orientation);
System.arraycopy(shape, 0, buffer, j*shape.length, shape.length);
}
System.out.println("filled spaces = " + getNewSpacesFilled());
updateGrid();
count++;
}
printGrid();
int emptySpaces = getEmptySpaces();
System.out.println("emptySpaces = " + emptySpaces +
" after " + count + " iterations");
}

private int[] getShape(int x, int y, int orientation)
{
int start = x + y * grid.length;
int index = 0;
int[] shape = new int[4];
switch(orientation)
{
case FLAT:
for(int j = 0; j < grid.length; j++)
{
for(int k = 0; k < grid[0].length; k++)
{
int loc = k + j * grid.length;
if(loc >= start && index < 4)
shape[index++] = loc;
}
}
break;
case VERT:
for(int j = 0; j < grid.length; j++)
{
for(int k = 0; k < grid[0].length; k++)
{
int loc = k + j * grid.length;
if(loc >= start && k == x && index < 4)
shape[index++] = loc;
}
}
break;
case TO_SE:
for(int j = 0; j < grid.length; j++)
{
for(int k = 0; k < grid[0].length; k++)
{
int loc = k + j * grid.length;
int inc = index * grid[0].length + index;
if(loc == start + inc && index < 4)
shape[index++] = loc;
}
}
break;
case TO_SW:
for(int j = 0; j < grid.length; j++)
{
for(int k = 0; k < grid[0].length; k++)
{
int loc = k + j * grid.length;
int inc = index * grid[0].length - index;
if(loc == start + inc && index < 4)
shape[index++] = loc;
}
}
}
return shape;
}

private boolean contains(int target, int[] domain)
{
for(int j = 0; j < domain.length; j++)
if(domain[j] == target)
return true;
return false;
}

private int getNewSpacesFilled()
{
int count = 0;
for(int j = 0; j < grid.length; j++)
for(int k = 0; k < grid[0].length; k++)
{
int pos = k + j * grid[0].length;
if(grid[j][k] == empty && contains(pos, buffer))
count++;
}
return count;
}

private void updateGrid()
{
for(int j = 0; j < grid.length; j++)
for(int k = 0; k < grid[0].length; k++)
{
int pos = k + j * grid[0].length;
if(grid[j][k] == empty && contains(pos, buffer))
grid[j][k] = filled;
}
}

private int getEmptySpaces()
{
int total = 0;
for(int j = 0; j < grid.length; j++)
for(int k = 0; k < grid[0].length; k++)
if(grid[j][k] == empty)
total++;
return total;
}

private void printGrid()
{
for(int j = 0; j < grid.length; j++)
{
for(int k = 0; k < grid[0].length; k++)
System.out.print(" " + grid[j][k] + " ");
System.out.println();
}
}

private void initGrid()
{
grid = new char[8][8];
for(int j = 0; j < grid.length; j++)
for(int k = 0; k < grid[0].length; k++)
{
if(k >= grid[0].length/2 + j)
grid[j][k] = block;
else
grid[j][k] = empty;
}
}

public static void main(String[] args)
{
Tetris t = new Tetris();
t.printGrid();
t.makeShapes();
}
}


  Re: Creating a Fitness Function, Need Help ASAP!  behrk2 at 03:35 on Thursday, October 27, 2005
 

Wow, thanks so much...

What you did is really good. Can I credit you in anyway?

  Re: Creating a Fitness Function, Need Help ASAP!  crwood at 09:05 on Thursday, October 27, 2005
 

Only if you want to. The code is a gift to you.








CodeToad Experts

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








Recent Forum Threads
•  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
•  Re: How to make an investment calculator
•  Re: line breaks in GUI
•  Re: Graph in Gui...
•  Graph in Gui...


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