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

21 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-07 22:15 -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"])) 

48missingbounds = {} 

49try: 

50 m.solve() 

51except UnboundedGP: 

52 gp = m.gp(checkbounds=False) 

53 missingbounds = gp.check_bounds() 

54 

55try: 

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

57except UnknownInfeasible: # pragma: no cover 

58 pass 

59 

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

61assert missingbounds[(m.D.key, 'lower')] == bpl + f"[({m.Ap}, 'lower')]" 

62assert missingbounds[(m.nu.key, 'lower')] == bpl + f"[({m.Ap}, 'lower')]" 

63# ordering is arbitrary: 

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

65 bpl + (f"[({m.D}, 'lower')] or [({m.nu}, 'lower')]"), 

66 bpl + (f"[({m.nu}, 'lower')] or [({m.D}, 'lower')]"))