Coverage for docs/source/examples/boundschecking.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-03 16:49 -0500

1"Verifies that bounds are caught through monomials" 

2from gpkit import Model, parse_variables 

3from gpkit.exceptions import UnboundedGP, UnknownInfeasible 

4 

5 

6class BoundsChecking(Model): 

7 """Implements a crazy set of unbounded variables. 

8 

9 Variables 

10 --------- 

11 Ap [-] d 

12 D [-] e 

13 F [-] s 

14 mi [-] c 

15 mf [-] r 

16 T [-] i 

17 nu [-] p 

18 Fs 0.9 [-] t 

19 mb 0.4 [-] i 

20 rf 0.01 [-] o 

21 V 300 [-] n 

22 

23 Upper Unbounded 

24 --------------- 

25 F 

26 

27 Lower Unbounded 

28 --------------- 

29 D 

30 

31 """ 

32 @parse_variables(__doc__, globals()) 

33 def setup(self): 

34 self.cost = F 

35 return [ 

36 F >= D + T, 

37 D == rf*V**2*Ap, 

38 Ap == nu, 

39 T == mf*V, 

40 mf >= mi + mb, 

41 mf == rf*V, 

42 Fs <= mi 

43 ] 

44 

45 

46m = BoundsChecking() 

47print(m.str_without(["lineage"])) 

48try: 

49 m.solve() 

50except UnboundedGP: 

51 gp = m.gp(checkbounds=False) 

52 missingbounds = gp.check_bounds() 

53 

54try: 

55 sol = gp.solve(verbosity=0) # Errors on mosek_cli 

56except UnknownInfeasible: # pragma: no cover 

57 pass 

58 

59bpl = ", but would gain it from any of these sets: " 

60assert missingbounds[(m.D.key, 'lower')] == bpl + "[(%s, 'lower')]" % m.Ap 

61assert missingbounds[(m.nu.key, 'lower')] == bpl + "[(%s, 'lower')]" % m.Ap 

62# ordering is arbitrary: 

63assert missingbounds[(m.Ap.key, 'lower')] in ( 

64 bpl + ("[(%s, 'lower')] or [(%s, 'lower')]" % (m.D, m.nu)), 

65 bpl + ("[(%s, 'lower')] or [(%s, 'lower')]" % (m.nu, m.D)))