<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: weicheng95</title>
    <description>The latest articles on DEV Community by weicheng95 (@weicheng95).</description>
    <link>https://dev.to/weicheng95</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F350765%2F842d54f8-be3b-4aac-92c5-b6d7bb514fde.jpeg</url>
      <title>DEV Community: weicheng95</title>
      <link>https://dev.to/weicheng95</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/weicheng95"/>
    <language>en</language>
    <item>
      <title>Picking a credit card is an optimization problem, so I run a solver in a browser tab</title>
      <dc:creator>weicheng95</dc:creator>
      <pubDate>Mon, 02 Nov 2026 01:45:23 +0000</pubDate>
      <link>https://dev.to/weicheng95/picking-a-credit-card-is-an-optimization-problem-so-i-run-a-solver-in-a-browser-tab-1m9j</link>
      <guid>https://dev.to/weicheng95/picking-a-credit-card-is-an-optimization-problem-so-i-run-a-solver-in-a-browser-tab-1m9j</guid>
      <description>&lt;p&gt;Most credit card calculators just multiply your monthly spend by a reward rate. That works fine until you run into minimum spend tiers or shared category caps—at which point simple math gives you the wrong answer.&lt;/p&gt;

&lt;p&gt;While building &lt;a href="https://duitbase.com" rel="noopener noreferrer"&gt;DuitBase&lt;/a&gt;, a credit card calculator for Malaysians, I wanted users to plug in a normal month of spending and get the exact best one-, two-, or three-card combo, down to where every single ringgit should go. I initially thought I could brute-force this with a few loops. Instead, I ended up embedding a linear programming solver in the browser, and then falling down a rabbit hole of solver bugs that only showed up under property-based testing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where greedy logic breaks
&lt;/h2&gt;

&lt;p&gt;Take a hypothetical month of spending across two made-up cards:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Category&lt;/th&gt;
&lt;th&gt;Spend&lt;/th&gt;
&lt;th&gt;Card A&lt;/th&gt;
&lt;th&gt;Card B&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Dining&lt;/td&gt;
&lt;td&gt;RM400&lt;/td&gt;
&lt;td&gt;5%&lt;/td&gt;
&lt;td&gt;1%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Groceries&lt;/td&gt;
&lt;td&gt;RM300&lt;/td&gt;
&lt;td&gt;5%&lt;/td&gt;
&lt;td&gt;1%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Petrol&lt;/td&gt;
&lt;td&gt;RM400&lt;/td&gt;
&lt;td&gt;0.2%&lt;/td&gt;
&lt;td&gt;2%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The catch: Card A requires a RM1,000 monthly minimum spend to unlock that 5% rate. Fall short, and everything on Card A drops to a flat 0.2%.&lt;/p&gt;

&lt;p&gt;If you use a naive greedy rule—"always swipe the card with the highest rate for each category"—you put dining and groceries on Card A, and petrol on Card B. Card A ends up with RM700, misses the RM1,000 threshold, and your cashback tanks to RM9.40.&lt;/p&gt;

&lt;p&gt;Here is how the numbers actually play out across different strategies:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Plan&lt;/th&gt;
&lt;th&gt;Cashback&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Best rate per category&lt;/td&gt;
&lt;td&gt;RM9.40&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Everything on Card B&lt;/td&gt;
&lt;td&gt;RM15.00&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Everything on Card A&lt;/td&gt;
&lt;td&gt;RM35.80&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;RM300 of petrol on Card A, RM100 on Card B&lt;/td&gt;
&lt;td&gt;RM37.60&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;To get the optimal RM37.60 payout, you deliberately put RM300 of petrol on the card with the &lt;em&gt;worse&lt;/em&gt; petrol rate just to cross Card A's RM1,000 threshold. A category-by-category loop will never find this because the value of one ringgit depends on where your other ringgit go. Shared caps cause the exact same headache, since spending a ringgit in one category eats into the bonus limit of another.&lt;/p&gt;

&lt;h2&gt;
  
  
  Modeling it with MILP
&lt;/h2&gt;

