def test_episode_2(s, reward): R, x, y, z, t = sp.ring('x,y,z,t', sp.FF(32003), 'grlex') F = [x**31 - x**6 - x - y, x**8 - z, x**10 - t] ideal_gen = FixedIdealGenerator(F) env = BuchbergerEnv(ideal_gen, rewards='reductions') agent = BuchbergerAgent(selection=s) assert run_episode(agent, env) == reward
def cyclic(n, coefficient_ring=sp.FF(32003), order='grevlex'): """Return the cyclic-n ideal.""" R, gens = sp.xring('x:' + str(n), coefficient_ring, order) F = [ sum(np.prod([gens[(i + k) % n] for k in range(d)]) for i in range(n)) for d in range(1, n) ] return F + [np.product(gens) - 1]
def make_env(args): """Return the training environment for this run.""" if args.environment in ['CartPole-v0', 'CartPole-v1', 'LunarLander-v2']: env = gym.make(args.environment) elif args.binned: dirname = "".join([ 'data/bins/{0}-{1}-{2}'.format(args.variables, args.degree, args.generators), '-consts' if args.constants else "", '-' + args.degree_distribution, '-homog' if args.homogeneous else "", '-pure' if args.pure else "", ]) ring = sp.xring('x:' + str(args.variables), sp.FF(32003), 'grevlex')[0] ideal_gen = FromDirectoryIdealGenerator(dirname, ring) env = BuchbergerEnv(ideal_gen, elimination=args.elimination, rewards=args.rewards) env = LeadMonomialsWrapper(env, k=args.k) elif args.environment == "MixedRandomBinomialIdeal": ideal_gen = MixedRandomBinomialIdealGenerator( args.variables, list(range(5, args.degree + 1)), list(range(4, args.generators + 1)), constants=args.constants, degrees=args.degree_distribution, homogeneous=args.homogeneous, pure=args.pure) env = BuchbergerEnv(ideal_gen, elimination=args.elimination, rewards=args.rewards) env = LeadMonomialsWrapper(env, k=args.k) elif args.environment == "RandomPolynomialIdeal": ideal_gen = RandomIdealGenerator(args.variables, args.degree, args.generators, args.l, constants=args.constants, degrees=args.degree_distribution) env = BuchbergerEnv(ideal_gen, elimination=args.elimination, rewards=args.rewards) env = LeadMonomialsWrapper(env, k=args.k) else: ideal_gen = RandomBinomialIdealGenerator( args.variables, args.degree, args.generators, constants=args.constants, degrees=args.degree_distribution, homogeneous=args.homogeneous, pure=args.pure) env = BuchbergerEnv(ideal_gen, elimination=args.elimination, rewards=args.rewards) env = LeadMonomialsWrapper(env, k=args.k) return env
def test_episode_1(e, reward): R, a, b, c, d = sp.ring('a,b,c,d', sp.FF(32003), 'grevlex') F = [ a + b + c + d, a * b + b * c + c * d + d * a, a * b * c + b * c * d + c * d * a + d * a * b, a * b * c * d - 1 ] ideal_gen = FixedIdealGenerator(F) env = BuchbergerEnv(ideal_gen, elimination=e, rewards='reductions') agent = BuchbergerAgent(selection=['normal', 'first']) assert run_episode(agent, env) == reward
def test_episode_0(s): R, a, b, c, d, e = sp.ring('a,b,c,d,e', sp.FF(32003), 'grevlex') F = [ a + 2 * b + 2 * c + 2 * d + 2 * e - 1, a**2 + 2 * b**2 + 2 * c**2 + 2 * d**2 + 2 * e**2 - a, 2 * a * b + 2 * b * c + 2 * c * d + 2 * d * e - b, b**2 + 2 * a * c + 2 * b * d + 2 * c * e - c, 2 * b * c + 2 * a * d + 2 * b * e - d ] ideal_gen = FixedIdealGenerator(F) env = BuchbergerEnv(ideal_gen, rewards='reductions') agent = BuchbergerAgent(selection=s) assert run_episode(agent, env) == -28
def test_pynumber(): a = sympy.FF(7)(3) b = sympify(a) assert isinstance(b, PyNumber) a = a + 1 b = b + 1 assert isinstance(b, PyNumber) assert b == a # Check equality via SymEngine assert a == b # Check equality via SymPy assert str(a) == str(b) a = 1 - a b = 1 - b assert isinstance(b, PyNumber) assert b == a # Check equality via SymEngine assert a == b # Check equality via SymPy a = 2 * a b = 2 * b assert isinstance(b, PyNumber) assert b == a # Check equality via SymEngine assert a == b # Check equality via SymPy if sympy.__version__ != '1.2': a = 2 / a b = 2 / b assert isinstance(b, PyNumber) assert b == a # Check equality via SymEngine assert a == b # Check equality via SymPy x = Symbol("x") b = x * sympy.FF(7)(3) assert isinstance(b, Mul) b = b / x assert isinstance(b, PyNumber)
def __init__(self, n, d, s, lam, coefficient_ring=sp.FF(32003), order='grevlex', constants=False, degrees='uniform'): self.ring = sp.xring('x:' + str(n), coefficient_ring, order)[0] self.dist = self._make_dist(n, d, constants, degrees) self.lam = lam self.generators = s self.bases = [basis(self.ring, i) for i in range(d + 1)]
def test_LeadMonomialsEnv_1(): R, x, y, z = sp.ring('x,y,z', sp.FF(101), 'grevlex') F = [y - x**2, z - x**3] ideal_gen = FixedIdealGenerator(F) env = LeadMonomialsEnv(ideal_gen) state = env.reset() assert np.array_equal(state, np.array([[2, 0, 0, 3, 0, 0]])) state, _, done, _ = env.step(0) assert np.array_equal(state, np.array([[2, 0, 0, 1, 1, 0]])) assert not done state, _, done, _ = env.step(0) assert np.array_equal(state, np.array([[1, 1, 0, 0, 2, 0]])) assert not done state, _, done, _ = env.step(0) assert done
def test_RandomBinomialIdealGenerator_3(): np.random.seed(10) ideal_gen = RandomBinomialIdealGenerator(5, 10, 5, degrees='uniform', homogeneous=True, coefficient_ring=sp.FF(101)) x0, x1, x2, x3, x4 = ideal_gen.ring.gens I = [ x0**5 * x1**3 + 10 * x0 * x2 * x3 * x4**5, x1**8 * x2 + 29 * x0**7 * x1 * x4, x0**4 * x1**2 * x2 + 74 * x0**3 * x1**2 * x2 * x3, x0**4 * x3**3 * x4**2 + 12 * x0 * x1**4 * x3 * x4**3, x1 * x3**3 + 63 * x1 * x2 * x3 * x4 ] assert next(ideal_gen) == I
def test_LeadMonomialsEnv_0(): R, x, y, z = sp.ring('x,y,z', sp.FF(101), 'grevlex') F = [y - x**2, z - x**3] ideal_gen = FixedIdealGenerator(F) env = LeadMonomialsEnv(ideal_gen, elimination='none') state = env.reset() assert np.array_equal(state, np.array([[2, 0, 0, 3, 0, 0]])) state, _, done, _ = env.step(0) assert (np.array_equal(state, np.array([[2, 0, 0, 1, 1, 0], [3, 0, 0, 1, 1, 0]])) or np.array_equal( state, np.array([[3, 0, 0, 1, 1, 0], [2, 0, 0, 1, 1, 0]]))) assert not done action = 0 if np.array_equal(state[0], np.array([3, 0, 0, 1, 1, 0])) else 1 state, _, done, _ = env.step(action) assert np.array_equal(state, np.array([[2, 0, 0, 1, 1, 0]])) assert not done for _ in range(4): state, _, done, _ = env.step(0) assert done
def __init__(self, n=3, d=20, s=10, lam=0.5, dist='uniform', constants=False, homogeneous=False, coefficient_ring=sp.FF(32003), order='grevlex'): ring = sp.xring('x:' + str(n), coefficient_ring, order)[0] self.s = s self.lam = lam self.homogeneous = homogeneous self.bases = [basis(ring, i) for i in range(d + 1)] self.rng = np.random.default_rng() self.degree_dist = degree_distribution(ring, d, dist=dist, constants=constants) self.ring = ring self.P = ring.domain.characteristic()
def __init__(self, n, ds, ss, coefficient_ring=sp.FF(32003), order='grevlex', constants=False, degrees='uniform', homogeneous=False, pure=False): self.gens = [ RandomBinomialIdealGenerator(n, d, s, coefficient_ring=coefficient_ring, order=order, constants=constants, degrees=degrees, homogeneous=homogeneous, pure=pure) for d in ds for s in ss ] self.current_gen = np.random.choice(self.gens)
"""Tests for ideal generators.""" import numpy as np import pytest import sympy as sp from deepgroebner.ideals import * R1, x, y, z = sp.ring('x,y,z', sp.FF(32003), 'grevlex') R2, a, b, c, d = sp.ring('a,b,c,d', sp.QQ, 'lex') R3, t, u, v = sp.ring('t,u,v', sp.FF(101), 'grlex') def test_cyclic(): F = cyclic(3) x, y, z = F[0].ring.gens assert F == [x + y + z, x * y + y * z + z * x, x * y * z - 1] @pytest.mark.parametrize("ring, d, B", [ (R1, 0, [R1.one]), (R2, 1, [a, b, c, d]), (R1, 2, [x**2, x * y, x * z, y**2, y * z, z**2]), (R3, 3, [ t**3, t**2 * u, t**2 * v, t * u**2, t * u * v, t * v**2, u**3, u**2 * v, u * v**2, v**3 ]), ]) def test_basis(ring, d, B): assert set(basis(ring, d)) == set(B)
def parse_to_sympy(col): """Return list of SymPy ideals from the ideal column in df.""" ideals = col.str.replace('|', ',').str.replace('{', '[').str.replace( '}', ']').str.replace('^', '**') return [eval(ideal) for ideal in ideals] def parse_to_numpy(col): """Return list of NumPy arrays from the ideal column in df.""" ideals = parse_to_sympy(col) return np.array([[sum(f.monoms(), ()) for f in ideal] for ideal in ideals]) if __name__ == "__main__": if len(sys.argv) == 1: print("Usage: python scripts/make_array.py <distribution>") sys.exit() dist = sys.argv[1] df = pd.read_csv(f"data/stats/{dist}/{dist}.csv") n = int(dist.split('-')[0]) letters = [chr(i) for i in range(ord('a'), ord('a') + n)] R, *variables = sp.ring(",".join(letters), sp.FF(32003), 'grevlex') for i in range(len(letters)): exec(f"{letters[i]} = variables[{i}]") X = parse_to_numpy(df.Ideal) np.save(f"data/stats/{dist}/{dist}.npy", X)