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 plans a requirements schedule you enter by every method of Chapter 5: the optimal plan by the Wagner-Whitin recursion, the three heuristics, lot-for-lot, the periodic order quantity, and the adjusted economic order quantity. It costs each plan and draws the stock each one carries. It starts from the belt’s year; Reset returns to it. The page runs in your browser and records nothing. Its methods are a restatement in JavaScript of the book’s code, and they are tested against that code on the belt and on thirty other schedules.
What This Page Assumes
The requirements. Known for every period of a finite horizon, entered one number per period, separated by commas, spaces or new lines. Zeros are allowed. At most 120 periods.
The costs. A fixed cost \(k\) per order and a holding cost \(h\) per unit carried into the next period, both the same in every period. The unit price is constant too, so it is left out: every cost shown is the relevant cost.
The system. No shortages, orders available in the period they are placed, and stock starting and ending at zero.
NoteFor anything else, use the software
Costs or prices that change from period to period, a rolling horizon, and the plans of a bill of material are in the code the chapter teaches, in the chapter’s Using the Software section, and the chapter’s workbook builds the same recursion cell by cell.
Your Schedule
viewof text = Inputs.textarea({value:"40, 60, 120, 300, 420, 260, 120, 40, 0, 80, 180, 220",rows:4,label:"Requirements, one per period"})viewof k = Inputs.number({value:300,label:"Ordering cost, k ($ an order)"})viewof h = Inputs.number({value:1,label:"Holding cost, h ($ a unit a period)"})viewof shown = Inputs.select(["Wagner-Whitin","Silver-Meal","Least unit cost","Part-period balancing","Lot-for-lot","Periodic order quantity","Adjusted economic order quantity"], {value:"Silver-Meal",label:"Plan to draw beside the optimum"})resetButton([viewof text, viewof k, viewof h, viewof shown])
import {allMethods, checkSchedule} from"./explorers/lib/dls.js"result = {const tokens = text.split(/[\s,;]+/).filter(Boolean);const d = tokens.map(Number);const problems = [];if (!tokens.length) problems.push("Enter at least one requirement.");if (d.some(x =>!Number.isFinite(x) || x <0)) problems.push("Every requirement must be a number, 0 or more.");if (d.length>120) problems.push(`The schedule has ${d.length} periods; this page takes at most 120.`);if (d.length&& d.every(x => x ===0)) problems.push("Every requirement is zero, so there is nothing to plan.");if (!(k >0)) problems.push("The ordering cost must be positive.");if (!(h >0)) problems.push("The holding cost must be positive.");if (problems.length) return {ok:false, problems};const schedule = {d, k, h};checkSchedule(schedule);const methods =allMethods(schedule);return {ok:true, d, methods,optimum: methods[0],picked: methods.find(m => m.name=== shown)};}
{if (!result.ok) {returnhtml`<div class="callout callout-style-simple callout-warning"><div class="callout-body"> <p><strong>The page cannot plan this schedule.</strong></p> <ul>${result.problems.map(p =>html`<li>${p}</li>`)}</ul></div></div>`; }const {d, methods, optimum} = result;const total = d.reduce((a, x) => a + x,0);const dollars = x =>"$"+ x.toLocaleString("en-US", {minimumFractionDigits:0,maximumFractionDigits:2});returnhtml`<p>${d.length} periods and ${total.toLocaleString("en-US")} units in all, an average of${(total / d.length).toFixed(2)} a period; k/h = ${(k / h).toFixed(2)}.</p> <table class="table table-sm" style="max-width: 46rem"> <thead><tr><th>Method</th><th>Order periods</th><th>Setup</th><th>Carrying</th><th>Relevant cost</th><th>Above optimal</th></tr></thead> <tbody>${methods.map(m =>html`<tr style="${m.name=== shown ?"font-weight: 600":""}"> <td>${m.name}${m.supply?`, T = ${m.supply}`:""}</td><td>${m.orders.join(", ")}</td> <td>${dollars(m.setup)}</td><td>${dollars(m.carrying)}</td><td>${dollars(m.relevant)}</td> <td>${m === optimum ?"optimal":`${(100* (m.relevant/ optimum.relevant-1)).toFixed(2)}%`}</td></tr>`)}</tbody> </table>`;}
The plot shows the requirements as gray bars and the stock carried out of each period under the optimal plan, the solid blue line, and the plan you chose, the dashed red one.
result.ok? Plot.plot({style: plotStyle,height:280,marginLeft:60,marginBottom:45,x: {label:"Period",labelAnchor:"center",labelOffset:38,domain: result.d.map((_, i) => i +1),tickFormat: t => (result.d.length>24&& t %5?"": t)},y: {label:"Units",grid:true},color: {legend:true,domain: ["Requirement","Optimal: stock carried",`${shown}: stock carried`],range: [theme.grid, theme.primary, theme.accent]},marks: [ Plot.barY(result.d.map((x, i) => ({t: i +1, x})), {x:"t",y:"x",fill: theme.grid}), Plot.line(result.optimum.I.map((x, i) => ({t: i +1, x})), {x:"t",y:"x",stroke: theme.primary,strokeWidth:2}), Plot.line(result.picked.I.map((x, i) => ({t: i +1, x})), {x:"t",y:"x",stroke: theme.accent,strokeWidth:2,strokeDasharray:"5,3"}) ],ariaLabel:"Requirements and the stock carried each period",ariaDescription:`The optimal plan costs $${result.optimum.relevant.toFixed(2)}; ${shown} costs $${result.picked.relevant.toFixed(2)}.`}) :html``
Things to Try
Start from the belt and set month 9 from 0 to 200. The optimum moves its fifth order to month 9 and costs $2,600, and Silver-Meal’s window from month 5 now stops at month 8, because month 9’s requirement makes the cost per month rise.
Enter a flat schedule, twelve periods of 100, with \(k = \$300\) and \(h = \$1\). Every method that orders in windows costs $2,400, whether it orders every two periods or every three; only lot-for-lot, at $3,600, does worse. With the requirement constant, the economic order quantity’s picture is the right one.
Enter a lumpy schedule, 0, 0, 500, 0, 0, 0, 20, 0, 0, 600. The optimum and part-period balancing order in periods 3 and 10 for $680. Least unit cost orders in period 7 for periods 7 through 10 and carries the 600 units three periods, for $2,400: dividing by more units flatters the long window, as the chapter warns.