Coverage for docs\source\examples\issue_1522.py: 0%
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"Tests broadcast_sub function for returned-dictionary substitutions"
2from gpkit import Variable, Model, ConstraintSet, Vectorize
3from gpkit.small_scripts import broadcast_substitution
5class Pie(Model):
6 "Pie model"
7 def setup(self):
8 self.x = x = Variable("x")
9 z = Variable("z")
10 constraints = [
11 x >= z,
12 ]
13 substitutions = {'z': 1}
14 return constraints, substitutions
16class Cake(Model):
17 "Cake model, containing a vector of Pies"
18 def setup(self):
19 self.y = y = Variable("y")
20 with Vectorize(2):
21 s = Pie()
22 constraints = [y >= s.x]
23 constraints += [s]
24 subs = {'x': broadcast_substitution(s.x, [2, 3])}
25 return constraints, subs
27class Yum1(Model):
28 "Total dessert system model containing 5 Cakes"
29 def setup(self):
30 with Vectorize(5):
31 cake = Cake()
32 y = cake.y
33 self.cost = sum(y)
34 constraints = ConstraintSet([cake])
35 return constraints
37m = Yum1()
38sol = m.solve(verbosity=0)
39print(sol.table())
41class Yum2(Model):
42 "Total dessert system model containing 1 Cake"
43 def setup(self):
44 with Vectorize(1):
45 cake = Cake()
46 y = cake.y
47 self.cost = sum(y)
48 constraints = ConstraintSet([cake])
49 return constraints
51m = Yum2()
52sol = m.solve(verbosity=0)
53print(sol.table())