Coverage for gpkit/tests/t_solution_array.py: 100%
92 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-07 22:15 -0500
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-07 22:15 -0500
1"""Tests for SolutionArray class"""
2import unittest
3import numpy as np
4from gpkit import Variable, VectorVariable, Model, SignomialsEnabled
5import gpkit
6from gpkit.solution_array import var_table
7from gpkit.varkey import VarKey
8from gpkit.small_classes import Strings, Quantity
11class TestSolutionArray(unittest.TestCase):
12 """Unit tests for the SolutionArray class"""
14 def test_call(self):
15 A = Variable('A', '-', 'Test Variable')
16 prob = Model(A, [A >= 1])
17 sol = prob.solve(verbosity=0)
18 self.assertAlmostEqual(sol(A), 1.0, 8)
20 def test_call_units(self):
21 # test from issue541
22 x = Variable("x", 10, "ft")
23 y = Variable("y", "m")
24 m = Model(y, [y >= x])
25 sol = m.solve(verbosity=0)
26 self.assertAlmostEqual(sol("y")/sol("x"), 1.0, 6)
27 self.assertAlmostEqual(sol(x)/sol(y), 1.0, 6)
29 def test_call_vector(self):
30 n = 5
31 x = VectorVariable(n, 'x')
32 prob = Model(sum(x), [x >= 2.5])
33 sol = prob.solve(verbosity=0)
34 solx = sol(x)
35 self.assertEqual(type(solx), Quantity)
36 self.assertEqual(type(sol["variables"][x]), np.ndarray)
37 self.assertEqual(solx.shape, (n,))
38 for i in range(n):
39 self.assertAlmostEqual(solx[i], 2.5, places=4)
41 def test_subinto(self):
42 # pylint: disable=invalid-name
43 nsweep = 20
44 Pvals = np.linspace(13, 24, nsweep)
45 H_max = Variable("H_max", 10, "m", "Length")
46 A_min = Variable("A_min", 10, "m^2", "Area")
47 P_max = Variable("P", Pvals, "m", "Perimeter")
48 H = Variable("H", "m", "Length")
49 W = Variable("W", "m", "Width")
50 m = Model(12/(W*H**3),
51 [H <= H_max,
52 H*W >= A_min,
53 P_max >= 2*H + 2*W])
54 sol = m.solve(verbosity=0)
55 Psol = sol.subinto(P_max)
56 self.assertEqual(len(Psol), nsweep)
57 self.assertAlmostEqual(0*gpkit.ureg.m,
58 np.max(np.abs(Pvals*gpkit.ureg.m - Psol)))
59 self.assertAlmostEqual(0*gpkit.ureg.m,
60 np.max(np.abs(Psol - sol(P_max))))
62 def test_table(self):
63 x = Variable('x')
64 gp = Model(x, [x >= 12])
65 sol = gp.solve(verbosity=0)
66 tab = sol.table()
67 self.assertTrue(isinstance(tab, Strings))
69 def test_units_sub(self):
70 # issue 809
71 t = Variable("T", "N", "thrust")
72 t_min = Variable("T_{min}", "N", "minimum thrust")
73 m = Model(t, [t >= t_min])
74 tminsub = 1000 * gpkit.ureg.lbf
75 m.substitutions.update({t_min: tminsub})
76 sol = m.solve(verbosity=0)
77 self.assertAlmostEqual(sol(t_min), tminsub)
78 self.assertFalse(
79 "1000N" in
80 sol.table().replace(" ", "").replace("[", "").replace("]", ""))
82 def test_key_options(self):
83 # issue 993
84 x = Variable("x")
85 y = Variable("y")
86 with SignomialsEnabled():
87 m = Model(y, [y + 6*x >= 13 + x**2])
88 msol = m.localsolve(verbosity=0)
89 spsol = m.sp().localsolve(verbosity=0) # pylint: disable=no-member
90 gpsol = m.program.gps[-1].solve(verbosity=0)
91 self.assertEqual(msol(x), msol("x"))
92 self.assertEqual(spsol(x), spsol("x"))
93 self.assertEqual(gpsol(x), gpsol("x"))
94 self.assertEqual(msol(x), spsol(x))
95 self.assertEqual(msol(x), gpsol(x))
98class TestResultsTable(unittest.TestCase):
99 """TestCase for var_table()"""
101 def test_nan_printing(self):
102 """Test that solution prints when it contains nans"""
103 x = VarKey(name='x')
104 data = {x: np.array([np.nan, 1, 1, 1, 1])}
105 title = "Free variables"
106 printstr = "\n".join(var_table(data, title))
107 self.assertTrue(" - " in printstr) # nan is printed as " - "
108 self.assertTrue(title in printstr)
110 def test_result_access(self):
111 x = Variable("x")
112 y = Variable("y")
113 with SignomialsEnabled():
114 sig = y + 6*x >= 13 + x**2
115 m = Model(y, [sig])
116 sol = m.localsolve(verbosity=0)
117 self.assertTrue(all((isinstance(gp.result.table(), Strings)
118 for gp in m.program.gps)))
119 self.assertAlmostEqual(sol["cost"]/4.0, 1.0, 5)
120 self.assertAlmostEqual(sol("x")/3.0, 1.0, 3)
122TESTS = [TestSolutionArray, TestResultsTable]
124if __name__ == "__main__": # pragma: no cover
125 # pylint: disable=wrong-import-position
126 from gpkit.tests.helpers import run_tests
127 run_tests(TESTS)