<?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 Usman Noor on Medium]]></title>
        <description><![CDATA[Stories by Usman Noor on Medium]]></description>
        <link>https://medium.com/@musmannoor2004?source=rss-eb406b2b08a1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*zId3TB2umH_4UMti</url>
            <title>Stories by Usman Noor on Medium</title>
            <link>https://medium.com/@musmannoor2004?source=rss-eb406b2b08a1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 12 Nov 2026 18:17:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@musmannoor2004/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[Generic Algorithm]]></title>
            <link>https://medium.com/@musmannoor2004/generic-algorithm-340b194ecc6c?source=rss-eb406b2b08a1------2</link>
            <guid isPermaLink="false">https://medium.com/p/340b194ecc6c</guid>
            <dc:creator><![CDATA[Usman Noor]]></dc:creator>
            <pubDate>Sun, 02 Apr 2023 14:44:25 GMT</pubDate>
            <atom:updated>2023-04-03T05:13:23.987Z</atom:updated>
            <content:encoded><![CDATA[<p>In machine learning, the ability of the machine — more specifically, the mobile Robot — to solve problems on its own is crucial. To ensure that it responds appropriately to the environment, the various circumstances, and the various situations, we must set up some conditions for it. We must go by a set of guidelines and an algorithm while building this kind of machine so that it can learn how to address various issues.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*a2HhmNNSnIXDeljf.png" /></figure><p>One of the easiest ways and an excellent place to start for novices to learn how to build a machine that can solve a problem is with a generic algorithm. Generic algorithms can be used to tackle a variety of issues, including the N-queens problem and maze path finder. To determine if the robot will move forward, backward, right, or left on its own without human intervention, we turn to coding. Coding is a means to instruct machines, or more specifically, robots, to carry out all job independently of human assistance. There is no need for humans to perform work after the code has been written and is functioning correctly.</p><h3><strong>OUR PROJECT:</strong></h3><p>The task assigned to us is to program a computer to navigate a maze in order to discover the fastest and easiest path for a robot to follow. For this, we employ a generic algorithm. As the name suggests, generic means that it is based on chromosomes, which we refer to as the building blocks of the next generation, fitness in the next generation, and many other factors. I will go over each of these factors individually.</p><h3>Main Headings:</h3><p>The main headings or in my case the functions I use in making this project are given.</p><ol><li>Baby</li><li>Path_finder</li><li>Cross_over</li><li>Mutation</li><li>Bubble sort</li></ol><h3>Baby</h3><p>Sorry for an inconvenient name this is used instead for chromosome as we already made one same type of algorithm in CP-1. When I am making this project I thought we already made chromosome so now its time for upgradation and we will make baby now hahahaha!. So far so good this function is used for generating chromosomes for a particular Grid (row,colums) some of the examples of chromosomes are given these chromosomes are from different grids.</p><pre>Baby is: [1, 16, 20, 20, 18, 19, 19, 19, 17, 20]<br>Baby is: [1, 16, 20, 20, 18, 19, 20] etc<br>def baby(row: int, colum: int, pop: int):<br>  babies = []<br>  for _ in range(pop):<br>    sublist = []<br>    sublist.append(1)<br>  for _ in range(1, colum-1):<br>    sublist.append(random.randint(1, row))<br>    sublist.append(row)<br>    babies.append(sublist)<br>  return babies</pre><p>This is nothing but a brief explanation of the chromosomes</p><h3>Path_Finder:</h3><p>Finding a route to the goal is the most crucial step in a maze solver’s generic algorithm. There are many in generic algorithm to find a path like row wise row first and colum first same in the case of colum wise. We must instruct the computer on how the robot should move within a particular chromosome.</p><p>As an example, consider how I implemented the code such that it finds the route of colum wise for both colum first and row first:</p><p><strong>Colum:</strong></p><p>In column wise of column first will always go the next column means if the value is (0,0) then it will go to (1,0) then (2,0) then (3,0)and so on until the it reaches the value of column which is next to it. The given diagram will explain it better.</p><figure><img alt="Colum wise Colum First" src="https://cdn-images-1.medium.com/max/420/1*FZMwESjoInqw_G6aDYfeHw.png" /></figure><p><strong>Row First</strong></p><p>In column wise of row first will always go to the next row means if the value is (0,0) then it will go to (0,1) then (0,2) and so on until it reaches the max value or the next column value is reached. The given diagram will explain it better.</p><figure><img alt="Colum Wise Row First" src="https://cdn-images-1.medium.com/max/405/1*Jx7-7SxqfqT9EZ-QI0n41Q.png" /></figure><p>In path finder we not only determine the path but also the path len() the infesible steps means that the robot crosses the wall or passes through the wall. We have to check it and determine whether the step taken by the robot is allowed or not if not then we will increase the infesible step by one.</p><pre>def fitness(d: list, Mapm: dict, babies: list, k: int):<br>    infesi = 0<br>    for i in range(len(d)-1):<br>        row, colum = d[i]<br>        if row == colum == (len(d)):<br>            break<br>        if Mapm[d[i]][&#39;E&#39;] == 0 and d[i+1] == (row, colum+1):<br>            infesi += 1<br>        elif Mapm[d[i]][&#39;W&#39;] == 0 and d[i+1] == (row, colum-1):<br>            infesi += 1<br>        elif Mapm[d[i]][&#39;N&#39;] == 0 and d[i+1] == (row-1, colum):<br>            infesi += 1<br>        elif Mapm[d[i]][&#39;S&#39;] == 0 and d[i+1] == (row+1, colum):<br>            infesi += 1<br>    return infesi</pre><p>After finding the turns taken by the robot in the given arena, the path and the infesible steps we have to calculate a general answer of all of this for this we use generic algorithm formula given to us in CP-1 course</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/375/1*iSPpK8UTyKuVvRQATx-Pmw.png" /></figure><p>We can manipulate the constants wf,wl&amp;wt as we desire according to our conditions. In the project I uses wf =4,wl=wt=2 the reason is that to make the infesible steps to effect the result more than the turns and the length</p><h3>Cross_Over:</h3><p>Cross over is, in my opinion, the most crucial function in a generic algorithm. Since following a generation, the outcome will rely on the genes that were crossed across.By some unfortunate cross_over that I use is not that much good it is like a hit-and-miss approach to find the answer. Although I know how to make a cross_over good as much as that my program will never fails to find an answer. In that technique which is in my mind not in the project I will make two chromosome to fight and then do some calculation that obviously I am not discussing it here. The cross-over I utilised is the most straightforward, in which I swap out the newborns with those who are fitter after half the population.</p><pre>def crossover(babies: list, pop: int, colum: int, row: int, infesible: list):<br>    if pop % 2 == 0:<br>        cross_over = int((pop/2)-1)<br>    else:<br>        cross_over = int(((pop-1)/2)-1)<br>    pt = random.randrange(colum)<br>    for i in range(0, cross_over+1, 2):<br>        for j in range(int(colum/2)):<br>            babies[cross_over+i][j] = babies[i][j]<br>            babies[cross_over+i+1][j] = babies[i+1][j]<br>        for k in range(pt, colum):<br>            babies[cross_over+i][k] = babies[i+1][k]<br>            babies[cross_over+i+1][k] = babies[i][k]<br>    mutation(babies, pop, row, colum, infesible)<br>    <br>    return babies</pre><h3>Mutation:</h3><p>Just replacing some of the baby indexes with values that are within the bounds of the provided labyrinth is what is meant by mutation. The code is given below</p><pre>def mutation(babies: list, pop: int, row: int, colum: int, infesible: list):<br>    for i in range(0, pop):<br>        index = random.randrange(colum)<br>        if infesible[i] &lt;= 6: continue<br>        if index == 0 or index == colum-1: continue<br>        value = random.randrange(1, row)<br>        babies[i][index] = value<br>    return babies</pre><h3>Bubble Sort:</h3><p>The fitness value is used in Bubble Sort to order the lists, with the fittest individuals rising to the top of the list. I also Sort baby the path and infesible, the turns and all the things as you can see in the given function.</p><pre>def bublesort(dic: dict, babies: list, path: list, Infesible: list, Turns: list, fitness: list, D_B: list, O_B: list, pop: int):<br>    fitt = [];babb = [];Di_B = [];Ori_B = [];infesi = [];Path = [];turn = []<br>    <br>    soort = [(fitness[i], babies[i], Infesible[i], path[i],Turns[i], D_B[i], O_B[i]) for i in range(pop)]<br>    soort.sort(reverse=True)<br>    for fit, bab, infe, pth, tur, D_B, O_B in soort:<br>        fitt.append(fit)<br>        babb.append(bab)<br>        infesi.append(infe)<br>        Path.append(pth)<br>        turn.append(tur)<br>        Di_B.append(D_B)<br>        Ori_B.append(O_B)<br>    for i in range (pop):<br>        dic[tuple(babb[i])] = (infesi[i], Path[i],turn[i], Di_B[i], Ori_B[i], fitt[i])<br>    return fitt, babb, infesi, Di_B, Ori_B, dic</pre><h3><strong>Results:</strong></h3><p>After reviewing the program’s results, I conclude that there is need for improvement because this approach with cross over unaltered is not very effective in greater grids. Sometimes it doesn’t offer the solution, such <strong>25x25</strong> or more.</p><p>A graph of a solvable grid with infeasibility, fitness, paths, and turns is provided so that you can observe the trend of how the answer is calculated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*kkXG_Y-Izr33jdRS2-RWEA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*X6qSdXnMQAJcLTis_s5uBg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*_jI8XwCpewVGv5O3nSjz6g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*M0uRkv_0vuIS6SP4yI06xA.png" /></figure><p>The upper all the graphs are plot with the help of python library of matplotlib</p><h3><strong>ANS OF 20X20 GRID</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6NVYmolqEuH3xEX7w4nqPQ.gif" /></figure><h3>ANS OF 20X15 GRID</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2R_oF7-0B4Q10xjqpvA59Q.gif" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DJAFMncdBVLNb0sAjm8H-A.png" /></figure><h3>GITHUB LINK OF GIST:</h3><p><a href="https://gist.github.com/usmanNoor5/853f879b8d8e77c8f22f612d8954fcde">2021_MC_16_CEP.py (github.com)</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=340b194ecc6c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>