from cvxpy import *
x = Variable()
y = Variable()
obj = Minimize(0)
constr = [2*x + y == 4,
-x + 5*y == 0]
Problem(obj, constr).solve()
print x.value, y.value
1.81818181818 0.363636363636
of an army via various foods (with different nutritional benefits and prices) under cost constraints
with x giving units of hamburgers and cheerios
from cvxpy import *
import numpy as np
h = np.array([.8, .4, .5])
c = np.array([0, .3, 2.0])
r = np.array([1, 2.1, 1.7])
p = np.array([1, .25])
x = Variable(2) # amounts of hamburgers and cheerios
obj = Minimize(0)
constr = [x.T*p <= 130,
h*x[0] + c*x[1] >= 50*r,
x >= 0]
prob = Problem(obj, constr)
# use the SCS solver because the default, ECOS, has a bug with 0 objectives
prob.solve(solver='SCS')
print x.value
[[ 62.82945211] [ 266.57466712]]
x = Variable(2) # amounts of hamburgers and cheerios
obj = Minimize(x.T*p)
constr = [h*x[0] + c*x[1] >= 50*r,
x >= 0]
prob = Problem(obj, constr)
prob.solve(solver='SCS')
print prob.value
print x.value
129.139602104 [[ 62.44796859] [ 266.76653406]]