gpkit.constraints package¶
Submodules¶
gpkit.constraints.array module¶
Implements ArrayConstraint
-
class
gpkit.constraints.array.
ArrayConstraint
(constraints, left, oper, right)¶ Bases:
gpkit.constraints.single_equation.SingleEquationConstraint
,gpkit.constraints.set.ConstraintSet
A ConstraintSet for prettier array-constraint printing.
ArrayConstraint gets its sub method from ConstrainSet, and so left and right are only used for printing.
When created by NomialArray left and right are likely to be be either NomialArrays or Varkeys of VectorVariables.
-
subinplace
(subs)¶ Substitutes in place.
-
gpkit.constraints.bounded module¶
Implements BoundedConstraintSet
-
class
gpkit.constraints.bounded.
BoundedConstraintSet
(constraints, substitutions=None, verbosity=1, eps=1e-30, lower=None, upper=None)¶ Bases:
gpkit.constraints.set.ConstraintSet
Bounds contained variables so as to ensure dual feasibility.
- constraints : iterable
- constraints whose varkeys will be bounded
- substitutions : dict
- as in ConstraintSet.__init__
- verbosity : int
- how detailed of a warning to print
- 0: nothing 1: print warnings
- eps : float
- default lower bound is eps, upper bound is 1/eps
- lower : float
- lower bound for all varkeys, replaces eps
- upper : float
- upper bound for all varkeys, replaces 1/eps
-
process_result
(result)¶ Creates (and potentially prints) a dictionary of unbounded variables.
-
sens_from_dual
(las, nus)¶ Return sensitivities while capturing the relevant lambdas
-
gpkit.constraints.bounded.
varkey_bounds
(varkeys, lower, upper)¶ Returns constraints list bounding all varkeys.
- varkeys : iterable
- list of varkeys to create bounds for
- lower : float
- lower bound for all varkeys
- upper : float
- upper bound for all varkeys
gpkit.constraints.costed module¶
Implement CostedConstraintSet
-
class
gpkit.constraints.costed.
CostedConstraintSet
(cost, constraints, substitutions=None)¶ Bases:
gpkit.constraints.set.ConstraintSet
A ConstraintSet with a cost
cost: gpkit.Posynomial constraints: Iterable substitutions: dict
-
controlpanel
(*args, **kwargs)¶ Easy model control in IPython / Jupyter
Like interact(), but with the ability to control sliders and their ranges live. args and kwargs are passed on to interact()
-
interact
(ranges=None, fn_of_sol=None, **solvekwargs)¶ Easy model interaction in IPython / Jupyter
By default, this creates a model with sliders for every constant which prints a new solution table whenever the sliders are changed.
- fn_of_sol : function
- The function called with the solution after each solve that displays the result. By default prints a table.
- ranges : dictionary {str: Slider object or tuple}
- Determines which sliders get created. Tuple values may contain two or three floats: two correspond to (min, max), while three correspond to (min, step, max)
- **solvekwargs
- kwargs which get passed to the solve()/localsolve() method.
-
reset_varkeys
(init_dict=None)¶ Resets varkeys to what is in the cost and constraints
-
rootconstr_latex
(excluded=None)¶ The appearance of a ConstraintSet in addition to its contents
-
rootconstr_str
(excluded=None)¶ The appearance of a ConstraintSet in addition to its contents
-
subinplace
(subs)¶ Substitutes in place.
-
gpkit.constraints.linked module¶
Implements LinkedConstraintSet
-
class
gpkit.constraints.linked.
LinkedConstraintSet
(constraints, include_only=None, exclude=None)¶ Bases:
gpkit.constraints.set.ConstraintSet
A ConstraintSet that links duplicate variables in its constraints
VarKeys with the same .str_without([“models”]) are linked.
The new linking varkey will have the same attributes as the first linked varkey of that name, without any value, models, or modelnums.
If any of the constraints have a substitution for a linked varkey, the linking varkey will have that substitution as well; if more than one linked varkey has a substitution a ValueError will be raised.
- constraints: iterable
- valid argument to ConstraintSet
- include_only: set
- whitelist of variable names to include
- exclude: set
- blacklist of variable names, supercedes include_only
-
process_result
(result)¶
gpkit.constraints.model module¶
Implements Model
-
class
gpkit.constraints.model.
Model
(cost=None, constraints=None, substitutions=None, name=None)¶ Bases:
gpkit.constraints.costed.CostedConstraintSet
Symbolic representation of an optimization problem.
The Model class is used both directly to create models with constants and sweeps, and indirectly inherited to create custom model classes.
- cost : Posynomial (optional)
- Defaults to Monomial(1).
- constraints : ConstraintSet or list of constraints (optional)
- Defaults to an empty list.
- substitutions : dict (optional)
- This dictionary will be substituted into the problem before solving, and also allows the declaration of sweeps and linked sweeps.
- name : str (optional)
- Allows “naming” a model in a way similar to inherited instances, and overrides the inherited name if there is one.
program is set during a solve solution is set at the end of a solve
-
gp
(verbosity=1, substitutions=None, **kwargs)¶ Return program version of self
- program: NomialData
- Class to return, e.g. GeometricProgram or SignomialProgram
- return_attr: string
- attribute to return in addition to the program
-
link
(other, include_only=None, exclude=None)¶ Connects this model with a set of constraints
-
localsolve
(solver=None, verbosity=1, skipsweepfailures=False, *args, **kwargs)¶ Forms a mathematical program and attempts to solve it.
- solver : string or function (optional)
- If None, uses the default solver found in installation.
- verbosity : int (optional)
- If greater than 0 prints runtime messages. Is decremented by one and then passed to programs.
- skipsweepfailures : bool (optional)
- If True, when a solve errors during a sweep, skip it.
*args, **kwargs : Passed to solver
- sol : SolutionArray
- See the SolutionArray documentation for details.
ValueError if the program is invalid. RuntimeWarning if an error occurs in solving or parsing the solution.
-
name
= None¶
-
num
= None¶
-
program
= None¶
-
solution
= None¶
-
solve
(solver=None, verbosity=1, skipsweepfailures=False, *args, **kwargs)¶ Forms a mathematical program and attempts to solve it.
- solver : string or function (optional)
- If None, uses the default solver found in installation.
- verbosity : int (optional)
- If greater than 0 prints runtime messages. Is decremented by one and then passed to programs.
- skipsweepfailures : bool (optional)
- If True, when a solve errors during a sweep, skip it.
*args, **kwargs : Passed to solver
- sol : SolutionArray
- See the SolutionArray documentation for details.
ValueError if the program is invalid. RuntimeWarning if an error occurs in solving or parsing the solution.
-
sp
(verbosity=1, substitutions=None, **kwargs)¶ Return program version of self
- program: NomialData
- Class to return, e.g. GeometricProgram or SignomialProgram
- return_attr: string
- attribute to return in addition to the program
-
subconstr_latex
(excluded=None)¶ The collapsed appearance of a ConstraintBase
-
subconstr_str
(excluded=None)¶ The collapsed appearance of a ConstraintBase
-
zero_lower_unbounded_variables
()¶ Recursively substitutes 0 for variables that lack a lower bound
gpkit.constraints.prog_factories module¶
Scripts for generating, solving and sweeping programs
-
gpkit.constraints.prog_factories.
run_sweep
(genfunction, self, solution, skipsweepfailures, constants, sweep, linkedsweep, solver, verbosity, *args, **kwargs)¶ Runs through a sweep.
gpkit.constraints.set module¶
Implements ConstraintSet
-
class
gpkit.constraints.set.
ConstraintSet
(constraints, substitutions=None)¶ Bases:
list
Recursive container for ConstraintSets and Inequalities
-
as_gpconstr
(x0)¶ Returns GPConstraint approximating this constraint at x0
When x0 is none, may return a default guess.
-
as_posyslt1
(substitutions=None)¶ Returns list of posynomials which must be kept <= 1
-
flat
(constraintsets=True)¶ Yields contained constraints, optionally including constraintsets.
-
latex
(excluded=None)¶ LaTeX representation of a ConstraintSet.
-
process_result
(result)¶ Does arbitrary computation / manipulation of a program’s result
There’s no guarantee what order different constraints will process results in, so any changes made to the program’s result should be careful not to step on other constraint’s toes.
- check that an inequality was tight
- add values computed from solved variables
-
reset_varkeys
(init_dict=None)¶ Goes through constraints and collects their varkeys.
-
rootconstr_latex
(excluded=None)¶ The appearance of a ConstraintSet in addition to its contents
-
rootconstr_str
(excluded=None)¶ The appearance of a ConstraintSet in addition to its contents
-
sens_from_dual
(las, nus)¶ Computes constraint and variable sensitivities from dual solution
- las : list
- Sensitivity of each posynomial returned by self.as_posyslt1
- nus: list of lists
- Each posynomial’s monomial sensitivities
- constraint_sens : dict
- The interesting and computable sensitivities of this constraint
- var_senss : dict
- The variable sensitivities of this constraint
-
str_without
(excluded=None)¶ String representation of a ConstraintSet.
-
subconstr_latex
(excluded=None)¶ The collapsed appearance of a ConstraintSet
-
subconstr_str
(excluded=None)¶ The collapsed appearance of a ConstraintSet
-
subinplace
(subs)¶ Substitutes in place.
-
variables_byname
(key)¶ Get all variables with a given name
-
gpkit.constraints.signomial_program module¶
Implement the SignomialProgram class
-
class
gpkit.constraints.signomial_program.
SignomialProgram
(cost, constraints, substitutions=None, verbosity=1)¶ Bases:
gpkit.constraints.costed.CostedConstraintSet
Prepares a collection of signomials for a SP solve.
- cost : Posynomial
- Objective to minimize when solving
- constraints : list of Constraint or SignomialConstraint objects
- Constraints to maintain when solving (implicitly Signomials <= 1)
- verbosity : int (optional)
- Currently has no effect: SignomialPrograms don’t know anything new after being created, unlike GeometricPrograms.
gps is set during a solve result is set at the end of a solve
>>> gp = gpkit.geometric_program.SignomialProgram( # minimize x, [ # subject to 1/x - y/x, # <= 1, implicitly y/10 # <= 1 ]) >>> gp.solve()
-
gp
(x0=None, verbosity=1)¶ Get a GP approximation of this SP at x0
-
localsolve
(solver=None, verbosity=1, x0=None, rel_tol=0.0001, iteration_limit=50, **kwargs)¶ Locally solves a SignomialProgram and returns the solution.
- solver : str or function (optional)
- By default uses one of the solvers found during installation. If set to “mosek”, “mosek_cli”, or “cvxopt”, uses that solver. If set to a function, passes that function cs, A, p_idxs, and k.
- verbosity : int (optional)
- If greater than 0, prints solve time and number of iterations. Each GP is created and solved with verbosity one less than this, so if greater than 1, prints solver name and time for each GP.
- x0 : dict (optional)
- Initial location to approximate signomials about.
- rel_tol : float
- Iteration ends when this is greater than the distance between two consecutive solve’s objective values.
- iteration_limit : int
- Maximum GP iterations allowed.
- *args, **kwargs :
- Passed to solver function.
- result : dict
- A dictionary containing the translated solver result.
gpkit.constraints.single_equation module¶
Implements SingleEquationConstraint
-
class
gpkit.constraints.single_equation.
SingleEquationConstraint
(left, oper, right)¶ Bases:
object
Constraint expressible in a single equation.
-
func_opers
= {'<=': <built-in function le>, '=': <built-in function eq>, '>=': <built-in function ge>}¶
-
latex
(excluded=None)¶ Latex representation without attributes in excluded list
-
latex_opers
= {'<=': '\\leq', '=': '=', '>=': '\\geq'}¶
-
process_result
(result)¶ Process solver results
-
str_without
(excluded=None)¶ String representation without attributes in excluded list
-
sub
(subs)¶ Returns a substituted version of this constraint.
-
subconstr_latex
(excluded)¶ The collapsed latex of a constraint
-
subconstr_str
(excluded)¶ The collapsed string of a constraint
-
-
gpkit.constraints.single_equation.
trycall
(obj, attr, arg, default)¶ Try to call method of an object, returning default if it does not exist
gpkit.constraints.tight module¶
Implements TightConstraintSet
-
class
gpkit.constraints.tight.
TightConstraintSet
(constraints, substitutions=None, reltol=1e-06, raiseerror=False)¶ Bases:
gpkit.constraints.set.ConstraintSet
ConstraintSet whose inequalities must result in an equality.
-
process_result
(result)¶ Checks that all constraints are satisfied with equality
-
Module contents¶
Contains ConstraintSet and related classes and objects