{"id":15575910,"url":"https://github.com/virresh/evolutionary_search_algorithms","last_synced_at":"2025-04-09T17:31:24.117Z","repository":{"id":122370450,"uuid":"172948661","full_name":"virresh/evolutionary_search_algorithms","owner":"virresh","description":"Implementation of Genetic Algorithm, Memetic Algorithm and Constraint Satisfaction on a Time Table scheduling problem. Also has an implementation of MiniMax Strategy for TicTacToe ","archived":false,"fork":false,"pushed_at":"2019-02-27T16:17:46.000Z","size":260,"stargazers_count":7,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-23T19:39:06.320Z","etag":null,"topics":["ai","alpha-beta-pruning","artificial-intelligence","constraint-satisfaction-problem","csp","ga","genetic-algorithm","ma","memetic-algorithms","min-max","scheduling-algorithms","tictactoe"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/virresh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-02-27T16:17:20.000Z","updated_at":"2024-03-07T07:06:48.000Z","dependencies_parsed_at":"2024-06-07T09:32:39.033Z","dependency_job_id":null,"html_url":"https://github.com/virresh/evolutionary_search_algorithms","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/virresh%2Fevolutionary_search_algorithms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/virresh%2Fevolutionary_search_algorithms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/virresh%2Fevolutionary_search_algorithms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/virresh%2Fevolutionary_search_algorithms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/virresh","download_url":"https://codeload.github.com/virresh/evolutionary_search_algorithms/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248077304,"owners_count":21043936,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","alpha-beta-pruning","artificial-intelligence","constraint-satisfaction-problem","csp","ga","genetic-algorithm","ma","memetic-algorithms","min-max","scheduling-algorithms","tictactoe"],"created_at":"2024-10-02T18:41:09.692Z","updated_at":"2025-04-09T17:31:24.011Z","avatar_url":"https://github.com/virresh.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"---\ntitle: Artificial Intelligence Programming Assignment 2 \nauthor: Viresh Gupta | Roll No. - 2016118\ndate: 12 October 2018\n\n---\n\n##  Q1. User vs Computer TicTacToe with min-max and alpha-beta pruning  \n###  Methodology:  \nBoth min-max and alpha-beta pruning have been implemented with no randomisation.\ni.e if at any point, there are two states with the same utility value, the algorithm will choose\nthe state that occurs first.  \n\nNo memoization is done, so the computer calculates all steps everytime.\n\nBy default, the computer uses only min-max strategy without alpha-beta pruning. To use alpha-beta\npruning, pass the `--alphabeta` switch.\n\n#### Usage:\nTo use the program and play against it, type:\n```bash\npython PA2_2016118_Viresh_Gupta_minmax.py\n```\nTo give input from a file instead of choosing steps interactively:\n```bash\npython PA2_2016118_Viresh_Gupta_minmax.py \u003c user_moves.txt\n```\nTo use alpha-beta pruning, use:\n```bash\npython PA2_2016118_Viresh_Gupta_minmax.py --alphabeta\n```\n\n### Experiments:\nI calculated the time taken for each algorithm, by using the unix `time` command.\ne.g\n```bash\ntime python PA2_2016118_Viresh_Gupta_minmax.py \u003c user_moves.txt\ntime python PA2_2016118_Viresh_Gupta_minmax.py --alphabeta \u003c user_moves.txt\n```\n\n### Observations:\n\n+---------+----------+-------------+\n| time    | Min-Max  | Alpha-Beta  |\n+=========+==========+=============+\n|real     | 0m 1.293s| 0m 0.132s   |\n+----------------------------------+\n|user     | 0m 1.280s| 0m 0.109s   |\n+----------------------------------+\n|sys      | 0m 0.012s| 0m 0.020s   |\n+----------------------------------+\n\n### Inference:\nThus, we can observe that the time taken when using alpha-beta pruning strategy for making a decision is considerably smaller, and\nthe alpha-beta strategy is giving a big boost to the decision making (~ 10 times).\n\n------------------------------------------\n\n##  Q2. Course time scheduling using GA, MA and CSP\n\n### Assumptions:\nI have taken the following assumption:\n\n- A class fits completely into one time slot.\n- No class needs to span for more than one slot.\n\nA clash is indicated when a professor is teaching two times in a given time slot.  \nSimilarly a venue clash occurs when two or more courses are being taught in the same venue at the\nsame day-slot combination.  \n\n### a. Logical constraints on the given problem:  \nVarious logical constraints that I have taken on the problem are:\n\n- No professor is teaching at two places at a time.\n- Only one course is taught at a venue at a time.\n- A course has class only once a week, in one of the given time slots\n- The institute is a *basic* CS institute, where each professor has basic knowledge of each CS domain, so\n  they can teach any course floated at the institute.\n\n### b. Implementations using Genetic and Memetic learning\n\n#### Methodology:\nI take up five parameters for a gene:\n\n- Day of week (1-5)\n- Slot of day (1-8)\n- Hall number (1-N)\n- Course number (1-M)\n- Professor id (1-P)\n\nA gene completely determines a course's slot in that week, since there is only one class per course in a week.  \n\nA chromosome is thus comprised of M genes, one for each course, and this determines the schedule for the week.  \n\n##### Usage:\n\nThe code for GA can be run as:\n```\npython PA2_2016118_VireshGupta_GA_MA.py\n```\n\nThe code for MA can be run as:\n```\npython PA2_2016118_VireshGupta_GA_MA.py --ma\n```\n\nTo see a full experiment successfully converging, the parameters for the number of days / slots and courses,\ninstructors and halls can be changed in the python file and number of iterations increased.\n\nAfter changing the default params:\n```\npython PA2_2016118_VireshGupta_GA_MA.py -i 40 --ma\n```\n\n#### Experiments and Observation:\n\nI tested the implementations with 50 courses, 10 professors and 10 halls.\nThus with 50 (\u003e 8 * 5 = 40) courses, the initial population is expected to have some small number\nof clashes, since naively putting every course in every slot won't work.  \n\nCrossovers are single point rectangular crossovers, (one cut in the horizontal direction, \nswap and join, and then one cut in the vertical direction, then swap).  \n\nMutations are completely eradicatory, a chromosome that undergoes a mutation can be disrupted and replaced with a\nfully randomised chromosome, or just the (hall, professor, course) combination gets randomised in a chromosome.  \n\nFor memetic algorithm, the hillclimbing permutes the chromosome genes across properties to evaluate a nearby\nchromosome, thus leading to better parent selection before crossovers.  \nThis has significant benefits over GAs in terms of faster convergence to a valid schedule.  \n\nA sample run of my GA implementation:\n```\ngeneration #0 census 15 Best of this generation  0.3332555736994701 clash val 6\ngeneration #1 census 24 Best of this generation  0.4998000799680128 clash val 4\ngeneration #2 census 33 Best of this generation  0.4998000799680128 clash val 4\ngeneration #3 census 42 Best of this generation  0.4998000799680128 clash val 4\ngeneration #4 census 51 Best of this generation  0.4998000799680128 clash val 4\ngeneration #5 census 19 Best of this generation  0.4998000799680128 clash val 4\ngeneration #6 census 28 Best of this generation  0.4998000799680128 clash val 4\ngeneration #7 census 37 Best of this generation  0.4998000799680128 clash val 4\ngeneration #8 census 46 Best of this generation  0.4998000799680128 clash val 4\ngeneration #9 census 55 Best of this generation  0.4998000799680128 clash val 4\ngeneration #10 census 19 Best of this generation  0.4998000799680128 clash val 4\ngeneration #11 census 28 Best of this generation  0.4998000799680128 clash val 4\ngeneration #12 census 37 Best of this generation  0.4998000799680128 clash val 4\ngeneration #13 census 46 Best of this generation  0.4998000799680128 clash val 4\ngeneration #14 census 55 Best of this generation  0.4998000799680128 clash val 4\ngeneration #15 census 19 Best of this generation  1250.0 clash val 0\ngeneration #16 census 28 Best of this generation  1250.0 clash val 0\ngeneration #17 census 37 Best of this generation  1250.0 clash val 0\ngeneration #18 census 46 Best of this generation  1666.6666666666667 clash val 0\ngeneration #19 census 55 Best of this generation  1666.6666666666667 clash val 0\n```\n\nA sample run of my MA implementation:\n```\ngeneration #0 census 15 Best of this generation  0.49972515116685823 clash val 4\ngeneration #1 census 24 Best of this generation  0.4998000799680128 clash val 4\ngeneration #2 census 33 Best of this generation  0.4998000799680128 clash val 4\ngeneration #3 census 42 Best of this generation  0.4998000799680128 clash val 4\ngeneration #4 census 51 Best of this generation  0.9993004896572399 clash val 2\ngeneration #5 census 19 Best of this generation  0.9993004896572399 clash val 2\ngeneration #6 census 28 Best of this generation  1000.0 clash val 0\ngeneration #7 census 37 Best of this generation  1000.0 clash val 0\ngeneration #8 census 46 Best of this generation  1000.0 clash val 0\ngeneration #9 census 55 Best of this generation  1000.0 clash val 0\ngeneration #10 census 19 Best of this generation  1000.0 clash val 0\ngeneration #11 census 28 Best of this generation  1000.0 clash val 0\ngeneration #12 census 37 Best of this generation  1000.0 clash val 0\ngeneration #13 census 46 Best of this generation  1000.0 clash val 0\ngeneration #14 census 55 Best of this generation  1000.0 clash val 0\ngeneration #15 census 19 Best of this generation  1000.0 clash val 0\ngeneration #16 census 28 Best of this generation  1000.0 clash val 0\ngeneration #17 census 37 Best of this generation  1250.0 clash val 0\ngeneration #18 census 46 Best of this generation  1250.0 clash val 0\ngeneration #19 census 55 Best of this generation  1250.0 clash val 0\n```\n\n#### Observations:\nThe initial population has around 6 clashes in the most fit schedule, and with generations passing by, we\nget better results.  \nBy $15^{th}$ generation, we get 0 clashes and by $19^{th}$ generation we have improvised upon this schedule even\nfurther.\n\n#### Inference:\nWe can infer that GAs often suffer from the problem that the population is not improving after each generation, it may\nhappen that a single most chromosome is the best one out of all the rest and thus it persists for a lot of generations\nbefore a mutation occurs and leads to a better solution.  \n\nWhereas for MAs, this problem is not that significant unless a local maxima is encountered.  \n\n### c. Comparision between GA and MA\n\nIn my implementations, achieving a no clash schedule is not the only objective that the algorithm needs to achieve.  \nIn addition to clashes, the fitness function also takes into account:  \n- Free slots (the less free slots, the better)\n- Free professors (the less free professors, the better) \n\nThus even after achieving a no clash schedule, the algorithm can evolve further so that the load is evenly distributed\nacross professors and the classes are not all conducted in the same day, thus providing sufficient time for repair and\nmaintenance of the institute by having classes spread over venues on different days and time.\n\nGA and MA both are able to reach no collision schedule, although once MA has achieved no clash, the evolution in that\nschedules happens very slowly, which most likely can be attributed to a local maxima, due to which it needs to wait for\na favorable mutation.  \nWhereas in a GA, once we have reached a no clash schedule, the evolution happens at the same pace, and we often are able\nto achieve a more fit solution, although it takes more iterations.\n\nMA is able to find a solution faster than GA, because of the inherent improvement in the parent selection process.  \n\nPlots for the same:  \n(Can be replicated using : `python plotter.py` )\n\n![Cost Plot](/mnt/Common/IIITD18m/AI/ProgrammingAssignment2/cost_plot.png)\n![Clash Plot](/mnt/Common/IIITD18m/AI/ProgrammingAssignment2/clash_plot.png)\n\n### e. Solution using Constraint Satisfaction Problem Approach\n\nFor this approach, I use the same logical constraints as defined in part a). This helps in comparing the two approaches.  \nFor the CSP algorithm, since only one class per course exists in a week's schedule, I am representing the schedule again\nas a 5xM matrix, with column i (0-indexed) representing course i+1.\n\nFor the main algorithm, I put a valid combination one-by-one for each course (i.e one column at a time). In case the\naddition of the column makes the schedule invalid, I take a step back and using the type of clash returned from a\nclash calculation function, I choose what action to take:  \n- Whether to replace the time slot\n- Whether to replace the professor\n- Whether to replace the venue\n\nAll these decisions are taken intelligently. Thus we obtain a valid solution once all M courses have been placed\nsuccessfully and the algorithm terminates. If at any point of time, it so happens that a course cannot be put without\nviolating one of the constraints, there is no schedule possible, and my algorithm terminates with an indication that no\nsuch schedule can exist.\n\n##### Usage:\n```\npython PA2_2016118_Viresh_Gupta_CSP.py\n```\n\n### Inference:\nThe runtime of finding a solution using CSP is much much lesser than the one found by GA, and by choosing an appropriate\nreplacement strategy for clashes, we can also develop a spread-out schedule. Thus CSP are more useful in finding a solution\nfor a shorter instance of the problem, but they fail terribly when there is no clash-free solution.  \n\nIn such a case we have to utilize GA/MA approaches to get a schedule that is not completely clash-free, but has minimum\nclashes.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvirresh%2Fevolutionary_search_algorithms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvirresh%2Fevolutionary_search_algorithms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvirresh%2Fevolutionary_search_algorithms/lists"}