Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add all code in the documentation to unit tests (#1509)
- Loading branch information
Showing
25 changed files
with
347 additions
and
244 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"Example code for solution saving and differencing." | ||
import pickle | ||
from gpkit import Model, Variable | ||
|
||
# build model (dummy) | ||
# decision variable | ||
x = Variable("x") | ||
y = Variable("y") | ||
|
||
# objective and constraints | ||
objective = 0.23 + x/y # minimize x and y | ||
constraints = [x + y <= 5, x >= 1, y >= 2] | ||
|
||
# create model | ||
m = Model(objective, constraints) | ||
|
||
# solve the model | ||
sol = m.solve() | ||
|
||
# save the current state of the model | ||
sol.save("last_verified.sol") | ||
|
||
# uncomment the line below to verify a new model | ||
last_verified_sol = pickle.load(open("last_verified.sol", mode="rb")) | ||
if not sol.almost_equal(last_verified_sol, reltol=1e-3): | ||
print(last_verified_sol.diff(sol)) | ||
|
||
# Note you can replace the last three lines above with | ||
# print(sol.diff("last_verified.sol")) | ||
# if you don't mind doing the diff in that direction. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
"Example pre-solve evaluated fixed variable" | ||
from gpkit import Variable, Model | ||
|
||
# code from t_GPSubs.test_calcconst in tests/t_sub.py | ||
x = Variable("x", "hours") | ||
t_day = Variable("t_{day}", 12, "hours") | ||
t_night = Variable("t_{night}", lambda c: 24 - c[t_day], "hours") | ||
|
||
# note that t_night has a function as its value | ||
m = Model(x, [x >= t_day, x >= t_night]) | ||
sol = m.solve(verbosity=0) | ||
assert sol["variables"][t_night] == 12 | ||
|
||
# call substitutions | ||
m.substitutions.update({t_day: ("sweep", [8, 12, 16])}) | ||
sol = m.solve(verbosity=0) | ||
assert (sol["variables"][t_night] == [16, 12, 8]).all() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"Example post-solve evaluated variable" | ||
from gpkit import Variable, Model | ||
|
||
# code from t_constraints.test_evalfn in tests/t_sub.py | ||
x = Variable("x") | ||
x2 = Variable("x^2", evalfn=lambda v: v[x]**2) | ||
m = Model(x, [x >= 2]) | ||
m.unique_varkeys = set([x2.key]) | ||
sol = m.solve(verbosity=0) | ||
assert abs(sol(x2) - 4) <= 1e-4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"Example of freeing fixed variables" | ||
from gpkit import Variable, Model | ||
x = Variable("x") | ||
y = Variable("y", 3) # fix value to 3 | ||
m = Model(x, [x >= 1 + y, y >= 1]) | ||
sol = m.solve() # optimal cost is 4; y appears in sol["constants"] | ||
|
||
assert abs(sol["cost"] - 4) <= 1e-4 | ||
assert y in sol["constants"] | ||
|
||
del m.substitutions["y"] | ||
sol = m.solve() # optimal cost is 2; y appears in Free Variables | ||
assert abs(sol["cost"] - 2) <= 1e-4 | ||
assert y in sol["freevariables"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
"The getting started page" | ||
from gpkit import Variable, VectorVariable, Model | ||
from gpkit.nomials import Monomial, Posynomial, PosynomialInequality | ||
|
||
### Example Free Variables | ||
# Declare a variable, x | ||
x = Variable("x") | ||
|
||
# Declare a variable, y, with units of meters | ||
y = Variable("y", "m") | ||
|
||
# Declare a variable, z, with units of meters, and a description | ||
z = Variable("z", "m", "A variable called z with units of meters") | ||
|
||
### Example Fixed Variables 1 | ||
rho = Variable("rho", 1.225, "kg/m^3", "Density of air at sea level") | ||
|
||
### Example Fixed Variables 2 | ||
#Declare pi equal to 3.14 | ||
pi = Variable("pi", 3.14159, "-", constant=True) | ||
|
||
### Example Vector Variables | ||
# Declare a 3-element vector variable "x" with units of "m" | ||
x = VectorVariable(3, "x", "m", "Cube corner coordinates") | ||
x_min = VectorVariable(3, "x", [1, 2, 3], "m", "Cube corner minimum") | ||
|
||
### Example Creating Monomials and Posynomials 1 | ||
# create a Monomial term xy^2/z | ||
x = Variable("x") | ||
y = Variable("y") | ||
z = Variable("z") | ||
m = x * y**2 / z | ||
assert isinstance(m, Monomial) | ||
|
||
### Example Creating Monomials and Posynomials 2 | ||
# create a Posynomial expression x + xy^2 | ||
x = Variable("x") | ||
y = Variable("y") | ||
p = x + x * y**2 | ||
assert isinstance(p, Posynomial) | ||
|
||
### Example Declaring Constraints | ||
# consider a block with dimensions x, y, z less than 1 | ||
# constrain surface area less than 1.0 m^2 | ||
x = Variable("x", "m") | ||
y = Variable("y", "m") | ||
z = Variable("z", "m") | ||
S = Variable("S", 1.0, "m^2") | ||
c = (2*x*y + 2*x*z + 2*y*z <= S) | ||
assert isinstance(c, PosynomialInequality) | ||
|
||
### Example Formulating a Model | ||
x = Variable("x") | ||
y = Variable("y") | ||
z = Variable("z") | ||
S = 200 | ||
objective = 1/(x*y*z) | ||
constraints = [2*x*y + 2*x*z + 2*y*z <= S, | ||
x >= 2*y] | ||
m = Model(objective, constraints) | ||
|
||
### Example Solving the Model | ||
sol = m.solve(verbosity=0) | ||
|
||
### Printing Results 1 | ||
print(sol.table()) | ||
|
||
### Printing Results 2 | ||
print("The optimal value is %s." % sol["cost"]) | ||
|
||
### Example variable sensitivity usage | ||
x = Variable("x") | ||
x_min = Variable("x_{min}", 2) | ||
sol = Model(x, [x_min <= x]).solve() | ||
sens_x_min = sol["sensitivities"]["variables"][x_min] | ||
|
||
x = Variable("x") | ||
x_squared_min = Variable("x^2_{min}", 2) | ||
sol = Model(x, [x_squared_min <= x**2]).solve() | ||
sens_x_min = sol["sensitivities"]["variables"][x_squared_min] |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"Example Loose ConstraintSet usage" | ||
from gpkit import Variable, Model | ||
from gpkit.constraints.loose import Loose | ||
|
||
Loose.reltol = 1e-4 # set the global tolerance of Loose | ||
x = Variable('x') | ||
x_min = Variable('x_{min}', 1) | ||
m = Model(x, [Loose([x >= 2], senstol=1e-4), # set the specific tolerance | ||
x >= x_min]) | ||
m.solve(verbosity=0) # prints warning |
Oops, something went wrong.