Glossary¶
For an alphabetical listing of all commands, check out the Index
The GPkit Package¶
Lightweight GP Modeling Package
For examples please see the examples folder.
Requirements¶
numpy MOSEK or CVXOPT scipy(optional): for complete sparse matrix support sympy(optional): for latex printing in iPython Notebook
Attributes¶
- settings : dict
- Contains settings loaded from
./env/settings
-
gpkit.
disable_signomials
()¶ Disables signomial support in a particular instance of GPkit.
-
gpkit.
disable_units
()¶ Disables units support in a particular instance of GPkit.
Posynomials created after calling this are incompatible with those created before.
If gpkit is imported multiple times, this needs to be run each time.
- The correct way to call this is:
- import gpkit gpkit.disable_units()
- The following will not have the intended effect:
- from gpkit import disable_units disable_units()
-
gpkit.
enable_signomials
()¶ Enables signomial support in a particular instance of GPkit.
-
gpkit.
enable_units
()¶ Enables units support in a particular instance of GPkit.
Posynomials created after calling this are incompatible with those created before.
If gpkit is imported multiple times, this needs to be run each time.
Subpackages¶
Submodules¶
gpkit.geometric_program¶
Module for creating GeometricProgram instances.
Example¶
>>> gp = gpkit.GeometricProgram(cost, constraints, substitutions)
-
class
gpkit.geometric_program.
GPSolutionArray
¶ Bases:
gpkit.small_classes.DictOfLists
DictofLists extended with posynomial substitution.
-
getvars
(*args)¶
-
senssubinto
(p)¶ Returns array of each solution’s sensitivity substituted into p
Returns only scalar values.
-
subinto
(p)¶ Returns PosyArray of each solution substituted into p.
-
table
(tables=['cost', 'freevariables', 'sweptvariables', 'constants', 'sensitivities'], fixedcols=True)¶
-
-
class
gpkit.geometric_program.
GeometricProgram
(*args, **kwargs)¶ Bases:
gpkit.model.Model
Holds a model and cost function for passing to solvers.
- cost : Constraint
- Posynomial to minimize when solving
- constraints : list of (lists of) Constraints
- Constraints to maintain when solving (MonoEQConstraints will be turned into <= and >= constraints)
- substitutions : dict {varname: float or int} (optional)
- Substitutions to be applied before solving (including sweeps)
- solver : str (optional)
- Name of solver to use
- options : dict (optional)
- Options to pass to solver
>>> gp = gpkit.GeometricProgram( # minimize 0.5*rho*S*C_D*V**2, [ # subject to Re <= (rho/mu)*V*(S/A)**0.5, C_f >= 0.074/Re**0.2, W <= 0.5*rho*S*C_L*V**2, W <= 0.5*rho*S*C_Lmax*V_min**2, W >= W_0 + W_w, W_w >= W_w_surf + W_w_strc, C_D >= C_D_fuse + C_D_wpar + C_D_ind ], substitutions) >>> gp.solve()
-
model_nums
= defaultdict(<type 'int'>, {})¶
-
solve
(*args, **kwargs)¶
-
test
(solver=None, printing=False, skipfailures=False)¶
-
vars
(*args)¶
gpkit.model¶
Module for creating models.
Currently these are only used for GP instances, but they may be further abstractable.
-
class
gpkit.model.
Model
¶ Bases:
object
Abstract class with substituion, loading / saving, and p_idx/A generation
-
add_constraints
(constraints)¶
-
checkbounds
()¶
-
genA
(allpos=True, printing=True)¶
-
load
(posytuple)¶
-
rm_constraints
(constraints)¶
-
sub
(substitutions, val=None, frombase='last', replace=True)¶ Substitutes into a model, modifying it in place.
gp.sub({‘x’: 1, y: 2}) gp.sub(x, 3, replace=True)
- substitutions : dict or key
- Either a dictionary whose keys are strings, Variables, or VarKeys, and whose values are numbers, or a string, Variable or Varkey.
- val : number (optional)
- If the substitutions entry is a single key, val holds the value
- frombase : string (optional)
- Which model state to update. By default updates the current state.
- replace : boolean (optional, default is True)
- Whether the substitution should only subtitute currently unsubstituted values (False) or should also make replacements of current substitutions (True).
No return value: the model is modified in place.
-
gpkit.nomials¶
Signomial, Posynomial, Monomial, Constraint, & MonoEQCOnstraint classes
-
class
gpkit.nomials.
Constraint
(p1, p2)¶ Bases:
gpkit.nomials.Posynomial
A constraint of the general form monomial > posynomial Stored internally (exps, cs) as a single Posynomial (1 >= self) Usually initialized via operator overloading, e.g. cc = y**2 >= 1 + x Additionally stores input format (lhs vs rhs) in self.right and self.left Form is self.left >= self.right.
TODO: this documentation needs to address Signomial Constraints.
-
class
gpkit.nomials.
MonoEQConstraint
(p1, p2)¶ Bases:
gpkit.nomials.Constraint
A Constraint of the form Monomial == Monomial. Stored internally as a Monomial Constraint, (1 == self).
-
class
gpkit.nomials.
Monomial
(exps=None, cs=1, varlocsandkeys=None, require_positive=True, **descr)¶ Bases:
gpkit.nomials.Posynomial
A Posynomial with only one term
Same as Signomial. Note: Monomial historically supported several different init formats
These will be deprecated in the future, replaced with a single __init__ syntax, same as Signomial.
-
class
gpkit.nomials.
Posynomial
(exps=None, cs=1, varlocsandkeys=None, require_positive=True, **descr)¶ Bases:
gpkit.nomials.Signomial
A Signomial with strictly positive cs
Same as Signomial. Note: Posynomial historically supported several different init formats
These will be deprecated in the future, replaced with a single __init__ syntax, same as Signomial.
-
class
gpkit.nomials.
Signomial
(exps=None, cs=1, varlocsandkeys=None, require_positive=True, **descr)¶ Bases:
object
A representation of a signomial.
- exps: tuple of dicts
- Exponent dicts for each monomial term
- cs: tuple
- Coefficient values for each monomial term
- varlocsandkeys: dict
- mapping from variable name to list of indices of monomial terms that variable appears in
- require_positive: bool
- If True and signomials not enabled, c <= 0 will raise ValueError
Signomial Posynomial (if the input has only positive cs) Monomial (if the input has one term and only positive cs)
-
descr
(descr)¶
-
diff
(wrt)¶ Derivative of this with respect to a Variable
- wrt (Variable):
- Variable to take derivative with respect to
Signomial (or Posynomial or Monomial)
-
mono_approximation
(x0)¶
-
prod
()¶
-
sub
(substitutions, val=None, require_positive=True)¶ Returns a nomial with substitued values.
3 == (x**2 + y).sub({‘x’: 1, y: 2}) 3 == (x).gp.sub(x, 3)
- substitutions : dict or key
- Either a dictionary whose keys are strings, Variables, or VarKeys, and whose values are numbers, or a string, Variable or Varkey.
- val : number (optional)
- If the substitutions entry is a single key, val holds the value
- require_positive : boolean (optional, default is True)
- Controls whether the returned value can be a signomial.
Returns substituted nomial.
-
subcmag
(substitutions, val=None)¶
-
sum
()¶
-
to
(arg)¶
-
value
¶ Self, with values substituted for variables that have values
float, if no symbolic variables remain after substitution (Monomial, Posynomial, or Signomial), otherwise.
gpkit.posyarray¶
Module for creating PosyArray instances.
Example¶
>>> x = gpkit.Monomial('x')
>>> px = gpkit.PosyArray([1, x, x**2])
-
class
gpkit.posyarray.
PosyArray
¶ Bases:
numpy.ndarray
A Numpy array with elementwise inequalities and substitutions.
input_array : array-like
>>> px = gpkit.PosyArray([1, x, x**2])
-
c
¶
-
left
¶ Self, sampled one index down, with zeropad
-
outer
(other)¶ Returns the array and argument’s outer product.
-
right
¶ Self, sampled one index up, with zeropad
-
sub
(subs, val=None, require_positive=True)¶ Substitutes into the array
-
gpkit.small_classes¶
Miscellaneous small classes
-
class
gpkit.small_classes.
CootMatrix
¶ Bases:
gpkit.small_classes.CootMatrix
A very simple sparse matrix representation.
-
append
(row, col, data)¶
-
shape
= (None, None)¶
-
tocoo
()¶ Converts to a Scipy sparse coo_matrix
-
tocsc
()¶
-
tocsr
()¶
-
todense
()¶
-
todia
()¶
-
todok
()¶
-
update_shape
()¶
-
-
gpkit.small_classes.
CootMatrixTuple
¶ alias of
CootMatrix
-
class
gpkit.small_classes.
DictOfLists
¶ Bases:
dict
A hierarchy of dicionaries, with lists at the bottom.
-
append
(sol)¶ Appends a dict (of dicts) of lists to all held lists.
-
atindex
(i)¶ Indexes into each list independently.
-
toarray
(shape=None)¶ Converts all lists into arrays.
-
-
class
gpkit.small_classes.
HashVector
(*args, **kwargs)¶ Bases:
dict
A simple, sparse, string-indexed vector. Inherits from dict.
The HashVector class supports element-wise arithmetic: any undeclared variables are assumed to have a value of zero.
arg : iterable
>>> x = gpkit.nomials.Monomial('x') >>> exp = gpkit.small_classes.HashVector({x: 2})
-
class
gpkit.small_classes.
PosyTuple
(exps, cs, varlocs, substitutions)¶ Bases:
tuple
-
cs
¶ Alias for field number 1
-
exps
¶ Alias for field number 0
-
substitutions
¶ Alias for field number 3
-
varlocs
¶ Alias for field number 2
-
-
gpkit.small_classes.
append_dict
(i, o)¶ Recursviely travels dict o and appends items found in i.
-
class
gpkit.small_classes.
count
¶ Bases:
object
-
gpkit.small_classes.
enlist_dict
(i, o)¶ Recursviely copies dict i into o, placing non-dict items into lists.
-
gpkit.small_classes.
enray_dict
(i, o)¶ Recursively turns lists into numpy arrays.
-
gpkit.small_classes.
index_dict
(idx, i, o)¶ Recursviely travels dict i, placing items at idx into dict o.
gpkit.small_scripts¶
-
gpkit.small_scripts.
diff
(p, vk)¶
-
gpkit.small_scripts.
flatten
(ible, classes)¶ Flatten an iterable that contains other iterables
- l : Iterable
- Top-level container
- out : list
- List of all objects found in the nested iterables
- TypeError
- If an object is found whose class was not in classes
-
gpkit.small_scripts.
invalid_types_for_oper
(oper, a, b)¶ Raises TypeError for unsupported operations.
-
gpkit.small_scripts.
is_sweepvar
(sub)¶ Determines if a given substitution indicates a sweep.
-
gpkit.small_scripts.
isequal
(a, b)¶
-
gpkit.small_scripts.
latex_num
(c)¶
-
gpkit.small_scripts.
locate_vars
(exps)¶ From exponents form a dictionary of which monomials each variable is in.
-
gpkit.small_scripts.
mag
(c)¶ Return magnitude of a Number or Quantity
-
gpkit.small_scripts.
mono_approx
(p, x0)¶
-
gpkit.small_scripts.
results_table
(data, title, minval=0, printunits=True, fixedcols=True, varfmt='%s : ', valfmt='%-.4g ', vecfmt='%-8.3g')¶ Pretty string representation of a dict of VarKeys Iterable values are handled specially (partial printing)
- data: dict whose keys are VarKey’s
- data to represent in table
title: string minval: float
skip values with all(abs(value)) < minvalprintunits: bool fixedcols: bool
if True, print rhs (val, units, label) in fixed-width cols- varfmt: string
- format for variable names
- valfmt: string
- format for scalar values
- vecfmt: string
- format for vector values
-
gpkit.small_scripts.
sort_and_simplify
(exps, cs)¶ Reduces the number of monomials, and casts them to a sorted form.
-
gpkit.small_scripts.
unitstr
(units, into='%s', options='~', dimless='-')¶
gpkit.substitution¶
Module containing the substitution function
-
gpkit.substitution.
getsubs
(varkeys, varlocs, substitutions)¶
-
gpkit.substitution.
substitution
(varlocs, varkeys, exps, cs, substitutions, val=None)¶ Efficient substituton into a list of monomials.
- varlocs : dict
- Dictionary mapping variables to lists of monomial indices.
- exps : Iterable of dicts
- Dictionary mapping variables to exponents, for each monomial.
- cs : list
- Coefficient for each monomial.
- substitutions : dict
- Substitutions to apply to the above.
- val : number (optional)
- Used to substitute singlet variables.
-
gpkit.substitution.
vectorsub
(subs, var, sub, varset)¶ Vectorized substitution