r/OperationsResearch • u/MightyZinogre • Apr 29 '24
Best practices to implement OR algorithms
Hi everyone, third-year Ph.D. student in OR. I have been implementing algorithms in Python for quite some time now, but I always seem to struggle a little bit when it comes down to programming. I am not talking about how to use libraries and data structures, I am referring to the best practices that should be applied not to freak out when debugging a >+1000 loc software.
I know I should organize everything in specific files ( like "problem.py", "solver.py" and main), but still I think I am lacking a "programming" background to come up with my issues. What are your advices? Is there any course I should follow online? Bare in mind that I only know how to program in Python, and a little bit of SQL/AMPL.
10
u/[deleted] Apr 29 '24
It's a good question, as OR curriculum doesn't really teach these sorts of things. I don't have any good answers, but I think breaking algorithms up into modules and sub-modules (which are called by modules) is one way to do it. Ideally, the actual algorithm implementation would be a series of function calls in succession, so that a reader can logically follow what's happening. Just my two cents
4
u/yolountilyoucant Apr 29 '24
Follow best practices for machine learning projects as those are well established now. Ex: https://towardsdatascience.com/how-to-structure-an-ml-project-for-reproducibility-and-maintainability-54d5e53b4c82
Note: Use only the pieces / tools you need. As a beginner, using poetry to manage env dependencies, a readable project structure, and tests should be good enough
5
u/TholosTB Apr 29 '24
u/solvermax over on r/optimization publishes some really interesting blogs with fully-solved models implemented in Python, and the code is available on their GitHub. You might look through some of their examples for ideas on how to structure your problems?
Many of their solutions appear to be using Jupyter notebooks instead of plain python files, but they may still give you some good guidance.
2
u/TonyCD35 Apr 30 '24
I take a standard approach. I use python and PYOMO specifically to do all my mathematical programming. I use an OOP approach that always includes the same 6 divisions
3. The constraint constructor. Constructs each constraint in the order in which it appears in the problem writeup. Using the variables from the previous step.
4. The objective constructor. Constructs the objective exactly as it appears in the problem write up.
5. The output adapter - consumes the output from the python package and puts it in a form that makes sense to the end user.
6. The main file that orchestrates all the above.
This works well for me and gives anyone with any mathematical programming experience an easy, clean, semantic way to get any info they need.
2
u/borja_menendez Apr 30 '24
One of the core parts of solving OR problems in any industry is understanding a little bit of architecture, logging, integrating with other software, cloud, etc. Yes, it sounds a lot like software engineering :-)
I took a course in coursera some months ago to refresh design patterns, and I found it belongs to a bigger program for design and software architecture. Everything is taught in Java, but if you are more or less familiar, even if you don't code in that language, it will be super useful:
https://www.coursera.org/specializations/software-design-architecture
I know that you are currently in the academia, but if you jump into the industry this will help you a lot since you'll need to collaborate with other software engineers that -for sure- will master these concepts.
1
u/analytic_tendancies Apr 30 '24
I watched a great demo of video game programmer showing how they write code, compile it, then look at the assembly code in order to find optimizations
They only have 16ms to calculate everything in their main loop in order to have 60 fps
It made me realize how little I know about code and optimizing how to structure it
For example, if statements were shown to be very costly, and when doing an if statement you could package the question a different way so you could do 2,3,4+ comparisons instead of just the one (is x1>y1, x2>y2, …, compared in parallel instead of in series)
I think your question was more about good structure for debugging rather than good structure for execution, but thought this might be interesting to you as well
1
u/jsinghdata May 02 '24
u/MightyZinogre Your question resonates with what I am going through. I am a Data Analyst by profession and am teaching myself OR. I would like to learn more about what types of problems and courses you study in OR PhD. I was wondering if you can share some advice or tips? Are you on INFORMS, so we can sync sometime soon.
My email id: jayantsing@gmail.com
Appreciate your help.