
    
        
        
        
                
        
        
        
                
        
        
        
                
        
        
        
            
{"version":"https:\/\/jsonfeed.org\/version\/1","title":"mathspp.com feed","home_page_url":"https:\/\/mathspp.com\/blog\/tags\/simulation","feed_url":"https:\/\/mathspp.com\/blog\/tags\/simulation.json","description":"Stay up-to-date with the articles on mathematics and programming that get published to mathspp.com.","author":{"name":"Rodrigo Gir\u00e3o Serr\u00e3o"},"items":[{"title":"Deducing physics formulas with genetic algorithms","date_published":"2024-03-01T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/deducing-physics-formulas-with-genetic-algorithms","url":"https:\/\/mathspp.com\/blog\/deducing-physics-formulas-with-genetic-algorithms","content_html":"<p>This tutorial shows how to use a simple genetic algorithm to deduce physics formulas.<\/p>\n\n<h2 id=\"what-s-a-genetic-algorithm\">What's a genetic algorithm?<a href=\"#what-s-a-genetic-algorithm\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>A genetic algorithm is an algorithm that borrows ideas from Darwin's theory of evolution to find solutions to optimisation problems.\nSo, if you have a problem and if you have a way of determining how good or bad a solution is to that problem, then a genetic algorithm can be used to run a simulation that tries to find better solutions to that problem.<\/p>\n<p>In order to be able to run a genetic algorithm, you need to be able to do a couple of different things:<\/p>\n<ol><li>you need to be able to generate random possible solutions to the problem;<\/li>\n<li>you need a way to quantify how good, or how bad, a given solution is;<\/li>\n<li>you need to have a way to take two solutions and combine them to create a third solution; and<\/li>\n<li>you need to be able to make small random changes to existing solutions.<\/li>\n<\/ol><p>The steps of combining solutions and mutating solutions are supposed to emulate the natural process of evolution as described by Darwin's theory of evolution.<\/p>\n<p>Step 1. is how the genetic algorithm starts.\nYou start by creating a number of random solutions, to which you call the <em>population<\/em>.\nEach solution is an <em>individual<\/em>.<\/p>\n<p>Then, you repeat steps 2 - 4 a number of times.\nFor each repetition, you figure out how good each individual in the population is.\nYou call <em>fitness<\/em> or <em>fitness level<\/em> to the value that quantifies how good a solution is.<\/p>\n<p>In Darwin's theory of evolution, the fittest individuals of a species tend to survive and reproduce while the least fit tend to die.\nThe same thing will happen in our genetic algorithm.\nWe pick the fittest individuals, let them reproduce, give them a change to mutate, and then we repeat this.<\/p>\n<h2 id=\"deducing-a-physics-formula\">Deducing a physics formula<a href=\"#deducing-a-physics-formula\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>Genetic algorithms can be implemented to any type of problem you can think of, as long as you're able to implement the different steps outlined above.\nIn this article we will try to deduce the formula that determines the position of a body in motion as a function of time, given its initial position, initial velocity, and acceleration.\nThe formula looks like this:<\/p>\n<p class=\"mathjax mathjax--block\">\\[\nx(t) = \\frac12 a t^2 + v_0t + x_0\\]<\/p>\n<p>In the formula above, we have that<\/p>\n<ul><li><span class=\"mathjax mathjax--inline\">\\(x_0\\)<\/span> is the initial position of the body;<\/li>\n<li><span class=\"mathjax mathjax--inline\">\\(v_0\\)<\/span> is the initial velocity of the body;<\/li>\n<li><span class=\"mathjax mathjax--inline\">\\(a\\)<\/span> is the acceleration of the body;<\/li>\n<li><span class=\"mathjax mathjax--inline\">\\(t\\)<\/span> is the time for which we want to compute the position; and<\/li>\n<li><span class=\"mathjax mathjax--inline\">\\(x(t)\\)<\/span> is the position of the body after <span class=\"mathjax mathjax--inline\">\\(t\\)<\/span> seconds.<\/li>\n<\/ul><p>In our simulation, we'll have to start by creating random formulas that will be nowhere close to the correct formula shown above.\nThen, we'll figure out what are the formulas that better approximate the correct formula, we'll mix and match those formulas, and we'll mutate some of them.\nWe'll repeat this a number of times and by the end of...<\/p>","summary":"This tutorial shows how to use a simple genetic algorithm to deduce physics formulas.","date_modified":"2025-07-23T16:49:02+02:00","tags":["algorithms","optimisation","programming","python","simulation"],"image":"\/user\/pages\/02.blog\/deducing-physics-formulas-with-genetic-algorithms\/thumbnail.webp"},{"title":"Elo rating system simulation","date_published":"2022-01-25T00:00:00+01:00","id":"https:\/\/mathspp.com\/blog\/elo-rating-system-simulation","url":"https:\/\/mathspp.com\/blog\/elo-rating-system-simulation","content_html":"<p>Join me as I create a simulation that tries to test an Elo-based rating system for quizzes.<\/p>\n\n<figure class=\"image-caption\"><img title=\"Screenshot of the simulation web app.\" alt=\"Screenshot of a Python Streamlit web app.\" src=\"\/images\/a\/1\/3\/6\/2\/a1362e99125e7c0764cc35a8e9d02d4a224f3260-thumbnail.webp\"><figcaption class=\"\">Screenshot of the simulation web app.<\/figcaption><\/figure><h2 id=\"introduction\">Introduction<a href=\"#introduction\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>In this article, I will tell you about a simulation that I built.\nThis simulation is built around a rating system that is based off of the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Elo_rating_system\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Elo rating system<\/a>,\nand I will use <a href=\"\/blog\/til\/026\">Streamlit<\/a> to build the app that will control the simulation.\nThis will also make it easier to share the simulation with others.<\/p>\n<p>In short, I'll be walking you through the process of writing <a href=\"https:\/\/github.com\/mathspp\/elo-simulation\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">this code<\/a> and,\nin the end, the code I linked to in that GitHub repository is going to be used\nto host the Streamlit web app that you can play with live, <a href=\"https:\/\/share.streamlit.io\/mathspp\/elo-simulation\/main\/simulation.py\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">here<\/a>.<\/p>\n<p>The screenshot above also refers to the same Streamlit web app.<\/p>\n<h2 id=\"elo-based-rating-system-for-quizzes\">Elo-based rating system for quizzes<a href=\"#elo-based-rating-system-for-quizzes\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>What I'm trying to do is design a decent rating system for a quiz app.\nThe idea is that the quiz questions will have a rating,\nand so will the &ldquo;players&rdquo;: the people answering the quiz.<\/p>\n<p>Every time a person answers a question, their rating gets adjusted depending on whether they answered the question correctly.\nSimilarly, the question's rating gets updated.\nIf the player answers correctly, the player's rating goes up and the question's goes down.\nIf the player answers incorrectly, the ratings change the other way around.<\/p>\n<p>So, how can we build such a system?<\/p>\n<p>The <a href=\"https:\/\/en.wikipedia.org\/wiki\/Elo_rating_system\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Elo rating system<\/a> is a system that attributes a numeric rating to players,\nin order to determine the relative skill of the players.\nThe Elo rating is probably best known for being the rating used in chess.<\/p>\n<p>I have seen the website chess.com use an Elo-like system for their chess problems:\nyou play the problem and, if you get it right, your problem-solving rating increases;\nat the same time, the rating of the problem decreases.\nIf you get it wrong, vice-versa.\nI want to try and mimic this functionality.<\/p>\n<p>The purpose of building a simple simulation is to try and see if my assumptions\nabout the rating system work, or if the system will go bonkers when used in real life!\nOf course, the simulation will represent a simplification of reality:<\/p>\n<ul><li>if the simulation shows bad behaviour, then the system probably won't work in real life; but<\/li>\n<li>if the simulation shows good behaviour, then <em>maybe<\/em> the system will work in real life.<\/li>\n<\/ul><h2 id=\"updating-the-rating\">Updating the rating<a href=\"#updating-the-rating\" class=\"toc-anchor after\" data-anchor-icon=\"#\" aria-label=\"Anchor\"><\/a><\/h2>\n<p>The key thing that I need is the formula to update the rating when two players face each other off.\nIf we scroll the Wikipedia article to the section that mentions the mathematical details,\nwe get what we need.<\/p>\n<p>Suppose that players <span class=\"mathjax mathjax--inline\">\\(A\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(B\\)<\/span> have ratings <span class=\"mathjax mathjax--inline\">\\(R_A\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(R_B\\)<\/span>, respectively.\nThe Elo rating system determines how well it <em>thinks<\/em> each player should perform,\nwhen compared to the other player.\nIn other words, the ratings <span class=\"mathjax mathjax--inline\">\\(R_A\\)<\/span> and <span class=\"mathjax mathjax--inline\">\\(R_B\\)<\/span> don't really say how good &ndash; or how bad &ndash;\neach player is; the ratings only say...<\/p>","summary":"Join me as I create a simulation that tries to test an Elo-based rating system for quizzes.","date_modified":"2025-07-23T16:49:02+02:00","tags":["chess","mathematics","programming","python","simulation","streamlit"],"image":"\/user\/pages\/02.blog\/elo-rating-system-simulation\/thumbnail.webp"},{"title":"Solving diophantine equations with random walks","date_published":"2017-10-29T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/random-walks-diophantine-equations","url":"https:\/\/mathspp.com\/blog\/random-walks-diophantine-equations","content_html":"<p>Here's how I like to solve my equations: just walk around randomly until I trip over a solution!<\/p>\n\n<p><img alt=\"A 3D graph showing the random walk evolution\" src=\"\/images\/c\/2\/e\/f\/5\/c2ef5310f3331f816a89f762538218c2db628093-erdos1all.webp\"><\/p>\n<p>Random walks (check my older post <a href=\"\/blog\/random-walk-simulations\">here<\/a>) and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Diophantine_equation\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Diophantine equations<\/a> are two simple mathematical beasts. After a college seminar, I tried putting them together to make something neat, and came up with this: just pick a Diophantine equation, simulate a random walk, and try to see if the random walk went over any solutions! In <a href=\"https:\/\/drive.google.com\/open\" id=\"0ByBeLS6ciLYVY2hTYU9TV2lUb1k\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">this report<\/a> I briefly go over how I came up with this, and show the code I wrote\/some results. The Matlab code is all in <a href=\"https:\/\/github.com\/RodrigoGiraoSerrao\/projects\/tree\/master\/randomWalks\/diophantineEqs\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">here<\/a> (I also have some Python code about random walks <a href=\"https:\/\/github.com\/RodrigoGiraoSerrao\/projects\/tree\/master\/randomWalks\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">here<\/a>, check <a href=\"https:\/\/mathspp.blogspot.pt\/2017\/10\/random-walk-simulations.html\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">my post<\/a>!).<\/p>\n<p>In the image above, the blue line represents a path taken by the random walk and in orange\/red the nodes tell us how far we are from a solution (the smaller the node, the closer we are). Note that this notion of proximity isn't given by actually computing the distance to a known solution, but just by looking at how similar the two sides of the equation are. The image below shows the evolution of three independent random walks trying to find an example solution of <span class=\"mathjax mathjax--inline\">\\(\\frac{4}n = \\frac1x + \\frac1y + \\frac1z\\)<\/span> with <span class=\"mathjax mathjax--inline\">\\(n = 25\\)<\/span> (that expression is the expression of the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Erd%C5%91s%E2%80%93Straus_conjecture\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Erd\u00f6s-Straus conjecture<\/a>).<\/p>\n<p><img alt=\"Three random walks in 3D\" src=\"\/user\/pages\/02.blog\/random-walks-diophantine-equations\/erdos25Pos.webp\"><\/p>","summary":"In this post I try to solve some diophantine equations with random walks.","date_modified":"2024-08-13T12:47:46+02:00","tags":["mathematics","programming","diophantine equations","matlab","number theory","simulation","visualisation"],"image":"\/user\/pages\/02.blog\/random-walks-diophantine-equations\/erdos1All.webp"},{"title":"Random walk simulations in 2D","date_published":"2017-10-26T00:00:00+02:00","id":"https:\/\/mathspp.com\/blog\/random-walk-simulations","url":"https:\/\/mathspp.com\/blog\/random-walk-simulations","content_html":"<p>Think of a drunk man that continuously tumbles left and right, back and forth, with no final destination.<\/p>\n\n<p><img alt=\"A blue 2D random walk on a green background\" src=\"\/images\/e\/f\/d\/f\/2\/efdf2de1162ef1c66911c2dba75dd7f9e81cc52a-2drandomwalk.webp\"><\/p>\n<p>This drunk man walking around can be thought of as a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Random_walk\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">random walk<\/a> on the plane... Now imagine that the drunk man has a shorter leg and tumbles more to one of the sides: that is a biased random walk. Now imagine the drunk man can teleport to a nearby location. That is (kind of) a <a href=\"https:\/\/en.wikipedia.org\/wiki\/L%C3%A9vy_flight\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">L\u00e8vy flight<\/a>! All those are quite interesting to observe in motion and I implemented them in Python. (the code can be found <a href=\"https:\/\/github.com\/RodrigoGiraoSerrao\/projects\/tree\/master\/randomWalks\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">here<\/a>). From those, only the tilted walks and the one with the trail are such that the particle's movement wraps around the borders (leaving the green area makes the particle appear in the opposite side, just like the snake from the game Snake).<\/p>\n<p>The image above is a screenshot of the execution of the random walk that leaves a coloured trail.<\/p>\n<p>I also implemented an animation where the screen gets progressively filled with circles of different colours, as if being splattered with paint. Not sure why... But it looks interesting:<\/p>\n<p><img alt=\"A black background with several randomly-coloured circles\" src=\"\/user\/pages\/02.blog\/random-walk-simulations\/splatter.webp\"><\/p>\n<p>The implementation is in the same directory as the walks.<\/p>\n<p>For all these, I got windows executables. <a href=\"https:\/\/drive.google.com\/open\" id=\"0ByBeLS6ciLYVX1k0M2Z2Z2RjYkU\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">Here<\/a> you can find all the executables for the random walks and <a href=\"https:\/\/drive.google.com\/open\" id=\"0ByBeLS6ciLYVcDh0a051T3plRlk\" target=\"_blank\" rel=\"nofollow noopener noreferrer\" class=\"external-link no-image\">here<\/a> for the paint splatter.<\/p>","summary":"Check what a 2D random walk really is and how it looks like.","date_modified":"2024-08-13T12:47:46+02:00","tags":["mathematics","programming","random walks","simulation","visualisation","pygame","python"],"image":"\/user\/pages\/02.blog\/random-walk-simulations\/2d_random_walk.webp"}]}
