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. 1.A linear programAgrees
    Maximise 3x+5y3x + 5y subject to x≤4x \le 4, 2y≤122y \le 12, 3x+2y≤183x + 2y \le 18, x,y≥0x, y \ge 0.
    Independent answer
    The corners are (0,0),(4,0),(4,3),(2,6),(0,6)(0,0), (4,0), (4,3), (2,6), (0,6) with values 0,12,27,36,300, 12, 27, 36, 30: the maximum is 36 at (2,6)(2, 6).
    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. 2.Shadow pricesAgrees
    For the same program, find the dual solution (shadow prices) and check that the dual value equals 36.
    Independent answer
    At (2,6)(2,6) the second and third constraints are tight. Solving y1⋅1+3y3=3y_1 \cdot 1 + 3y_3 = 3, 2y2+2y3=52y_2 + 2y_3 = 5 with y1=0y_1 = 0 gives y=(0,32,1)y = (0, \tfrac32, 1); 4⋅0+12⋅32+18⋅1=364 \cdot 0 + 12 \cdot \tfrac32 + 18 \cdot 1 = 36.
    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. 3.MinimisingAgrees
    Minimise 3x+2y3x + 2y subject to x+y≥4x + y \ge 4, x+3y≥6x + 3y \ge 6, x,y≥0x, y \ge 0.
    Independent answer
    The corners are (6,0),(3,1),(0,4)(6,0), (3,1), (0,4) with values 18,11,818, 11, 8: the minimum is 8 at (0,4)(0, 4).
    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. 4.InfeasibilityAgrees
    Show that x+y≤1x + y \le 1, x+y≥2x + y \ge 2, x,y≥0x, y \ge 0 has no solution.
    Independent answer
    Adding the first inequality to the negative of the second gives 0≤−10 \le -1.
    RatMath code
    discrete.linearProgram({ maximize: [1, 1], constraints: [[[1, 1], "<=", 1], [[1, 1], ">=", 2]] }).status
    Output
    infeasible
  5. 5.UnboundednessAgrees
    Show that maximising x+yx + y subject to x−y≤1x - y \le 1, x,y≥0x, y \ge 0 is unbounded.
    Independent answer
    Along (0,t)(0, t) every constraint holds and x+y=tx + y = t grows without bound.
    RatMath code
    discrete.linearProgram({ maximize: [1, 1], constraints: [[[1, -1], "<=", 1]] }).status
    Output
    unbounded
  6. 6.Integer programmingAgrees
    Maximise 5x+4y5x + 4y subject to 6x+4y≤246x + 4y \le 24, x+2y≤6x + 2y \le 6, x,y≥0x, y \ge 0 integers. Compare with the LP relaxation.
    Independent answer
    The relaxation peaks at (3,32)(3, \tfrac32) with value 21. Among integer points, (4,0)(4, 0) gives 20, (3,1)(3, 1) 19, (2,2)(2, 2) 18: the integer optimum is 20 at (4,0)(4, 0).
    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. 7.KnapsackAgrees
    Items weigh 2, 3, 4, 5 and are worth 3, 4, 5, 6. Which should go in a knapsack holding weight 5?
    Independent answer
    The pairs within weight 5: only {1,2}\{1, 2\} (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: true makes every variable 0 or 1.
    Fix: ratmath/discrete#7, transportation, binary option on integerProgram, coreVertices
  8. 8.Assignment problemAgrees
    Assign four workers to four jobs at least total cost, with costs (9278643758187694)\begin{pmatrix} 9&2&7&8\\6&4&3&7\\5&8&1&8\\7&6&9&4 \end{pmatrix}.
    Independent answer
    By the Hungarian method: worker 1 → job 2, 2 → 1, 3 → 3, 4 → 4, cost 2+6+1+4=132 + 6 + 1 + 4 = 13.
    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. 9.Transportation problemAgrees
    Two warehouses hold 20 and 30 units; three stores need 10, 25 and 15. Shipping costs per unit are (861091213)\begin{pmatrix} 8&6&10\\9&12&13 \end{pmatrix}. Find the cheapest plan's cost.
    Independent answer
    Warehouse 1 sends 20 to store 2; warehouse 2 sends 10, 5, 15. Cost 120+90+60+195=465120 + 90 + 60 + 195 = 465 (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]]]
    transportation solves the program exactly; its dual prices are the stepping-stone potentials that prove the plan optimal.
    Fix: ratmath/discrete#7, transportation, binary option on integerProgram, coreVertices
  10. 10.Optimising a rectangleAgrees
    A rectangle has perimeter 20. What is the largest area it can have?
    Independent answer
    With sides xx and 10−x10 - x, A=x(10−x)A = x(10 - x) is largest at x=5x = 5: 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. 11.One-variable minimumAgrees
    Find the minimum of f(x)=x4−3x3+2f(x) = x^4 - 3x^3 + 2.
    Independent answer
    f′(x)=x2(4x−9)f'(x) = x^2(4x - 9): critical points 0 (not an extremum) and 94\tfrac94. f(94)=−1675256≈−6.5430f(\tfrac94) = -\tfrac{1675}{256} \approx -6.5430.
    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. 12.Critical points in the planeAgrees
    Find and classify the critical point of f=x2+xy+y2−3xf = x^2 + xy + y^2 - 3x.
    Independent answer
    fx=2x+y−3=0f_x = 2x + y - 3 = 0, fy=x+2y=0f_y = x + 2y = 0: (2,−1)(2, -1). The Hessian (2112)\begin{pmatrix}2&1\\1&2\end{pmatrix} is positive definite, so it is a minimum, f=−3f = -3.
    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. 13.Second-derivative testAgrees
    Classify the critical points of f=x3−3x+y2f = x^3 - 3x + y^2.
    Independent answer
    fx=3x2−3f_x = 3x^2 - 3, fy=2yf_y = 2y: (±1,0)(\pm1, 0). The Hessian is diag⁡(6x,2)\operatorname{diag}(6x, 2): at (1,0)(1, 0) a minimum, at (−1,0)(-1, 0) 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. 14.Lagrange multipliersAgrees
    Find the point of the line x+2y=5x + 2y = 5 closest to the origin.
    Independent answer
    Minimise x2+y2x^2 + y^2: 2x=λ2x = \lambda, 2y=2λ2y = 2\lambda, so y=2xy = 2x and x=1x = 1, y=2y = 2; 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 candidates
    Output
    [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/min are only guaranteed on a closed bounded constraint set.
  15. 15.A constrained maximumAgrees
    Maximise xyzxyz on the plane x+y+z=3x + y + z = 3 with x,y,z>0x, y, z > 0.
    Independent answer
    By symmetry (or AM–GM, xyz3≤1\sqrt[3]{xyz} \le 1), the maximum is 1 at (1,1,1)(1, 1, 1).
    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 candidates
    Output
    [1, 1, 1, 1, 1]
  16. 16.ConvexityAgrees
    Is f=2x2+2xy+3y2f = 2x^2 + 2xy + 3y^2 convex? Give the Hessian's eigenvalues.
    Independent answer
    The Hessian (4226)\begin{pmatrix}4&2\\2&6\end{pmatrix} has eigenvalues 5±5≈2.7639,7.23615 \pm \sqrt5 \approx 2.7639, 7.2361, 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. 17.Least squaresAgrees
    Fit a line y=a+bxy = a + bx to (0,1),(1,3),(2,4),(3,4)(0, 1), (1, 3), (2, 4), (3, 4) by least squares.
    Independent answer
    xˉ=1.5\bar x = 1.5, yˉ=3\bar y = 3, Sxy=5S_{xy} = 5, Sxx=5S_{xx} = 5: b=1b = 1, a=1.5a = 1.5.
    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. 18.Gradient descentAgrees
    Take one gradient-descent step of size 0.1 from (1,1)(1, 1) on f=x2+4y2f = x^2 + 4y^2.
    Independent answer
    ∇f=(2x,8y)=(2,8)\nabla f = (2x, 8y) = (2, 8), so the step lands at (1,1)−0.1(2,8)=(0.8,0.2)(1, 1) - 0.1(2, 8) = (0.8, 0.2).
    RatMath code
    analytic.gradientDescent("x^2 + 4*y^2", ["1", "1"], { step: "1/10", steps: 1 }).final
    Output
    [4/5, 1/5]
    gradientDescent keeps 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. 19.Newton's method for a minimumAgrees
    Take two Newton steps for minimising f(x)=x2−ln⁡xf(x) = x^2 - \ln x from x0=1x_0 = 1. What is the exact minimiser?
    Independent answer
    f′=2x−1/xf' = 2x - 1/x, f′′=2+1/x2f'' = 2 + 1/x^2. x1=1−13=23x_1 = 1 - \tfrac{1}{3} = \tfrac23, x2=23−4/3−3/22+9/4=23+251=1217≈0.70588x_2 = \tfrac23 - \frac{4/3 - 3/2}{2 + 9/4} = \tfrac23 + \tfrac{2}{51} = \tfrac{12}{17} \approx 0.70588. The minimiser is 1/2≈0.7071071/\sqrt2 \approx 0.707107.
    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 (x2=0.70588235…=12/17x_2 = 0.70588235\ldots = 12/17); the root is a proven enclosure.
  20. 20.DualityAgrees
    Write the dual of: maximise x+yx + y subject to x+2y≤4x + 2y \le 4, 3x+y≤63x + y \le 6, x,y≥0x, y \ge 0; show both optimal values agree.
    Independent answer
    Dual: minimise 4u+6v4u + 6v with u+3v≥1u + 3v \ge 1, 2u+v≥12u + v \ge 1, u,v≥0u, v \ge 0. Primal optimum at (85,65)(\tfrac85, \tfrac65) is 145\tfrac{14}{5}; dual optimum at (25,15)(\tfrac25, \tfrac15) is 85+65=145\tfrac{8}{5} + \tfrac65 = \tfrac{14}{5}.
    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]]