I am learning C# to implement Genetic Algorithm. From several execution, I found that the random number did not produce good result, that is the random number is not uniformly distributed.
Here is my code:
class Individual
{
private int[,] myInd;
public Individual()
{
//
}
public Individual(int numberOfChromosomes, int numberOfJobs, int numberOfMachines)
{
Random rand = new Random();
this.myInd = new int[numberOfChromosomes, numberOfJobs];
int machineid;
int something;
for (int i = 0; i < numberOfChromosomes; i++)
{
int[] dummy = new int[numberOfJobs];
if (i == 0)
{
for (int j = 0; j < numberOfJobs; j++)
{
dummy[j] = j;
}
dummy = Shuffle<int>(dummy);
}
else if (i == 1)
{
for (int j = 0; j < numberOfChromosomes; j++)
{
//Random rand = new Random();
machineid = rand.Next(numberOfMachines);
dummy[j] = machineid;
}
dummy = Shuffle<int>(dummy);
}
else
{
for (int j = 0; j < numberOfChromosomes; j++)
{
something = rand.Next(10);
dummy[j] = something;
}
dummy = Shuffle<int>(dummy);
}
for (int j = 0; j < numberOfJobs; j++)
{
this.myInd[i, j] = dummy[j];
}
dummy = null;
}
}//End for Constructor
public void Show()
{
int numberOfChromosomes = this.GetMyInd().GetUpperBound(0);
int numberOfJobs = this.GetMyInd().GetUpperBound(1);
for (int i = 0; i <= numberOfChromosomes; i++)
{
for(int j = 0; j <= numberOfJobs; j++)
{
System.Console.Write(this.GetMyInd()[i, j]);
}
System.Console.WriteLine();
}
}
public T[] Shuffle<T>(T[] array)
{
Random rand = new Random();
var random = rand;
for (int i = array.Length; i > 1; i--)
{
// Pick random element to swap.
int j = random.Next(i); // 0 <= j <= i-1
// Swap.
T tmp = array[j];
array[j] = array[i - 1];
array[i - 1] = tmp;
}
return array;
}
public int[,] GetMyInd()
{
return this.myInd;
}
}//End for Individual Class
That zero value occurs more than any other value in one execution. Here is for example.
9360471582
0000002002
0002009009
The second and the third chromosome have a problem with random number. Can you show whats wrong with my code? Thanks. Best regards, Bobby
Aucun commentaire:
Enregistrer un commentaire