Examples

iPython Notebook Examples

More examples, including some in-depth and experimental models, can be seen on nbviewer.

A Trivial GP

The most trivial GP we can think of: minimize \(x\) subject to the constraint \(x \ge 1\).

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(printing=False)

# print selected results
print "Optimal cost:  %s" % sol['cost']
print "Optimal x val: %s" % sol(x)

Of course, the optimal value is 1. Output:

Optimal cost:  1.0
Optimal x val: 1.0

Maximizing the Volume of a Box

This example comes from Section 2.4 of the GP tutorial, by S. Boyd et. al.

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(printing=False)

# Print results table
print sol.table()

The output is

Cost
----
 0.003674 [1/m**3] 

Free variables
--------------
d : 8.17   [m] depth 
h : 8.163  [m] height
w : 4.081  [m] width 

Swept variables
---------------

Constants
---------
A_{floor} : 50   [m**2] upper limit, floor area        
 A_{wall} : 200  [m**2] upper limit, wall area         
    alpha : 2           lower limit, wall aspect ratio 
     beta : 10          upper limit, wall aspect ratio 
    delta : 10          upper limit, floor aspect ratio
    gamma : 2           lower limit, floor aspect ratio

Constant and swept variable sensitivities
-----------------------------------------
A_{wall} : -1.5 upper limit, wall area        
   alpha : 0.5  lower limit, wall aspect ratio

Water Tank

Say we had a fixed mass of water we wanted to contain within a tank, but also wanted to minimize the cost of the material we had to purchase (i.e. the surface area of the 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.table()

The output is

Cost
----
 1.293 [m**2] 

Free variables
--------------
A : 1.293                             [m**2] Surface Area of the Tank
V : 0.1                               [m**3] Volume of the Tank      
d : [ 0.464     0.464     0.464    ]  [m]    Dimension Vector        

Swept variables
---------------

Constants
---------
   M : 100   [kg]      Mass of Water in the Tank   
\rho : 1000  [kg/m**3] Density of Water in the Tank

Constant and swept variable sensitivities
-----------------------------------------
   M : 0.6667  Mass of Water in the Tank   
\rho : -0.6667 Density of Water in the Tank

Simple Wing

This example comes from Section 3 of Geometric Programming for Aircraft Design Optimization, by W. Hoburg and P. Abbeel.

from gpkit.shortcuts import *
import numpy as np

# Define the constants in the problem
CDA0 = Var('(CDA_0)', 0.0306, 'm^2', label='Fuselage drag area')
C_Lmax = Var('C_{L,max}', 2.0, label='Maximum C_L, flaps down')
e = Var('e', 0.96, label='Oswald efficiency factor')
k = Var('k', 1.2, label='Form factor')
mu = Var('\\mu', 1.78E-5, 'kg/m/s', label='Viscosity of air')
N_lift = Var('N_{lift}', 2.5, label='Ultimate load factor')
rho = Var('\\rho', 1.23, 'kg/m^3', label='Density of air')
Sw_S = Var('\\frac{S_{wet}}{S}', 2.05, label='Wetted area ratio')
tau = Var('\\tau', 0.12, label='Airfoil thickness-to-chord ratio')
V_min = Var('V_{min}', 22, 'm/s', label='Desired landing speed')
W_0 = Var('W_0', 4940, 'N', label='Aircraft weight excluding wing')
cww1 = Var('cww1', 45.42, 'N/m^2', 'Wing weight area factor')
cww2 = Var('cww2', 8.71E-5, '1/m', 'Wing weight bending factor')

# Define decision variables
A = Var('A', label='Aspect ratio')
C_D = Var('C_D', label='Drag coefficient')
C_L = Var('C_L', label='Lift coefficient')
C_f = Var('C_f', label='Skin friction coefficient')
S = Var('S', 'm^2', label='Wing planform area')
Re = Var('Re', label='Reynolds number')
W = Var('Re', 'N', label='Total aircraft weight')
W_w = Var('W_w', 'N', label='Wing weight')
V = Var('V', 'm/s', label='Cruise velocity')

# Define objective function
objective = 0.5 * rho * V**2 * C_D * S

# Define constraints
constraints = [C_f * Re**0.2 >= 0.074,
               C_D >= CDA0 / S + k * C_f * Sw_S + C_L**2 / (np.pi * A * e),
               0.5 * rho * V**2 * C_L * S >= W,
               W >= W_0 + W_w,
               W_w >= (cww1*S +
                       cww2 * N_lift * A**1.5 * (W_0 * W * S)**0.5 / tau),
               0.5 * rho * V_min**2 * C_Lmax * S >= W,
               Re == rho * V * (S/A)**0.5 / mu]

# Formulate the GP
gp = GP(objective, constraints)

# Solve the GP
sol = gp.solve()

# Print the solution table
print sol.table()

The output is

Using solver 'cvxopt'
Solving for 9 variables.
Solving took 0.0629 seconds.

Cost
----
 255 [kg*m/s**2] 

Free variables
--------------
  A : 12.7              Aspect ratio             
C_D : 0.0231            Drag coefficient         
C_L : 0.6512            Lift coefficient         
C_f : 0.003857          Skin friction coefficient
 Re : 7189       [N]    Total aircraft weight    
 Re : 2.598e+06         Reynolds number          
  S : 12.08      [m**2] Wing planform area       
  V : 38.55      [m/s]  Cruise velocity          
W_w : 2249       [N]    Wing weight              

Swept variables
---------------

Constants
---------
          (CDA_0) : 0.0306    [m**2]    Fuselage drag area              
        C_{L,max} : 2                   Maximum C_L, flaps down         
         N_{lift} : 2.5                 Ultimate load factor            
          V_{min} : 22        [m/s]     Desired landing speed           
              W_0 : 4940      [N]       Aircraft weight excluding wing  
\frac{S_{wet}}{S} : 2.05                Wetted area ratio               
              \mu : 1.78e-05  [kg/m/s]  Viscosity of air                
             \rho : 1.23      [kg/m**3] Density of air                  
             \tau : 0.12                Airfoil thickness-to-chord ratio
             cww1 : 45.42     [N/m**2]  Wing weight area factor         
             cww2 : 8.71e-05  [1/m]     Wing weight bending factor      
                e : 0.96                Oswald efficiency factor        
                k : 1.2                 Form factor                     

Constant and swept variable sensitivities
-----------------------------------------
          (CDA_0) : 0.1097  Fuselage drag area              
        C_{L,max} : -0.1307 Maximum C_L, flaps down         
         N_{lift} : 0.2923  Ultimate load factor            
          V_{min} : -0.2614 Desired landing speed           
              W_0 : 0.9953  Aircraft weight excluding wing  
\frac{S_{wet}}{S} : 0.4108  Wetted area ratio               
              \mu : 0.08217 Viscosity of air                
             \rho : -0.1718 Density of air                  
             \tau : -0.2923 Airfoil thickness-to-chord ratio
             cww1 : 0.09428 Wing weight area factor         
             cww2 : 0.2923  Wing weight bending factor      
                e : -0.4795 Oswald efficiency factor        
                k : 0.4108  Form factor