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

Tiramon • 2 years ago

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

Sablier • 2 years ago

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

[CG]jupoulton • 2 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 • 2 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.

nicola • 2 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 • 1 year 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 • 1 year 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.

punxxy • 2 years ago

Great work! Thanks for sharing your knowledge.

chossette • 2 years ago

I agree, thanks !

C_Winikka • 2 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 !']
['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 solution
OR
# TODO: print each chromosome from answers

zuko • 2 months ago

Thank you for comment. That was my problem

lD • 2 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 • 2 years ago

Yes, this is on our todo list ;)

gf3741 • 2 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...)

Anonymous • 1 year 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 months ago

looks like your chromlist actually a string

dhmoclex • 2 years ago

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

It's really a very nice introduction.

Sylver-Phoenix • 2 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 • 2 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 • 2 years ago

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

SoraGtg • 3 months 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 • 4 months 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 • 1 year ago

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

thefifthrider • 1 year ago

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

Orc_Peon • 1 year 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 • 1 year ago

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

Gopi Shankar • 2 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 ):
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.

Dario Vélez • 1 year 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 • 2 years ago

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

My code:

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

Dario Vélez • 1 year ago

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

dezigo • 2 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 • 2 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 • 2 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 • 2 years ago

Great Playground :D

Rami Awar • 2 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 • 2 years ago

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

John_Slow • 2 years ago

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

Niklex • 2 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 • 2 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 • 2 years ago

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

Sablier • 2 years ago

Peraps you never mutate the gene for this letter…?

Maxlaxs • 2 years ago

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

Alos • 2 years ago

print(answers[0])

Maxlaxs • 2 years ago

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

Alos • 2 years ago

hi,
my code for the "encoding" and "tools" parts seems correct (all parts are green) but I have trouble with the algorithm, witch didn't provide solution in due time.
have you some clue about population_size and number of children to generate at each turn ?
Another point: is it possible to show the execution console when algorithm is running ?

Alos • 2 years ago

ok, as usual I had a bug in my code ... (infinite loop)
All is good now ! thank you Sablier for this very educational exercise !

Daniel Sánchez Suárez • 10 months ago

In the first question of the second page (tools) I get this two strings as inputs:
Key: QweJWLgWLIhdvkwyHouO
Chrom: wedfyIXpkKdZJoGYKYaF
This two don't match in a single position but the test is still telling me is expecting a 1 as result.

Daniel Sánchez Suárez • 10 months ago

Forget about it. Didn't read the "a value between 0 and 1" part of the problem https://media2.giphy.com/me...