# Coverage for docs/source/examples/relaxation.py: 100%

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

## 35 statements

1"Relaxation examples"

3from gpkit import Variable, Model

5x = Variable("x")

6x_min = Variable("x_min", 2)

7x_max = Variable("x_max", 1)

8m = Model(x, [x <= x_max, x >= x_min])

9print("Original model")

10print("==============")

11print(m)

12print("")

13# m.solve() # raises a RuntimeWarning!

15print("With constraints relaxed equally")

16print("================================")

18from gpkit.constraints.relax import ConstraintsRelaxedEqually

20allrelaxed = ConstraintsRelaxedEqually(m)

21mr1 = Model(allrelaxed.relaxvar, allrelaxed)

22print(mr1)

23print(mr1.solve(verbosity=0).table()) # solves with an x of 1.414

24from gpkit.breakdowns import Breakdowns

25Breakdowns(mr1.solution).trace("cost")

26print("")

28print("With constraints relaxed individually")

29print("=====================================")

31from gpkit.constraints.relax import ConstraintsRelaxed

33constraintsrelaxed = ConstraintsRelaxed(m)

34mr2 = Model(constraintsrelaxed.relaxvars.prod() * m.cost**0.01,

35 # add a bit of the original cost in

36 constraintsrelaxed)

37print(mr2)

38print(mr2.solve(verbosity=0).table()) # solves with an x of 1.0

39print("")

41print("With constants relaxed individually")

42print("===================================")

44from gpkit.constraints.relax import ConstantsRelaxed

46constantsrelaxed = ConstantsRelaxed(m)

47mr3 = Model(constantsrelaxed.relaxvars.prod() * m.cost**0.01,

48 # add a bit of the original cost in

49 constantsrelaxed)

50print(mr3)

51print(mr3.solve(verbosity=0).table()) # brings x_min down to 1.0

52print("")