<?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 Juan Garcia on Medium]]></title>
        <description><![CDATA[Stories by Juan Garcia on Medium]]></description>
        <link>https://medium.com/@jgarcialopez837?source=rss-cf412967c8ab------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*d8y0E0McAGlZH0cJ</url>
            <title>Stories by Juan Garcia on Medium</title>
            <link>https://medium.com/@jgarcialopez837?source=rss-cf412967c8ab------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 10:35:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jgarcialopez837/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[Complex problems don’t always need complex solutions: how we reduced a combinatorial problem by 99%]]></title>
            <link>https://medium.com/enviame-engineering/complex-problems-dont-always-need-complex-solutions-how-we-reduced-a-combinatorial-problem-by-99-0507250e0900?source=rss-cf412967c8ab------2</link>
            <guid isPermaLink="false">https://medium.com/p/0507250e0900</guid>
            <dc:creator><![CDATA[Juan Garcia]]></dc:creator>
            <pubDate>Wed, 23 Jul 2025 20:57:06 GMT</pubDate>
            <atom:updated>2025-07-24T14:37:25.365Z</atom:updated>
            <content:encoded><![CDATA[<p>Optimization is a concept we find in every aspect of life from how a company allocates its budget to maximize profit, to how we plan our daily routines or choose the fastest route home. In business, technology, at home, and beyond, optimizing means making smart decisions when faced with many possible options.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*j36whUIlY6KZgvSZv2092A.jpeg" /><figcaption>Image generated by Leonardo.AI</figcaption></figure><p>But turning theory into practice isn’t always straightforward. One of the most challenging areas in this field is <strong>combinatorial optimization</strong> — the task of finding the best combination within a set of possibilities that is often overwhelmingly large.</p><p>This article walks through our real-world experience tackling one of these problems. We explored advanced tools, complex algorithms, and enterprise-grade solutions from major tech companies. Yet, in the end, we chose a much simpler and more effective approach: <strong>brute force, applied intelligently</strong>.</p><p>So, what’s better: the elegant and sophisticated, or the simple and practical? Interestingly, in many cases, what seems rudimentary can outperform the complex. Because when a problem is well-defined and the search space is reduced with intention, a direct solution can not only be valid it can be the most robust one.</p><h4><strong>🧩 Framing the Problem</strong></h4><p>Imagine you own an online store that sells t-shirts featuring adorable dog prints. Your products need to reach customers all over the country, and to do that, you rely on multiple courier companies that handle the delivery for you. Each courier offers different pricing, depending on various factors such as the weight and size of the package, the pickup location (your warehouse, for instance), and the final delivery address (your customer’s home).</p><p>With all this information at hand, it might seem like the obvious solution is to simply choose the cheapest courier for each shipment, right? But that’s where the complications begin.</p><p>First, not all couriers provide service to every destination. Some operate efficiently in certain regions, while others don’t. Second, if you always chose the cheapest option for each destination, you’d end up working with too many couriers at once, possibly dozens (let’s say around 50 on average). That level of coordination would make daily logistics incredibly difficult to manage. The ideal scenario would be to leverage <strong>a limited number of couriers</strong> (multi-coureir) in a way that still guarantees coverage of all your deliveries at the lowest possible operational cost. Relying on a single courier is usually insufficient, but working with all of them is simply too complex operationally.</p><p>So the real question becomes:</p><p><strong>What is the best combination of couriers (maximum of five) that allows you to maintain competitive prices and reach the widest possible coverage to optimize your multi-courier logistics operation?</strong></p><h4>🤔 Breaking It Down</h4><p>Let’s take it step by step. So far, we know the following:</p><ul><li>There’s a set of <strong>available couriers</strong>, let’s call that number <strong>C</strong> (on average, around fifty).</li><li>We want to work with a limited number of them at once let’s say <strong>X</strong>, where <strong>X &lt; C</strong>.</li></ul><p>Now imagine that your business handles <strong>N</strong> deliveries per month, where <strong>N</strong> is any natural number greater than or equal to 1. Each shipment can be represented with three practical attributes:</p><ul><li>An origin (your central t-shirt warehouse).</li><li>A destination (each customer’s delivery address).</li><li>And an average package weight (in kilograms).</li></ul><p>We can visualize this data in a table like the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BTJinjFd0lQsp-konk97hQ.png" /><figcaption>Coverages and prices from couriers</figcaption></figure><p>Where:</p><ul><li><strong>OW</strong> represents your warehouse, the starting point for all deliveries.</li><li><strong>Dᵢ</strong> is the destination for shipment <em>i</em>, where <em>i</em> ranges from 1 to <em>N</em>.</li><li><strong>Wᵢ</strong> is the average weight of the package for shipment <em>i</em>.</li><li><strong>Pₖᵢ</strong> is the price charged by courier <em>k</em> to fulfill shipment <em>i</em> (with <em>k</em> ranging from 1 to <em>C</em>).</li><li>And if <strong>Pₖᵢ</strong> is null, it means that courier <em>k</em> doesn’t serve that particular destination</li></ul><p>So, the challenge is to find the best combination(s) of up to 5 couriers (where <strong>1 ≤ X ≤ 5</strong>) that:</p><ul><li><strong>Maximize overall coverage</strong>, and</li><li><strong>Minimize total operational cost</strong>.</li></ul><p>To achieve this, we need to evaluate <strong>every possible combination</strong> of the <strong>C</strong> couriers — in groups of 1, 2, 3, 4, and 5.</p><p>For each candidate combination, we must:</p><ol><li>Calculate the <strong>total joint coverage</strong>.</li><li>Calculate the <strong>total operational cost</strong>, by selecting the cheapest available price for each shipment.</li><li>Compare each combination against the others to find the best balance between cost and coverage.</li></ol><p>This is a classic <strong>combinatorial optimization</strong> problem and the search space grows fast. Just using our average values, the number of possible combinations of 5 couriers out of 50 is already <strong>2,118,760</strong>. And that’s not even counting the smaller combinations of 4, 3, 2, or 1.</p><p>Even though the problem seems easy to grasp at first, the sheer size of the data and number of combinations makes it far more complex than it appears. Solving it effectively requires not just the right tools, but also practical criteria for decision-making under real-world constraints.</p><h4>🧭 Trying to find the best path</h4><p>When we first faced this problem, our natural instinct as developers was to reach for “advanced” tools. One of the first that came up was <strong>OR-Tools</strong>, Google’s powerful optimization framework. With extensive documentation and a large community behind it, it seemed like the perfect choice: it offered modules for routing problems, integer programming, resource allocation, and more.</p><p>But as we dove deeper, reality began to set in. While OR-Tools excels in many structured problem types (like the classic traveling salesman problem), our case didn’t fit as cleanly. The data was sparse and irregular, the objective involved selecting subsets of couriers and comparing dynamic prices for each shipment, and we were dealing with multiple goals (cost <em>and</em> coverage). Trying to bend our problem into OR-Tools’ model quickly became tedious, impractical, and limiting.</p><p>We also explored another route: <strong>greedy algorithms</strong>. These are popular because of their speed — they pick the “best-looking” option at each step based on local information. But that’s precisely the issue: they tend to optimize with a short-term lens, often getting stuck in <strong>local optima</strong> — solutions that look good in isolation, but fall short when viewed across the entire problem space. And in our case, where the goal was to evaluate <em>all</em> combinations to guarantee maximum coverage at minimum cost, that kind of compromise wasn’t acceptable. We weren’t aiming for “pretty good.” We needed <strong>the best possible outcome</strong>.</p><p>In short: while OR-Tools is an excellent library for many optimization problems, and greedy algorithms work well when there’s room for error, in this specific case they caused more headaches than results. We found ourselves facing a classic dilemma: go with something sophisticated or go with something simple. Spoiler: we chose the latter — but not without thinking it through.</p><p>And that’s when the idea hit us: <strong>What if we tried something much simpler… like brute force?</strong></p><h4>🧠 Breaking the chaos</h4><p>After the headaches brought on by OR-Tools and greedy algorithms, we decided to try something that, at first glance, felt completely opposite to what “optimal” usually means: <strong>brute force</strong>. But it wasn’t about blindly iterating through everything. The key was to channel computational effort smartly — applying strategies to drastically reduce complexity without sacrificing the quality of results.</p><p><strong>Step 1: Remove what doesn’t belong. </strong>The first thing we did was eliminate every courier that didn’t cover a single shipping zone. Obvious in hindsight, but these “empty” couriers were still present in the dataset, taking up memory and inflating the number of possible combinations. Removing them was the first filter that significantly simplified the problem.</p><p><strong>Step 2: Who has the best price?</strong>Next, we analyzed which couriers offered the best prices for each destination. This helped us extract a subset of couriers who were <em>at least once</em> the most competitive for a given zone. That gave us a much more meaningful candidate pool.</p><p><strong>Step 3: Skip redundant combinations. </strong>While generating combinations of up to 5 couriers, we noticed an interesting pattern: some 5-courier combinations ended up using prices from only 3 of them. Why? Because the other two didn’t offer better rates for any shipment. In practical terms: if a 5-courier group behaves exactly like a 3-courier one, why keep the larger version? This insight let us skip over redundant combinations, prioritizing only those where each courier added real value. Fewer combinations to test = faster computation.</p><p><strong>Step 4: Combine — but only what’s worth it. </strong>With our courier set trimmed and focused, we generated all remaining combinations (up to 5 couriers per group) in a brute-force fashion. This became the core of our comparative analysis between total coverage and operational cost.</p><p><strong>Step 5: Treat memory like gold. </strong>The data volume was still significant, so we made sure our code was memory-efficient. That meant avoiding unnecessary intermediate structures, creating variables only when needed (instead of preloading everything), and reusing intermediate calculations wherever possible.</p><p>For example, let’s say we wanted to store courier combinations like this:</p><pre>combination: {<br>  name: &quot;courier-x&quot;,<br>  num_carriers: 1,<br>  prices: [x,y,z,...], # N-prices by coverage<br>  total_price: 0.0,<br>  ...<br>}</pre><p>Now, when building combinations of 2, 3, up to 5 couriers, their prices ultimately derive from these initial single-courier structures. So yes, we <em>could</em> pre-fill the prices array for every combination… But if you’re dealing with 50 couriers, you end up storing over <strong>2.1 MM</strong> such combinations, each with a price array of length <em>N</em>. That’s a huge memory footprint. Instead, we chose to only populate price arrays for the <em>top</em> combinations — say, the best 3. That way, price details were filled in only <strong>when</strong> needed for final output (e.g., generating an Excel report), avoiding wasteful memory usage.</p><pre>...<br><br>// get top 3 combination only with total price without prices array<br>const top3 = getTopByAllCombinations(allCombinations)<br>// then load prices array only for top combinations<br>for (let bestCombination of top3) {<br>  bestCombination.prices = loadPricesFor(bestCombination); // here load all prices array<br>}<br>...</pre><p>Thanks to these steps, we transformed an initially unmanageable problem into something efficient and actionable — without sacrificing precision or depth. And remarkably, we did it all <strong>without</strong> relying on complex algorithms or specialized libraries.</p><h4>🏅Results</h4><p>At the end of the day, every optimization effort must be measured by its real-world impact. And in our case, the difference between the naive, unfiltered solution and the intelligently reduced version was dramatic.</p><p>To give some context: we implemented the entire solution using an algorithm written in <strong>TypeScript</strong>, mainly for practical reasons. The team was already fluent in the language, and the ecosystem offered stronger support for working with and styling Excel files — the required format for delivering final results.</p><p>We started with a <strong>real dataset of 1,583 shipments</strong> (N = 1,583), provided by an internal client, with price quotes from <strong>47 different couriers</strong> (C = 47). Without applying any kind of filtering or reduction, the total number of possible courier combinations was massive. To be precise:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/405/0*SFMa8rMF10cZOOM7" /></figure><p>This meant that for each of those 1.7 MM combinations, the algorithm had to evaluate the best price among the involved couriers for each of the 1,583 shipments. That gave us a search matrix of roughly:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/275/0*qW-4HDaZFdRc7-tm" /></figure><p>Yes, <strong>over 2,700 MM iterations</strong>.</p><p>However, thanks to the preprocessing filters we introduced — such as removing couriers with no useful coverage, keeping only those who were the cheapest for at least one region, and eliminating redundant combinations — we managed to reduce the courier pool from 47 to <strong>13 relevant candidates</strong>. This led to a dramatic drop in possible combinations:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/370/0*LppDnyY6pz_S1r-d" /></figure><p>Which brought the new total evaluation volume down to:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/0*fQ8R5tngCs_l2cfm" /></figure><p>The results were impressive: the final, optimized algorithm with filters in place ran in just <strong>7 seconds</strong>. In contrast, running the unfiltered version on the full dataset was estimated to take around <strong>5,040 seconds</strong> (1 hour and 24 minutes).</p><p>That’s a <strong>99%+ improvement in execution time</strong>, without compromising the quality of the results — and with a much leaner, more practical solution.</p><p>Here’s a side-by-side comparison:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zJbguC3GrnX9M-9qy2-ong.png" /><figcaption>Comparative table of filtered and unfiltered data.</figcaption></figure><p>Lastly, it’s worth noting that these optimizations didn’t just improve execution time — they also reduced the <strong>physical resource consumption</strong> required to run the system. By narrowing the search space and working with lighter data structures, we ensured the service could run stably and efficiently even in environments with <strong>limited infrastructure</strong>.</p><p>In our case, the final solution was deployed on <strong>Google Cloud Run</strong> with a configuration of <strong>4 CPUs and 4 GB of memory</strong>, and the performance was more than satisfactory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*kj2pBGM859d_GCusQUYV7Q.png" /><figcaption>CPU usage graph during execution of the optimized algorithm in Cloud Run</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*f8hrxUcoZhiIFenyiWi0vQ.png" /><figcaption>Memory usage graph during the execution of the optimized algorithm in Cloud Run</figcaption></figure><h4>✨ Thoughts and conclusions</h4><p>Beyond algorithmic performance, this experience also taught us a valuable lesson about the relationship between code design and infrastructure efficiency. As we showed earlier, by reducing the search space and avoiding heavy data structures, the algorithm was able to run smoothly in a resource-constrained environment. The performance charts made it clear: CPU and memory usage remained stable, with no unnecessary spikes and consistently fast execution. This reinforces a fundamental principle — <strong>infrastructure efficiency starts with code quality</strong>.</p><p>Of course, we understand that problems like these are rarely solved once and for all. While we achieved a significant improvement in runtime and computational efficiency, the algorithm still has plenty of room to evolve. Currently, our model considers only two variables: <strong>total coverage</strong> and <strong>overall operational cost</strong>. But in real-world logistics, not all shipments carry the same weight — figuratively or literally. For example, a route from A → B with high volume or frequency may have a much greater operational impact than a rare shipment from C → D. That’s why a natural next step would be to introduce <strong>route weighting</strong>, assigning each destination a relative value based on shipment frequency, volume, or business impact. This would add a new dimension to the algorithm: <strong>the relative importance of each shipment</strong>, not just its existence or cost.</p><p>And we could go even further. Future iterations of the algorithm could include additional variables like <strong>courier service levels</strong>, <strong>response times</strong>, or even <strong>environmental impact</strong>. By integrating all these dimensions, we wouldn’t just be optimizing for the cheapest or most widespread option — we’d be aiming for the most <strong>balanced and realistic solution</strong>, fixed to operational priorities.</p><p>That said, the core approach should remain unchanged. Because beyond any algorithm, the real breakthrough came from careful <strong>pre-analysis</strong>: narrowing down the problem space, identifying what truly matters, and simplifying as much as possible <em>before</em> we even started calculating.</p><p>After all, <strong>optimization isn’t always about finding the best solution within a massive universe — it’s about designing the universe so that finding the best solution actually makes sense</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0507250e0900" width="1" height="1" alt=""><hr><p><a href="https://medium.com/enviame-engineering/complex-problems-dont-always-need-complex-solutions-how-we-reduced-a-combinatorial-problem-by-99-0507250e0900">Complex problems don’t always need complex solutions: how we reduced a combinatorial problem by 99%</a> was originally published in <a href="https://medium.com/enviame-engineering">Envíame Engineering Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>