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

11 statements  

« prev     ^ index     » next       coverage.py v7.4.0, created at 2024-01-07 22:13 -0500

1"Can be found in gpkit/docs/source/examples/external_constraint.py" 

2from external_function import external_code 

3 

4 

5class ExternalConstraint: # pylint: disable=too-few-public-methods 

6 "Class for external calling" 

7 

8 def __init__(self, x, y): 

9 # We need a GPkit variable defined to return in our constraint. The 

10 # easiest way to do this is to read in the parameters of interest in 

11 # the initiation of the class and store them here. 

12 self.x = x 

13 self.y = y 

14 

15 def as_gpconstr(self, x0): 

16 "Returns locally-approximating GP constraint" 

17 # Creating a default constraint for the first solve 

18 if self.x not in x0: 

19 return self.y >= self.x 

20 # Otherwise calls external code at the current position... 

21 x_star = x0[self.x] 

22 res = external_code(x_star) 

23 # ...and returns a posynomial approximation around that position 

24 return self.y >= res * self.x/x_star