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

## 42 statements

, created at 2022-07-28 12:35 -0400

1"The getting started page"

2from gpkit import Variable, VectorVariable, Model

3from gpkit.nomials import Monomial, Posynomial, PosynomialInequality

5### Example Free Variables

6# Declare a variable, x

7x = Variable("x")

9# Declare a variable, y, with units of meters

10y = Variable("y", "m")

12# Declare a variable, z, with units of meters, and a description

13z = Variable("z", "m", "A variable called z with units of meters")

15### Example Fixed Variables 1

16rho = Variable("rho", 1.225, "kg/m^3", "Density of air at sea level")

18### Example Fixed Variables 2

19#Declare pi equal to 3.14

20pi = Variable("pi", 3.14159, "-", constant=True)

22### Example Vector Variables

23# Declare a 3-element vector variable "x" with units of "m"

24x = VectorVariable(3, "x", "m", "Cube corner coordinates")

25x_min = VectorVariable(3, "x", [1, 2, 3], "m", "Cube corner minimum")

27### Example Creating Monomials and Posynomials 1

28# create a Monomial term xy^2/z

29x = Variable("x")

30y = Variable("y")

31z = Variable("z")

32m = x * y**2 / z

33assert isinstance(m, Monomial)

35### Example Creating Monomials and Posynomials 2

36# create a Posynomial expression x + xy^2

37x = Variable("x")

38y = Variable("y")

39p = x + x * y**2

40assert isinstance(p, Posynomial)

42### Example Declaring Constraints

43# consider a block with dimensions x, y, z less than 1

44# constrain surface area less than 1.0 m^2

45x = Variable("x", "m")

46y = Variable("y", "m")

47z = Variable("z", "m")

48S = Variable("S", 1.0, "m^2")

49c = (2*x*y + 2*x*z + 2*y*z <= S)

50assert isinstance(c, PosynomialInequality)

52### Example Formulating a Model

53x = Variable("x")

54y = Variable("y")

55z = Variable("z")

56S = 200

57objective = 1/(x*y*z)

58constraints = [2*x*y + 2*x*z + 2*y*z <= S,

59 x >= 2*y]

60m = Model(objective, constraints)

62### Example Solving the Model

63sol = m.solve(verbosity=0)

65### Printing Results 1

66print(sol.table())

68### Printing Results 2

69print("The optimal value is %.4g." % sol["cost"])

71### Example variable sensitivity usage

72x = Variable("x")

73x_min = Variable("x_{min}", 2)

74sol = Model(x, [x_min <= x]).solve(verbosity=0)

75sens_x_min = sol["sensitivities"]["variables"][x_min]

77x = Variable("x")

78x_squared_min = Variable("x^2_{min}", 2)

79sol = Model(x, [x_squared_min <= x**2]).solve(verbosity=0)

80sens_x_min = sol["sensitivities"]["variables"][x_squared_min]