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

12 statements  

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

1"""Adapted from t_SP in tests/t_geometric_program.py""" 

2from gpkit import Model, Variable, SignomialsEnabled 

3 

4# Decision variables 

5x = Variable('x') 

6y = Variable('y') 

7 

8# must enable signomials for subtraction 

9with SignomialsEnabled(): 

10 constraints = [x >= 1-y, y <= 0.1] 

11 

12# create and solve the SP 

13m = Model(x, constraints) 

14print(m.localsolve(verbosity=0).summary()) 

15assert abs(m.solution(x) - 0.9) < 1e-6 # pylint:disable=not-callable 

16 

17# full interim solutions are available 

18# pylint: disable=consider-using-f-string 

19print("x values of each GP solve (note convergence)") 

20print(", ".join("%.5f" % sol["freevariables"][x] for sol in m.program.results)) 

21 

22# use x0 to give the solution, reducing number of GPs needed 

23m.localsolve(verbosity=0, x0={x: 0.9, y:0.1}) 

24assert len(m.program.results) == 2