Input:
Convex optimization
where $F_1,\dots,F_n$ and $G$ are positive semi-definite matrices, and $A\in\mathbb{R}^{p\times n}$.
A student wants to have a nice snack buying brownies and cheesecakes. He/She wants 1) to reduce total cost, and 2) to satisfy certain dietary requirements.
Chocolate | Sugar | Cheese | Cost | |
---|---|---|---|---|
Brownie | 3 | 2 | 2 | 50 |
Cheesecake | 0 | 4 | 5 | 80 |
------------ | ---------- | ------ | -------- | ------- |
Requirement | 6 | 10 | 8 |
Two situations:
What should be the optimal buying plan?
# Add required packages
Pkg.update()
Pkg.add("Optim")
Pkg.add("JuMP")
Pkg.add("Cbc") # for LP and MILP (mixed-integer linear program)
Pkg.add("Ipopt") # for QP and other NLPs
#Pkg.add("Clp")
#Pkg.add("SCS") # for conic programs
#Pkg.add("Gurobi") # for conic and integer programs (commercial license required)
# Pkg.add("CPLEX") # for conic and integer programs (commercial)
INFO: Updating METADATA... INFO: Computing changes... INFO: No packages to install, update or remove INFO: Nothing to be done INFO: Nothing to be done INFO: Nothing to be done INFO: Nothing to be done
# Fractional Shop
using JuMP
using Cbc
#using Gurobi # for LP and MILP
snack = Model(solver=CbcSolver())
#snack = Model(solver=GurobiSolver())
@variable(snack, b >= 0)
@variable(snack, c >= 0)
@objective(snack, Min, 50b + 80c)
@constraints(snack, begin
3b >= 6
2b + 4c >= 10
2b + 5c >= 8
end)
status = solve(snack)
b = getvalue(b)
c = getvalue(c)
cost = getobjectivevalue(snack)
println("Solver status = $(status)")
println("Brownie $(b), Cheesecake $(c): Total cost $(cost) cents")
Solver status = Optimal Brownie 2.0, Cheesecake 1.5: Total cost 220.0 cents
# Integer Shop
using JuMP
using Cbc
#using Gurobi # for LP and MILP
snack = Model(solver=CbcSolver())
#snack = Model(solver=GurobiSolver()) # this gives an exactly integral solution
@variable(snack, b >= 0, Int)
@variable(snack, c >= 0, Int)
@objective(snack, Min, 50b + 80c)
@constraints(snack, begin
3b >= 6
2b + 4c >= 10
2b + 5c >= 8
end)
status = solve(snack)
b = getvalue(b)
c = getvalue(c)
cost = getobjectivevalue(snack)
println("Solver status = $(status)")
println("Brownie $(b), Cheesecake $(c): Total cost $(cost) cents")
Solver status = Optimal Brownie 3.0, Cheesecake 0.9999999999999999: Total cost 230.0 cents