-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation bug fixes and Sphinx refactor (replace stray Python/.txt with ..literalinclude) #1508
Changes from all commits
ba46ab0
7560094
3400603
05cccb5
f9ae65d
0ca98a7
33588b9
a3f25b9
fbaa470
e5909e9
fc12f08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from gpkit import Variable | ||
|
||
# create a Monomial term xy^2/z | ||
x = Variable("x") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs to import from gpkit |
||
y = Variable("y") | ||
z = Variable("z") | ||
m = x * y**2 / z | ||
print(type(m)) # gpkit.nomials.Monomial |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from gpkit import Variable | ||
|
||
# create a Posynomial expression x + xy^2 | ||
x = Variable("x") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs to import from gpkit |
||
y = Variable("y") | ||
p = x + x * y**2 | ||
print(type(p)) # gpkit.nomials.Posynomial |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from gpkit import Variable | ||
|
||
# consider a block with dimensions x, y, z less than 1 | ||
# constrain surface area less than 1.0 m^2 | ||
x = Variable("x", "m") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs to import from gpkit |
||
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) | ||
print(type(c)) # gpkit.nomials.PosynomialInequality |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import gpkit | ||
from gpkit import Variable, Model | ||
|
||
# code from t_GPSubs.test_calcconst in tests/t_sub.py | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs imports There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed lines 8, 11, and 12 here: ba46ab0#diff-9847f1403832c7286b52e60d7fe3313fR8-R12 I wasn't quite sure how they were relevant to the code. They may be artifacts from the |
||
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) | ||
|
||
# call substitutions | ||
m.substitutions.update({t_day: ("sweep", [8, 12, 16])}) | ||
sol = m.solve(verbosity=0) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from gpkit import Variable, Model | ||
|
||
# code from t_constraints.test_evalfn in tests/t_sub.py | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs imports There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from gpkit import Variable | ||
|
||
# Declare \rho equal to 1.225 kg/m^3. | ||
# NOTE: in python string literals, backslashes must be doubled | ||
rho = Variable("\\rho", 1.225, "kg/m^3", "Density of air at sea level") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs to import from GPkit |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from gpkit import Variable | ||
|
||
#Declare pi equal to 3.14 | ||
pi = Variable("\\pi", 3.14) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs to import, should probably add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see anything in the docs about |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from gpkit import Variable, Model | ||
|
||
x = Variable("x") | ||
y = Variable("y") | ||
z = Variable("z") | ||
S = 200 | ||
objective = 1/(x*y*z) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs to import and declare variables |
||
constraints = [2*x*y + 2*x*z + 2*y*z <= S, | ||
x >= 2*y] | ||
m = Model(objective, constraints) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from gpkit import Variable | ||
|
||
# Declare a variable, x | ||
x = Variable("x") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs to import |
||
|
||
# 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") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from gpkit import Variable, Model | ||
x = Variable("x") | ||
y = Variable("y", 3) # fix value to 3 | ||
m = Model(x, [x >= 1 + y, y >= 1]) | ||
_ = m.solve() # optimal cost is 4; y appears in sol["constants"] | ||
|
||
del m.substitutions["y"] | ||
_ = m.solve() # optimal cost is 2; y appears in Free Variables |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from gpkit import Variable, VectorVariable, Model | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be part of a larger file. this particular line can be excerpted as part of the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from gpkit import Variable, Model | ||
from gpkit.constraints.loose import Loose | ||
|
||
Loose.reltol = 1e-4 # set the global tolerance of Tight | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sol = m.solve(verbosity=0) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# adapted from t_sub.py / t_NomialSubs / test_Vector | ||
from gpkit import Variable, VectorVariable | ||
x = Variable("x") | ||
y = Variable("y") | ||
z = VectorVariable(2, "z") | ||
p = x*y*z | ||
assert all(p.sub({x: 1, "y": 2}) == 2*z) | ||
assert all(p.sub({x: 1, y: 2, "z": [1, 2]}) == z.sub(z, [2, 4])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is throwing this error:
Haven't quite been able to fix it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, should probably create a dummy model here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a dummy model here: 33588b9
I noticed when running
last_verified_sol = pickle.load(open("last_verified.sol"))
here: https://github.com/convexengineering/gpkit/pull/1508/files#diff-79562c84e117012e71221b6d88dd595bR23I was getting this error:
so I changed the code to:
last_verified_sol = pickle.load(open("last_verified.sol", mode="rb"))
and it worked successfully.Not sure if this is a bug or not.