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 page solves the constrained lot sizing problem of Chapter 4 for items you enter: it checks whether the limit binds, finds the multiplier at which the plan uses the whole of the limit and no more when it does, and reports each item’s quantity and cost. It also draws the items’ exchange curve. It starts from the chapter’s investment example; Reset returns to it. 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.
What This Page Assumes
Each item. Constant, known demand, no shortages and instantaneous replenishment, as in the economic order quantity. Enter one item per line as its demand rate, unit cost and ordering cost, separated by commas or spaces. At most 50 items.
Holding. A carrying charge \(i\) on the value held, the same for every item, so that \(h_j = i c_j\).
The limit. Either average investment, \(\sum_j c_j Q_j/2 \le A\), or replenishments a year, \(\sum_j \lambda_j/Q_j \le A\), or none. One limit at a time.
NoteFor anything else, use the software
A space limit with a volume per unit, holding cost quoted directly rather than as a carrying charge, several limits at once, and joint replenishment are in the code the chapter teaches, in the chapter’s Using the Software section, and the chapter’s workbook solves the same problems with a nonlinear solver.
Your Items
viewof text = Inputs.textarea({value:"12000, 20, 50\n25000, 10, 50\n8000, 15, 50",rows:6,label:"Demand rate, unit cost, ordering cost"})viewof i = Inputs.number({value:0.3,label:"Carrying charge, i (a year)"})viewof kind = Inputs.select(newMap([["Average investment","investment"], ["Replenishments a year","replenishment"], ["No limit","none"]]), {value:"investment",label:"The shared limit"})viewof A = Inputs.number({value:10000,label:"Limit, A"})resetButton([viewof text, viewof i, viewof kind, viewof A])
import {solve, planAt, exchange, checkItems} from"./explorers/lib/multiitem.js"result = {const problems = [];const lines = text.split("\n").map(s => s.trim()).filter(Boolean);const items = lines.map((s, n) => {const v = s.split(/[,\s]+/).map(Number);if (v.length!==3|| v.some(x =>!Number.isFinite(x) || x <=0)) problems.push(`Line ${n +1} needs three positive numbers: demand rate, unit cost and ordering cost.`);return {label:`Item ${n +1}`,lambda: v[0],c: v[1],k: v[2]}; });if (!lines.length) problems.push("Enter at least one item.");if (lines.length>50) problems.push(`There are ${lines.length} items; this page takes at most 50.`);if (!(i >0)) problems.push("The carrying charge must be positive.");if (kind !=="none"&&!(A >0)) problems.push("The limit must be positive.");if (problems.length) return {ok:false, problems};checkItems(items, i);const plan = kind ==="none"? {...planAt(items, i,"investment",0),binds:false,free:planAt(items, i,"investment",0)} :solve(items, i, kind, A);return {ok:true, items, plan,curve:exchange(items)};}
{if (!result.ok) {returnhtml`<div class="callout callout-style-simple callout-warning"><div class="callout-body"> <p><strong>The page cannot solve these items.</strong></p> <ul>${result.problems.map(p =>html`<li>${p}</li>`)}</ul></div></div>`; }const {plan} = result, free = plan.free;const dollars = x =>"$"+ x.toLocaleString("en-US", {minimumFractionDigits:2,maximumFractionDigits:2});const verdict = kind ==="none"?"With no limit, every item orders its economic quantity.": plan.binds?`The limit binds. Its multiplier is θ* = ${plan.theta.toPrecision(6)}, and the plan costs ${dollars(plan.relevant)} a year, ${dollars(plan.relevant- free.relevant)} more than the free plan's ${dollars(free.relevant)}.`:`The limit does not bind: the free plan uses ${kind ==="investment"?dollars(free.investment) : free.orders.toFixed(2)}, within ${kind ==="investment"?dollars(A) : A}, so every item orders its economic quantity.`;returnhtml`<p>${verdict}</p> <table class="table table-sm" style="max-width: 46rem"> <thead><tr><th>Item</th><th>Free Q</th><th>Q</th><th>Q / free Q</th><th>Ordering</th><th>Holding</th></tr></thead> <tbody>${plan.rows.map((r, j) =>html`<tr><td>${r.label}</td><td>${free.rows[j].Q.toFixed(2)}</td><td>${r.Q.toFixed(2)}</td> <td>${(r.Q/ free.rows[j].Q).toFixed(5)}</td><td>${dollars(r.ordering)}</td><td>${dollars(r.holding)}</td></tr>`)} <tr style="font-weight: 600"><td>Total</td><td></td><td></td><td></td><td>${dollars(plan.ordering)}</td><td>${dollars(plan.holding)}</td></tr></tbody> </table> <p>The plan holds ${dollars(plan.investment)} of average investment and places ${plan.orders.toFixed(2)} orders a year. On the exchange curve, investment times ordering cost is ${result.curve.constant.toExponential(4)} at every point, and the variety index is ${result.curve.varietyIndex.toFixed(2)}.</p>`;}
The plot is the items’ exchange curve: each point orders every item in its economic quantity at some price \(\theta\) on investment, and the product of its two coordinates is the same \(S^{2}/2\) at every point. The gray square is your carrying charge, \(\theta = i\); under an investment limit that binds, the red dot is the plan, at \(\theta = i + \theta^{*}\).
result.ok? (() => {const {curve, plan} = result;const marks = [ Plot.line(d3.range(0,201).map(j => {const t = i *0.1*Math.pow(100, j /200);return {oc: curve.orderingAt(t),inv: curve.investmentAt(t)};}), {x:"oc",y:"inv",stroke: theme.primary,strokeWidth:1.5}), Plot.dot([{oc: curve.orderingAt(i),inv: curve.investmentAt(i)}], {x:"oc",y:"inv",r:6,symbol:"square",fill: theme.muted}) ];if (kind ==="investment"&& plan.binds) {const t = i + plan.theta; marks.push(Plot.dot([{oc: curve.orderingAt(t),inv: curve.investmentAt(t)}], {x:"oc",y:"inv",r:6,fill: theme.accent})); }return Plot.plot({style: plotStyle,height:280,marginLeft:80,marginBottom:45,x: {label:"Ordering cost a year ($)",labelAnchor:"center",labelOffset:38,grid:true},y: {label:"Average investment ($)",grid:true}, marks,ariaLabel:"The exchange curve of the items entered",ariaDescription:`The variety index is ${curve.varietyIndex.toFixed(2)}.` });})() :html``
Things to Try
The starting items are the chapter’s investment example. The limit binds, every quantity is cut by the same factor, 0.81975, and the plan costs $145.04 a year more than the free plan.
Change the ordering costs to 50, 20 and 80 and choose a limit of 50 replenishments a year: the chapter’s dock example. The factors now differ by item, 1.51215, 2.05340 and 1.34317, because the multiplier joins the ordering cost rather than the carrying charge; and they exceed one, because fewer orders means larger ones.
Raise the investment limit to $13,000. The free plan uses $12,198.77, so the limit no longer binds.