lundi 9 mai 2022

How To Generate Different Set of Numbers for Each Iteration in While Loop

I am working on a genetic algorithm. I am trying to initialize the population with random numbers. These values are then used as input weights for a neural network. The issue is that most of the weights give prediction values of all 1's. So I am only adding the set of weights to the population if they do not give all 1's. To do this, I am using a while loop to generate the weights. The issue is that each iteration through the while loop gives the same weights. Here is the code I believe to be relevant:

while len(final_init_pop[9]) == 0:
        for k in range(36):
            pop_trial.append(random.uniform(-1,1))    
            
        for k in range(12):
            pop_trial.append(random.uniform(-1,1))    
        
        for k in range(2):
            pop_trial.append(random.uniform(-1,1))  

If the random values give a solution that is not all 1's, they will be appended to the final_init_pop list, so the while loops executes while the final_init_pop list is not full.

I hope I explained this clearly. If you need more code let me know and I can provide it.

Update: Other solutions I have tried:

while len(final_init_pop[9]) == 0:
        for k in range(36):
            weights_rand = random.uniform(-1,1)
            pop_trial.append(weights_rand)    
            
        for k in range(12):
            weights_rand = random.uniform(-1,1)
            pop_trial.append(weights_rand)    
        
        for k in range(2):
            weights_rand = random.uniform(-1,1)
            pop_trial.append(weights_rand)    

This gives a different number for each element in the list, but the list is the same for every iteration.

I also tried :

weights_rand = random.uniform(-1,1)
while len(final_init_pop[9]) == 0:
        for k in range(36):
            pop_trial.append(weights_rand)    
            
        for k in range(12):
            pop_trial.append(weights_rand)    
        
        for k in range(2):
            pop_trial.append(weights_rand)    

Which gives the same number for every element in the list and for each iteration (as expected).
I am stumped.




Aucun commentaire:

Enregistrer un commentaire