dtsn

Hi,

I'm trying to create a GA to solve Sudoku problems. For this i have to create a population, i am using a class to hold the 9 by 9 grid and the fitness of each grid. However when i iterate through my ArrayList which holds the populate i am generating random numbers to fill in all the blank squares. However what happens is that every grid turns out the same. The random number works and is generating random numbers but it creates the same numbers for each grid. Heres my code:

suduko su = new suduko();
su.grid = game;

Random Random = new Random();
int dummy = 0;

for (int i = 0; i < PopSize; i++) pop.Add(su);

// populate population with random numbers

for (int k = 0; k < PopSize; k++)
{
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
dummy = Random.Next(1, 10);
if (((suduko)pop[k]).grid[i, j] == 0) ((suduko)pop[k]).grid[i, j] = dummy;
}
}
//((suduko)pop[k]).Fitness = fitness((suduko)pop[k]);
}

I don't quite know whats going on with the random number becuase i am not creating a new number just calling next every time. By chance does the number repeat itself every 81 times

Thanks

Daniel


Re: Visual C# General Random problems

cutealien

try providing seed! like current time ! but i think default Random constructor takes current time as seed and it shouldn't have done this everytime ...

Random r = new Random(DateTime.Now.TimeOfDay.Seconds);





Re: Visual C# General Random problems

dtsn

Hi,

Thanks for replying, yes by default the random constructor takes time, but to be on the safe side i still try changing the seed but with no luck .

I just don't understand, if i'm not recalling Random, i should always get a different number Even if it's in a massive for loop Not 100 grids all exactly the same

Daniel




Re: Visual C# General Random problems

dtsn

I think that the random isn't my problem. I have just tried the following code:

Code Snippet

foreach (suduko Grid in pop)
{
counter++;
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
dummy = Random.Next(1, 10);
if (Grid.grid[i, j] == 0) Grid.grid[i, j] = dummy;
}
}
Grid.Fitness = fitness(Grid) + counter;
}



What i get is that every fitness of every grid is the same! Which means whatever is being passed to the last grid in my arraylist seems to be applied throughout! Is there anything i'm missing about arraylists

N.B. I have used a foreach loop in the code above where before i used a for loop both give me the same results, but foreach looks neater.

Daniel




Re: Visual C# General Random problems

Matthew Watson

Here is your problem:



suduko su = new suduko(); // Only one 'su' object created
su.grid = game;
for (int i = 0; i < PopSize; i++) pop.Add(su); // Adds the same 'su' object again and again!




You're creating one 'su' object and pointing multiple elements of 'pop' at it.

If you want them to be separate, you have to create a separate object for each element.




Re: Visual C# General Random problems

dtsn

Hi,

Yeah i've just figured that out! However i now have an even stranger problem, my varable which gets passed to the Create() function keeps getting redefined, even though i am not redefining it.

Code Snippet
for (int i = 0; i < PopSize; i++)
{
suduko su = new suduko();
su.grid = game;

for (int j = 0; j < 9; j++)
{
for (int k = 0; k < 9; k++)
{
dummy = Random.Next(1, 10);
if (su.grid[j, k] == 0) su.grid[j, k] = dummy;
}
}
su.Fitness = fitness(su.grid);
pop.Add(su);
}

So for this code i create a suduko grid, which works, however game which holds all the grids default information somehow gets redefined to the last grid. For example if i had a 3x3 grid with a 1 in and all the rest 0's i will call this function it will replace all the 0's with a random number then magically game gets redefined to become this grid.

Am i missing something




Re: Visual C# General Random problems

Matthew Watson

I don't really know, because I can't see the definition of 'game'.

However, note that the line:

su.grid = game;

Will make *every* 'su.grid' reference the same 'game'.

Is that what you intended




Re: Visual C# General Random problems

dtsn

Hi,

Sorry for late reply, i have been on Holiday . Yes every su.grid should refrence game becuase game is the orginal suduko grid, so i need a population made up of 100's of the same grids, then i can randomise the blank numbers to give me the result. However game seems to get redeclared every iteration even though i am not calling it.

Game is defined as a paramater for the function as a [,] array. Surely this should remain constant specially because the function isn't being redeclared It gets redfined every iteration of the for loop

Here's my complete code listing:

Code Snippet


class Population
{
//Declare population constants
int PopSize;
ArrayList pop;

public class suduko
{
public int Fitness = 0;
public int[,] grid = new int[9, 9];
}

public class GridComparer : IComparer
{
int IComparer.Compare(object x, object y)
{
if (((suduko)x).Fitness > ((suduko)y).Fitness) return 1;
else if (((suduko)x).Fitness == ((suduko)y).Fitness) return 0;
else return -1;
}
}

public Population(int PopulationSize)
{
PopSize = PopulationSize;
pop = new ArrayList(PopSize);
}

public void Create(int [,] game)
{
Random Random = new Random();
int dummy = 0;

// populate population with random numbers

for (int i = 0; i < PopSize; i++)
{
suduko su = new suduko();
su.grid = game;

for (int j = 0; j < 9; j++)
{
for (int k = 0; k < 9; k++)
{
dummy = Random.Next(1, 10);
if (su.grid[j, k] == 0) su.grid[j, k] = dummy;
}
}
su.Fitness = fitness(su.grid);
pop.Add(su);
}
evolve(pop);
}

protected int fitness(int[,] Grid)
{
int Columnfitness = 0;
int Rowfitness = 0;
int Squarefitness = 0;

for (int i = 0; i < 9; i++)
{
int[] store = new int[9];
for (int j = 0; j < 9; j++)
{
//Check to see if value is unique
if (store[Grid[i, j]-1] == 0) store[Grid[i, j]-1] = Grid[i, j]; else Columnfitness++;
}
}
for (int i = 0; i < 9; i++)
{
int[] store = new int[9];
for (int j = 0; j < 9; j++)
{
//Check to see if value is unique
if (store[Grid[j, i]-1] == 0) store[Grid[j, i]-1] = Grid[j, i]; else Rowfitness++;
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
int[] store = new int[9];
for (int k = 0; k < 3; k++)
{
for (int l = 0; l < 3; l++)
{
if (store[Grid[k + j * 3, l + i * 3]-1] == 0) store[Grid[k + j * 3, l + i * 3]-1] = Grid[k + j * 3, l + i * 3]; else Squarefitness++;
}
}
}
}

return Columnfitness * Rowfitness * Squarefitness;
}




Thanks for you help!

Daniel

N.B. I might move this to a new thread since it is starting to get off topic.




Re: Visual C# General Random problems

Matthew Watson

First, a question:

How do you create the 'game' array that you pass to Create()
What does the code look like where you call Create()

Next, an observation:

In your class sudoko, you do this:

public int[,] grid = new int[9, 9];

However, where you create a sudoko object, you immediately reassign 'grid'
to a different array:

suduko su = new suduko();
su.grid = game;

If you always do this, then you don't need to initialise 'grid' to an array in the
sudoko class. It will just create an array and then immediately overwrite it,
making the creation of the array a waste of time.