&lt;p&gt;Under the hood, this is a mixed-integer linear program (MILP)—a linear optimization problem where some variables are forced to be whole numbers. I used &lt;a href="https://www.npmjs.com/package/yalps" rel="noopener noreferrer"&gt;yalps&lt;/a&gt;, a lightweight JavaScript solver. &lt;/p&gt;

&lt;p&gt;Here is that exact problem running on yalps 0.6.4 to find the RM37.60 split:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;solve&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;yalps&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;direction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;maximize&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;objective&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cashback&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;constraints&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Every ringgit must go somewhere.&lt;/span&gt;
    &lt;span class="na"&gt;dining&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;groceries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;petrol&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="c1"&gt;// Card A's spend must reach RM1,000 when `reached` is 1.&lt;/span&gt;
    &lt;span class="na"&gt;minSpend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;min&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="c1"&gt;// The 5% rate is off when `reached` is 0.&lt;/span&gt;
    &lt;span class="na"&gt;bonusGate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;max&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;variables&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;diningA5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;cashback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.05&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;dining&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;minSpend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;bonusGate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;groceriesA5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;cashback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.05&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;groceries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;minSpend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;bonusGate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;diningA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;cashback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.002&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;dining&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;minSpend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;groceriesA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;cashback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.002&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;groceries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;minSpend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;petrolA&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;cashback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.002&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;petrol&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;minSpend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;diningB&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;cashback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.01&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;dining&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;groceriesB&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;cashback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.01&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;groceries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;petrolB&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;cashback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.02&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;petrol&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;reached&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;minSpend&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;bonusGate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1100&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;binaries&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;reached&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;variables&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;solve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromEntries&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;variables&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="c1"&gt;// optimal 37.60 { diningA5: 400, groceriesA5: 300, petrolA: 300, petrolB: 100, reached: 1 }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The production model in DuitBase follows the same structure. Every variable represents the ringgit from a specific spending segment assigned to a specific card rule. Minimum-spend tiers get binary switches, and caps—including shared caps across categories—get their own constraint rows.&lt;/p&gt;

&lt;p&gt;Before committing to a full solver, I tried two shortcuts:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Greedy fill across every tier combination:&lt;/strong&gt; Breaks down as soon as categories share a cap.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pre-filtering to the top K cards by single-card score:&lt;/strong&gt; Missed the true optimal combo on 28% to 56% of my benchmark months, underperforming by up to 17%.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Running it in the browser
&lt;/h2&gt;

&lt;p&gt;Because financial data is personal and the UI updates live as you type, I didn't want to bounce requests off a backend. Everything runs locally inside a Web Worker on each keystroke, and your numbers never leave your device unless you explicitly generate a share link.&lt;/p&gt;

&lt;p&gt;When firing a solver on every keystroke, you have to make sure a slow solve from three keystrokes ago doesn't overwrite a fresher result. The worker wrapper tags every message with an incrementing ID and drops stale replies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lastId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;inFlight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;submit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;lastId&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;inFlight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;lastId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;lastId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;receive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;inFlight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// A newer request replaced this one.&lt;/span&gt;
  &lt;span class="nx"&gt;inFlight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In CI, the benchmark script fails if p95 latency creeps above 50 ms in Node. On an older commit, my iPhone clocked cash plans at 52.7 ms on a sample month and 187.2 ms on randomized months. The solver is much faster now, though I haven't re-run the mobile numbers or tested on a mid-range Android yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  When "optimal" isn't actually optimal
&lt;/h2&gt;

&lt;p&gt;The biggest surprise of the project happened after my benchmarks were green and the plans looked completely fine to the naked eye. I wired up property-based testing with &lt;a href="https://fast-check.dev/" rel="noopener noreferrer"&gt;fast-check&lt;/a&gt;, and it immediately started unearthing edge cases where &lt;code&gt;yalps&lt;/code&gt; claimed a valid model was infeasible—or worse, confidently returned the wrong answer.&lt;/p&gt;

