Course Test · University: upper level
Optimisation: RatMath answers
The same twenty exercises, answered with RatMath Core and the Shelf libraries only. The code below was run when this page was built; its output is shown as printed, and compared with the independent answer.
Agrees 20
Libraries used here: core, analytic, linalg, discrete. In this code each library is in scope under its own name (linalg.det, stats.tTest), and Core's names (R, Rational, …) are in scope directly.
- 1.A linear programAgreesMaximise subject to , , , .Independent answerThe corners are with values : the maximum is 36 at .RatMath code
const lp = discrete.linearProgram({ maximize: [3, 5], constraints: [[[1, 0], "<=", 4], [[0, 2], "<=", 12], [[3, 2], "<=", 18]] }); [lp.status, lp.value, lp.x]Output["optimal", 36, [2, 6]]
- 2.Shadow pricesAgreesFor the same program, find the dual solution (shadow prices) and check that the dual value equals 36.Independent answerAt the second and third constraints are tight. Solving , with gives ; .RatMath code
const lp = discrete.linearProgram({ maximize: [3, 5], constraints: [[[1, 0], "<=", 4], [[0, 2], "<=", 12], [[3, 2], "<=", 18]] }); [lp.dual, lp.check()]Output[[0, 3/2, 1], true]
- 3.MinimisingAgreesMinimise subject to , , .Independent answerThe corners are with values : the minimum is 8 at .RatMath code
const lp = discrete.linearProgram({ minimize: [3, 2], constraints: [[[1, 1], ">=", 4], [[1, 3], ">=", 6]] }); [lp.value, lp.x]Output[8, [0, 4]]
- 4.InfeasibilityAgreesShow that , , has no solution.Independent answerAdding the first inequality to the negative of the second gives .RatMath code
discrete.linearProgram({ maximize: [1, 1], constraints: [[[1, 1], "<=", 1], [[1, 1], ">=", 2]] }).statusOutputinfeasible
- 5.UnboundednessAgreesShow that maximising subject to , is unbounded.Independent answerAlong every constraint holds and grows without bound.RatMath code
discrete.linearProgram({ maximize: [1, 1], constraints: [[[1, -1], "<=", 1]] }).statusOutputunbounded
- 6.Integer programmingAgreesMaximise subject to , , integers. Compare with the LP relaxation.Independent answerThe relaxation peaks at with value 21. Among integer points, gives 20, 19, 18: the integer optimum is 20 at .RatMath code
const ip = discrete.integerProgram({ maximize: [5, 4], constraints: [[[6, 4], "<=", 24], [[1, 2], "<=", 6]] }); [ip.value, ip.x, ip.relaxation.value]Output[20, [4, 0], 21]
- 7.KnapsackAgreesItems weigh 2, 3, 4, 5 and are worth 3, 4, 5, 6. Which should go in a knapsack holding weight 5?Independent answerThe pairs within weight 5: only (weight 5, worth 7); single items are worth at most 6. Take items 1 and 2, worth 7.RatMath code
const ip = discrete.integerProgram({ maximize: [3, 4, 5, 6], constraints: [[[2, 3, 4, 5], "<=", 5]] }, { binary: true }); [ip.value, ip.x]Output[7, [1, 1, 0, 0]]
binary: truemakes every variable 0 or 1. - 8.Assignment problemAgreesAssign four workers to four jobs at least total cost, with costs .Independent answerBy the Hungarian method: worker 1 → job 2, 2 → 1, 3 → 3, 4 → 4, cost .RatMath code
const a = discrete.assignment([[9, 2, 7, 8], [6, 4, 3, 7], [5, 8, 1, 8], [7, 6, 9, 4]]); [a.cost, a.assignment]Output[13, [1, 0, 2, 3]]
- 9.Transportation problemAgreesTwo warehouses hold 20 and 30 units; three stores need 10, 25 and 15. Shipping costs per unit are . Find the cheapest plan's cost.Independent answerWarehouse 1 sends 20 to store 2; warehouse 2 sends 10, 5, 15. Cost (checked by the stepping-stone test: every unused route has nonnegative reduced cost).RatMath code
const t = discrete.transportation([20, 30], [10, 25, 15], [[8, 6, 10], [9, 12, 13]]); [t.cost, t.plan]Output[465, [[0, 20, 0], [10, 5, 15]]]
transportationsolves the program exactly; its dual prices are the stepping-stone potentials that prove the plan optimal. - 10.Optimising a rectangleAgreesA rectangle has perimeter 20. What is the largest area it can have?Independent answerWith sides and , is largest at : area 25.RatMath code
analytic.extrema("x*(10 - x)", [0, 10]).exact().filter((e) => e.kind === "max").map((e) => [e.at, e.value])Output[[5, 25]]
- 11.One-variable minimumAgreesFind the minimum of .Independent answer: critical points 0 (not an extremum) and . .RatMath code
analytic.extrema("x^4 - 3*x^3 + 2", [-2, 4]).exact().filter((e) => e.kind === "min").map((e) => [e.at, e.value])Output[[9/4, -1675/256]]
- 12.Critical points in the planeAgreesFind and classify the critical point of .Independent answer, : . The Hessian is positive definite, so it is a minimum, .RatMath code
analytic.field("x^2 + x*y + y^2 - 3*x", ["x", "y"]).criticalPoints({ x: [-5, 5], y: [-5, 5] }).exact().map((c) => [c.kind, c.point.map((v) => Math.round(1e9 * ((v.low.toNumber() + v.high.toNumber()) / 2)) / 1e9), Math.round(1e9 * ((c.value.low.toNumber() + c.value.high.toNumber()) / 2)) / 1e9])Output[["min", [2, -1], -3]]
- 13.Second-derivative testAgreesClassify the critical points of .Independent answer, : . The Hessian is : at a minimum, at a saddle.RatMath code
analytic.field("x^3 - 3*x + y^2", ["x", "y"]).criticalPoints({ x: [-3, 3], y: [-3, 3] }).exact().map((c) => [c.kind, Math.round((c.point[0].low.toNumber() + c.point[0].high.toNumber()) / 2)]).sort((a, b) => a[1] - b[1])Output[["saddle", -1], ["min", 1]]
- 14.Lagrange multipliersAgreesFind the point of the line closest to the origin.Independent answerMinimise : , , so and , ; distance squared 5.RatMath code
const mid = (v) => (v.low.toNumber() + v.high.toNumber()) / 2; const r = analytic.lagrange("x^2 + y^2", "x + 2*y - 5", ["x", "y"], { x: [-10, 10], y: [-10, 10] }); const c = r.candidates[0]; [...Object.values(c.point).map(mid), mid(c.value), r.candidates.length] // x, y, value, how many candidatesOutput[1, 2, 5, 1]
The line is not bounded, so the library proves this is the only candidate in the box and leaves the minimum to the reader:max/minare only guaranteed on a closed bounded constraint set. - 15.A constrained maximumAgreesMaximise on the plane with .Independent answerBy symmetry (or AM–GM, ), the maximum is 1 at .RatMath code
const mid = (v) => (v.low.toNumber() + v.high.toNumber()) / 2; const r = analytic.lagrange("x*y*z", "x + y + z - 3", ["x", "y", "z"], { x: ["1/2", 2], y: ["1/2", 2], z: ["1/2", 2] }); [...Object.values(r.candidates[0].point).map(mid), mid(r.candidates[0].value), r.candidates.length] // x, y, z, value, how many candidatesOutput[1, 1, 1, 1, 1]
- 16.ConvexityAgreesIs convex? Give the Hessian's eigenvalues.Independent answerThe Hessian has eigenvalues , both positive: strictly convex.RatMath code
const H = [[4, 2], [2, 6]]; [linalg.definiteness(H).exact().kind, linalg.eigenvalues(H).exact().real.map((e) => e.value)]Output["positive-definite", [2.763932022500…, 7.236067977499…]]
- 17.Least squaresAgreesFit a line to by least squares.Independent answer, , , : , .RatMath code
linalg.leastSquares([[1, 0], [1, 1], [1, 2], [1, 3]], [1, 3, 4, 4]).exact().x.toArray().flat()Output[3/2, 1]
- 18.Gradient descentAgreesTake one gradient-descent step of size 0.1 from on .Independent answer, so the step lands at .RatMath code
analytic.gradientDescent("x^2 + 4*y^2", ["1", "1"], { step: "1/10", steps: 1 }).finalOutput[4/5, 1/5]
gradientDescentkeeps exact iterates for a polynomial objective; it is labelled a simulation of the method.Fix: ratmath/analytic#19,termsForError,newton(..., { exact: true }),darboux,pde.classify,gradientDescent - 19.Newton's method for a minimumAgreesTake two Newton steps for minimising from . What is the exact minimiser?Independent answer, . , . The minimiser is .RatMath code
const r = analytic.newton("2*x - 1/x", 1); // Newton's method on f' [r.iterations[1].x, r.iterations[2].x, r.root]Output[0.6666666666666667, 0.7058823529411764, 0.7071067811865475244:0.70710678118654752441]
The iterates are shown in floating point (); the root is a proven enclosure. - 20.DualityAgreesWrite the dual of: maximise subject to , , ; show both optimal values agree.Independent answerDual: minimise with , , . Primal optimum at is ; dual optimum at is .RatMath code
const p = discrete.linearProgram({ maximize: [1, 1], constraints: [[[1, 2], "<=", 4], [[3, 1], "<=", 6]] }); const d = discrete.linearProgram({ minimize: [4, 6], constraints: [[[1, 3], ">=", 1], [[2, 1], ">=", 1]] }); [p.value, p.x, d.value, d.x]Output[14/5, [8/5, 6/5], 14/5, [2/5, 1/5]]