{"id":7840,"date":"2020-08-14T10:49:47","date_gmt":"2020-08-14T08:49:47","guid":{"rendered":"https:\/\/www.codemotion.com\/magazine\/?p=7840"},"modified":"2021-12-23T12:52:09","modified_gmt":"2021-12-23T11:52:09","slug":"genetic-algorithms","status":"publish","type":"post","link":"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/","title":{"rendered":"Genetic Algorithms: A Developer&#8217;s Perspective"},"content":{"rendered":"\t\t\t\t<div class=\"wp-block-uagb-table-of-contents uagb-toc__align-left uagb-toc__columns-1  uagb-block-d2b74457      \"\n\t\t\t\t\tdata-scroll= \"1\"\n\t\t\t\t\tdata-offset= \"30\"\n\t\t\t\t\tstyle=\"\"\n\t\t\t\t>\n\t\t\t\t<div class=\"uagb-toc__wrap\">\n\t\t\t\t\t\t<div class=\"uagb-toc__title\">\n\t\t\t\t\t\t\tTable Of Contents\t\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<div class=\"uagb-toc__list-wrap \">\n\t\t\t\t\t\t<ol class=\"uagb-toc__list\"><li class=\"uagb-toc__list\"><a href=\"#learning-as-evolving\" class=\"uagb-toc-link__trigger\">Learning as evolving<\/a><ul class=\"uagb-toc__list\"><li class=\"uagb-toc__list\"><a href=\"#convergence-in-revolutionary-algorythms\" class=\"uagb-toc-link__trigger\">Convergence in revolutionary algorythms<\/a><li class=\"uagb-toc__list\"><li class=\"uagb-toc__list\"><a href=\"#the-goal-of-a-genetic-algorithm\" class=\"uagb-toc-link__trigger\">The goal of a genetic algorithm<\/a><\/li><\/ul><\/li><li class=\"uagb-toc__list\"><a href=\"#how-to-implement-genetic-algorithms-in-python\" class=\"uagb-toc-link__trigger\">How to implement genetic algorithms in Python<\/a><li class=\"uagb-toc__list\"><a href=\"#want-to-launch-a-developer-event-despite-the-challenges-of-covid-19\" class=\"uagb-toc-link__trigger\">Want to launch a developer event despite the challenges of  COVID-19?<\/a><\/ul><\/ol>\t\t\t\t\t<\/div>\n\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\n\n\n<p class=\"eplus-F8bC6d\">The life of a <span style=\"user-select: auto;\" id=\"urn:enhancement-85b39e1f\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/software_developer\">developer<\/span> nowadays is a very exciting one: we have several languages, frameworks, and numerous excellent <span style=\"user-select: auto;\" id=\"urn:enhancement-f6da0db8\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/tool\">tools<\/span> available to select from according to our needs.&nbsp; <\/p>\n\n\n\n<p class=\"eplus-geX43Q\">Not only that, we also have a lot of interesting and useful techniques that have been developed to allow us to accomplish purposes far beyond the traditional <span style=\"user-select: auto;\" id=\"urn:enhancement-d2fae02a\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/business\">business<\/span>&#8211; or science-oriented <span style=\"user-select: auto;\" id=\"urn:enhancement-6ae2d279\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computation\">computations<\/span> demanded of <span style=\"user-select: auto;\" id=\"urn:enhancement-dad0931d\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/software_developer_2\">programmer<\/span>-developed <span style=\"user-select: auto;\" id=\"urn:enhancement-5f82d829\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computer_program\">programs<\/span>.<\/p>\n\n\n\n<p class=\"eplus-9VcLHP\">For example, in our <span id=\"urn:enhancement-c1e6a1e3\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/web_application\">Web applications<\/span> we want nice <span id=\"urn:enhancement-55710887\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/responsiveness\">responsive<\/span> <span id=\"urn:enhancement-d50dbce0\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/graphical_user_interface\">GUIs<\/span>, <span id=\"urn:enhancement-c56e41a1\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/system_integration\">integration<\/span> with identity and access <span id=\"urn:enhancement-3fd857d\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/management_system\">management systems<\/span>, no <span id=\"urn:enhancement-1395b9a1\" class=\"textannotation disambiguated wl-creative-work\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/sql\">SQL<\/span> databases to properly store <span id=\"urn:enhancement-22279330\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/unstructured_data\">unstructured<\/span> <span id=\"urn:enhancement-c1638c55\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/data\">data<\/span> downloaded from the <span id=\"urn:enhancement-95eb2f33\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/internet\">Internet<\/span>, etc. etc.<\/p>\n\n\n\n<p class=\"eplus-VEV0Rf\">Time and again, our <span id=\"urn:enhancement-73e56442\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/customer\">customers<\/span> require a <span id=\"urn:enhancement-6b37c219\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/class_computer_programming\">class<\/span> of techniques and algorithms relating to <a href=\"https:\/\/www.codemotion.com\/magazine\/dev-hub\/machine-learning-dev\/artificial-intelligence-the-new-electricity\/\">Artificial Intelligence (AI)<\/a>. From games to <span id=\"urn:enhancement-c944403a\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/search_algorithm\">search<\/span> engines, from service desks to e-commerce sites, <span id=\"urn:enhancement-d923930a\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/artificial_intelligence\">AI<\/span> seems to be ubiquitous and a necessary <span id=\"urn:enhancement-e7e8119c\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/tool\">tool<\/span> for any <span id=\"urn:enhancement-b37839fc\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/software_developer\">developer<\/span> to know, or better still, know how to use. <\/p>\n\n\n\n<p class=\"eplus-IW6vKJ\">Not surprisingly, <span style=\"user-select: auto;\" id=\"urn:enhancement-c3eedec2\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/artificial_intelligence\">artificial intelligence<\/span> will be one of the main topics covered at the upcoming <strong style=\"user-select: auto;\">Codemotion Online Tech Conference<\/strong> on October 2020: check the agenda and get your free ticket at <a aria-label=\"undefined (opens in a new tab)\" style=\"user-select: auto;\" href=\"https:\/\/events.codemotion.com\/conferences\/online\/2020\/codemotion-online-tech-conference\/\" target=\"_blank\" rel=\"noreferrer noopener\">this link<\/a>!<\/p>\n\n\n\n<p class=\"eplus-ECuSVm\">And, if you want to know more about how modern technologies and tools can support you for &#8211; and during &#8211; the organisation of a virtual event, don&#8217;t miss this article showcasing the best tools we used to <a style=\"user-select: auto;\" href=\"https:\/\/www.codemotion.com\/magazine\/articles\/events\/planning-virtual-conference\/\">host our online conferences<\/a> since the COVID-19 outbreak.<\/p>\n\n\n\n<p class=\"eplus-BSjMDN\">However, it is worth noting that the equation <span id=\"urn:enhancement-dacdb438\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/artificial_intelligence\">AI<\/span> = ML (<span id=\"urn:enhancement-9b9d4fd8\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/machine_learning_2\">machine learning<\/span>) = ANN (artificial neural networks) is definitely false: <span id=\"urn:enhancement-fe41042\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/artificial_intelligence\">AI<\/span> is a broader field than <span id=\"urn:enhancement-bc06b13e\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/machine_learning_2\">ML<\/span>, and <span id=\"urn:enhancement-176eb67a\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/machine_learning_2\">ML<\/span> involves many algorithms unrelated to neural networks. A particularly interesting <span id=\"urn:enhancement-600eb7b4\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/class_computer_programming\">class<\/span> of such algorithms are <em>evolutionary algorithms<\/em>.<\/p>\n\n\n\n<h2 class=\"eplus-NmfcPy wp-block-heading\" id=\"h-learning-as-evolving\">Learning as evolving<\/h2>\n\n\n\n<p class=\"eplus-6tNzWd\">As <a style=\"user-select: auto;\" href=\"https:\/\/www.codemotion.com\/magazine\/tag\/neural-networks\/\"><span style=\"user-select: auto;\" id=\"urn:local-annotation-892709\" class=\"textannotation disambiguated\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/artificial_neural_network\">neural networks<\/span><\/a>, which draw inspiration from an oversimplified model of the human brain, evolutionary algorithms are inspired by biology &#8211; not from individual physiology, but rather from species behaviour. The most basic <span style=\"user-select: auto;\" id=\"urn:enhancement-93d19c98\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/cluster_analysis\">clusters<\/span> of living creatures, species evolve to adapt to a changing environment. <\/p>\n\n\n\n<p class=\"eplus-lg96YY\">The evolutionary <span style=\"user-select: auto;\" id=\"urn:enhancement-9e0c2da9\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/process_computing\">process<\/span> uses <span style=\"user-select: auto;\" id=\"urn:enhancement-11961cad\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/errors_and_residuals_in_statistics\">random errors<\/span> in <a aria-label=\"undefined (opens in a new tab)\" style=\"user-select: auto;\" href=\"https:\/\/en.wikipedia.org\/wiki\/DNA_replication\" target=\"_blank\" rel=\"noreferrer noopener\"><span style=\"user-select: auto;\" id=\"urn:local-annotation-779484\" class=\"textannotation disambiguated\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/dna\">DNA<\/span> copying<\/a> when individuals reproduce to originate new <span style=\"user-select: auto;\" id=\"urn:enhancement-27c10951\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/feature_machine_learning\">features<\/span> in individuals.&nbsp; These new <span style=\"user-select: auto;\" id=\"urn:enhancement-16dc0c0c\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/feature_machine_learning\">features<\/span> allow said individuals to live more successfully in the changed conditions of their environment.<\/p>\n\n\n\n<p class=\"eplus-Od2OKq\">Roughly speaking, and oversimplifying somewhat, the basic <span id=\"urn:enhancement-9838fba3\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/feature_machine_learning\">features<\/span> of evolution in <span id=\"urn:enhancement-45eef750\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/biological_system\">biological systems<\/span> are:<\/p>\n\n\n\n<ol class=\"eplus-Wxzxrl wp-block-list\"><li>Individuals of a species live in a changing environment.<\/li><li>Only a fraction of individuals reach adult age and reproduce.<\/li><li>Individuals are formed (in a complex way) by their genetic code.<\/li><li>Individuals compete to reproduce.<\/li><li>During reproduction, both gene recombination and genetic mutation occur.<\/li><li>Individuals displaying mutated characteristics experience natural selection due to their evolving fitness for the environmental conditions.<\/li><\/ol>\n\n\n\n<p class=\"eplus-TGephY\">The idea of evolutionary algorithms is to use the <span style=\"user-select: auto;\" id=\"urn:enhancement-57372edd\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/process_computing\">process<\/span> of individual mutation and species evolution as an iterative optimization algorithm.&nbsp; The ultimate <span style=\"user-select: auto;\" id=\"urn:enhancement-c1ba3597\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/goal\">goal<\/span> of such an algorithm is to determine the individual most fitted for a certain environment. <\/p>\n\n\n\n<p class=\"eplus-FLMHL4\">Of course, algorithms do not deal with living creatures; their \u2018individuals\u2019 are objects upon which a certain <span style=\"user-select: auto;\" id=\"urn:enhancement-5efa265f\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/fitness_function\">fitness function<\/span> may be computed.<\/p>\n\n\n\n<p class=\"eplus-7vBwoK\">Therefore, evolutionary algorithms allow possible solutions of a single problem to compete and recombine so as to produce new solutions. From these new solutions, the most improved are selected, and the <span style=\"user-select: auto;\" id=\"urn:enhancement-8533afbf\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/process_computing\">process<\/span> continues until the optimal solution is found. <\/p>\n\n\n\n<p class=\"eplus-QcpF69\"><span style=\"user-select: auto;\" id=\"urn:enhancement-43d67b9\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/evolutionary_algorithm\">Evolutionary algorithms<\/span> should be considered <span style=\"user-select: auto;\" id=\"urn:enhancement-15a5d4ba\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/list_of_machine_learning_concepts\">machine learning algorithms<\/span> as they <em style=\"user-select: auto;\">learn<\/em> the optimal solution by iteration and trials: they do not need a <a style=\"user-select: auto;\" href=\"https:\/\/www.codemotion.com\/magazine\/dev-hub\/machine-learning-dev\/understanding-ai-training\/\">training set<\/a>, but work on the <span style=\"user-select: auto;\" id=\"urn:enhancement-db23a1fc\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/structure\">structure<\/span> of the possible solutions by randomly making changes according to the response of the environment. This is a <span style=\"user-select: auto;\" id=\"urn:enhancement-30e09791\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/fitness_function\">fitness function<\/span> that ranks the computed solutions according to how optimal they are. <\/p>\n\n\n\n<p class=\"eplus-GFvpQC\">In short, evolutionary algorithms <span style=\"user-select: auto;\" id=\"urn:enhancement-dd752015\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/learning\">learn<\/span> by enforcing the evolution of <span style=\"user-select: auto;\" id=\"urn:enhancement-b44d74a4\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/data\">data<\/span> that represents solutions to <span style=\"user-select: auto;\" id=\"urn:enhancement-abd4de0e\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/optimization_problem\">optimization problems<\/span> (a neural network also evolves its <span style=\"user-select: auto;\" id=\"urn:enhancement-5122818e\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/weight_function\">weights<\/span> in a sense, using the backpropagation algorithm, but it needs several sets of <span style=\"user-select: auto;\" id=\"urn:enhancement-ab74d426\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/training\">training<\/span> <span style=\"user-select: auto;\" id=\"urn:enhancement-4edffe42\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/data\">data<\/span> to arrive at the solution to the problem).<\/p>\n\n\n\n<h3 class=\"eplus-gNyXqe wp-block-heading\" id=\"h-convergence-in-revolutionary-algorythms\">Convergence in revolutionary algorythms<\/h3>\n\n\n\n<p class=\"eplus-ha4I8I\">There are various interesting theories regarding convergence in evolutionary algorithms, but these are of no concern to us here. Our interest is in <span style=\"user-select: auto;\" id=\"urn:enhancement-a9d423c6\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/understanding\">understanding<\/span> how these algorithms may be used to solve <span style=\"user-select: auto;\" id=\"urn:enhancement-5dbed804\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/artificial_intelligence\">Artificial Intelligence<\/span> problems, rather than in <span style=\"user-select: auto;\" id=\"urn:enhancement-e32f7628\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/understanding\">understanding<\/span> why they actually work (although it is important to stress that there is a lot of theoretical work in analysing the properties of evolutionary algorithms, unlike when dealing with neural networks).<\/p>\n\n\n\n<p class=\"eplus-N0ODcu\">One important class of evolutionary algorithms used in practical <span style=\"user-select: auto;\" id=\"urn:enhancement-e4afe655\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/application_software\">applications<\/span> is <em style=\"user-select: auto;\">genetic algorithms<\/em>: these stress the importance of the <span style=\"user-select: auto;\" id=\"urn:enhancement-df18789b\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/data_computing\">data representation<\/span> used to encode possible solutions to our <span style=\"user-select: auto;\" id=\"urn:enhancement-b868e6c9\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/optimization_problem\">optimization problem<\/span>.&nbsp; <\/p>\n\n\n\n<p class=\"eplus-WcgNev\">The class name is inspired by an analogy with genetic code &#8211; the material that encodes (in a very complex way) our \u2018phenotype\u2019 or physical appearance. The use of the adjective \u2018genetic\u2019 reflects the fact that evolving solutions are represented by data <span style=\"user-select: auto;\" id=\"urn:enhancement-bfb69128\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/structure\">structures<\/span>, usually strings, reminiscent of biological genetic code.<\/p>\n\n\n\n<p class=\"eplus-z2ue9W\">Since the terminology in this field is borrowed from genetics, let us clarify some definitions for these terms used in the context of <span id=\"urn:enhancement-e0dae85\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/computing\">computing<\/span>:<\/p>\n\n\n\n<ul class=\"eplus-OB2kCd wp-block-list\"><li><em>phenotype<\/em> is a possible solution to our optimization problem.<\/li><li><em>chromosome<\/em> is a data structure which completely encodes a phenotype (in biological systems several chromosomes correspond to a phenotype, in genetic algorithms the correspondence is 1-1).<\/li><li><em>population<\/em> is a set of chromosomes.<\/li><li><em>generation<\/em> is a population at a certain time.<\/li><li><em>fitness<\/em> is a function which measures how optimal a phenotype is for the goal.<\/li><li><em>evaluation<\/em> is the process of getting a phenotype from a chromosome to apply the fitness function to it.<\/li><\/ul>\n\n\n\n<h3 class=\"eplus-9YrYXY wp-block-heading\" id=\"h-the-goal-of-a-genetic-algorithm\">The goal of a genetic algorithm<\/h3>\n\n\n\n<p class=\"eplus-h1EHYg\">The <span style=\"user-select: auto;\" id=\"urn:enhancement-32b5e4d\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/goal\">goal<\/span> of a genetic algorithm is to discover a phenotype that maximises fitness, by allowing a certain population to evolve across several generations.<\/p>\n\n\n\n<p class=\"eplus-kyOdTg\">The next question is: how does the evolution of individuals happen? <span id=\"urn:enhancement-ecf602cf\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/genetic_algorithm\">Genetic algorithms<\/span> apply a set of \u2018genetic operations\u2019 to chromosomes of each generation to allow them to reproduce and, in the <span id=\"urn:enhancement-4a7cb46\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/process_computing\">process<\/span>, introduce casual mutations, much as occurs in most living beings.<\/p>\n\n\n\n<p class=\"eplus-tBytzx\">The <span id=\"urn:enhancement-61bcb8a5\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/process_computing\">process<\/span> which allows individuals to reproduce is the <em>crossover operator<\/em>; this takes two chromosomes, which are <span id=\"urn:enhancement-10765369\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/data\">data<\/span> <span id=\"urn:enhancement-1b8bc26e\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/structure\">structures<\/span>, and produces two new chromosomes.&nbsp; These new chromosomes are obtained by splitting each original chromosome into two pieces and recombining these.<\/p>\n\n\n\n<p class=\"eplus-Lb9CZQ\">For example, supposing the chromosomes to be <span id=\"urn:enhancement-d514923c\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/byte\">bytes<\/span>: if we have <code><em>c<\/em>1 = |01001010|<\/code> and <code><em>c<\/em>2 = |11010101|<\/code>, then we may choose to crossover them by splitting them into two nibbles (a.k.a. 4-bits) and recombine them in one of two possible ways:<\/p>\n\n\n\n<ul class=\"eplus-qtq8KN wp-block-list\"><li><code>|0100|0101|<\/code> (the first nibble of <code><em>c<\/em>1<\/code> concatenated with the second nibble of <code><em>c<\/em>2<\/code>)&nbsp;<\/li><li><code>|1101|1010|<\/code> (the first nibble of <code><em>c<\/em>2<\/code> concatenated with the second nibble of <code><em>c<\/em>1<\/code>)<\/li><\/ul>\n\n\n\n<p class=\"eplus-mnhO4a\">Here, we are cut\/pasting chromosomes at their mid-point, but we could choose to cut elsewhere, or even to cut at more points. The same <span id=\"urn:enhancement-23f70605\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/process_computing\">process<\/span> could be carried out with <span id=\"urn:enhancement-30cf38d9\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/character_computing\">character<\/span> strings or arrays.<\/p>\n\n\n\n<p class=\"eplus-SR36Om\"><span style=\"user-select: auto;\" id=\"urn:enhancement-1a25134a\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/crossover_genetic_algorithm\">Crossover<\/span> takes place between individuals of a generation to produce the next generation. However, not all individuals reproduce; instead (as readers will surely know from many wildlife documentaries) only the best-adapted individuals have that opportunity. <\/p>\n\n\n\n<p class=\"eplus-XbynKO\">Similarly, crossover is used in genetic algorithms only between certain individuals, chosen from among those best fitted (although variants of these algorithms of course require that all individuals should reproduce).<\/p>\n\n\n\n<p class=\"eplus-dIMSgS\">A typical measure of the fitness needed to reproduce is to compute, for each individual <em><code>x<\/code><\/em>, the <span id=\"urn:enhancement-ec400fba\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/probability\">probability<\/span><\/p>\n\n\n\n<p class=\"has-text-align-center eplus-Ibehuw\"><em>p<\/em>(<em>x<\/em>) = Fitness of&nbsp;<em>x \/ <\/em>sum of fitness of all individuals<\/p>\n\n\n\n<p class=\"eplus-dbvudX\">the individuals for which the <span id=\"urn:enhancement-f3be6892\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/probability\">probability<\/span> is highest are then chosen.<\/p>\n\n\n\n<p class=\"eplus-Pr4tWC\">The <em><span id=\"urn:enhancement-2880952d\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/mutation_genetic_algorithm\">mutation<\/span> operator<\/em> introduces <span id=\"urn:enhancement-23621520\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/randomness\">randomness<\/span> into the crossover; this operator <span id=\"urn:enhancement-18e881fd\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/randomness\">randomly<\/span> changes one or more elements of the chromosome. For example, one can choose to mutate two bits in a <span id=\"urn:enhancement-4115e418\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/byte\">byte<\/span> following each crossover. Usually the <span id=\"urn:enhancement-4fba9cc9\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/mutation_genetic_algorithm\">mutation<\/span> rate is low.<\/p>\n\n\n\n<p class=\"eplus-5RWflL\">Now we can write down the general scheme of a genetic algorithm: we should have<\/p>\n\n\n\n<ul class=\"eplus-h8wxfg wp-block-list\"><li>a fitness function <em>F<\/em>(<em>x<\/em>) which may be applied to a phenotype;&nbsp;<\/li><li>a threshold <em>F_T<\/em> such that if <em>F<\/em>(<em>x<\/em>) \u2265 <em>F_T<\/em> then <em>x<\/em> is considered to be an optimal solution for our problem;&nbsp;<\/li><li>the number <em>N<\/em> of individuals in our population (remember: individuals, phenotypes and chromosomes are in 1-1 correspondence);&nbsp;<\/li><li>the number 0\u2264 <em>r<\/em> \u2264 1 of individuals in a generation which will be replaced by crossover-generated individuals;&nbsp;<\/li><li>the mutation rate 0 \u2264 <em>m <\/em>\u2264 1.<\/li><\/ul>\n\n\n\n<p class=\"eplus-j7VFrn\">Then the algorithm runs as follows:<\/p>\n\n\n<pre class=\"wp-block-code eplus-EdDXR5\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">Initialize the population generation to N random chromosomes.\nWhile max(F(x) <span class=\"hljs-keyword\">for<\/span> x <span class=\"hljs-keyword\">in<\/span> generation) &lt; F_T:\n    Initialize a <span class=\"hljs-keyword\">new<\/span> empty population next_generation\n    For each x <span class=\"hljs-keyword\">in<\/span> generation:\n    compute s&#91;x] = F(x) \/ sum(F(y) <span class=\"hljs-keyword\">for<\/span> y <span class=\"hljs-keyword\">in<\/span> generation)\n    Insert into next_generation the (<span class=\"hljs-number\">1<\/span>-r)*N chromosomes <span class=\"hljs-keyword\">from<\/span> generation\n    which have highest scores s&#91;x]\n    Select r*N\/<span class=\"hljs-number\">2<\/span> individuals (<span class=\"hljs-keyword\">with<\/span> highest score) <span class=\"hljs-keyword\">from<\/span> generation\n    and <span class=\"hljs-keyword\">let<\/span> them reproduce by crossover, adding the two offsprings\n    to next_generation.\n    Select at random m*N individuals <span class=\"hljs-keyword\">from<\/span> next_generation and mutate\n    their chromosomes.\n    Let generation = next_generation<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"eplus-KHu8rY\">Note that a new population is produced; this will be the next generation, into which we insert the best-adapted individuals of the current generation, and their offspring. The resulting (next) generation is the same size as the previous generation.<\/p>\n\n\n\n<p class=\"eplus-YeblBe\">It is worth stressing that such an algorithm is an <span id=\"urn:enhancement-f487f74a\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/iterative_method\">iterative algorithm<\/span> in which a series of <span id=\"urn:enhancement-fbfd3b5d\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/parameter\">parameters<\/span> change at each <span id=\"urn:enhancement-2736956d\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/iteration\">iteration<\/span> (the chromosomes), and which is governed by certain hyper-<span id=\"urn:enhancement-843df070\" class=\"textannotation disambiguated wl-thing\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/parameter\">parameters<\/span> such as numbers <em>r <\/em>and <em>m<\/em>. Namely, a typical machine learning algorithm.<\/p>\n\n\n\n<p class=\"eplus-z9Teyv\">The previous algorithm is almost <span id=\"urn:local-annotation-777373\" class=\"textannotation disambiguated\" itemid=\"http:\/\/data.wordlift.io\/wl01770\/entity\/python_programming_language\">Python<\/span>-esque in its coding, so it\u2019s impossible to resist implementing it in a working program to play with. We\u2019ll use this to solve a simple regression problem with genetic algorithms.<\/p>\n\n\n\n<h2 class=\"eplus-pn4aKH wp-block-heading\" id=\"h-how-to-implement-genetic-algorithms-in-python\">How to implement genetic algorithms in Python<\/h2>\n\n\n\n<p class=\"eplus-wyQxij\">Let us try to implement a simple evolutionary algorithm: suppose we have <em>N <\/em>data to fit, <em>y<\/em>1,&#8230;,<em>yN<\/em>, for example <em>N<\/em> numbers which represent measurements of a certain variable at given instants: temperatures, house prices, etc. For simplicity, let us assume that instants <em>t<\/em>1,<em>t<\/em>2&#8230;,<em>tN<\/em> at which our measurements were taken are 0,1,&#8230;,<em>N<\/em>\u22121.<\/p>\n\n\n\n<p class=\"eplus-RAfY2R\">We want to find a curve that fits the data, i.e., we want to <em>interpolate<\/em> it.<\/p>\n\n\n\n<p class=\"eplus-bDsMSa\">There are several classic methods of achieving that which do not require machine learning (although neural networks may be efficiently used for that purpose), for example Lagrange interpolation: the latter finds a polynomial of degree <em>N<\/em>, thus a function of the form <em>p<\/em>(<em>x<\/em>) = <em>a<\/em><sub>0<\/sub> + <em>a<\/em><sub>1 <\/sub><em>x <\/em>+ <em>a<\/em><sub>2 <\/sub><em>x<\/em><sup>2<\/sup> + &#8230; + <em>a<\/em><em><sub>N <\/sub><\/em><em>x<\/em><em><sup>N<\/sup><\/em> to achieve the desired outcome.<\/p>\n\n\n\n<p class=\"eplus-0BYRoM\">To crossover polynomials is a difficult matter (even if polynomials can easily be represented as arrays (<em>a<\/em><sub>0<\/sub>, <em>a<\/em><sub>1<\/sub>, &#8230;, <em>a<\/em><em><sub>N<\/sub><\/em>)), so we will stick to simpler functions, e.g.,&nbsp; <em>piecewise linear functions<\/em>.<\/p>\n\n\n\n<p class=\"eplus-6Wmcgj\">Such a function is simply a joining of segments along vertices, as in the following graph:<\/p>\n\n\n\n<div class=\"wp-block-image eplus-3iTsJ3\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"380\" height=\"248\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/08\/image1-2.png\" alt=\"Genetic Algorithms\" class=\"wp-image-7843\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/08\/image1-2.png 380w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/08\/image1-2-300x196.png 300w\" sizes=\"auto, (max-width: 380px) 100vw, 380px\" \/><\/figure><\/div>\n\n\n\n<p class=\"eplus-4k47Ej\">A piecewise linear function is determined by the set of its vertices, which are points (<em>x<\/em>,<em>y<\/em>) in the Cartesian plane. In our case, the vertices are (<em>t<\/em><sub>0<\/sub>, <em>p<\/em><sub>0<\/sub>), (<em>t<\/em><sub>1<\/sub>, <em>p<\/em><sub>1<\/sub>), &#8230;, (<em>t<\/em><em><sub>N<\/sub><\/em>, <em>p<\/em><em><sub>N<\/sub><\/em>) (I use <em>p<\/em><em><sub>i<\/sub><\/em> to suggest \u2018piecewise\u2019).<\/p>\n\n\n\n<p class=\"eplus-CA8DmM\">We will use an evolutionary algorithm, trying to fit <em>N <\/em>points into such a piecewise linear function: a function of this sort is determined by its values [<em>p<\/em><sub>0<\/sub>, <em>p<\/em><sub>1<\/sub>, &#8230;, <em>p<\/em><em><sub>N<\/sub><\/em>], so, as developers would say, it is just an array of numbers with <em>N<\/em>+1 elements.<\/p>\n\n\n\n<p class=\"eplus-e0NsZy\">In the genetic algorithm parlance introduced above:<\/p>\n\n\n\n<ul class=\"eplus-8BglQX wp-block-list\"><li>a piecewise linear function is a phenotype;<\/li><li>its vector encoding is a chromosome;<\/li><li>the fitting function takes the piecewise linear function <em>p<\/em>(<em>t<\/em>) corresponding to a chromosome, evaluates it at points <em>t<\/em><sub>0<\/sub>, &#8230;, <em>t<\/em><em><sub>N<\/sub><\/em>, which is essentially just considering the vector (<em>p<\/em><sub>0<\/sub>, &#8230;, <em>p<\/em><em><sub>N<\/sub><\/em>), and comparing it with the vector (<em>y<\/em><sub>0<\/sub>, &#8230;, <em>y<\/em><em><sub>N<\/sub><\/em>), for example by taking the norm of their difference. The more optimal the solution, the smaller the latter will be, so we will take its inverse (C \u2212\u2225p \u2212 y\u2225)\/C as the fitting function for C.<\/li><\/ul>\n\n\n\n<p class=\"eplus-6i7DVm\">Notice once again that a phenotype is represented by a chromosome; in this case the phenotype is a function but the chromosome is a fixed length vector.<\/p>\n\n\n\n<p class=\"eplus-l1tlH7\">We\u2019ll artificially produce the points we want to fit, so that they are on a damped sinusoid:<\/p>\n\n\n<pre class=\"wp-block-code eplus-mqu3Eh\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">import matplotlib.pyplot <span class=\"hljs-keyword\">as<\/span> plt\nimport numpy <span class=\"hljs-keyword\">as<\/span> np\nfrom numpy.random import randint, uniform\n \n<span class=\"hljs-comment\"># Points to fit<\/span>\nn_points = <span class=\"hljs-number\">15<\/span>\ntimes = np.linspace(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">12<\/span>, num = n_points)\npoints = np.<span class=\"hljs-keyword\">array<\/span>(np.exp(<span class=\"hljs-number\">-0.2<\/span>*times)*(np.cos(times)))\nfiner = np.linspace(<span class=\"hljs-number\">0<\/span>,<span class=\"hljs-number\">12<\/span>)\nplt.plot(finer,np.<span class=\"hljs-keyword\">array<\/span>(np.exp(<span class=\"hljs-number\">-0.2<\/span>*finer)*(np.cos(finer))))\nplt.show()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<div class=\"wp-block-image eplus-av1rbm\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"380\" height=\"248\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/08\/image3-2.png\" alt=\"Genetic Algorithms\" class=\"wp-image-7844\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/08\/image3-2.png 380w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/08\/image3-2-300x196.png 300w\" sizes=\"auto, (max-width: 380px) 100vw, 380px\" \/><\/figure><\/div>\n\n\n\n<p class=\"eplus-rTRf1P\">As usual, our implementation of the previous algorithm will be very simple and will demand as little as possible of our language (Python) so as to be easily understandable and portable.&nbsp; An appropriate alternative would be to define a class GA to perform our algorithm, however we will stick to a script-like program here.<\/p>\n\n\n\n<p class=\"eplus-aWulO0\">In our first code snippet we set hyper-parameters, initialize the population and plot the piecewise linear function corresponding to selected chromosomes.<\/p>\n\n\n<pre class=\"wp-block-code eplus-TXyPqS\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Hyper parameters<\/span>\nN = <span class=\"hljs-number\">50<\/span>\nd = n_points\nr = <span class=\"hljs-number\">0.7<\/span>    <span class=\"hljs-comment\"># percentage of chromosomes to crossover<\/span>\nm = <span class=\"hljs-number\">0.1<\/span>    <span class=\"hljs-comment\"># percentage of chromosomes to mutate<\/span>\n \n<span class=\"hljs-comment\"># The current generation (which is formed by chromosomes)<\/span>\ngeneration = np.random.uniform(<span class=\"hljs-number\">-2<\/span>, <span class=\"hljs-number\">2<\/span>, (N, d))\n \ndef Fitness(i):\n    <span class=\"hljs-string\">\"\"<\/span><span class=\"hljs-string\">\"Fitness function for chromosome i: the higher the function,\n        the better the phenotype fits the goal.\"<\/span><span class=\"hljs-string\">\"\"<\/span>\n    <span class=\"hljs-keyword\">return<\/span> (<span class=\"hljs-number\">1000<\/span> - np.linalg.norm(generation&#91;i,:] - points)) \/ <span class=\"hljs-number\">1000<\/span>\n \nFT = <span class=\"hljs-number\">0.999<\/span>   <span class=\"hljs-comment\"># If Fitness &gt; FT we have an optimal solution<\/span>\n \n<span class=\"hljs-comment\"># Prints some phenotypes corresponding to initial population<\/span>\nplt.plot(times, generation&#91;<span class=\"hljs-number\">0<\/span>])\nplt.plot(times, generation&#91;<span class=\"hljs-number\">1<\/span>])\nplt.plot(times, generation&#91;<span class=\"hljs-number\">2<\/span>])\nplt.show()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<div class=\"wp-block-image eplus-9TUmBJ\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"380\" height=\"249\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/08\/image2-2.png\" alt=\"Genetic Algorithms\" class=\"wp-image-7845\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/08\/image2-2.png 380w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/08\/image2-2-300x197.png 300w\" sizes=\"auto, (max-width: 380px) 100vw, 380px\" \/><\/figure><\/div>\n\n\n\n<p class=\"eplus-B8vZf4\">Note that the fitness function should be higher for piecewise functions that are nearer to the function to fit. We use the norm of the difference of two vectors (the values of functions compared at fixed points) but we complement and normalize it between 0 and 1 to get a fitness function which is closer to 1 the more the piecewise function approximates the function to fit.<\/p>\n\n\n\n<p class=\"eplus-PgFFSc\">Now, we implement the genetic algorithm by translating the pseudocode used above into Python: we define the crossover function with a simple standard operator used for chromosomes made of arrays of real numbers.<\/p>\n\n\n\n<p class=\"eplus-hCTbOE\">Moreover, note that we compute scores and use them as probability, or better yet, as a probability distribution from which to draw samples. <\/p>\n\n\n\n<p class=\"eplus-wIyQiZ\">To do that we define a vector of indexes, which contains copies of the same index j according to the score of generator[j] and then choose an index uniformly from this vector, simulating the non-uniform drawing from the empirical distribution created by the scores.<\/p>\n\n\n\n<p class=\"eplus-6x7wXW\">Finally, mutations simply add a random number between -1 and 1 to the real numbers of the chromosome to be mutated: mutations are needed to explore new regions in the space state of the solutions to the problem, and to introduce a stochastic global search feature in the genetic algorithm.<\/p>\n\n\n<pre class=\"wp-block-code eplus-c81DX5\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">def crossover(j1, j2):\n    <span class=\"hljs-string\">\"\"<\/span><span class=\"hljs-string\">\"Cross over generation&#91;j1] and generation&#91;j2]. For each gene of the\n       Chromosome we take the max and min of the corresponding genes in j1 and\n       j2 and set the offspring coordinates to a random number in the interval\n       &#91;min, max] \"<\/span><span class=\"hljs-string\">\"\"<\/span>\n    offspring1 = &#91;]\n    offspring2 = &#91;]\n    <span class=\"hljs-keyword\">for<\/span> i in range(d):\n        m = min(generation&#91;j1,i], generation&#91;j2,i])\n        M = max(generation&#91;j1,i], generation&#91;j2,i])\n        offspring1.append(np.random.uniform(m, M))\n        offspring2.append(np.random.uniform(m, M))\n    <span class=\"hljs-keyword\">return<\/span> offspring1, offspring2\n \ndef scores():\n    <span class=\"hljs-string\">\"\"<\/span><span class=\"hljs-string\">\"Returns the vector of scores of the current generation\"<\/span><span class=\"hljs-string\">\"\"<\/span>\n    s = &#91;]  <span class=\"hljs-comment\"># array of scores<\/span>\n    <span class=\"hljs-keyword\">for<\/span> j in range(N):\n        s.append(Fitness(j) \/ sum(Fitness(k) <span class=\"hljs-keyword\">for<\/span> k in range(N)))\n    <span class=\"hljs-keyword\">return<\/span> s\n \nmax_iterations = <span class=\"hljs-number\">1000<\/span>\n<span class=\"hljs-keyword\">while<\/span> max_iterations &gt; <span class=\"hljs-number\">0<\/span> <span class=\"hljs-keyword\">and<\/span> max(Fitness(i) <span class=\"hljs-keyword\">for<\/span> i in range(N)) &lt; FT:\n    <span class=\"hljs-comment\"># decreases the loop counter<\/span>\n    max_iterations -= <span class=\"hljs-number\">1<\/span>\n \n    <span class=\"hljs-comment\">#Initialize a new empty population next_generation<\/span>\n    next_generation = np.zeros((N, d))\n    \n    <span class=\"hljs-comment\"># For each x in generation compute s&#91;x]=F(x)\/sum(F(y) for y in generation)<\/span>\n    s = scores()\n    \n    <span class=\"hljs-comment\"># Now create a vector of indexes where the most probable indexes (the one<\/span>\n    <span class=\"hljs-comment\"># with highest scores) are repeated more than others.<\/span>\n    v = &#91;]\n    <span class=\"hljs-keyword\">for<\/span> j in range(N):\n        v.extend(&#91;j] * int(N*s&#91;j]))\n    <span class=\"hljs-comment\"># Thus, if we choose uniformly random elements from v is like to draw from<\/span>\n    <span class=\"hljs-comment\"># the probabilistic distribution induced by probabilities s&#91;j]<\/span>\n    \n    <span class=\"hljs-comment\"># Insert into next_generation the (1-r)*N chromosomes from generation by<\/span>\n    <span class=\"hljs-comment\"># drawing from distribution s&#91;].<\/span>\n    j = <span class=\"hljs-number\">0<\/span>\n    <span class=\"hljs-keyword\">while<\/span> j &lt; int(N*(<span class=\"hljs-number\">1<\/span>-r)):\n        k = v&#91;np.random.randint(len(v))]\n        next_generation&#91;j] = generation&#91;k]\n        j += <span class=\"hljs-number\">1<\/span>\n    <span class=\"hljs-comment\"># Select r*N\/2 individuals (with highest score) from generation and let them<\/span>\n    <span class=\"hljs-comment\"># reproduce by crossover, adding the two offsprings to next_generation.<\/span>\n    <span class=\"hljs-keyword\">for<\/span> k in range(int(r*N\/<span class=\"hljs-number\">2<\/span>)):\n        <span class=\"hljs-comment\"># Select a pair (j1, j2) of individuals to reproduce<\/span>\n        j1 = v&#91;np.random.randint(len(v))]\n        j2 = v&#91;np.random.randint(len(v))]\n        <span class=\"hljs-keyword\">while<\/span> j1 == j2:\n            j2 = v&#91;np.random.randint(len(v))]\n        offspring1, offspring2 = crossover(j1, j2)\n        next_generation&#91;j,:] = offspring1\n        next_generation&#91;j+<span class=\"hljs-number\">1<\/span>,:] = offspring1\n        j += <span class=\"hljs-number\">2<\/span>\n \n    <span class=\"hljs-comment\"># Select at random m*N individuals from next_generation and mutate their<\/span>\n    <span class=\"hljs-comment\"># chromosomes.<\/span>\n    <span class=\"hljs-keyword\">for<\/span> j in range(int(m*N)):\n        j1 = v&#91;np.random.randint(len(v))]\n        <span class=\"hljs-keyword\">for<\/span> k in range(d):\n            next_generation&#91;j1,k] += np.random.uniform(<span class=\"hljs-number\">-1<\/span>,<span class=\"hljs-number\">1<\/span>)\n    \n    generation = next_generation<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"eplus-eBNVhj\">Let us plot the solution found by the algorithm: it is of course a piecewise linear function, so it cannot have the smooth appearance of the function to fit, but it does mimic the function\u2019s increase and decrease trends, giving at the very least a qualitative idea of the function.<\/p>\n\n\n<pre class=\"wp-block-code eplus-nxT06X\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\"># Plots the optimal solution<\/span>\ni_optimal = np.argmax(scores())\nplt.title(<span class=\"hljs-string\">\"Optimal solution\"<\/span>)\nplt.plot(times, generation&#91;i_optimal])\nplt.plot(np.linspace(<span class=\"hljs-number\">0<\/span>,<span class=\"hljs-number\">12<\/span>), np.<span class=\"hljs-keyword\">array<\/span>(np.exp(<span class=\"hljs-number\">-0.2<\/span>*np.linspace(<span class=\"hljs-number\">0<\/span>,<span class=\"hljs-number\">12<\/span>))*(np.cos(np.linspace(<span class=\"hljs-number\">0<\/span>,<span class=\"hljs-number\">12<\/span>)))))\nplt.show()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<div class=\"wp-block-image eplus-G9Bc0X\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"380\" height=\"264\" src=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/08\/image4-2.png\" alt=\"Genetic Algorithms\" class=\"wp-image-7846\" srcset=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/08\/image4-2.png 380w, https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/08\/image4-2-300x208.png 300w\" sizes=\"auto, (max-width: 380px) 100vw, 380px\" \/><\/figure><\/div>\n\n\n\n<p class=\"eplus-PmcN5S\">Of course, as is usual for machine learning algorithms, hyper-parameters may be fine tuned to improve the result, but for the sake of our expository discussion we can leave that to the reader to play with.<\/p>\n\n\n\n<p class=\"eplus-3Fkzn1\">Notice that if <em style=\"user-select: auto;\">r <\/em>= 0 then no crossover occurs, while if <em style=\"user-select: auto;\">m <\/em>= 0 no mutations occur: one can play with these numbers to see how the algorithm behaves with these extreme values. The crossover function may also be changed in several respects, but the main ingredients of genetic algorithms have been displayed in this test example.<\/p>\n\n\n\n<h2 class=\"eplus-viWvHk wp-block-heading\" id=\"h-want-to-launch-a-developer-event-despite-the-challenges-of-covid-19\">Want to launch a developer event despite the challenges of  COVID-19?<\/h2>\n\n\n\n<p class=\"eplus-Dlirgv\">If you want to know more about how modern technologies and tools can support you for \u2013 and during \u2013 the organisation of a virtual event, don\u2019t miss this article showcasing the best tools we used to&nbsp;<a style=\"user-select: auto;\" href=\"https:\/\/www.codemotion.com\/magazine\/articles\/events\/hosting-virtual-conference\/\">host our online conferences<\/a>&nbsp;since the COVID-19 outbreak.<\/p>\n\n\n","protected":false},"excerpt":{"rendered":"<p>The life of a developer nowadays is a very exciting one: we have several languages, frameworks, and numerous excellent tools available to select from according to our needs.&nbsp; Not only that, we also have a lot of interesting and useful techniques that have been developed to allow us to accomplish purposes far beyond the traditional&#8230; <a class=\"more-link\" href=\"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/\">Read more<\/a><\/p>\n","protected":false},"author":5,"featured_media":6517,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_editorskit_title_hidden":false,"_editorskit_reading_time":11,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","_uag_custom_page_level_css":"","_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","footnotes":""},"categories":[46],"tags":[68],"collections":[],"class_list":{"0":"post-7840","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-ai-ml","8":"tag-python","9":"entry"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.5) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Genetic Algorithms: A Developer&#039;s Perspective - Codemotion Magazine<\/title>\n<meta name=\"description\" content=\"We&#039;ll learn what genetic algorithms are, how they are framed within the scope of artificial intelligence, and how to implement them in Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Genetic Algorithms: A Developer&#039;s Perspective\" \/>\n<meta property=\"og:description\" content=\"We&#039;ll learn what genetic algorithms are, how they are framed within the scope of artificial intelligence, and how to implement them in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/\" \/>\n<meta property=\"og:site_name\" content=\"Codemotion Magazine\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Codemotion.Italy\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-08-14T08:49:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-23T11:52:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"675\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Paolo Caressa\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@www_caressa_it\" \/>\n<meta name=\"twitter:site\" content=\"@CodemotionIT\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Paolo Caressa\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/ai-ml\\\/genetic-algorithms\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/ai-ml\\\/genetic-algorithms\\\/\"},\"author\":{\"name\":\"Paolo Caressa\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#\\\/schema\\\/person\\\/11b502309bc50a6923aafd79c6259f85\"},\"headline\":\"Genetic Algorithms: A Developer&#8217;s Perspective\",\"datePublished\":\"2020-08-14T08:49:47+00:00\",\"dateModified\":\"2021-12-23T11:52:09+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/ai-ml\\\/genetic-algorithms\\\/\"},\"wordCount\":2403,\"publisher\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/ai-ml\\\/genetic-algorithms\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled.jpg\",\"keywords\":[\"Python\"],\"articleSection\":[\"AI\\\/ML\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/ai-ml\\\/genetic-algorithms\\\/\",\"url\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/ai-ml\\\/genetic-algorithms\\\/\",\"name\":\"Genetic Algorithms: A Developer's Perspective - Codemotion Magazine\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/ai-ml\\\/genetic-algorithms\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/ai-ml\\\/genetic-algorithms\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled.jpg\",\"datePublished\":\"2020-08-14T08:49:47+00:00\",\"dateModified\":\"2021-12-23T11:52:09+00:00\",\"description\":\"We'll learn what genetic algorithms are, how they are framed within the scope of artificial intelligence, and how to implement them in Python.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/ai-ml\\\/genetic-algorithms\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/ai-ml\\\/genetic-algorithms\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/ai-ml\\\/genetic-algorithms\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled.jpg\",\"contentUrl\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/wp-content\\\/uploads\\\/2020\\\/07\\\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled.jpg\",\"width\":1200,\"height\":675,\"caption\":\"AI (artificial intelligence) developer\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/ai-ml\\\/genetic-algorithms\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"AI\\\/ML\",\"item\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/ai-ml\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Machine Learning\",\"item\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/ai-ml\\\/machine-learning\\\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Genetic Algorithms: A Developer&#8217;s Perspective\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#website\",\"url\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/\",\"name\":\"Codemotion Magazine\",\"description\":\"We code the future. Together\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#organization\",\"name\":\"Codemotion\",\"url\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/wp-content\\\/uploads\\\/2019\\\/11\\\/codemotionlogo.png\",\"contentUrl\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/wp-content\\\/uploads\\\/2019\\\/11\\\/codemotionlogo.png\",\"width\":225,\"height\":225,\"caption\":\"Codemotion\"},\"image\":{\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/Codemotion.Italy\\\/\",\"https:\\\/\\\/x.com\\\/CodemotionIT\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/#\\\/schema\\\/person\\\/11b502309bc50a6923aafd79c6259f85\",\"name\":\"Paolo Caressa\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b55795d60b54b1cc8b605f9967dbbe68b3fcf826249490cecafd797ee4f18d4c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b55795d60b54b1cc8b605f9967dbbe68b3fcf826249490cecafd797ee4f18d4c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/b55795d60b54b1cc8b605f9967dbbe68b3fcf826249490cecafd797ee4f18d4c?s=96&d=mm&r=g\",\"caption\":\"Paolo Caressa\"},\"description\":\"I spent the first part of my life enjoying studies up to a math BS+MS and PhD. Next I worked both as math researcher (differential geometry) and as IT consultant (R&amp;D, feasibility studies, business analysis). Eventually I left academia and worked some years in finance (maths &amp; implementation of derivative pricings and risk management models), then again in IT (as technical consultant and writer, project manager, program manager). In the meanwhile I write books and articles on maths and computer science and I give lectures on workshops and conferences (applied maths, AI, etc.). I also serve as adjunct professor in the Engineering Department of \\\"Sapienza\\\" University of Rome (calculus and CS classes).\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/paolocaressa\\\/\",\"https:\\\/\\\/x.com\\\/www_caressa_it\"],\"url\":\"https:\\\/\\\/www.codemotion.com\\\/magazine\\\/author\\\/paolo-caressa\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Genetic Algorithms: A Developer's Perspective - Codemotion Magazine","description":"We'll learn what genetic algorithms are, how they are framed within the scope of artificial intelligence, and how to implement them in Python.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/","og_locale":"en_US","og_type":"article","og_title":"Genetic Algorithms: A Developer's Perspective","og_description":"We'll learn what genetic algorithms are, how they are framed within the scope of artificial intelligence, and how to implement them in Python.","og_url":"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/","og_site_name":"Codemotion Magazine","article_publisher":"https:\/\/www.facebook.com\/Codemotion.Italy\/","article_published_time":"2020-08-14T08:49:47+00:00","article_modified_time":"2021-12-23T11:52:09+00:00","og_image":[{"width":1200,"height":675,"url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled.jpg","type":"image\/jpeg"}],"author":"Paolo Caressa","twitter_card":"summary_large_image","twitter_creator":"@www_caressa_it","twitter_site":"@CodemotionIT","twitter_misc":{"Written by":"Paolo Caressa","Est. reading time":"15 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/#article","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/"},"author":{"name":"Paolo Caressa","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/11b502309bc50a6923aafd79c6259f85"},"headline":"Genetic Algorithms: A Developer&#8217;s Perspective","datePublished":"2020-08-14T08:49:47+00:00","dateModified":"2021-12-23T11:52:09+00:00","mainEntityOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/"},"wordCount":2403,"publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled.jpg","keywords":["Python"],"articleSection":["AI\/ML"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/","url":"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/","name":"Genetic Algorithms: A Developer's Perspective - Codemotion Magazine","isPartOf":{"@id":"https:\/\/www.codemotion.com\/magazine\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/#primaryimage"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/#primaryimage"},"thumbnailUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled.jpg","datePublished":"2020-08-14T08:49:47+00:00","dateModified":"2021-12-23T11:52:09+00:00","description":"We'll learn what genetic algorithms are, how they are framed within the scope of artificial intelligence, and how to implement them in Python.","breadcrumb":{"@id":"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/#primaryimage","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled.jpg","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled.jpg","width":1200,"height":675,"caption":"AI (artificial intelligence) developer"},{"@type":"BreadcrumbList","@id":"https:\/\/www.codemotion.com\/magazine\/ai-ml\/genetic-algorithms\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.codemotion.com\/magazine\/"},{"@type":"ListItem","position":2,"name":"AI\/ML","item":"https:\/\/www.codemotion.com\/magazine\/ai-ml\/"},{"@type":"ListItem","position":3,"name":"Machine Learning","item":"https:\/\/www.codemotion.com\/magazine\/ai-ml\/machine-learning\/"},{"@type":"ListItem","position":4,"name":"Genetic Algorithms: A Developer&#8217;s Perspective"}]},{"@type":"WebSite","@id":"https:\/\/www.codemotion.com\/magazine\/#website","url":"https:\/\/www.codemotion.com\/magazine\/","name":"Codemotion Magazine","description":"We code the future. Together","publisher":{"@id":"https:\/\/www.codemotion.com\/magazine\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.codemotion.com\/magazine\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.codemotion.com\/magazine\/#organization","name":"Codemotion","url":"https:\/\/www.codemotion.com\/magazine\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/","url":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png","contentUrl":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2019\/11\/codemotionlogo.png","width":225,"height":225,"caption":"Codemotion"},"image":{"@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Codemotion.Italy\/","https:\/\/x.com\/CodemotionIT"]},{"@type":"Person","@id":"https:\/\/www.codemotion.com\/magazine\/#\/schema\/person\/11b502309bc50a6923aafd79c6259f85","name":"Paolo Caressa","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/b55795d60b54b1cc8b605f9967dbbe68b3fcf826249490cecafd797ee4f18d4c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/b55795d60b54b1cc8b605f9967dbbe68b3fcf826249490cecafd797ee4f18d4c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/b55795d60b54b1cc8b605f9967dbbe68b3fcf826249490cecafd797ee4f18d4c?s=96&d=mm&r=g","caption":"Paolo Caressa"},"description":"I spent the first part of my life enjoying studies up to a math BS+MS and PhD. Next I worked both as math researcher (differential geometry) and as IT consultant (R&amp;D, feasibility studies, business analysis). Eventually I left academia and worked some years in finance (maths &amp; implementation of derivative pricings and risk management models), then again in IT (as technical consultant and writer, project manager, program manager). In the meanwhile I write books and articles on maths and computer science and I give lectures on workshops and conferences (applied maths, AI, etc.). I also serve as adjunct professor in the Engineering Department of \"Sapienza\" University of Rome (calculus and CS classes).","sameAs":["https:\/\/www.linkedin.com\/in\/paolocaressa\/","https:\/\/x.com\/www_caressa_it"],"url":"https:\/\/www.codemotion.com\/magazine\/author\/paolo-caressa\/"}]}},"featured_image_src":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled-600x400.jpg","featured_image_src_square":"https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled-600x600.jpg","author_info":{"display_name":"Paolo Caressa","author_link":"https:\/\/www.codemotion.com\/magazine\/author\/paolo-caressa\/"},"uagb_featured_image_src":{"full":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled.jpg",1200,675,false],"thumbnail":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled-150x150.jpg",150,150,true],"medium":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled-300x169.jpg",300,169,true],"medium_large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled-768x432.jpg",768,432,true],"large":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled-1024x576.jpg",1024,576,true],"1536x1536":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled.jpg",1200,675,false],"2048x2048":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled.jpg",1200,675,false],"small-home-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled.jpg",100,56,false],"sidebar-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled-180x128.jpg",180,128,true],"genesis-singular-images":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled-896x504.jpg",896,504,true],"archive-featured":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled-400x225.jpg",400,225,true],"gb-block-post-grid-landscape":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled-600x400.jpg",600,400,true],"gb-block-post-grid-square":["https:\/\/www.codemotion.com\/magazine\/wp-content\/uploads\/2020\/07\/hitesh-choudhary-t1PaIbMTJIM-unsplash-scaled-600x600.jpg",600,600,true]},"uagb_author_info":{"display_name":"Paolo Caressa","author_link":"https:\/\/www.codemotion.com\/magazine\/author\/paolo-caressa\/"},"uagb_comment_info":0,"uagb_excerpt":"The life of a developer nowadays is a very exciting one: we have several languages, frameworks, and numerous excellent tools available to select from according to our needs.&nbsp; Not only that, we also have a lot of interesting and useful techniques that have been developed to allow us to accomplish purposes far beyond the traditional&#8230;&hellip;","lang":"en","_links":{"self":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/7840","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/comments?post=7840"}],"version-history":[{"count":13,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/7840\/revisions"}],"predecessor-version":[{"id":12024,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/posts\/7840\/revisions\/12024"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media\/6517"}],"wp:attachment":[{"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/media?parent=7840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/categories?post=7840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/tags?post=7840"},{"taxonomy":"collections","embeddable":true,"href":"https:\/\/www.codemotion.com\/magazine\/wp-json\/wp\/v2\/collections?post=7840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}