Examples¶
A Trivial GP¶
from gpkit import Variable, GP
# Decision variable
x = Variable('x')
# Constraint
constraints = [x >= 1]
# Objective (to minimize)
objective = x
# Formulate the GP
gp = GP(objective, constraints)
# Solve the GP
sol = gp.solve()
# Print results table
print sol.table()
Maximizing the Volume of a Box¶
from gpkit import Variable, GP
# Parameters
alpha = Variable("alpha", 2, "-", "lower limit, wall aspect ratio")
beta = Variable("beta", 10, "-", "upper limit, wall aspect ratio")
gamma = Variable("gamma", 2, "-", "lower limit, floor aspect ratio")
delta = Variable("delta", 10, "-", "upper limit, floor aspect ratio")
A_wall = Variable("A_{wall}", 200, "m^2", "upper limit, wall area")
A_floor = Variable("A_{floor}", 50, "m^2", "upper limit, floor area")
# Decision variables
h = Variable("h", "m", "height")
w = Variable("w", "m", "width")
d = Variable("d", "m", "depth")
#Constraints
constraints = [A_wall >= 2*h*w + 2*h*d,
A_floor >= w*d,
h/w >= alpha,
h/w <= beta,
d/w >= gamma,
d/w <= delta]
#Objective function
V = h*w*d
objective = 1/V #To maximize V, we minimize its reciprocal
# Formulate the GP
gp = GP(objective, constraints)
# Solve the GP
sol = gp.solve()
# Print results table
print sol.table()
Water Tank¶
from gpkit import Variable, VectorVariable, GP
M = Variable("M", 100, "kg", "Mass of Water in the Tank")
rho = Variable("\\rho", 1000, "kg/m^3", "Density of Water in the Tank")
A = Variable("A", "m^2", "Surface Area of the Tank")
V = Variable("V", "m^3", "Volume of the Tank")
d = VectorVariable(3, "d", "m", "Dimension Vector")
constraints = (A >= 2*(d[0]*d[1] + d[0]*d[2] + d[1]*d[2]),
V == d[0]*d[1]*d[2],
M == V*rho
)
gp = GP(A, constraints)
sol = gp.solve(printing=False)
print sol(A)
print sol(V)
print sol(d)