<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Maartens Lourens on Medium]]></title>
        <description><![CDATA[Stories by Maartens Lourens on Medium]]></description>
        <link>https://medium.com/@maartens.lourens?source=rss-f6eeee19274a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*nigw4M1IlbxQ49bK</url>
            <title>Stories by Maartens Lourens on Medium</title>
            <link>https://medium.com/@maartens.lourens?source=rss-f6eeee19274a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 13:01:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@maartens.lourens/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Data Science Meets Project Management: How to Optimise Resource Scheduling]]></title>
            <link>https://blog.mesh-ai.com/data-science-meets-project-management-how-to-optimise-resource-scheduling-a138de144c44?source=rss-f6eeee19274a------2</link>
            <guid isPermaLink="false">https://medium.com/p/a138de144c44</guid>
            <category><![CDATA[resource-scheduling]]></category>
            <category><![CDATA[constraints-programming]]></category>
            <category><![CDATA[project-scheduling]]></category>
            <category><![CDATA[optimisation]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Maartens Lourens]]></dc:creator>
            <pubDate>Thu, 17 Oct 2024 09:49:25 GMT</pubDate>
            <atom:updated>2024-10-17T09:49:48.396Z</atom:updated>
            <content:encoded><![CDATA[<p>by <a href="https://medium.com/u/f6eeee19274a">Maartens Lourens</a> and <a href="https://medium.com/u/50b72b52ce9a">Bárbara Correia</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HI1T-fhD84qBn9FTtwEYmg.png" /></figure><h3>TL;DR</h3><p>In this article, we explore open-source and commercial optimisation tools for Resource-Constrained Project Scheduling using Constraint Programming, a technique that helps businesses minimise delays, maximise resource utilisation, and reduce costs by finding efficient schedules for projects when resourcing is limited. We discuss how to implement solutions with tools like OR-Tools, Process Scheduler, and Gurobi, and examine their respective advantages and disadvantages. You&#39;ll learn how to model and solve project scheduling problems using these tools.</p><h3>Introduction</h3><p>Sometimes you encounter a problem that involves scheduling various tasks for a project where you have limited resources, like people, machines, or materials. This is typically called a Resource-Constrained Project Scheduling Problem (RCPSP). A project manager runs into this situation when they need to manage and complete a project while juggling the availability of different resources to optimise the project schedule and avoid delays or bottlenecks. Sound familiar?</p><p>We recently came across a problem of this kind, where the exact placement of a task within a schedule needed to be optimised. This got us thinking about the broader problem space, and what approaches might be taken to solve such a problem.</p><p>It turns out that at least two related paradigms exist: Linear Programming (LP) and Constraint Programming (CP). Both have strengths and weaknesses, but in the end we were drawn to CP’s more general and flexible approach.</p><p>Let us explain. Linear Programming makes it possible to find a provably <em>optimal solution</em> to an optimisation problem - provable thanks to the principle of duality (described in the <a href="https://en.wikipedia.org/wiki/Dual_linear_program">duality theorems</a>). This is great when you need to optimise to the nth degree, for example when allocating tasks to machines in a factory. Nevertheless, in practice an optimal solution is not usually required for something like a project schedule - as long as it satisfies business rules or practical limitations, known in optimisation parlance as <em>constraints</em>. This sounds a lot more like <a href="https://en.wikipedia.org/wiki/Constraint_programming">Constraint Programming</a>.</p><p>In CP, constraints can be linear, non-linear, logical, or a combination, and allows a much broader range of constraints compared to LP. In short, it’s <strong>flexible</strong>.</p><p>The problem is first treated as a constraint satisfaction problem (CSP). Once a solution is found that satisfies all constraints, it will then attempt to further optimise it if an optimisation criterion is specified. Unlike LP, CP does not necessarily guarantee finding the optimal solution for the criteria, instead providing a near-optimal (but not necessarily globally optimal) solution - done using techniques like <a href="https://www.geeksforgeeks.org/branch-and-bound-algorithm/">branch-and-bound</a> or <a href="https://www.simplilearn.com/local-search-algorithms-in-ai-article">local search</a>.</p><h3>The problem</h3><p>Ok, now with that bit of theoretical preamble out of the way, can we solve a r̶e̶a̶l̶ toy scheduling problem using CP? To find out, we need to describe our problem. Here are some of the terms we will use:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2qdn6z_eZcuDv7BDCVKVlQ.png" /><figcaption>Scheduling Optimisation Terms</figcaption></figure><p>Now suppose our project has 10 tasks. We’ll set up the following testable constraints on these tasks:</p><ul><li>Task 0 has to happen before everything else</li><li>Tasks 4 and 5 are not allowed to happen simultaneously.</li></ul><p>We’ll also set up two objective functions:</p><ul><li>Minimise task start time given weights for each task</li><li>Minimise the makespan</li></ul><p>To complete the picture we’ll add two resource groups, the first with 6 people (at a cost of 5 units each), and the other with 8 people (at a cost of 1 unit each).</p><h3>Choice of tools</h3><p>To implement the solution to our stated problem we need a CP solver. The king of open source CP solvers is undoubtedly Google’s <a href="https://developers.google.com/optimization/">OR Tools</a>. As it says on its web page “OR-Tools won gold in the international constraint programming competition every year since 2013”. So that looks like a solid starting point. Nevertheless, I figured it would be prudent to compare it to an alternative with a different emphasis. After some searching I found Thomas Paviot’s <a href="https://processscheduler.github.io/">Process Scheduler</a>.</p><p>From a programming perspective, the most obvious difference is that Process Scheduler offers high level objects, whereas with OR Tools you have to compose them from more basic building blocks. However there are a number of other differences as well, as summarised in the table below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4dkt15DBaauBLR1hFq9Czw.png" /><figcaption>Comparison of Open Source Tools</figcaption></figure><p>There are other tools available, but these two offer a good spread of characteristics. We will now go ahead and implement a solution to our problem scenario with both.</p><h3>Implementation</h3><p>Unlike normal programming, in which the steps and instructions are specified, in CP the properties of the solution are specified declaratively<em>.</em> We first set up our constraints and objectives, and then let the solver find a solution for us.</p><p>Here we will go over some of the salient code snippets with respect to setting up the constraints and the objectives. However, I won’t be delving into all of the surrounding code as that will become too long winded.</p><h4>OR Tools</h4><p><strong><em>Setup</em></strong></p><p>With OR Tools we start by importing the cp_model from the sat solver library, and instantiating it. Then, given the number of tasks (n), durations (p), resource requirements (u) and resource capacity (c) we set up variables for the horizon, start times, end times, and intervals.</p><pre>from ortools.sat.python import cp_model<br><br>model = cp_model.CpModel()<br>horizon = sum(p)<br>start = [model.NewIntVar(0, horizon, f’start_{i}’) for i in range(n)]<br>end = [model.NewIntVar(0, horizon, f’end_{i}’) for i in range(n)]<br>intervals = [model.NewIntervalVar(start[i], p[i], end[i], f’interval_{i}’) for i in range(n)]</pre><p><strong><em>Constraints</em></strong></p><p>We can then begin to add constraints. First, we add a constraint that Task 0 should take place before everything else.</p><pre>for i in range(1, n):<br>    model.Add(end[0] &lt;= start[i])</pre><p>Second, we add a constraint that Task 4 and Task 5 cannot take place at the same time.</p><pre>model.AddNoOverlap([intervals[4], intervals[5]])</pre><p>Third, we should ensure that resources for each task do not exceed capacity.</p><pre>for r in range(2):<br>    model.AddCumulative(<br>        [intervals[i] for i in range(n) if u[i][r] &gt; 0],<br>        [u[i][r] for i in range(n) if u[i][r] &gt; 0],<br>        c[r]<br>    )</pre><p>Finally, let’s constrain the makespan to be no later than the end time.</p><pre>makespan = model.NewIntVar(0, horizon, &#39;makespan&#39;)<br>model.AddMaxEquality(makespan, end)</pre><p><strong><em>Objectives</em></strong></p><p>Now that we have our constraints in place, we can create the objective function.</p><pre>obj_expr = makespan<br>for i in range(n):<br>    obj_expr += start[i] * early_start_time_weight[i]</pre><p>Finally, minimise the objective function.</p><pre>model.Minimize(obj_expr)</pre><p><strong><em>Solve</em></strong></p><p>All that’s left is to solve the problem.</p><pre>solver = cp_model.CpSolver()<br>status = solver.Solve(model)</pre><p><strong><em>Run and Test</em></strong></p><p>Using the streamlit app, where the <em>resource requirements</em> for each task is set to (2, 2) by default, let’s set the <em>available resources</em> to (2, 2) so that there will only be enough resources for one task at a time.</p><p>At the same time, let’s set the <em>early start time weights</em> for Task 4 and 5 to a value of 10 (all the others are set to 1 by default).</p><p>Given these values, and the constraints and objectives we have set, we should expect to see all the tasks running sequentially with none overlapping. Task 0 should run first, with a combination of task 4 and 5 next. There should be no time gaps between the end of one task and the start of another.</p><p>By clicking on ‘Solve and Visualise’ we see the results, and so it proves.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*OV_QWpYPOLm4GhrmrQOeGw.png" /><figcaption>Schedule</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eKXHm76sJFjqJmT1LXAWsA.png" /><figcaption>Detail</figcaption></figure><p>By setting the <em>available resources</em> back to 6 and 8 we can test that Task 4 and 5 follow Task 0 (in any order) without overlapping.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*L9lbI6boYLRrOuArAYDXEQ.png" /><figcaption>Schedule</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9cQixs-2YFuF-6S2Acr5KQ.png" /><figcaption>Detail</figcaption></figure><p>Indeed, it still looks good.</p><h4>Process Scheduler</h4><p>We’ll go through the same process with the Process Scheduler.</p><p><strong><em>Setup</em></strong></p><p>Given the number of tasks (n), durations (p), resource requirements (u) and resource capacity © variables, we start by defining our tasks and resources, and instantiating the problem object.</p><pre>pb = ps.SchedulingProblem(name=&quot;TaskScheduling&quot;)<br>tasks = [ps.FixedDurationTask(name=f&quot;Task_{i+1}&quot;, duration=p[i]) for i in range(n)]<br>People1 = [ps.Worker(name=f&quot;People1_{i}&quot;) for i in range(c[0])]<br>People2 = [ps.Worker(name=f&quot;People2_{i}&quot;) for i in range(c[1])]</pre><p>We can then assign resources to tasks.</p><pre>for i in range(n):<br>    u1, u2 = u[i]<br><br>    if u1 != 0:<br>        tasks[i].add_required_resource(<br>            ps.SelectWorkers(<br>                list_of_workers=People1,<br>                nb_workers_to_select=u1,<br>                kind=&quot;exact&quot;<br>            )<br>        )<br><br>    if u2 != 0:<br>        tasks[i].add_required_resource(<br>            ps.SelectWorkers(<br>                list_of_workers=People2,<br>                nb_workers_to_select=u2,<br>                kind=&quot;exact&quot;<br>            )<br>        )</pre><p><strong><em>Constraints</em></strong></p><p>Next we can add the two task constraints</p><pre>for i in range(1, n):<br>    ps.TaskPrecedence(task_before=tasks[0], task_after=tasks[i])<br>    ps.TasksDontOverlap(task_1=tasks[4], task_2=tasks[5])</pre><p><strong><em>Objectives</em></strong></p><p>Then we add the objectives.</p><pre>for i in range(n):<br>    # Create a start time indicator for each task<br>    start_time_indicator = ps.IndicatorFromMathExpression(<br>        name=f&quot;StartTime_{i+1}&quot;, <br>        expression=tasks[i]._start<br>    )<br>    <br>    # Add an objective to minimise the start time of each task with the given weight<br>    ps.ObjectiveMinimizeIndicator(<br>        name=f&quot;Minimise_StartTime_{i+1}&quot;, <br>        target=start_time_indicator, <br>        weight=early_start_time_weights[i]<br>    )<br>    # Minimise<br>    ps.ObjectiveMinimizeMakespan()</pre><p><strong><em>Solve</em></strong></p><p>As we can see, Process Scheduler’s syntax is simpler than OR Tools’, mainly due its broader implementation of high level concepts.</p><p>All that’s left now is to solve the problem.</p><pre>solver = ps.SchedulingSolver(problem=pb)<br>solution = solver.solve()</pre><p><strong><em>Test and Run</em></strong></p><p>When we run the same tests as on OR Tools, we get the same results.</p><p>We can go one step further and increase the duration for Task 4 and 5 to a value of 5 and 6 in each case, to showcase a more complicated task schedule. This, too, works as expected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*tTN4nc4Z-jxT2tglaqfXNg.png" /><figcaption>Schedule</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5SEZZ1ogyEpBaJRxAxjFeQ.png" /><figcaption>Detail</figcaption></figure><h4>Performance</h4><p>These code snippets were not optimised for speed, but rather for clarity and functionality. The first point to observe is that Process Scheduler is a lot slower than OR Tools. So if performance and scaling is a factor then Process Scheduler might not be the best choice.</p><p>While OR Tools appears much faster, the picture isn’t entirely rosy either. Playing around with the parameters, it becomes clear that increasing the number of tasks soon results in much slower performance. What is causing this? There appears to be two main causes: the introduction of the early start time objective for optimisation, and an increase in the number of tasks. The findings are discussed in more detail below.</p><ul><li>Early start time objective</li></ul><p>By removing the early start time objective (while still leaving in the makespan minimisation objective) the performance improves markedly. This was tested with the same parameter configuration as in the final “Run and Test” above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w8E5Yf-YLEdkNug20Gik6w.png" /><figcaption>Performance — OR Tools</figcaption></figure><p>Extrapolating from this finding, optimising objectives in general could be slower than satisfying constraints. On the other hand, this view may be distorted by this toy scenario and it could simply be a side effect of general complexity.</p><ul><li>Increase in number of tasks</li></ul><p>Going from 14 to 15 tasks there is a leap in the time it takes. It&#39;s as if there is suddenly a whole new set of possibilities that the algorithm has to consider:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IPF411CGGrHMS80hHVQomw.png" /><figcaption>Performance — OR Tools (ctd.)</figcaption></figure><p>Adding a parallelisation step does in fact reduce the time taken to 147.494 seconds.</p><pre>solver.parameters.num_search_workers = 8</pre><p>However, for even more complex configurations this will likely not be enough. Other strategies may have to be applied, such as breaking the problem into smaller problems. One in particular that may offer some immediate relief is to simply limit the time.</p><pre>solver.parameters.max_time_in_seconds = 60.0</pre><p>We can add the early start time objective back in and test this with the same configurations. We find that it still provides a result that satisfies all the constraints, and gives the optimal early start time positions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*ogZr4RjJ62DzJYbSglvFhg.png" /></figure><p>Performance requirements can vary significantly from problem to problem, and while one use case may require near real time solving, another may be fine with an overnight run.</p><h3>Gurobi</h3><p>As we&#39;ve seen, increasing the complexity of the problem space will eventually incur a hit on performance, even with a suite as impressive as OR Tools. Optimisations can help up to a point, but you might run out of options.</p><p>If so, there are commercial options that, if your project has the budget, can offer significant performance improvements. One of the most impressive and popular is Gurobi.</p><p>Gurobi is a proprietary optimisation solver that offers both free and commercial licences. We will use the free licence to illustrate what can be achieved. The free licence imposes certain limitations, such as restrictions on the number of variables and constraints that can be utilised. However, should you need it for enterprise-level applications or production use, it is recommended that you contact Gurobi directly to obtain an appropriate commercial licence.</p><p>Let’s proceed to see how it works.</p><h3>Setup</h3><p>Gurobi can be used like any other Python library in a Python environment. It just needs to be installed via pip install:</p><pre>pip install gurobipy</pre><p>And then imported like any other library:</p><pre>from gurobipy import Model, GRB, quicksum</pre><p>We start by defining the model and variables:</p><pre>model = Model(&quot;resource_scheduling&quot;)<br>active_task = model.addVars(num_tasks, time_horizon, vtype=GRB.BINARY, name=&quot;active_task&quot;)<br>makespan = model.addVar(vtype=GRB.INTEGER, name=&quot;makespan&quot;)</pre><h3>Constraints</h3><p>Next, we add the following constraints to our Gurobi model:</p><ul><li>All tasks should be included in the scheduling:</li></ul><pre>for i in range(num_tasks):<br>    model.addConstr(quicksum(active_task[i, t] for t in range(time_horizon - durations[i] + 1)) == 1, name=f&quot;start_once_{i}&quot;)</pre><ul><li>Tasks can only be completed with the resources available:</li></ul><pre>for t in range(time_horizon):<br>    for p in range(len(resource_capacity)):<br>        model.addConstr(<br>            quicksum(<br>                resource_requirements[i][p] * active_task[i, s]<br>                for i in range(num_tasks)<br>                for s in range(max(0, t - durations[i] + 1), t + 1)<br>            ) &lt;= resource_capacity[p],<br>            name=f&quot;resource_capacity_time_{t}_p_{p}&quot;<br>        )</pre><ul><li>Task 0 should always come first:</li></ul><pre>for i in range(1, num_tasks):<br>    model.addConstr(<br>        quicksum(<br>            t * active_task[0, t] <br>            for t in range(time_horizon - durations[0] + 1)<br>        ) + durations[0]<br>        &lt;= quicksum(<br>            t * active_task[i, t] <br>            for t in range(time_horizon - durations[i] + 1)<br>        ),<br>        name=f&quot;task_0_before_task_{i}&quot;<br>    )</pre><ul><li>Tasks 4 and 5 should never overlap:</li></ul><pre>if num_tasks &gt; 5:<br>    for t in range(time_horizon):<br>        model.addConstr(<br>            quicksum(<br>                active_task[4, s] <br>                for s in range(max(0, t - durations[4] + 1), t + 1)<br>            ) +<br>            quicksum(<br>                active_task[5, s] <br>                for s in range(max(0, t - durations[5] + 1), t + 1)<br>            ) &lt;= 1,<br>            name=f&quot;task_4_5_not_simultaneous_{t}&quot;<br>        )</pre><ul><li>All active tasks should be in the time span:</li></ul><pre>for i in range(num_tasks):<br>    model.addConstr(<br>        makespan &gt;= quicksum(<br>            (t + durations[i]) * active_task[i, t] <br>            for t in range(time_horizon - durations[i] + 1)<br>        ),<br>        name=f&quot;makespan_task_{i}&quot;<br>    )</pre><h3>Objectives</h3><p>Similar to the other approaches, the objective function of the model is to minimise the time it takes to conclude all tasks and, when applicable, prioritise tasks according to their weighting.</p><pre>model.setObjective(<br>    makespan + quicksum(<br>        early_start_weights[i] * t * active_task[i, t]<br>        for i in range(num_tasks)<br>        for t in range(time_horizon - durations[i] + 1)<br>    ),<br>    GRB.MINIMIZE<br>)</pre><h3>Solve</h3><p>Running the model is relatively straightforward. After defining the model, as before, we just need to call the optimise function to solve the problem.</p><pre>model.optimize()</pre><h3>Test and Run</h3><p>Now that everything is in place, let&#39;s run the same tests we used for OR Tools and Process Scheduler.</p><p>On the first test, with the default values, we get the same solution, as expected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*-EeU03MEDKyUQYAUz53-xw.png" /><figcaption>Schedule</figcaption></figure><p>For test 2, we are expecting to have just one task at a time - by decreasing the values of available resources to 2 each, and prioritising tasks 4 and 5 by giving a higher Early Start Time weight, which can be seen in the figure below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*559p7v-WP-kLU3vNTVcfdQ.png" /><figcaption>Schedule</figcaption></figure><p>As a third test, let’s increase the complexity of the test by changing the durations of task 4 to 5, and the duration of task 5 to 6. The model outputs a viable solution, as expected.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*YRK0jDFxZU3hv66lVrrhyw.png" /><figcaption>Schedule</figcaption></figure><h3>Performance</h3><p>In terms of performance, Gurobi proves to be the most efficient solution across all tests conducted, up to the point of reaching the maximum number of variables allowed by the free licence. Gurobi achieves the same functionality as the other tools, consistently reaching a feasible solution when expected. However, from a computational perspective, Gurobi outperforms the other tools. Gurobi consistently takes less than a second to produce a solution, unlike the open source tools.</p><p>The following table displays the time taken by the Gurobi solver to run an increasing number of tasks. We compare performance with and without the early start weight, and observe that there is no significant degradation in performance. We also show the performance of OR Tools for reference.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*emDHCKdk3JkubX1DxNicQw.png" /><figcaption>Performance - Gurobi vs OR Tools</figcaption></figure><h3>Conclusion</h3><p>When you have a resource constrained project scheduling problem, constraint programming (CP) can be a useful approach. In such scheduling scenarios we are not necessarily trying to find a globally optimal solution (like in linear programming), but rather a solution that satisfies the constraints, with potentially some additional optimisation, CP therefore offers a flexible approach that can be adapted to a variety of business requirements.</p><p>Constraints are also easier to explain and verify than an objective function, which would make it easier to discuss the solution design with the business, and to build trust in a solution.</p><p>Open source options like OR Tools and Process Scheduler offer solid functionality with no licensing costs. However, performance is one area that could scupper the implementation of a solution, given the slowdown in computation as the complexity of the problem space increases.</p><p>Fortunately there are strategies that can be employed to improve performance, such as parallelising computation. There are also commercial tools like Gurobi that offer significant performance enhancements out of the box - at a price.</p><p>In short, CP offers a practical and versatile approach with multiple tooling options for solving resource constrained project scheduling problems.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a138de144c44" width="1" height="1" alt=""><hr><p><a href="https://blog.mesh-ai.com/data-science-meets-project-management-how-to-optimise-resource-scheduling-a138de144c44">Data Science Meets Project Management: How to Optimise Resource Scheduling</a> was originally published in <a href="https://blog.mesh-ai.com/">Mesh-AI Technology &amp; Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>