Andarin / Ant-Colony-Simulation-Python

GNU General Public License v3.0
6 stars 1 forks source link

Project #1

Open t2325 opened 10 months ago

t2325 commented 10 months ago

Hey Andarin! I am in the process of creating an ant simulator using Python for a school project and I stumbled upon your program through my research and found it very useful. I was just wondering if there was any help that you could offer. Thank you :)

Andarin commented 10 months ago

Hello ! I'm happy that my code helps someone :) What kind of help / advice would you need ?

For your information, I worked on 3 sub-projects concerning Ant Simulation:

  1. The 2D Python / Cython simulation with GUI with PyGame
  2. A statistical analysis / genetic algorithm to find the best parameters for my ants of 1. (e.g. how long walk straight, when to reconsider changing direction, by how much, etc)
  3. A 3D C++ Ant simulation

Maybe one advice right away: Really simulating individual ants at scale (Ant hive -> hundreds of ants) is computationally very heavy -> if you want to have a real-time game (1. or 3., not relevant for 2.), you have to make the ants simpler.

If you know matrix multiplication / numpy, it's worth having a look (1 ant = 1 small matrix, ant hive = big stack of these small matrices, model going from time t to t+1 as a matrix multiplication between the hive and an "action" matrix which models behavior changes). I modeled ants as objects, which is a proper, but not a fast way to do it. So if you're graded on nice code, you may want to go with objects, but it doesn't scale well.

If you want to go the object route, Cython gave my a speed increase of factor 2 without doing much, Pypy is an even cheaper way to gain some speed. There may also be other good ways to do a big simulation which you could read on - you could ask ChatGPT (chat.openai.com) if it has other suggestions.

If you have other specific questions, I'm happy to help :)

ghost commented 10 months ago

Thank you so much for getting back to me Andarin! :) I am struggling with how to even begin coding such a project. I am aiming to make it a simulation rather than a game, where you can view ant colonies interacting with one another in 2D realtime, with tools on a UI such as picking up and moving ants and barriers. I also want to implement sliders so that you can edit the stats of the ants. I only know how to code python and I think I will take the object route as it helps with programming structures to receive more marks. ChatGPT offered little help to me as all the code was wrong or not complex enough ;-; Is there any advice you could offer about how to get started? For example, I looked at SimPy but multiple sources say PyGame is better but I want it to be more complex. Your code helped a lot with understanding what variables to include, but I would prefer to have my own twist focusing on the complexity of the project, but not sure how to go about that?? Thank you very much, I really appreciate it :)))

Andarin commented 10 months ago

Hello ! Concerning OpenAI, I was thinking less about writing the code (though it can help a lot with prototyping something in a library you're not familiar with), but more about outlining advantages / disadvantages between, e.g. PyGame and Simpy. For example, I asked the question: "I want to simulate an ant hive with Python. Could you compare PyGame and Simpy for creating such a simulation ? What are their advantages and disadvantages ? Are there maybe other libraries I should consider ?" and it gave me quite a good response.

In gist, I don't think you can do a GUI in SimPy. If you want SimPy for its better simulation, you would still need to use PyGame (or something similar) to create a graphical representation. It also suggested Mesa which I didn't know, and maybe also something interesting for you ? (By the way, always be critical of its responses - it also suggested a library which doesn't exist...)

In hindsight, with some work experience, I would give you the following general advice for "how to even begin coding such a project":

Don't start to code right away. You're doing well to get some inspiration as you do for 1-2 days. Afterwards, think what functional features (= what can a user do) will satisfy you in the final demo / game. Write it down; they correspond to you user stories. They help you to evaluate what you achieved and prevent you from losing focus. They should not be technical, rather "I want to see ants move". "I want to be able to influence ant behavior". "I want real time". Prioritize them. Decide if you could live rather with a complex simulation with text output, or a simple simulation with GUI. Whatever you choose, focus on this, and start only working on the other part when you have something viable for the first. Depending on your time, SimPy + GUI + Real time interaction sounds ambitious, so you have to try not to loose yourself. You can add new stories later and re-prioritize later, but always go over this step before coding, to force yourself to conscious decision that some other thing may not be done.

Then think what needs to be done technically to have a minimal viable solution for each story, and estimate how much time it will take. Multiply this time by 5, and write it down (from experience :D). Compare later with reality to find your own factor. This helps getting better in your time management and estimations :)

Finally, as this is a school project, think about your evaluation criteria: Clean code ? Neat graphical representation ? Complexity of libraries used ? Accuracy of the simulation ? (Usually it's not the latter 2). Adapt how much time

A classic error (which I did for a long time) is to start coding on something which comes to mind and is fun to code right now, but this often leads to the project not really finished at the end, and creating spaghetti code. Better deliver 3 things well than 6 things half-done; the former is also neater to play with. This is why you need to have very early a presentable version, and iterate / add features on it, instead of working on details and hoping to finish something workable at the end. This philosophy is inspired by Agile development.

If you have more questions, feel free to ask !

ghost commented 10 months ago

Thank you very much! I've gotten the hang of it using your advice and have found it incredibly useful, I really appreciate it. I'm slowly working through it and will definitely ask if there is anything I am stuck on. Once again, thank you so much! :)

t2325 commented 5 months ago

Finally done with the write up!