Skip to content

Revision on genetic algorithm implementation#16

Open
pupupulp wants to merge 4 commits into
KonradLinkowski:masterfrom
pupupulp:issue-3-genetic-algorithm
Open

Revision on genetic algorithm implementation#16
pupupulp wants to merge 4 commits into
KonradLinkowski:masterfrom
pupupulp:issue-3-genetic-algorithm

Conversation

@pupupulp
Copy link
Copy Markdown

@pupupulp pupupulp commented Oct 9, 2020

Hi @KonradLinkowski ,

I have implemented a different approach for genetic algorithm in this pull request.

Here is a screenshot of the sample output:
image

To summarize the change, I have implemented the following:

  • class for handling image population (ImageSamplePopulation)
    • creation of images (population) via constructor which instantiates the ImageSample based on population size
    • adopted the sort fitness implementation
    • adopted drawBest implementation but renamed to showGeneration
    • implemented a populate function which handles the following:
      • backup of old image samples
      • triggering of image crossover
      • triggering of mutation
      • sorting of fitness
      • checking of best image samples
      • re-population for next generation
  • class for handling an image (ImageSample)
    • randomization of an image if no old image is passed
    • adopted the random color, int, float implementation
    • adopted the draw circle, shape implementation
    • implemented a mutate function which re-randomizes the image if the mutation rate is satisfied (may be implemented another way I guess)
    • implemented a crossover which produces 2 offspring images which were added back to the population via splice
    • adopted the compute fitness implementation

All other implementations are retained, except for the drawLoop and the requestAnimationFrame.

For tweaking these are the change-able values:

  • mutationRate (success threshold for mutation)
  • shapeRate (success threshold for randomization of shapes)
  • crossoverRate (threshold for image blending during crossovers)
  • parentsMutationRate (currently computed by averaging)
  • parentsShapeRate (currently computed by averaging)
  • offspring
    • MutationRate
    • shapeRate
  • bestParent (currently set to index 0)
  • randomParent (randomization for choosing a parent partner)
  • crossoverChance (success threshold for randomization of parent partner, otherwise randomParent is set to index 1)

I've added this as a another file, genetic.js, so that you can test it without overwriting the previous genetic algorithm implementation.

Hope this helps solve the problem :D :D :D

@KonradLinkowski
Copy link
Copy Markdown
Owner

Thanks for the PR. I will look at this in a free time

@pupupulp
Copy link
Copy Markdown
Author

Thank you @KonradLinkowski , hope this fits with the project :D :D :D

@pupupulp
Copy link
Copy Markdown
Author

Hi @KonradLinkowski ,

I've revised some parts of the genetic algorithm, I think it is still covered by this pull request though.

The fastest I have is at 2 minutes span here is the result already:
image

However, here is the greatest image match I have achieved after leaving it for a while now:
image

Additionally, based on my observations such a match really depends on the rates (the variables used for tweaking) provided because sometimes once a mutation happens the image drops to a lower match percentage or it slows down the increase of the match percentage. This means that the 2 mins span I stated above to achieve a 90% match percentage may differ from time to time depending on how the population reacts.

By the way, this project is so fun! It gives that exciting feeling when the image generated is so close to the original. :D :D :D

@KonradLinkowski
Copy link
Copy Markdown
Owner

Does it work? I'm asking because when I swapped main.js width genetic.js app breakes.

@pupupulp
Copy link
Copy Markdown
Author

Hi,

In what way aspect does it break so that I can cheeck :D :D :D
Is it unable to generate an image?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants