Hot-keys on this page
r m x p 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"
2import numpy as np
3from gpkit import Variable, Model, ConstraintSet, Vectorize
4from gpkit.small_scripts import broadcast_substitution
6class Pie(Model):
7 "Pie model"
8 def setup(self):
9 self.x = x = Variable("x")
10 z = Variable("z")
11 constraints = [
12 x >= z,
13 ]
14 substitutions = {'z': 1}
15 return constraints, substitutions
17class Cake(Model):
18 "Cake model, containing a vector of Pies"
19 def setup(self):
20 self.y = y = Variable("y")
21 with Vectorize(2):
22 s = Pie()
23 constraints = [y >= s.x]
24 constraints += [s]
25 subs = {'x': broadcast_substitution(s.x, [2, 3])}
26 return constraints, subs
28class Yum1(Model):
29 "Total dessert system model containing 5 Cakes"
30 def setup(self):
31 with Vectorize(5):
32 cake = Cake()
33 y = cake.y
34 self.cost = sum(y)
35 constraints = ConstraintSet([cake])
36 return constraints
38m = Yum1()
39sol = m.solve(verbosity=0)
40print(sol.table())
42class Yum2(Model):
43 "Total dessert system model containing 1 Cake"
44 def setup(self):
45 with Vectorize(1):
46 cake = Cake()
47 y = cake.y
48 self.cost = sum(y)
49 constraints = ConstraintSet([cake])
50 return constraints
52m = Yum2()
53sol = m.solve(verbosity=0)
54print(sol.table())