We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Tiramon • 4 years ago

You should mention somewhere which language you use for the coding examples (in encoding and tools) ... i guess it's python ..

Sablier • 4 years ago

True ! I'll update it :) it was intented to go in the about first, but since it desappear, I forgot about that... :)

lD • 4 years ago

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

Jerome Cance • 4 years ago

Yes, this is on our todo list ;)

gf3741 • 4 years ago

Agree with the part "translation of the exercises in another language" :-)

By the way, very interesting "tutorial" (which I have not finished yet...)

nicola • 4 years ago

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%.

gamoudis • 3 years ago

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

Andrew • 3 years ago

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.

4candles • 1 year ago

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

[CG]jupoulton • 4 years ago

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)

Sablier • 4 years ago

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.

punxxy • 4 years ago

Great work! Thanks for sharing your knowledge.

chossette • 4 years ago

I agree, thanks !

C_Winikka • 4 years ago

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 !']

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 solution
# TODO: print each chromosome from answers

zuko • 2 years ago

Thank you for comment. That was my problem

AngaAnandh • 3 years ago

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

zuko • 2 years ago

looks like your chromlist actually a string

dhmoclex • 4 years ago

I loved this playground :D !!
Thank you very much :D !!

It's really a very nice introduction.

Sylver-Phoenix • 4 years ago

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 ?)

Sablier • 4 years ago

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.

chossette • 4 years ago

Also observed, got result with 20%... might depend on the population size, and other factor

Anonymous • 1 month ago

I have one doubt: how can we initialize random population of genetic algorithm with TSP,.?

Xosaci • 9 months ago

Very well explained! Thank you. The step-by-step progression and the final code was great.

3ataja • 1 year ago

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?

[CG]Thibaud • 1 year ago

thank you for reporting it. We'll look into it as soon as possible

محمد • 1 year ago

thank you!

SoraGtg • 2 years ago

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 ?

suzanne • 2 years ago

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!!

ozgur • 3 years ago

Your tags contain only python. You should include tags like MachineLearning.

thefifthrider • 3 years ago

Does this code still working? Never get errors like this before. Check this one:
value = random.choice(score_values)
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
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 :(

Orc_Peon • 3 years ago

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!

matleg • 4 years ago

Thanks a lot, great tutorial! Very interesting and quite fun and satisfying to see the score increase at the end.

Gopi Shankar • 4 years ago

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 ):

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.

Dario Vélez • 3 years ago

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.

Winter_not_comming • 4 years ago

I try to solve first example, but test looks like by broken.

My code:

for a in range(len(chrom)):
if chrom[a]==key[a]:
how +=1
return how

Dario Vélez • 3 years ago

key = get_answer()
s = 0
for i in range(len(chrom)):
if chrom[i]== key[i]:
return s/len(chrom)

dezigo • 4 years ago

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")

dezigo • 4 years ago

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 (matchedScore)

print ("\n")
# TODO: implement the scoring function
# * compare the chromosome with the solution (how many character are in the correct position?)
return matchedScore

fattyAcid • 4 years ago

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

Crysaac • 4 years ago

Great Playground :D

Rami Awar • 4 years ago

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!

jvmaiaf • 4 years ago

i always receive an 'F' start the correct gene, how fix this?

John_Slow • 4 years ago

This is really cool stuff you have here. Thanks a lot for this !

Niklex • 4 years ago

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)

dhmoclex • 4 years ago

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%).

Jaime • 4 years ago

I always get stuck one letter away from the solutions. Maybe someone might know the reason ?

Sablier • 4 years ago

Peraps you never mutate the gene for this letter…?

Maxlaxs • 4 years ago

Can someone help me i always get the 4th test wrong? All other is right.

Alos • 4 years ago


Maxlaxs • 4 years ago

Nvm printed the answer like a list not a string :/