[Caution] I am a beginner for about a week after programming.
It is written as genetic algorithm, but it is not exact and the content is appropriate.
[Overview] Language: Python
Randomly generate 10 lists with 20 numbers from 1 to 1
Determine the list that has 2, 0 most (father) and the list that has the second most (mother)
3, Aggregate elements of father and mother by index and make a list (samples)
4. Randomly extract elements for each index from samples and generate 10 new generations (newgene)
5, loop steps 2-4
The operation so far works without problems.Problems i am experiencing
If i loop several times, all will be in the same list (all 0's will be the same), and the change will stop.
Only in that case (newgene is 1st place in the same ratio) as a mutation, one place of father
Processing to change to a random number (list replacement).
At this time, even though only father is changed, the same change occurs for mother for some reason.
Therefore, mutation has not been realized.
import random # Create 10 lists kid with 20 random numbers from 0 to 9 and list them as kids kid =  kids =  for _ in range (11): kids.append (kid) kid =  for _ in range (20): kid.append (random.randint (0, 9)) kids.pop (0) loop = 1 # -------------------- Loop below ------------------------- # Within about 1000 times, it will be the first place in the same ratio, and the 58th line will not work for _ in range (1000): # Count kids' 0's and list them in each_0_counts each_0_counts =  for x in kids: each_0_counts.append (x.count (0)) # Set max_score to 0 # Premax_score is the next highest number of 0 max_score = max (each_0_counts) premax_score = 0 for x in each_0_counts: if x == max_score: continue elif x>premax_score: premax_score = x else: pass # Processing when the number of 0s is the same (1st place in the same ratio) if premax_score == 0: premax_score = max_score else: pass # Get list number 1 and 2 and decide father and mother father_index = each_0_counts.index (max_score) mother_index = each_0_counts.index (premax_score) father = kids [father_index]mother = kids [mother_index] # Problem area # If the same rate is 1st place, change part of father randomly to avoid stopping the change # At this time, if you print and examine father and mother, it will change to mother for some reason if premax_score == max_score: father [random.randint (0, 19)] = random.randint (0, 9) else: pass parents =  parents.append (father) parents.append (mother) print ("\ nFather:" + str (father)) print ("Mother:" + str (mother)) # list parents elements in samples by index sample =  samples =  for y in range (21): samples.append (sample) sample =  for x in parents: try: sample.append (x [y]) except IndexError: continue samples.pop (0) # Randomly add elements from sample to generate 10 new individuals (newgenes) newgenes =  for _ in range (10): newgenes.append () xx = -1 xy = 0 for _ in range (20): xx + = 1 xy = 0 for _ in range (10): try: add = random.choice (samples [xx]) newgenes [xy] .append (add) xy + = 1 except IndexError: continue print ("No." + str (loop) + "Generation") for x in newgenes: print (str (x)) # Assign newgenes to kids to return to the loop loop + = 1 kids = newgenes
・ I think that there is a problem with father's list replacement, and replace part of the list with father itself instead of replacement.
When I tried assigning a string or an unrelated list, mother was not affected.
In other words, only when trying to replace part of father, there is a bug that mother is also changed.
The cause is unknown.
Answer # 1
Part-time reproduction when you reach first place.
max_score = max (each_0_counts) premax_score = 0 for x in each_0_counts: if x == max_score: continue elif x>premax_score: premax_score = x else: pass # Processing when the number of 0s is the same (1st place in the same ratio) if premax_score == 0: premax_score = max_score # Add comment: same score else: pass # Get list number 1 and 2 and decide father and mother father_index = each_0_counts.index (max_score) # Add comment: same index mother_index = each_0_counts.index (premax_score) # Add comment: same index father = kids [father_index] # Add comment: the same list object mother = kids [mother_index] # Add comment: the same list object
I don't know the whole code so I don't know if this is a good idea, but it's easy to use an empty slice (returning a new list object with the same value) to make it a separate object.
father = kids [father_index] mother = kids [mother_index] [:]
- [abc 142 d disjoint set of common divisors] questions about python logic errors
- [abc 085 d katana thrower] questions about python logic errors
- python - questions about learning with neural network models
- i want to perform ambiguous character replacement in python
- python - questions about how to aggregate using pandas
- python - i want to answer questions automatically in google classroom, but i can't use the api please give me some advice
- python - data frame replacement
- i have some questions in python exercises
- python - input data format when implementing multivariate lstm by keras
- python - i do not understand the cause of the low accuracy rate of image recognition
- python - i'm worried about the direction of studying machine learning
- data scraped by selenium using python cannot be stored in the list just by the for statement
- python - support for'tuple' object is not callable
- python - about "type error: expected ptr for argument'src'"
- python - i want to read mnist with imagedatagenerator
- python - from tensorflowlosses import huber_loss cannot be imported
- python ai lottery winning number prediction lottery 7
- python - there is a delay in the result predicted by keras lstm and i want to improve it