During our monthly workshops, we share knowledge and practice applying new techniques. This time on linear programming: how to transform a problem into a mathematical model and then optimise it.
5 pies does not a developer make
A workshop on the basics of linear programming
Written by Merel
![](https://images.ctfassets.net/ptuqs74o8e3y/4zny0DWiiRyD6D2Kxn61sn/87e325740a3082aac26742d7524d633e/Pippa_workshop.jpeg)
Linear Programming is still an unknown discipline for many of us. The idea is pretty cool: write your problem into a mathematical model and then let a program optimise it. A good example that came along was how this way you can calculate the optimal profit for a patisserie, taking into account cost, preparation time, wages, etc. A cheesecake might yield more profit, but could easily be a lot more work than, say, a cheaper carrot cake (sidenote: I understand software, not cakes. So maybe a cheesecake yields less, or is faster to make than a carrot cake, but I digress). You can also include things like production costs. With some mathematical magic, you then set up a formula for all of this.
Getting to know PuLP
With that formula, you can then turn to [PuLP](https://coin-or.github.io/pulp/ ‘Learn more about PulP’), a Python library designed exactly for this kind of problem. You then create a model, put in the production cost, production time and yield. Then all you have to do is add your formula for maximum profit to the mix and click play. Then, if you've done it right, the model comes up with the optimal solution. If you haven't done it right, forgetting for instance that as a baker you also want to sleep and don't want to work an 80-hour week, it will come up with something that is not necessarily implementable. PuLP does a lot for you, but you will still really have to specify all your requirements yourself.
Let's practice
This was just a theoretical example during the presentation. So unfortunately for the cake bakers among you, I can't tell you the ideal cake strategy to become flush with cash. In fact, the actual goal of the workshop was a bit spicier: automating our own monthly planning. As employees, we escape the monthly dance of manually creating this schedule, which seems to be getting quite time-consuming with a growing number of developers and projects. Our amazing bosses thought they could provide the ideal combination: all developers learn something about a specific programming puzzle and at the same time, making the planning becomes less of a nightmare.
Went amazing right?
*Insert success story here*
During the workshop, we had a chance to see if and how we could pull this off. It turns out there is a whole lot to consider. For instance, the idea is obviously not to have all developers working on one project. The model also has to take into account pre-arranged days, such as appointments with clients or my holiday to Bali. How to put all that into a model... I'm still lost.
Addition by Bauke
Apparently, linear programming revolves around solvers. Calculating optimal profit for selling cakes, taking into account cost price, preparation time and bakers' wages? Solvers! Calculate optimal use of trucks/drivers given distance, rest breaks and weight of load? Solvers! Or, as in this workshop: optimal staffing based on projects, suitability and availability? Solvers!
That sounds simple, but apparently you have to give a solver something on the basis of which it has to deliver. And that's not so simple after all. You have 3 responsibilities:
- Defining and quantifying the right variables. (What plays a role in your optimal calculation - e.g. degree of suitability for a project?)
- Defining the objective function (How should the optimal result be achieved? - e.g. most optimal match between suitability and project)
- Defining limits and constraints (Within which frameworks should the calculation be done? - e.g. who is free on which day?)
Soon you notice that you are missing a variable or have understated a constraint: In my first schedule, everyone worked full-time on the same project🤔. Maybe just add a constraint that a project can be run a maximum number of hours per month😇. After some puzzling I was able to get the solver to impose this restriction: nice! After an hour, I finally managed to put together a nice schedule - but it still needs some tinkering for optimal results😉.
Key takeaways
- Free and but also better (paid) solvers are available for complicated linear equations (thank god).
- Easy to set up/put into use
- Difficult to make complete and correct