Coverage for gpkit/constraints/loose.py: 91%

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

22 statements  

1"Implements Loose" 

2from .set import ConstraintSet 

3from ..small_scripts import appendsolwarning, initsolwarning 

4 

5 

6class Loose(ConstraintSet): 

7 "ConstraintSet whose inequalities must result in an equality." 

8 senstol = 1e-5 

9 raiseerror = False 

10 

11 def __init__(self, constraints, *, senstol=None): 

12 super().__init__(constraints) 

13 self.senstol = senstol or self.senstol 

14 

15 def process_result(self, result): 

16 "Checks that all constraints are satisfied with equality" 

17 super().process_result(result) 

18 initsolwarning(result, "Unexpectedly Tight Constraints") 

19 if "sensitivities" not in result: 

20 appendsolwarning("Could not evaluate due to choice variables.", 

21 (), result, "Unexpectedly Tight Constraints") 

22 return 

23 for constraint in self.flat(): 

24 c_senss = result["sensitivities"]["constraints"].get(constraint, 0) 

25 if c_senss >= self.senstol: 

26 cstr = ("Constraint [ %.100s... %s %.100s... )" 

27 % (constraint.left, constraint.oper, constraint.right)) 

28 msg = ("%s is not loose: it has a sensitivity of %+.4g." 

29 " (Allowable sensitivity: %.4g)" % 

30 (cstr, c_senss, self.senstol)) 

31 appendsolwarning(msg, (c_senss, constraint), result, 

32 "Unexpectedly Tight Constraints") 

33 if self.raiseerror: 

34 raise RuntimeWarning(msg)