Hi I have searched all over the internet for this answer and please let me explain before i get into the actual problem. it may seem simple to but the only choices that really helped me was Thread.Sleep and Thread.SpinWait but it basically stops my program during its sleep and spin wait also doesn't allow me to put it a specific time like number of seconds on top of that. I have also tried to use the built in timer but I had no idea how to get/create the callbacktimer or what it was which is needed to create the timer. I also used a simple timer in a while loop (below) in my other projects which worked for a countdown or count up so I tried it here too but it didn't work here
.
public bool timerDone(GameTime gt)
{
float timer = 0;
while (timer < 0.5f)
{
timer += (float)gt.ElapsedGameTime.TotalSeconds;
}
return true;
}
Now here's my actual problem which I've had for over 3 weeks now and have only become more confused by my searching on the internet with google, in forums, on help sites, etc. I am generating several numbers relatively close to one another and I get the same number several times in a row so as stated above I used sleep and wait timer which worked the best but their flaws make me not want to use them or I'm unable to use them correctly. This code below is just one instance of me using a random number in a function which is called in a for loop and getting the same number over and over again.
public Chromosome roulette()
{
int totalChromosomeFitness = 0;
for (int i = 0; i < activePopulation.getChromosomeList().Count; i++)
{
totalChromosomeFitness += activePopulation.getChromosomeList()[i].getChromosomeFitnessValue();
}
Random randFit = new Random();
int selectedFitness = randFit.Next(0, totalChromosomeFitness + 1);
int currentFitness = 0;
Chromosome selectedChromosome = activePopulation.getChromosomeList()[0];
for (int i = 0; i < activePopulation.getChromosomeList().Count; i++)
{
if (i < activePopulation.getChromosomeList().Count - 1)
{
if (currentFitness <= selectedFitness && (currentFitness + activePopulation.getChromosomeList()[i + 1].getChromosomeFitnessValue()) >= selectedFitness)
{
Debug.WriteLine("Gen: " + (generationNumber - 1) + " i: " + i);
selectedChromosome = activePopulation.getChromosomeList()[i];
return selectedChromosome;
}
}
else
{
Debug.WriteLine("Gen: " + (generationNumber - 1) + " i: " + i);
selectedChromosome = activePopulation.getChromosomeList()[i];
return selectedChromosome;
}
currentFitness += activePopulation.getChromosomeList()[i].getChromosomeFitnessValue();
}
return selectedChromosome;
}
This is where I'm calling the roulette function.
for (int i = 0; i < (activePopulation.getMaxChromosomes() - 2); i += 1)
{
//Chromosome mom = activePopulation.getChromosomeList()[i];
//Chromosome dad = activePopulation.getChromosomeList()[i + 1];
//newChromosome.Add(mom);
//newChromosome.Add(dad);
Thread.Sleep(500);
Chromosome mom = roulette();
Thread.Sleep(500);
Chromosome dad = roulette();
Thread.Sleep(500);
Chromosome baby1 = Crossover(dad, mom, gt);
newChromosome.Add(baby1);
//Thread.Sleep(500);
//Chromosome baby2 = Crossover(mom, dad, gt);
//newChromosome.Add(baby2);
}
As you can see this makes it so it will generate new numbers it unfortunately makes it so while it's sleeping the other areas of my program like the drawing function won't change as well (which I need it to) but the timer I used did count get to the proper value but didn't actually stop or delay when the random value is created (which I thought the while loop would help out with that a bit). I am aware of how random is time dependent and this is a side effect of said dependence. Also I've heard the term seed it a lot in the sites I visited searching for this solution and I sort of understand by seeding my random or something I can get it to change the value of the random.
So how can I get this to work the way it needs to without stopping the rest of my program (like how some languages or engines have a wait timer that acts as a delay for this part of the program only like unity's waitforseconds)?
I am sorry if my wording is weird I'm just having a lot of trouble with this and it's like 2:30am here and it's hard for me to sleep with this issue.
Aucun commentaire:
Enregistrer un commentaire