&lt;p&gt;To catch silent math errors, I brought in &lt;a href="https://highs.dev/" rel="noopener noreferrer"&gt;HiGHS&lt;/a&gt;, an industrial-strength solver compiled to ~4 MB of WebAssembly, to act as a test oracle. It's too heavy to ship to users, but perfect for comparing outputs in a test runner:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;fc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;fc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;property&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arbitraryMonth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;month&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;buildModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;month&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ours&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;solveWithYalps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;oracle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;solveWithHighs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;abs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ours&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;objective&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;oracle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;objective&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;toBeLessThan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tolerance&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}),&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two bugs caught by this setup really stood out:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The RM23 undercount:&lt;/strong&gt; On one randomly generated month, &lt;code&gt;yalps&lt;/code&gt; returned an "optimal" three-card plan paying RM342.72, while HiGHS found a RM365.80 plan on the exact same model. Because &lt;code&gt;yalps&lt;/code&gt; doesn't scale its matrix internally, packing an entire month's spend into the "is this card active?" binary constraint caused numerical instability. At its default $10^{-8}$ tolerance, &lt;code&gt;yalps&lt;/code&gt; falsely flagged the better branch as infeasible. Rather than loosening tolerances globally, I restructured the search to evaluate card combinations separately without top-level card binaries, using branch-and-bound to skip combinations that couldn't win. That fixed the accuracy bug and dropped cash p95 latency from over 2 seconds down to a few dozen milliseconds.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The broken cap:&lt;/strong&gt; Another run at $10^{-8}$ returned "optimal" while putting RM4,161.99 into a constraint capped at RM4,160. Now, the app explicitly verifies every row constraint and binary variable after &lt;code&gt;yalps&lt;/code&gt; finishes; if anything violates a rule, it re-solves once at $10^{-7}$. That runtime guard catches broken constraints, though you still need an oracle test in CI to catch valid-looking sub-optimal plans.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If there's one takeaway here: treat a solver's &lt;code&gt;"optimal"&lt;/code&gt; status as an unverified claim, not a mathematical proof.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pruning the search space
&lt;/h2&gt;

&lt;p&gt;You never need to evaluate a card if another card beats or ties it across every single category for that user's month. By comparing Card B's guaranteed base rate against Card A's maximum possible rate per segment, we can safely throw out dominated cards before building the model.&lt;/p&gt;

&lt;p&gt;Adding that pre-pass eliminated a median of 95 out of 112 candidate cards on the airline miles benchmark, and 49 out of 86 on cashback. Calculation p95 dropped from 78 ms to 16 ms for miles, and from 60 ms to 35 ms for cash—and the HiGHS oracle tests guarantee our pruning rules never accidentally toss out a winning card.&lt;/p&gt;

&lt;h2&gt;
  
  
  The final boss: rounding
&lt;/h2&gt;

&lt;p&gt;Even when the solver math is flawless, standard rounding can make the UI look broken. Rounding each category line item independently caused one plan's breakdown rows to add up to 1,994 miles right above a headline total of 1,993. Users check the math, and being off by one kills trust in the rest of the numbers.&lt;/p&gt;

&lt;p&gt;Switching the breakdown table to largest-remainder rounding solved it: floor every row first, then distribute the leftover units one by one to the rows with the largest fractional remainders. The breakdown always sums cleanly to the total, even if an individual line item occasionally differs by 1 unit from what that card's standalone table would show.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;When the value of one decision depends on your other decisions, stop writing &lt;code&gt;for&lt;/code&gt; loops and start writing a constraint model.&lt;/li&gt;
&lt;li&gt;Never trust a lightweight solver in production without fuzzing it against a heavy-duty oracle solver on random inputs.&lt;/li&gt;
&lt;li&gt;Benchmarks only tell you that your code is fast—property tests tell you if it's right.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you want to dig deeper into the domain rules, I wrote up the full breakdown on the &lt;a href="https://duitbase.com/how-we-calculate" rel="noopener noreferrer"&gt;How we calculate&lt;/a&gt; page. For anyone else who has shipped a solver in JS or WASM: how did you verify its outputs?&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Building my AI directory on a budget, all with Cloudflare</title>
      <dc:creator>weicheng95</dc:creator>
      <pubDate>Sun, 21 Apr 2024 07:38:00 +0000</pubDate>
      <link>https://dev.to/weicheng95/building-my-ai-directory-on-a-budget-all-with-cloudflare-4le8</link>
      <guid>https://dev.to/weicheng95/building-my-ai-directory-on-a-budget-all-with-cloudflare-4le8</guid>
      <description>&lt;p&gt;The SaaS world is bursting with products, and I figured – why not try building something myself?  I wanted to use the latest and greatest tech, so I put together an awesome stack:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cloudflare R2, D1, and Drizzle&lt;/strong&gt;: For storage and database (more on this in a minute!)&lt;br&gt;
