Skip to content

Commit

Permalink
closes #1522
Browse files Browse the repository at this point in the history
  • Loading branch information
bqpd committed Apr 27, 2021
1 parent cb07628 commit 8283615
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 0 deletions.
118 changes: 118 additions & 0 deletions docs/source/examples/issue_1513_output.txt
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]

54 changes: 54 additions & 0 deletions docs/source/examples/issue_1522.py
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())
77 changes: 77 additions & 0 deletions docs/source/examples/issue_1522_output.txt
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]

5 changes: 5 additions & 0 deletions gpkit/small_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import numpy as np


def broadcast_substitution(key, array):
"Broadcasts input into the shape of a given key"
return np.broadcast_to(array, reversed(key.key.shape)).T


def veclinkedfn(linkedfn, i):
"Generate an indexed linking function."
def newlinkedfn(c):
Expand Down
3 changes: 3 additions & 0 deletions gpkit/tests/t_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def test_dummy_example(self, example):
def test_issue_1513(self, example):
pass

def test_issue_1522(self, example):
pass

def test_autosweep(self, example):
from gpkit import ureg
bst1, tol1 = example.bst1, example.tol1
Expand Down

0 comments on commit 8283615

Please sign in to comment.