<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Marouane Najid on Medium]]></title>
        <description><![CDATA[Stories by Marouane Najid on Medium]]></description>
        <link>https://medium.com/@najid110?source=rss-5e28a2754782------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*HeRlgePADHca010U36C6wA.jpeg</url>
            <title>Stories by Marouane Najid on Medium</title>
            <link>https://medium.com/@najid110?source=rss-5e28a2754782------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 05:31:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@najid110/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Multi type of capacitated vehicle routing problem with a Genetic Algorithm (GA) and Deap library in…]]></title>
            <link>https://medium.com/@najid110/multi-type-of-capacitated-vehicle-routing-problem-with-a-genetic-algorithm-ga-and-deap-library-in-399135f6357a?source=rss-5e28a2754782------2</link>
            <guid isPermaLink="false">https://medium.com/p/399135f6357a</guid>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[genetic-algorithm]]></category>
            <category><![CDATA[shipping]]></category>
            <category><![CDATA[metaheuristics]]></category>
            <dc:creator><![CDATA[Marouane Najid]]></dc:creator>
            <pubDate>Thu, 09 Sep 2021 15:47:23 GMT</pubDate>
            <atom:updated>2021-09-12T20:35:37.672Z</atom:updated>
            <content:encoded><![CDATA[<h3>Multi type of capacitated vehicle routing problem with a Genetic Algorithm (GA) and Deap library in python.</h3><h4>A brief introduction to Metaheuristics</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cmQd5Xura5wFUx2ntsJbAQ.png" /></figure><h3>Introduction</h3><p>This last month I got interested in optimization problems and how I can solve them or only get a good enough solution. The biggest challenge is how to deal with <a href="https://www.britannica.com/science/NP-complete-problem">NP problems</a> efficiently. For example, the TRAVELING SALESMAN PROBLEM which is an <a href="https://www.britannica.com/science/optimization">optimization</a> problem in <a href="https://www.britannica.com/topic/graph-theory">graph theory</a> in which the nodes (cities) of a graph are connected by directed edges (routes), where the weight of an edge indicates the distance between two cities. The problem is to find a path for the traveling salesman that visits each city once, returns to the starting city, and minimizes the distance traveled.</p><p>The TSP present a complexity of <strong>n!</strong> and if we suppose we have a computational power of 1000000 operation in a second we get these results</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*M0A8BdQN6PBYVX-F" /></figure><p>which means if we are only dealing with 24 cities we need more than the age of the earth to find the optimal solution (if we want to check every possibility).</p><p>One of the solutions to handle this problem is to use non deterministic methods that give good results but not the perfect one. These methods are called metaheuristics.</p><h4>What is a metaheuristic?</h4><p>A metaheuristic is an optimization algorithm that does not guarantee that a globally optimal solution can be found on some class of problems. it’s a stochastic (non-deterministic) and iterative algorithm that starts with a random solution and tries to fit in during the execution. Metaheuristics are often used on difficult and complex optimization problems and they are generally inspired by nature, for example, simulated annealing (inspired from physics), ant colony optimization (inspired from etiology), evolutionary algorithms (inspired from biology).</p><h4>Genetic algorithm</h4><p>In my case, I’m going to use a population based metaheuristic algorithm called Genetic algorithm inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms (EA).</p><p>Genetic algorithm is based on these elements:</p><ul><li><strong>Population:</strong> it represents a set of solutions to our problem.</li><li><strong>Chromosome:</strong> is a set of parameters that define a proposed solution to the problem that the genetic algorithm is trying to solve.</li><li><strong>Gene:</strong> is an elementary information of the chromosome. it could be binary or not.</li><li><strong>Generation:</strong> is the transition from a population to another. generally, we set the generation number and make it the termination point.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/287/1*_FIHPCDAJqxuDew2Kf5YNQ.png" /></figure><ul><li><strong>Fitness function:</strong> is an <a href="https://en.wikipedia.org/wiki/Objective_function">objective function</a> that is used to summarize how close a given design solution is to achieving the set aims. The fitness function is used to guide simulations towards optimal design solutions. For a single-objective problem, the evaluation is systematic; however, for a multi-objective problem, we speak of a Pareto-front that characterizes several optimal solutions and then judges them according to some criteria.</li><li><strong>Selection:</strong> In each successive generation, a portion of the existing population is selected to generate a new generation. Individual solutions are selected by a fitness-based process, where the best-fitting solutions are generally more likely to be selected. we can list many types of selection, Roulette Wheel <strong>Selection</strong>, Rank <strong>Selection</strong>, Tournament <strong>Selection…</strong></li><li><strong>Crossover: </strong>used to combine the genetic information of two parents to generate new offspring. This is a way to stochastically generate new solutions from an existing population.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/231/1*znfzI09uK-Xs8TST63lEBg.png" /><figcaption>Single point crossover</figcaption></figure><ul><li><strong>Mutation:</strong> used to maintain genetic diversity from one generation of a population of genetic algorithm chromosomes to the next.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/527/1*8o_tWfyCc85A5wSQczMcRA.png" /></figure><p>Here are quick steps for how the genetic algorithm works:</p><ol><li><strong>Initial Population</strong>– Initialize the population randomly based on the data.</li><li><strong>Fitness function</strong>– Find the fitness value of each chromosome.</li><li><strong>Selection</strong>– Select the best fitted chromosomes as parents to pass the genes for the next generation and create a new population</li><li><strong>Cross-over</strong>– Create a new set of chromosomes by combining the parents and add them to a new population set</li><li><strong>Mutation</strong>– Perform mutation which alters one or more gene values in a chromosome in the new population set generated.</li></ol><p>The obtained population will be used in the next generation</p><p><strong>Repeat steps 2 to 5 again for each generation</strong></p><h3>Multi type of capacitated vehicle routing problem</h3><p>In this application, I’ll try to solve a multi type of capacitated vehicle open routing problem (MCORT) using a genetic algorithm and Deap library in python.</p><p>The <strong>vehicle routing problem</strong> (<strong>VRP</strong>) is an optimization problem that asks, What is the optimal set of routes for a fleet of vehicles to traverse in order to deliver to a given set of customers?.. The objective of the VRP is to minimize the total route cost and the number of vehicles.</p><p>Also in this case we are dealing with ships not vehicles (just for the fun)</p><p><strong>Hypothesis:</strong></p><ul><li>We are dealing with only one order</li><li>No time windows within which the deliveries (or visits) must be made.</li><li>Ships are not required to return to the depot (open routing problem).</li></ul><p><strong>Objective:</strong></p><ul><li>finding the shortest path that Minimizes the global transportation cost based on the global distance</li><li>Minimize and optimize the number of ships needed to serve the customer</li></ul><p>While the most important parts of our GA is built from scratch, we’ll use a few standard packages to make things easier:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f617f642f6e393ce579710238a0447a/href">https://medium.com/media/0f617f642f6e393ce579710238a0447a/href</a></iframe><h4>Create the initial map</h4><p>We first create a map that gives the distance between every two points (for an impossible road we give it a very large number). the p_inf controls the percentage of impossible roads. This function returns an N by N matrix.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b4ad635007e98e63cb8b9743ab5b9b3/href">https://medium.com/media/6b4ad635007e98e63cb8b9743ab5b9b3/href</a></iframe><p>We create now a Navire class that will allow us to create and handle our navies.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/776d17f8554329ce04a7e8cf5fe6c975/href">https://medium.com/media/776d17f8554329ce04a7e8cf5fe6c975/href</a></iframe><p>To evaluate our solution we need to create a fitness function. To do so, because this is a minimizing multi objective problem, we associate each impossible solution with a very large number. This function returns a tuple that contains the route distance and the number of ships.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a667452d3eac30462f720c025721bd06/href">https://medium.com/media/a667452d3eac30462f720c025721bd06/href</a></iframe><h4>Create population</h4><p>Our genotype (how the solution will be coded) will contain three parts, the first one is the road that all the navies will follow, the second one will be a list of four elements each one represent the chosen number of each type of navies for the trip, and the third part represents how the order will be distributed on the different types of ships.</p><p>ADD A PICTURE</p><p>We now create a function that distributes the order on the different types of ships. First of all, we select a random type of navie and we try to fill it up with the order, if it’s not sufficient we fill the available capacity and we complete with another type who is also chosen randomly.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b3e8a954b8ac2b2cfc7b08c12ade806/href">https://medium.com/media/5b3e8a954b8ac2b2cfc7b08c12ade806/href</a></iframe><p>We now can make our initial population (aka first generation). To do so, we need a way to create a function that produces a chromosome that follows our genotype.</p><p>the first part is the road that will follow all the ships starting from <strong>depart</strong> to <strong>arrivee</strong> (this means all the roads start by departure and end by arrival), the second part of our chromosome is created by choosing for each type of ship a random number between zero and the max number of ships that satisfy the order, and the last part is the distribution of the order on the ships.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d155d47d6d0068a88ff4c0c722a1e982/href">https://medium.com/media/d155d47d6d0068a88ff4c0c722a1e982/href</a></iframe><blockquote>Note: We only have to use these functions to create the initial population. Subsequent generations will be produced through crossover and mutation.</blockquote><h3>Genetic operators</h3><h4>Selection</h4><p>We are going to proceed with the NSGA (nondominated sorting genetic algorithm) which is already implemented in the Deap library.</p><h4>crossover</h4><p>With the population created, we can create the next generation by breeding our solutions. We simply pick two crossover points and splice the two lists together to produce an offspring.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0587471859a11c2b792a48fdf52f10cd/href">https://medium.com/media/0587471859a11c2b792a48fdf52f10cd/href</a></iframe><h4>Mutation</h4><p>Mutation helps to avoid local convergence by introducing some randomness to our chromosomes that will allow us to explore other parts of the solution space.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/acdc945aaeff4e3732e33ad1cede0871/href">https://medium.com/media/acdc945aaeff4e3732e33ad1cede0871/href</a></iframe><blockquote>Note: we specify a probability of having a crossover and mutation.</blockquote><p>Another design feature to consider is the use of <strong>elitism</strong>. With elitism, the best performing individuals from the population will automatically carry over to the next generation, ensuring that the most successful individuals persist.</p><h4>Statistics</h4><p>To keep an eye on the evolution of the generations, we are going to use the statistical tools implemented in the Deap library</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8790dceabf327710eb23b6795f9ebae2/href">https://medium.com/media/8790dceabf327710eb23b6795f9ebae2/href</a></iframe><h4>Setting parameters</h4><p>we set tuning parameters for our genetic algorithms, and we are also going to fix our initial map (for this example, departure is the node number 0 and the arrival is the node 5).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/afba35e5a210a5b347206f1842b775f6/href">https://medium.com/media/afba35e5a210a5b347206f1842b775f6/href</a></iframe><h4>Run the evolution</h4><p>let&#39;s put all the parts together and run the evolution</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cb49bc83b3571151aa0dcf5d28d58045/href">https://medium.com/media/cb49bc83b3571151aa0dcf5d28d58045/href</a></iframe><h4>Results</h4><p>we plot the results of each generation</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c68bd714d88c968704be7e6fe3310d4b/href">https://medium.com/media/c68bd714d88c968704be7e6fe3310d4b/href</a></iframe><h4>Conclusion</h4><p>I hope this was a fun, hands-on way to learn how to build your own GA. Try it for yourself and see how fast it goes. Or go further and try to implement a GA on another problem set (More complicated one to see the limits); see how you would change the genetic operators (crossovers, mutation, selection, …) to handle other types of chromosomes. We’re just scratching the surface here!</p><h3>Bibliography</h3><ul><li><a href="https://web.njit.edu/~horacio/Math451H/download/Seshadri_NSGA-II.pdf">https://web.njit.edu/~horacio/Math451H/download/Seshadri_NSGA-II.pdf</a></li><li><a href="https://github.com/najid110/Genetic-algorithm-MOGA/blob/main/Genetic%20algo%20-%20code%20source.ipynb">https://github.com/najid110/Genetic-algorithm-MOGA/blob/main/Genetic%20algo%20-%20code%20source.ipynb</a></li><li>pictures from Wikipedia</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=399135f6357a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>