theme = Generators.observe(notify => {const read = () => {const css =getComputedStyle(document.body);const v = (name, fallback) => (css.getPropertyValue(name).trim() || fallback);return {dark:document.body.classList.contains("quarto-dark"),fg:v("--bs-body-color","#212529"),bg:v("--bs-body-bg","#ffffff"),muted:v("--bs-secondary-color","#6c757d"),grid:v("--bs-border-color","#dee2e6"),primary:v("--bs-link-color","#0d6efd"),accent:v("--bs-danger","#dc3545"),good:v("--bs-success","#198754") }; };// Quarto swaps stylesheets when the theme changes, and the new variables apply only once// the stylesheet has loaded; read them again shortly after, and keep the last reading.let last ="";const emit = () => {const t =read();const key =JSON.stringify(t);if (key !== last) { last = key;notify(t); } };const update = () => { emit();setTimeout(emit,100);setTimeout(emit,400); };update();const observer =newMutationObserver(update); observer.observe(document.body, {attributes:true,attributeFilter: ["class"]});return () => observer.disconnect();})
plotStyle = ({// "inherit" takes the page's text color directly, so axes and labels follow a theme// switch even before the plot redraws.color:"inherit",background:"transparent",fontSize:"13px",overflow:"visible"})
themedButton = (label, counterLabel =null) => {const button =html`<button type="button" class="btn btn-sm btn-outline-primary bisc-explorer-button">${label}</button>`;// The counter shows which run is on screen, so every press visibly does something and a// reader can tell runs apart. It reads 1 at page load and after Reset.const counter = counterLabel?html`<span class="bisc-run-counter" aria-live="polite"></span>`:null;const form =html`<form class="bisc-explorer-control">${button}${counter ||""}</form>`;const show = () => { if (counter) counter.textContent=`${counterLabel}${form.value+1}`; }; form.value=0;show(); button.onclick= () => { form.value+=1; form.dispatchEvent(newEvent("input", {bubbles:true})); };// Reset sets the value back to 0 and dispatches input; refresh the counter either way. form.addEventListener("input", show);return form;}
resetButton = (controls, label ="Reset to the starting values") => {// This runs once per page, because the cell that calls it depends on the controls// themselves (viewof), not on their values; so these are the values at page load.const start = controls.map(c => c.value);const button =html`<button type="button" class="btn btn-sm btn-outline-primary bisc-explorer-button">${label}</button>`; button.onclick= () => { controls.forEach((c, k) => { c.value= start[k]; c.dispatchEvent(newEvent("input", {bubbles:true})); }); };returnhtml`<div style="margin: 0.5rem 0">${button}</div>`;}
// A small seeded generator (mulberry32), so a run can be repeated.makeRandom = (seed) => {let a = seed >>>0;return () => { a = (a +0x6D2B79F5) >>>0;let t = a; t =Math.imul(t ^ (t >>>15), t |1); t ^= t +Math.imul(t ^ (t >>>7), t |61);return ((t ^ (t >>>14)) >>>0) /4294967296; };}
This explorer draws the general inventory cycle of Chapter 3 and costs it, for the instance the chapter optimizes: demand at \(\lambda = 100\) units a year, production at \(p = 250\) units a year, an ordering cost of \(k = \$20\), a holding cost of \(h = \$4\) per unit per year, and a backorder cost of \(b = \$6\) per unit per year. Choose an order quantity \(Q\) and a backorder level \(\hat{B}\) and watch the cycle and its cost change. Switch off finite production or backordering, or add a charge \(\pi\) per unit short, and the page moves to the classical model the chapter’s table of special cases names.
Recall that \(a = 1 - \lambda/p\) is the fraction of each order that survives to become stock. The peak is \(\hat{I} = Qa - \hat{B}\), the cycle lasts \(T = Q/\lambda\), and the averages are the areas of two triangles divided by the cycle length:
The relevant cost is \(k\lambda/Q + h\bar{I} + b\bar{B}\), plus \(\pi\) on every unit that arrives to an empty shelf. The page runs in your browser and records nothing. Its formulas are a restatement in JavaScript of the book’s code, and they are tested against that code. For an item of your own, use the general explorer.
The Cycle
viewof finite = Inputs.toggle({label:"Production at p = 250 a year",value:true})viewof allowB = Inputs.toggle({label:"Backorders permitted, b = $6",value:true})viewof pi = Inputs.range([0,2], {step:0.05,value:0,label:"Charge per unit short, π ($)"})viewof Q = Inputs.range([10,150], {step:1,value:60,label:"Order quantity, Q"})viewof Bhat = Inputs.range([0,60], {step:0.5,value:10,label:"Backorder level, B̂"})useOptimum = {const button =html`<button type="button" class="btn btn-sm btn-outline-primary bisc-explorer-button">Use the optimal policy</button>`; button.onclick= () => {for (const [view, v] of [[viewof Q,Math.round(best.Q)], [viewof Bhat,Math.round(2* best.Bhat) /2]]) { view.value= v; view.dispatchEvent(newEvent("input", {bubbles:true})); } };returnhtml`<div style="margin: 0.5rem 0">${button}</div>`;}resetButton([viewof finite, viewof allowB, viewof pi, viewof Q, viewof Bhat])
{const n = (x, d =2) => x.toFixed(d);const dollars = x =>"$"+ x.toLocaleString("en-US", {minimumFractionDigits:2,maximumFractionDigits:2});const regime = best.regime==="interior"?"": best.regime==="no backorders"?"":` The closed form plans no shortage here (${best.regime.replace("boundary: ","")}), so the optimum is on the boundary B̂ = 0.`;const clipped = allowB && Bhat > Q * a ?` The backorder level is held at Qa = ${n(Q * a)}, the most an order can cover.`:"";const rows = [ ["Order quantity, Q",n(here.Q),n(opt.Q)], ["Backorder level, B̂",n(here.Bhat),n(opt.Bhat)], ["Peak on hand, Î",n(here.Ihat),n(opt.Ihat)], ["Cycle length, T (years)",n(here.T,4),n(opt.T,4)], ["Average on hand, Ī",n(here.Ibar,4),n(opt.Ibar,4)], ["Average backorders, B̄",n(here.Bbar,4),n(opt.Bbar,4)], ["Ready rate",n(here.readyRate,4),n(opt.readyRate,4)], ["Ordering, kλ/Q",dollars(here.ordering),dollars(opt.ordering)], ["Holding, hĪ",dollars(here.holding),dollars(opt.holding)], ["Backorders, bB̄",dollars(here.backorder),dollars(opt.backorder)], ["Units short, π",dollars(here.stockout),dollars(opt.stockout)], ["Relevant cost a year",dollars(here.relevant),dollars(opt.relevant)] ];returnhtml`<p>The model is the <strong>${modelName(item)}</strong>.${regime}${clipped}</p> <table class="table table-sm" style="max-width: 42rem"> <thead><tr><th>Measure</th><th>Your policy</th><th>The optimum</th></tr></thead> <tbody>${rows.map(r =>html`<tr><td>${r[0]}</td><td>${r[1]}</td><td>${r[2]}</td></tr>`)}</tbody> </table>`;}
The first plot is the net inventory over two cycles of your policy. It rises at \(p - \lambda\) while production runs, clearing the backorders and then building the peak, and falls at \(\lambda\) until the next run starts at \(-\hat{B}\). Notice that with \(p\) switched off the rise is a vertical jump.
path = {const [T1, T2, T3, T4] = here.segments, T = here.T, pts = [];for (let c =0; c <2; c++) {const t0 = c * T; pts.push({t: t0,y:-here.Bhat});if (!finite) pts.push({t: t0,y: here.Ihat});else pts.push({t: t0 + T1 + T2,y: here.Ihat}); pts.push({t: t0 + T,y:-here.Bhat}); }return pts;}Plot.plot({style: plotStyle,height:240,marginLeft:50,marginBottom:45,x: {label:"Time (years)",labelAnchor:"center",labelOffset:38,grid:true},y: {label:"Net inventory (units)",grid:true},marks: [ Plot.ruleY([0], {stroke: theme.muted}), Plot.line(path, {x:"t",y:"y",stroke: theme.primary,strokeWidth:1.75}) ],ariaLabel:"Net inventory over two cycles",ariaDescription:`Each cycle lasts ${here.T.toFixed(3)} years, peaks at ${here.Ihat.toFixed(2)} units and falls to ${(-here.Bhat).toFixed(2)}.`})
The second plot is the relevant cost against \(Q\), each \(Q\) with its best backorder level, \(\hat{B} = (hQa - \pi\lambda)/(h + b)\) held between 0 and \(Qa\). The red dot is your policy at the backorder level you chose. Notice how flat the curve is near its minimum.
curve = d3.range(10,151,1).map(q => ({Q: q,cost:evaluate(item, q,bestBackorder(item, q)).relevant}))Plot.plot({style: plotStyle,height:240,marginLeft:60,marginBottom:45,x: {label:"Order quantity, Q",labelAnchor:"center",labelOffset:38,grid:true},y: {label:"Relevant cost a year ($)",grid:true,domain: [0,2.5* opt.relevant]},marks: [ Plot.line(curve, {x:"Q",y:"cost",stroke: theme.muted,strokeWidth:1.5,clip:true}), Plot.dot([{Q,cost: here.relevant}], {x:"Q",y:"cost",r:6,fill: theme.accent,clip:true}) ],ariaLabel:"Relevant cost against the order quantity",ariaDescription:`The least cost is $${opt.relevant.toFixed(2)} at Q = ${opt.Q.toFixed(2)}. Your policy costs $${here.relevant.toFixed(2)}.`})
html`<p>Your $Q$ is ${(Q / best.Q).toFixed(3)} times the optimal one. ${allowB && pi >0?"":`With no charge per unit short and the best backorder level, the chapter's penalty ½(Q/Q* + Q*/Q) = ${penalty(Q / best.Q).toFixed(4)} gives the cost ratio, and the curve above has ${(evaluate(item, Q,bestBackorder(item, Q)).relevant/ opt.relevant).toFixed(4)}.`}</p>`
Things to Try
Press Use the optimal policy. The optimum is \(Q^{*} = 52.70\) and \(\hat{B}^{*} = 12.65\), the chapter’s answer, rounded here to the sliders’ steps of 1 and 0.5.
Start from \(Q = 60\) and \(\hat{B} = 10\). The shelf is empty during the two segments below zero, \(T_4 = \hat{B}/\lambda = 0.1\) year and \(T_1 = \hat{B}/(p - \lambda) = 0.0667\) year, out of a cycle of \(T = 0.6\) year. Thus, the ready rate is \(1 - 0.1667/0.6 = 0.7222\), which the table shows.
Switch off backorders. The model becomes the economic production quantity, every unit short is now forbidden rather than owed, and the optimum falls to \(Q^{*} = \sqrt{2k\lambda/(ha)} = 40.82\).
Switch off finite production as well. The model is the classical EOQ, \(Q^{*} = \sqrt{2(20)(100)/4} = 31.62\), and the cycle becomes a sawtooth.
With both switched on, raise \(\pi\). By \(\pi = 1\) the closed form’s backorder level is no longer positive, and by \(\pi = 1.55\) the root is gone: either way the optimum plans no shortage, as the chapter shows.
Check What You Found
Check what you found
1. On the chapter’s instance, the optimum with backorders is \(Q^{*} = 52.70\) with \(\hat{B}^{*} = 12.65\), at a relevant cost of $75.89 a year. Forbidding backorders gives \(Q^{*} = 40.82\) at $97.98 a year. Why does permitting backorders lower the cost?
2. On the same instance, raise the charge per unit short to \(\pi = 1.55\). What does the optimum do?