Skip to content

Commit

Permalink
Linked variable fixes, broadcast_substitutions script (#1543)
Browse files Browse the repository at this point in the history
  • Loading branch information
bqpd committed Apr 28, 2021
1 parent ec2871b commit 66a0c4d
Show file tree
Hide file tree
Showing 14 changed files with 405 additions and 56 deletions.
5 changes: 3 additions & 2 deletions docs/source/examples/evaluated_fixed_variables.py
@@ -1,10 +1,11 @@
"Example pre-solve evaluated fixed variable"
from gpkit import Variable, Model
from gpkit import Variable, Model, units

# 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")
t_night = Variable("t_{night}",
lambda c: 1*units.day - c(t_day), "hours")

# note that t_night has a function as its value
m = Model(x, [x >= t_day, x >= t_night])
Expand Down
85 changes: 85 additions & 0 deletions docs/source/examples/issue_1513.py
@@ -0,0 +1,85 @@
"Tests non-array linked functions & subs in a vectorization environment"
import numpy as np
from gpkit import Variable, Model, ConstraintSet, Vectorize

class Vehicle(Model):
"Vehicle model"
def setup(self):
self.a = a = Variable("a")
constraints = [a >= 1]
return constraints

class System(Model):
"System model"
def setup(self):
with Vectorize(1):
self.Fleet2 = Fleet2()
constraints = [self.Fleet2]
self.cost = sum(self.Fleet2.z)
return constraints

class Fleet2(Model):
"Fleet model (composed of multiple Vehicles)"
def setup(self):
x = Variable("x")
lambdafun = lambda c: [c[x]-1, np.ones(x.shape)]
with Vectorize(2):
y = Variable("y", lambdafun)
self.Vehicle = Vehicle()

self.z = z = Variable("z")
substitutions = {"x": 4}
constraints = [
z >= sum(y/x*self.Vehicle.a),
self.Vehicle,
]
return constraints, substitutions

m = System()
sol = m.solve(verbosity=0)
print(sol.table())

# now with more fleets per system
class System2(Model):
"System model"
def setup(self):
with Vectorize(3):
self.Fleet2 = Fleet2()
constraints = [self.Fleet2]
self.cost = sum(self.Fleet2.z)
return constraints

m = System2()
sol = m.solve(verbosity=0)
print(sol.table())


# now testing substitutions

class Simple(Model):
"Simple model"
def setup(self):
self.x = x = Variable("x")
y = Variable("y", 1)
z = Variable("z", 2)
constraints = [
x >= y + z,
]
return constraints

class Cake(Model):
"Cake model"
def setup(self):
with Vectorize(3):
s = Simple()
c = ConstraintSet([s])
self.cost = sum(s.x)
return c

m = Cake()
m.substitutions.update({
"y": ("sweep", [1, 2, 3]),
"z": lambda v: v("y")**2,
})
sol = m.solve(verbosity=0)
print(sol.table())
108 changes: 108 additions & 0 deletions docs/source/examples/issue_1513_output.txt
@@ -0,0 +1,108 @@

Optimal Cost
------------
1

Model Sensitivities (sorts models in sections below)
-------------------
+3.0 : System.Fleet2
+1.0 : System.Fleet2.Vehicle

Free Variables
--------------
| System.Fleet2
z : [ 1 ]

| System.Fleet2.Vehicle
a : [ 1 1 ]

Fixed Variables
---------------
| System.Fleet2
x : [ 4 ]
y : [ 3 1 ]

Variable Sensitivities
----------------------
| System.Fleet2
y : [ - +0.25 ]

Most Sensitive Constraints
--------------------------
| System.Fleet2
+1 : z[0] ≥ a[0,0]·x[0]^-1·y[0,0] + y[1,0]/x[0]·a[1,0]

| System.Fleet2.Vehicle
+0.75 : a[0,0] ≥ 1
+0.25 : a[1,0] ≥ 1


Optimal Cost
------------
3

Model Sensitivities (sorts models in sections below)
-------------------
+3.0 : System2.Fleet2
+1.0 : System2.Fleet2.Vehicle

Free Variables
--------------
| System2.Fleet2
z : [ 1 1 1 ]

| System2.Fleet2.Vehicle
a : [ 1 1 1
1 1 1 ]

Fixed Variables
---------------
| System2.Fleet2
x : [ 4 4 4 ]
y : [ 3 3 3
1 1 1 ]

Variable Sensitivities
----------------------
| System2.Fleet2
y : [ - - -
+0.083 +0.083 +0.083 ]

Most Sensitive Constraints
--------------------------
| System2.Fleet2
+0.33 : z[0] ≥ a[0,0]·x[0]^-1·y[0,0] + y[1,0]/x[0]·a[1,0]
+0.33 : z[1] ≥ a[0,1]·x[1]^-1·y[0,1] + y[1,1]/x[1]·a[1,1]
+0.33 : z[2] ≥ a[0,2]·x[2]^-1·y[0,2] + y[1,2]/x[2]·a[1,2]

| System2.Fleet2.Vehicle
+0.25 : a[0,0] ≥ 1
+0.25 : a[0,1] ≥ 1


Optimal Cost
------------
20

Swept Variables
---------------
y : [ 1 2 3 ]

Free Variables
--------------
x : [ 2 6 12 ]

Fixed Variables
---------------
z : [ 1 4 9 ]

Variable Sensitivities
----------------------
y : [ +0.15 +0.5 +1 ]

Most Sensitive Constraints
--------------------------
+0.6 : x[2] ≥ y[2] + z[2]
+0.3 : x[1] ≥ y[1] + z[1]
+0.1 : x[0] ≥ y[0] + z[0]

54 changes: 54 additions & 0 deletions docs/source/examples/issue_1522.py
@@ -0,0 +1,54 @@
"Tests broadcast_sub function for returned-dictionary substitutions"
import numpy as np
from gpkit import Variable, Model, ConstraintSet, Vectorize
from gpkit.small_scripts import broadcast_substitution

class Pie(Model):
"Pie model"
def setup(self):
self.x = x = Variable("x")
z = Variable("z")
constraints = [
x >= z,
]
substitutions = {'z': 1}
return constraints, substitutions

class Cake(Model):
"Cake model, containing a vector of Pies"
def setup(self):
self.y = y = Variable("y")
with Vectorize(2):
s = Pie()
constraints = [y >= s.x]
constraints += [s]
subs = {'x': broadcast_substitution(s.x, [2, 3])}
return constraints, subs

class Yum1(Model):
"Total dessert system model containing 5 Cakes"
def setup(self):
with Vectorize(5):
cake = Cake()
y = cake.y
self.cost = sum(y)
constraints = ConstraintSet([cake])
return constraints

m = Yum1()
sol = m.solve(verbosity=0)
print(sol.table())

class Yum2(Model):
"Total dessert system model containing 1 Cake"
def setup(self):
with Vectorize(1):
cake = Cake()
y = cake.y
self.cost = sum(y)
constraints = ConstraintSet([cake])
return constraints

m = Yum2()
sol = m.solve(verbosity=0)
print(sol.table())
69 changes: 69 additions & 0 deletions docs/source/examples/issue_1522_output.txt
@@ -0,0 +1,69 @@

Optimal Cost
------------
15

Model Sensitivities (sorts models in sections below)
-------------------
+1.0 : Yum1.Cake
: Yum1.Cake.Pie

Free Variables
--------------
| Yum1.Cake
y : [ 3 3 3 3 3 ]

Fixed Variables
---------------
| Yum1.Cake.Pie
x : [ 2 2 2 2 2
3 3 3 3 3 ]
z : [ 1 1 1 1 1
1 1 1 1 1 ]

Variable Sensitivities
----------------------
| Yum1.Cake.Pie
x : [ +7.1e-07 +7.1e-07 +7.1e-07 +7.1e-07 +7.1e-07
+0.2 +0.2 +0.2 +0.2 +0.2 ]

Most Sensitive Constraints
--------------------------
| Yum1.Cake
+0.2 : y[0] ≥ x[1,0]
+0.2 : y[1] ≥ x[1,1]
+0.2 : y[2] ≥ x[1,2]
+0.2 : y[3] ≥ x[1,3]
+0.2 : y[4] ≥ x[1,4]


Optimal Cost
------------
3

Model Sensitivities (sorts models in sections below)
-------------------
+1.0 : Yum2.Cake
: Yum2.Cake.Pie

Free Variables
--------------
| Yum2.Cake
y : [ 3 ]

Fixed Variables
---------------
| Yum2.Cake.Pie
x : [ 2 3 ]
z : [ 1 1 ]

Variable Sensitivities
----------------------
| Yum2.Cake.Pie
x : [ +8.4e-08 +1 ]

Most Sensitive Constraints
--------------------------
| Yum2.Cake
+1 : y[0] ≥ x[1,0]

8 changes: 3 additions & 5 deletions docs/source/examples/simpleflight_output.txt
Expand Up @@ -104,8 +104,8 @@ V_{min} : [ -9.1% -9.1% +13.6% +13.6% ] takeoff speed
W : [ -6.8% -12.8% -5.1% -12.2% ] total aircraft weight
C_f : [ -7.3% -12.7% - -5.0% ] skin friction coefficient

Absolute Differences |above 0|
------------------------------
Absolute Differences |above 0.1|
--------------------------------
Re : [ +1.7e+06 +3.6e+06 -4.1e+04 +1.1e+06 ] Reynold's number
W : [ -5e+02 -9.4e+02 -3.8e+02 -9e+02 ] [N] total aircraft weight
W_w : [ -5e+02 -9.4e+02 -3.8e+02 -9e+02 ] [N] wing weight
Expand All @@ -114,9 +114,7 @@ Absolute Differences |above 0|
S : [ +2.1 +0.9 -4.4 -5.3 ] [m²] total wing area
V_{min} : [ -2 -2 +3 +3 ] [m/s] takeoff speed
A : [ -2.3 -3.7 +0.38 -1.3 ] aspect ratio
C_L : [ -0.2 -0.3 -0.036 -0.19 ] Lift coefficent of wing
C_D : [ -0.006 -0.0083 -0.001 -0.0049 ] Drag coefficient of wing
C_f : [ -0.00026 -0.00046 +8e-06 -0.00018 ] skin friction coefficient
C_L : [ -0.2 -0.3 - -0.19 ] Lift coefficent of wing

Sensitivity Differences |above 0.1|
-----------------------------------
Expand Down

0 comments on commit 66a0c4d

Please sign in to comment.