-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
414 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
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() | ||
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() | ||
print(sol.table()) |
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,118 @@ | ||
Using solver 'cvxopt' | ||
for 3 free variables | ||
in 4 posynomial inequalities. | ||
Solving took 0.00804 seconds. | ||
|
||
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 | ||
|
||
Using solver 'cvxopt' | ||
for 9 free variables | ||
in 10 posynomial inequalities. | ||
Solving took 0.0195 seconds. | ||
|
||
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 | ||
|
||
Sweeping with 1 solves: | ||
Sweeping took 0.0224 seconds. | ||
|
||
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] | ||
|
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,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() | ||
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() | ||
print(sol.table()) |
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,77 @@ | ||
Using solver 'cvxopt' | ||
for 5 free variables | ||
in 11 posynomial inequalities. | ||
Solving took 0.0145 seconds. | ||
|
||
Optimal Cost | ||
------------ | ||
15 | ||
|
||
Model Sensitivities (sorts models in sections below) | ||
------------------- | ||
+1.0 : Yum1.Cake.Pie | ||
: Yum1.Cake | ||
|
||
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] | ||
|
||
Using solver 'cvxopt' | ||
for 1 free variables | ||
in 3 posynomial inequalities. | ||
Solving took 0.0132 seconds. | ||
|
||
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] | ||
|
Oops, something went wrong.