def test_ast(self): # pylint: disable=too-many-statements if sys.platform[:3] == "win": return t = Variable("t") u = Variable("u") v = Variable("v") w = Variable("w") x = VectorVariable(3, "x") y = VectorVariable(3, "y") z = VectorVariable(3, "z") a = VectorVariable((3, 2), "a") # print(w >= x) # TODO: this always prints the vector on the left self.assertEqual(str(3*(x + y)*z), "3·(x[:] + y[:])·z[:]") nni = 3 ii = np.tile(np.arange(1, nni+1), a.shape[1:]+(1,)).T self.assertEqual(str(w*NomialArray(ii)/nni)[:4], "w·[[") self.assertEqual(str(w*NomialArray(ii)/nni)[-4:], "]]/3") self.assertEqual(str(NomialArray(ii)*w/nni)[:2], "[[") self.assertEqual(str(NomialArray(ii)*w/nni)[-6:], "]]·w/3") self.assertEqual(str(w*ii/nni)[:4], "w·[[") self.assertEqual(str(w*ii/nni)[-4:], "]]/3") self.assertEqual(str(w*(ii/nni))[:4], "w·[[") self.assertEqual(str(w*(ii/nni))[-2:], "]]") self.assertEqual(str(w >= (x[0]*t + x[1]*u)/v), "w >= (x[0]·t + x[1]·u)/v") self.assertEqual(str(x), "x[:]") self.assertEqual(str(x*2), "x[:]·2") self.assertEqual(str(2*x), "2·x[:]") self.assertEqual(str(x + 2), "x[:] + 2") self.assertEqual(str(2 + x), "2 + x[:]") self.assertEqual(str(x/2), "x[:]/2") self.assertEqual(str(2/x), "2/x[:]") self.assertEqual(str(x**3), "x[:]³") self.assertEqual(str(-x), "-x[:]") self.assertEqual(str(x/y/z), "x[:]/y[:]/z[:]") self.assertEqual(str(x/(y/z)), "x[:]/(y[:]/z[:])") self.assertEqual(str(x >= y), "x[:] >= y[:]") self.assertEqual(str(x >= y + z), "x[:] >= y[:] + z[:]") self.assertEqual(str(x[:2]), "x[:2]") self.assertEqual(str(x[:]), "x[:]") self.assertEqual(str(x[1:]), "x[1:]") self.assertEqual(str(y * [1, 2, 3]), "y[:]·[1, 2, 3]") self.assertEqual(str(x[:2] == (y*[1, 2, 3])[:2]), "x[:2] = (y[:]·[1, 2, 3])[:2]") self.assertEqual(str(y + [1, 2, 3]), "y[:] + [1, 2, 3]") self.assertEqual(str(x == y + [1, 2, 3]), "x[:] = y[:] + [1, 2, 3]") self.assertEqual(str(x >= y + [1, 2, 3]), "x[:] >= y[:] + [1, 2, 3]") self.assertEqual(str(a[:, 0]), "a[:,0]") self.assertEqual(str(a[2, :]), "a[2,:]") g = 1 + 3*a[2, 0]**2 gstrbefore = str(g) g.ast = None gstrafter = str(g) self.assertEqual(gstrbefore, gstrafter)
def test_ast(self): # pylint: disable=too-many-statements t = Variable("t") u = Variable("u") v = Variable("v") w = Variable("w") x = VectorVariable(3, "x") y = VectorVariable(3, "y") z = VectorVariable(3, "z") a = VectorVariable((3, 2), "a") print(w >= x) self.assertEqual(str(3 * (x + y) * z), "3*(x[:] + y[:])*z[:]") nni = 3 ii = np.tile(np.arange(1., nni + 1.), a.shape[1:] + (1, )).T self.assertEqual(str(w * NomialArray(ii) / nni)[:4], "w*[[") self.assertEqual(str(w * NomialArray(ii) / nni)[-4:], "]]/3") self.assertEqual(str(NomialArray(ii) * w / nni)[:2], "[[") self.assertEqual(str(NomialArray(ii) * w / nni)[-6:], "]]*w/3") self.assertEqual(str(w * ii / nni)[:4], "w*[[") self.assertEqual(str(w * ii / nni)[-4:], "]]/3") self.assertEqual(str(w * (ii / nni))[:4], "w*[[") self.assertEqual(str(w * (ii / nni))[-2:], "]]") self.assertEqual(str(w >= (x[0] * t + x[1] * u) / v), "w >= (x[0]*t + x[1]*u)/v") self.assertEqual(str(x), "x[:]") self.assertEqual(str(x * 2), "x[:]*2") self.assertEqual(str(2 * x), "2*x[:]") self.assertEqual(str(x + 2), "x[:] + 2") self.assertEqual(str(2 + x), "2 + x[:]") self.assertEqual(str(x / 2), "x[:]/2") self.assertEqual(str(2 / x), "2/x[:]") if sys.version_info <= (3, 0): self.assertEqual(str(x**3), "x[:]^3") self.assertEqual(str(-x), "-x[:]") self.assertEqual(str(x / y / z), "x[:]/y[:]/z[:]") self.assertEqual(str(x / (y / z)), "x[:]/(y[:]/z[:])") self.assertEqual(str(x >= y), "x[:] >= y[:]") self.assertEqual(str(x >= y + z), "x[:] >= y[:] + z[:]") self.assertEqual(str(x[:2]), "x[:2]") self.assertEqual(str(x[:]), "x[:]") self.assertEqual(str(x[1:]), "x[1:]") self.assertEqual(str(y * [1, 2, 3]), "y[:]*[1, 2, 3]") self.assertEqual(str(x[:2] == (y * [1, 2, 3])[:2]), "x[:2] = (y[:]*[1, 2, 3])[:2]") self.assertEqual(str(y + [1, 2, 3]), "y[:] + [1, 2, 3]") self.assertEqual(str(x == y + [1, 2, 3]), "x[:] = y[:] + [1, 2, 3]") self.assertEqual(str(x >= y + [1, 2, 3]), "x[:] >= y[:] + [1, 2, 3]") self.assertEqual(str(a[:, 0]), "a[:,0]") self.assertEqual(str(a[2, :]), "a[2,:]") g = 1 + 3 * a[2, 0]**2 gstrbefore = str(g) g.ast = None gstrafter = str(g) if sys.version_info <= (3, 0): self.assertEqual(gstrbefore, gstrafter)
def test_init(self): """Test VectorVariable initialization""" # test 1 n = 3 v = VectorVariable(n, 'v', label='dummy variable') self.assertTrue(isinstance(v, NomialArray)) v_mult = 3*v for i in range(n): self.assertTrue(isinstance(v[i], PlainVariable)) self.assertTrue(isinstance(v[i], Monomial)) # test that operations on Variable cast to Monomial self.assertTrue(isinstance(v_mult[i], Monomial)) self.assertFalse(isinstance(v_mult[i], PlainVariable)) # test 2 x = VectorVariable(3, 'x', label='dummy variable') x_0 = Variable('x', idx=(0,), shape=(3,), label='dummy variable') x_1 = Variable('x', idx=(1,), shape=(3,), label='dummy variable') x_2 = Variable('x', idx=(2,), shape=(3,), label='dummy variable') x2 = NomialArray([x_0, x_1, x_2]) self.assertEqual(x, x2) # test inspired by issue 137 N = 20 x_arr = np.arange(0, 5, 5/N) + 1e-6 x = VectorVariable(N, 'x', x_arr, 'm', "Beam Location")
def test_constraint_gen(self): x = VectorVariable(3, 'x', label='dummy variable') x_0 = Monomial('x', idx=(0, ), shape=(3, ), label='dummy variable') x_1 = Monomial('x', idx=(1, ), shape=(3, ), label='dummy variable') x_2 = Monomial('x', idx=(2, ), shape=(3, ), label='dummy variable') v = NomialArray([1, 2, 3]).T p = [x_0, x_1 / 2, x_2 / 3] self.assertEqual((x <= v).as_posyslt1(), p)
def test_constraint_gen(self): x = VectorVariable(3, 'x', label='dummy variable') x_0 = Variable('x', idx=(0, ), shape=(3, ), label='dummy variable') x_1 = Variable('x', idx=(1, ), shape=(3, ), label='dummy variable') x_2 = Variable('x', idx=(2, ), shape=(3, ), label='dummy variable') v = NomialArray([1, 2, 3]).T p = [x_0, x_1 / 2, x_2 / 3] self.assertEqual(list((x <= v).flathmaps({})), [e.hmap for e in p])
def test_constraint_creation_units(self): v = VectorVariable(2, "v", "m/s") c = (v >= 40*gpkit.units("ft/s")) c2 = (v >= np.array([20, 30])*gpkit.units("ft/s")) if gpkit.units: self.assertTrue(c.right.units) self.assertTrue(NomialArray(c2.right).units) else: self.assertEqual(type(c.right), int) self.assertEqual(type(c2.right), np.ndarray)
def test_array_mult(self): x = VectorVariable(3, 'x', label='dummy variable') x_0 = Monomial('x', idx=(0, ), shape=(3, ), label='dummy variable') x_1 = Monomial('x', idx=(1, ), shape=(3, ), label='dummy variable') x_2 = Monomial('x', idx=(2, ), shape=(3, ), label='dummy variable') p = x_0**2 + x_1**2 + x_2**2 self.assertEqual(x.dot(x), p) m = NomialArray([[x_0**2, x_0 * x_1, x_0 * x_2], [x_0 * x_1, x_1**2, x_1 * x_2], [x_0 * x_2, x_1 * x_2, x_2**2]]) self.assertEqual(x.outer(x), m)
def test_init(self): """Test VectorVariable initialization""" # test 1 n = 3 v = VectorVariable(n, 'v', label='dummy variable') self.assertTrue(isinstance(v, NomialArray)) v_mult = 3*v for i in range(n): self.assertTrue(isinstance(v[i], PlainVariable)) self.assertTrue(isinstance(v[i], Monomial)) # test that operations on Variable cast to Monomial self.assertTrue(isinstance(v_mult[i], Monomial)) self.assertFalse(isinstance(v_mult[i], PlainVariable)) # test 2 x = VectorVariable(3, 'x', label='dummy variable') x_0 = Variable('x', idx=(0,), shape=(3,), label='dummy variable') x_1 = Variable('x', idx=(1,), shape=(3,), label='dummy variable') x_2 = Variable('x', idx=(2,), shape=(3,), label='dummy variable') x2 = NomialArray([x_0, x_1, x_2]) self.assertEqual(x, x2) # test inspired by issue 137 N = 20 x_arr = np.arange(0, 5, 5/N) + 1e-6 x = VectorVariable(N, 'x', x_arr, 'm', "Beam Location") with self.assertRaises(ValueError): _ = VectorVariable(2, "x", [1, 2, 3]) with Vectorize(2): x = VectorVariable(3, "x", np.array([13, 15, 17])) self.assertEqual(x[0, 0].value, 13) self.assertEqual(x[1, 0].value, 15) self.assertEqual(x[2, 0].value, 17) self.assertEqual(x[0, 0].value, x[0, 1].value) self.assertEqual(x[1, 0].value, x[1, 1].value) self.assertEqual(x[2, 0].value, x[2, 1].value)
def test_elementwise_mult(self): m = Monomial('m') x = VectorVariable(3, 'x', label='dummy variable') x_0 = Monomial('x', idx=(0, ), shape=(3, ), label='dummy variable') x_1 = Monomial('x', idx=(1, ), shape=(3, ), label='dummy variable') x_2 = Monomial('x', idx=(2, ), shape=(3, ), label='dummy variable') # multiplication with numbers v = NomialArray([2, 2, 3]).T p = NomialArray([2 * x_0, 2 * x_1, 3 * x_2]).T self.assertEqual(x * v, p) # division with numbers p2 = NomialArray([x_0 / 2, x_1 / 2, x_2 / 3]).T self.assertEqual(x / v, p2) # power p3 = NomialArray([x_0**2, x_1**2, x_2**2]).T self.assertEqual(x**2, p3) # multiplication with monomials p = NomialArray([m * x_0, m * x_1, m * x_2]).T self.assertEqual(x * m, p) # division with monomials p2 = NomialArray([x_0 / m, x_1 / m, x_2 / m]).T self.assertEqual(x / m, p2)
def __init__(self, df, ivar, dvars, nobounds=False, err_margin=False, airfoil=False): self.airfoil = airfoil self.dvars = dvars self.ivar = ivar K = int(df["K"].iloc[0]) d = int(df["d"].iloc[0]) ftype = df["ftype"].iloc[0] A = np.array(df[[ "e%d%d" % (k, i) for k in range(1, K + 1) for i in range(1, d + 1) ]])[0].astype(float) B = np.array(df[["c%d" % k for k in range(1, K + 1)]])[0].astype(float) withvector = False withvar = False for dv in self.dvars: if hasattr(dv, "__len__"): withvector = True N = len(dv) else: withvar = True if withvector: if withvar: self.dvars = np.array([ dv if isinstance(dv, NomialArray) else [dv] * N for dv in self.dvars ]).T else: self.dvars = np.array(self.dvars).T else: self.dvars = np.array([self.dvars]) monos = [ B * NomialArray([(dv**A[k * d:(k + 1) * d]).prod() for k in range(K)]) for dv in self.dvars ] if err_margin == "Max": maxerr = float(df["max_err"].iloc[0]) self.mfac = Variable( "m_{fac-fit}", 1 + maxerr, "-", "max error of " + ivar.descr["label"] + " fit") elif err_margin == "RMS": rmserr = float(df["rms_err"].iloc[0]) self.mfac = Variable( "m_{fac-fit}", 1 + rmserr, "-", "RMS error of " + ivar.descr["label"] + " fit") else: self.mfac = Variable("m_{fac-fit}", 1.0, "-", "fit factor") if ftype == "ISMA": # constraint of the form 1 >= c1*u1^exp1*u2^exp2*w^(-alpha) + .... alpha = np.array(df[["a%d" % k for k in range(1, K + 1)]])[0].astype(float) lhs = 1 rhs = NomialArray([(mono / (ivar / self.mfac)**alpha).sum() for mono in monos]) elif ftype == "SMA": # constraint of the form w^alpha >= c1*u1^exp1 + c2*u2^exp2 +.... alpha = float(df["a1"].iloc[0]) lhs = (ivar / self.mfac)**alpha rhs = NomialArray([mono.sum() for mono in monos]) elif ftype == "MA": # constraint of the form w >= c1*u1^exp1, w >= c2*u2^exp2, .... lhs, rhs = (ivar / self.mfac), NomialArray(monos) if K == 1: # when possible, return an equality constraint if withvector: cstrt = [(lh == rh) for rh, lh in zip(rhs, lhs)] else: cstrt = (lhs == rhs) else: if withvector: cstrt = [(lh >= rh) for rh, lh in zip(rhs, lhs)] else: cstrt = (lhs >= rhs) constraints = [cstrt] if not hasattr(self.mfac, "__len__"): self.mfac = [self.mfac] * len(self.dvars) if not hasattr(self.ivar, "__len__"): self.ivar = [self.ivar] * len(self.dvars) self.bounds = [] for dvar in self.dvars: bds = {} for i, v in enumerate(dvar): bds[v] = [ float(df["lb%d" % (i + 1)].iloc[0]), float(df["ub%d" % (i + 1)].iloc[0]) ] self.bounds.append(bds) ConstraintSet.__init__(self, constraints)