&lt;strong&gt;Cloudflare Pages&lt;/strong&gt;: Hosting, because who doesn't love free?&lt;br&gt;
&lt;strong&gt;Sveltekit&lt;/strong&gt;: My favorite framework for a smooth, snappy experience.&lt;br&gt;
&lt;strong&gt;shadcn&lt;/strong&gt;: To make it look good (Tailwind is the best!).&lt;/p&gt;

&lt;p&gt;Honestly, this is probably the most affordable way to build something with Svelte right now.  Cloudflare has blown me away with what it gives away, and it lets a scrappy developer like me get started for next to nothing!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cloudflare to the Rescue!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At first, I was thinking about using Supabase for my database.  But then I discovered that Cloudflare actually has their own built-in SQLite database – &lt;strong&gt;D1&lt;/strong&gt;!  And guess what? It's super fast.  So I figured, why switch gears when I've got an amazing solution right here at Cloudflare?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What I've Done So Far&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Honestly, it's still basic. Right now, you can:&lt;/p&gt;

&lt;p&gt;Browse AI products&lt;br&gt;
Submit a new AI product&lt;/p&gt;

&lt;p&gt;Please head over to &lt;a href="https://spaceofai.tools/"&gt;https://spaceofai.tools/&lt;/a&gt; and let me know what you think!&lt;/p&gt;

</description>
      <category>cloudflare</category>
      <category>svelte</category>
      <category>tailwindcss</category>
      <category>saas</category>
    </item>
    <item>
      <title>Sonderbase - Travel Content Sharing Platform</title>
      <dc:creator>weicheng95</dc:creator>
      <pubDate>Tue, 22 Mar 2022 03:52:01 +0000</pubDate>
      <link>https://dev.to/weicheng95/sonderbase-travel-content-sharing-platform-5c9h</link>
      <guid>https://dev.to/weicheng95/sonderbase-travel-content-sharing-platform-5c9h</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--95mVabze--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/656g3jzj7zb2swk40ryp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--95mVabze--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/656g3jzj7zb2swk40ryp.png" alt="Image description" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Sonderbase is the platform that allows travelers to share their travel experiences with the community.&lt;/p&gt;

&lt;p&gt;Treating it as a personal blog, Sonderbase aggregate all the travel-related information, articles, providing your a one-stop solution.&lt;/p&gt;

&lt;p&gt;In Sonderbase, you can create your personal profile, write and share your experiences with the community.&lt;/p&gt;

&lt;p&gt;In the future, we will bring in features like trip planner, personal travel recorder(Leaving digital footprint that others can learn more about you), and more. If you would like to see any feature, feel free to leave me some comments.&lt;br&gt;
here is our website: &lt;a href="https://sonderbase.com"&gt;Sonderbase&lt;/a&gt;&lt;/p&gt;

</description>
      <category>travel</category>
      <category>travelcontent</category>
      <category>trip</category>
      <category>backpacker</category>
    </item>
    <item>
      <title>test</title>
      <dc:creator>weicheng95</dc:creator>
      <pubDate>Wed, 09 Mar 2022 13:52:25 +0000</pubDate>
      <link>https://dev.to/weicheng95/test-4gci</link>
      <guid>https://dev.to/weicheng95/test-4gci</guid>
      <description>&lt;p&gt;test&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
