{"id":18807032,"url":"https://github.com/zriyansh/go-boid-go","last_synced_at":"2026-01-08T22:30:17.681Z","repository":{"id":122977146,"uuid":"452247056","full_name":"zriyansh/go-boid-go","owner":"zriyansh","description":null,"archived":false,"fork":false,"pushed_at":"2022-01-28T10:11:22.000Z","size":25,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-12-29T21:30:03.478Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","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/zriyansh.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":"2022-01-26T11:20:14.000Z","updated_at":"2022-01-28T09:20:31.000Z","dependencies_parsed_at":null,"dependency_job_id":"997b1e8e-4c2b-42d4-8afa-d45425cfac36","html_url":"https://github.com/zriyansh/go-boid-go","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/zriyansh%2Fgo-boid-go","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zriyansh%2Fgo-boid-go/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zriyansh%2Fgo-boid-go/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zriyansh%2Fgo-boid-go/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zriyansh","download_url":"https://codeload.github.com/zriyansh/go-boid-go/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239742417,"owners_count":19689309,"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":[],"created_at":"2024-11-07T22:50:36.781Z","updated_at":"2026-01-08T22:30:17.640Z","avatar_url":"https://github.com/zriyansh.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# go-boid-go\n\nNote: This is not a beginner friendly Go repository. \n\nFinal preview of what it looks like at the end. \n\n![](https://user-images.githubusercontent.com/52788043/151409751-9bdb3163-6160-4f96-a43b-34c927eb3dde.gif)\n\nThis 2D simulation tried to mimic flocking behaviour of large group of birds flying. \n\nConcept of Boids are in the introducture lecture of A-Life(Artificial Life) classes\n\nGraphics Library used - EBITEN. \n\n\u003chr\u003e\n\nSo what this seemingly green dots on a random terminal screen, all about? \u003cbr\u003e\nFirst of all, those random green dots are **BOIDs (Android Birds)**. \n\nThis project is an attempt to learn multi-threading in golang. \nA thread is a tool or abstraction that allows us to perform parallel computation. (remember your OS lectures?)\n\n\u003chr\u003e\n\nThere are some theorems we will be using to this program, indirectly. \n1. Amdahl's Law (you cannot keep on speeding up a work/process (only 1) to infinity by simply enough workers/processors at it, it reaches to a finite limit.)\n\u003cimg width=\"418\" src=\"https://user-images.githubusercontent.com/52788043/151524834-75e6dade-df47-46e4-9dc5-5c20c100d051.png\"\u003e\n\n2. Gustafson's Law (same as Amdahl's law but here you have more than 1 work/processes to do at a time)\n\u003cimg width=\"479\" src=\"https://user-images.githubusercontent.com/52788043/151524847-e981eb45-e024-4b6f-94d6-433954dda503.png\"\u003e\n\n\n\u003chr\u003e\n\nSome terminology:-\n1. Processes - You start 2 instances(windows) of notepad.exe, they both are separate processes. They are not sharing resources. One does not affect the other. Isolated. They are a bit heavy on resources relatively. E.g - fork()\n\nGo focuses more on threads and green threads. \n\n2. Threads(Kernel Level Thread) - A thread is the solution to downsides of a process. Threads share memory b/w them, unlike processes. Faster to create. Threads are not isolated. \n3. Green Thread(User Level Threads) - Efficient version of thread. It tries to reduce the time in context switching of threads when an interruption happens and new process/thread from ready queue enters into processing stage. OS picks a new thread from ready queue when the interruption occurs. Reduces context switch overhead. Helpful when there are a lot of processes on ready queue. \n\n\u003chr\u003e\n\nUser level thread runs inside kernel level thread. But a green thread has some disadvantages. Hence, Go uses a mixture of Green threads running under normal threads. Go reshuffles green threads as and when required, according to priority of task.\n\nHence, our BOIDs are actually threads, implemented by goroutines. \n\nOne boid = One thread. \n\nEach Boid has an velocity vector, position vector and Id.\n\nThere are 2 types of Inter-Process Communication(IPC) that can happen between processes. \n1. Message Passing\n2. Shared Memory\n\nWe define a ```viewRadius``` of a Boid (eyes of the boid) to observe which all other boids are in sight. \n\n\u003cimg width=\"694\" src=\"https://user-images.githubusercontent.com/52788043/151524639-f7f2a6e8-0c3a-4149-a0ed-0adc7e0530ed.png\"\u003e\n\n\u003chr\u003e\n\n3 properties of boids that we will define using memory sharing. \n1. Alignment - Align boids which come under the viewRadius\n2. Cohesion - keep them in a group\n3. Separation - do not let them merge together into a single boid. \n\nEvery boid is aware of position and velocity of other boids, using 2D array initialized to -1 (means no boid in that 2D frame), when a boid is found, we replace -1 with boidId. \n\nBoids in the viewRadius will adapt to one another's velocity and move in a group. (Alignment)\n\nNow we need a proper thread syncronozation (using mutexes) so that race condition do not occur. That is, boids shold not update their position when we scan them, else they might be counted twice, or not at all. \n\n\n```\n// defined as a variable\nlock = sync.Mutex{}\n\n// lock the process when it starts\nlock.Lock() \n\n// code //\n\n// unlock it at end to be used by other processes\nlock.Unlock()\n```\n\nBut this normal lock and unlock system is not efficient, so we use readers and writers lock system. \n\nThen we introduce a bounce mechanism that mimics real life birds when they see a wall and slows down and changes the direction.\n\n\u003cimg width=\"604\" src=\"https://user-images.githubusercontent.com/52788043/151524569-4905cec5-1646-4c01-8ac8-42f3285619ec.png\"\u003e\n\n\n# How to run this simulation in your system \n\nThis is fairly simple codebase with 3 .go files inside the ./boids directory where the simulation lives. \n\nThe sync dir is just an example code to demonstrate race condition. \n\n## Steps\n1. Clone this repository or download it as a zip file and unzip it. \n2. Open the folder in your editor OR cd to the root project folder using terminal.\n3. ```cd boids/```\n4. ```go run *.go``` OR ```go run .```\n5. That's all, a terminal or exe window should popup with the simulation.  \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzriyansh%2Fgo-boid-go","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzriyansh%2Fgo-boid-go","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzriyansh%2Fgo-boid-go/lists"}