We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.
You should mention somewhere which language you use for the coding examples (in encoding and tools) ... i guess it's python ..
True ! I'll update it :) it was intented to go in the about first, but since it desappear, I forgot about that... :)
Thanks for sharing it! It was quite difficult for me as i never use python, but still feasable. I don't know if the platform allows it, but it might be great if members of the community could improve others courses by "translating" exercises to other langages
Yes, this is on our todo list ;)
Agree with the part "translation of the exercises in another language" :-)
By the way, very interesting "tutorial" (which I have not finished yet...)
The 20% and 30% choice of the population is confusing.You might write something like:You must choose 50% of the population.* the best 30%,* the other 20% must be chosen amongst the worst 70%.
Does this still work ? I think the chromosome selection unit test is busted.
Initial chromosomes list : [nRLTPNabIglNSDsPkbBY, KVlwiRFaZdAYgWrSQGcM, grqFWbJtJpBEfbEZavSt, gVqyFBNMFrxJVYKhrcoa, SMQusibGODXYTteMkxPF, zlIMmIhIDmOmyDqrJwYs, vZqhviNbCEuzJEtswvqV, LDTnPNuaVdnSyrMpllIe, rhvNDnUIKHbfpUFntAtX, jmmqRZgmfcszPtXxusSG]Chromosomes scores : [0.35, 0.0, 0.25, 0.1, 0.05, 0.45, 0.05, 0.05, 0.05, 0.0]0.3 favorites : [zlIMmIhIDmOmyDqrJwYs, nRLTPNabIglNSDsPkbBY, grqFWbJtJpBEfbEZavSt]F======================================================================FAIL: test_selection (tools_tests.SelectionTest)----------------------------------------------------------------------Traceback (most recent call last): File "/project/target/tools_tests.py", line 47, in test_selection self.assertIn(x, blob, "Creation of new chromosoms is forbidden")AssertionError: 'zlIMmIhIDmOmyDqrJwYs' not found in ['KVlwiRFaZdAYgWrSQGcM', 'gVqyFBNMFrxJVYKhrcoa', 'SMQusibGODXYTteMkxPF', 'vZqhviNbCEuzJEtswvqV', 'LDTnPNuaVdnSyrMpllIe', 'rhvNDnUIKHbfpUFntAtX', 'jmmqRZgmfcszPtXxusSG'] : Creation of new chromosoms is forbidden
I have the same problem. I made sure I am returning the right list (printed the list in each step and checked) but i keep haveing the same error message. Please can someone else confirm it is not working properly? Or am I just really dumb? :D Because the list in the error message is clearly not the list I am returning from the function.
Probably the same issue I had... The code stub already contained the line return  so I foolishly entered the name of the list to be returned inside the brackets like so return [selection] thus returning a list within a list, instead of the correct which was return selection
This is really cool. I'm a little ashamed I didn't manage to find a solution in the final exercise =(
EDIT: I understood my mistake: I never kept any parents from one generation to the next (I thought incest would be bad)
Yes, a really good individual have a higher probability to stay in our population. In the evolution theory, an individual who is well adapted to his environment will have a longer life, and his genetic material will contribute more to the population.
Great work! Thanks for sharing your knowledge.
I agree, thanks !
First of all, thanks for giving me an interesting look at genetic algorithms. I teach computer science at the high school level, and I had all my students do the Hour of Code, so it was fun to have one more at my level.
Secondly, in case someone has the same trouble I had, I thought I might post the trouble I had on algorithm.py.
It took me a long time to figure out how to pass the tests even though the algorithm was producing the correct solutions.
The output looked like so:['Well done !']['This sentence is harder.']['You REALLY understood GENETIC ALGORITHMS !! Congratulations !']['jNxbthOwkccnaiqpfooJmxfIBqZNuZJYMouIbHLbDRFAcaZhgExowygcfNonxmNUGYdITZJQXnqxgAEZHkaljGHGadgAxRIWArGV']
I tried returning the answers (instead of printing them), casting each correct chromosome as a string before appending it to answers, but it wasn't until I used a for loop to print out each chromosome in answers separately.
I recommend a change to the final comment in algorithm.py: "# TODO: print the solution" I think it should either state: # TODO: use a for loop to print the solutionOR # TODO: print each chromosome from answers
Thank you for comment. That was my problem
Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/ddt.py", line 139, in wrapper return func(self, *args, **kwargs) File "/project/target/algorithm_test.py", line 41, in test_algorithm with capture(algorithm.algorithm) as output: File "/usr/local/lib/python3.6/contextlib.py", line 82, in __enter__ return next(self.gen) File "/project/target/algorithm_test.py", line 15, in capture command(*args, **kwargs) File "/project/target/algorithm.py", line 54, in algorithm population = generation(population) File "/project/target/algorithm.py", line 20, in generation select = selection(population) File "/project/target/tools.py", line 12, in selection chromlist.sort(key = score, reverse = True)AttributeError: 'str' object has no attribute 'sort'
I got this error in algorithm what's the problem .I don't have any idea anyone suggest pls
looks like your chromlist actually a string
I loved this playground :D !!Thank you very much :D !!
It's really a very nice introduction.
Hi, Very nice introduction :) By the way, is it normal that I obtain rally better result when I apply mutation to every of the children and not for only 0.1 to 1% of them as written in the Algorithm part ? (Or is it the probability for each letter to be modified ?)
Thank you :)The choice of the parameters in Genetic Algorithms is really problem-specific. A high mutation rate will explore the search area, but if it is too high, our algorithm will act like a random walk. A small mutation rate will tend to refine locally, but a too small rate will make you stuck in a local extremum.In our problem, the goal, at the gene-level, is to be exactly a X in the nth position, so a higher mutation rate will make this goal to be achieve more quickly locally.For some problems, an approach can be to have adaptative mutation and crossover probabilities over time.
Also observed, got result with 20%... might depend on the population size, and other factor
This is the result I get when using the mutation function of excercise 2. However, sometimes it exceeds the time limit at test #3 or #4:population: 200probability: 10%generation_strategy: pick two random numbers between 0 and n at each iterarion. repeat until len(select) children are made.Iterations test #1: 4363Iterations test #2: 12714Iterations test #3: 37514Iterations test #4 : 38209Elapsed time: 14.861s.
population: 200probability: 10%generation_strategy: pick two random numbers between 0 and n at each iterarion. repeat until len(select) children are made.Iterations test #1: 4363Iterations test #2: 12714Iterations test #3: 37514Iterations test #4 : 38209Elapsed time: 14.861s.
I get better results if I use the built-in mutation function. Is it possible that a different random distribution is used ?
For me, all exercises are already solved. Is this intentional?
AssertionError: False is not true : This chromosome is not a solution
----------------------------------------------------------------------Ran 4 tests in 9.118s
I am getting failures but the console is printing the sentence okay-ish I guess.
I have one doubt: how can we initialize random population of genetic algorithm with TSP,.?
Very well explained! Thank you. The step-by-step progression and the final code was great.
I'm getting "An internal error occurred. Try again or contact codersHS@tech.io for help" after running my code, did anyone have a similar issue and solved it?
thank you for reporting it. We'll look into it as soon as possible
I appreciated the lesson, it was very educative and well presented. Only one thing, I've been pulling out my hair because of that instruction : Mutation Probability : from 0.1% to 1% Each child have a chance to have a randomly modified gene thanks to the mutation(chromosome) function.
so basically I understood this as : for each child, there is between 1 and 0.1 % chance to mutate which I wanted to implement this way : mutationProbability = random.randint(100,1000) if(random.randint(1,mutationProbability) == 1): child = mutation(child)
but the thing is that if you do that then this will hugely impact the score evolution (basically, starting at some point, it won't budge unless there's a mutation and 1 in a hundred or in a thousand is getting rare). Therefore, the algorithm is not able to provide the solution within the time limit if you actually satisfy to that direction. I suggest that it either be removed or at least edited so that it fits with the tests expectations (like changing the mutation frequency to 10% eg)
Or is it that I misunderstood something ?
Hi, I was wondering if I wanted to make an automatic timetable generator how would I encode the information that I will need to mutate, for example: classroom, subject etc..? Help would be appreciated!!
Your tags contain only python. You should include tags like MachineLearning.
Does this code still working? Never get errors like this before. Check this one: value = random.choice(score_values)score_values.remove(value) and got this error - list.remove(x) - x not in list !? So I can pick value from list which actually is not in list. Very bad, lost time for nothing. Other error vas values = d.values(), then values.remove(x) error: dict_values object do not have method remove !? d.values() should return regular python list not a dict object. Totally broken, do not try this, you'll lose time and get headeche :(
I've *thoroughly* enjoyed this playground. Thank you Sablier for creating it.
And to anyone who gets stuck debugging algorithm.py for hours like me.. keep at it! I've doubted in many things during the debugging process; including my CPU and my sanity. Turned out it was edge cases in different functions from earlier chapters.
Also, if I hadn't read comments I'm not sure if I'd realize that population_size could be as small as 10! I had started at.. 2000. Hahahaha!
Thanks a lot, great tutorial! Very interesting and quite fun and satisfying to see the score increase at the end.
For the Chromosome selection I am using the following logic
best_chrom = math.ceil(GRADED_RETAIN_PERCENT*len(temp)) rand_chrom = math.ceil(NONGRADED_RETAIN_PERCENT*len(temp)) selected_chrom, left = temp[0:best_chrom], temp[best_chrom:] for i in range(1,rand_chrom ): selected_chrom.append(random.choice(left))
whats wrong in the selected_chrom list? Here I am taking temp as the sorted chromosome. Can't figure out the problem with the logic.
tienes que considerar que una vez que has seleccionado el 30% de los mejores, te queda el 70% , de ese 70% escoge el 20% de forma aleatoria, y el vector resultante se lo envia por return.
Hey,I try to solve first example, but test looks like by broken.
how=0for a in range(len(chrom)): if chrom[a]==key[a]: how +=1return how
key = get_answer() s = 0 for i in range(len(chrom)): if chrom[i]== key[i]: s+=1 return s/len(chrom)
Tests are broken.I have data: chrom: QweJWLgWLIhdvkwyHouO solution : QweJWLgWLIhdvkwyHouO
The answer should be : 20, not 1.
self.assertTrue(fitting.get_score("QweJWLgWLIhdvkwyHouO") == 1, "A solution to the problem should have a score of 1")
ok, i've fixed it.
from answer import get_answer
def get_score(chrom): solution = get_answer()
print (" chrom: "+ chrom) print (" solution : " + solution) matchedScore = 0 i = 0
for a in chrom: if (solution[i] == a): matchedScore += 1 i += 1
if (matchedScore > 0): matchedScore = matchedScore / i
print ("\n") # TODO: implement the scoring function # * compare the chromosome with the solution (how many character are in the correct position?) return matchedScore
The chromosome selection "error checker" can be tricked with the following code inside the selection function....
for chrom in chromosomes_list: i_score = score(chrom) chrom_dict[i_score] = chrom
dict_scores =  dict_chroms = 
dict_scores = list(chrom_dict.keys()) dict_chroms = list(chrom_dict.values())
#Must be array of chroms return dict_chroms
Great Playground :D
Hi there Sablier, i wrote the code in C++, so if theres the option to provide 2 language implementations ill be happy to post mine!
i always receive an 'F' start the correct gene, how fix this?
This is really cool stuff you have here. Thanks a lot for this !
Really great intro! In the pre-last task, I was wondering what is the mathematically best number for pop_size? Does anybody have any ideas?
P.S. If it's more than 17-20, it gets a bad results. But I'm not not sure what's the best within range of (7-20)
The influence of population size is quite surprising :- 5 : no solution in due time- 10 : Ran 4 tests in 3.987s- 100 : Ran 4 tests in 4.296s- 1000 : Ran 4 tests in 18.324s
Resolution times seems quite stable (+- 25%).
I always get stuck one letter away from the solutions. Maybe someone might know the reason ?
Peraps you never mutate the gene for this letter…?