def test_sameType(self):
        n_tests = 0
        total_test = 1
        for i in range(total_test):
            #Agents: Same type.
            #Object: Not on edges.
            #Distance: target_dist steps.

            obj_pos, agent_pos = utils.generate_initial_conditions(
                global_defs.N_TYPES, 2)
            #ipdb.set_trace()
            if True:
                n_tests += 1
                print("-----------Test Iter: {}-------------".format(i))
                with self.subTest(i=i, msg='Experiment {}'.format(i)):

                    a1 = agent_lifter.agent_lifter(agent_pos[0], 2)
                    a2 = agent_lifter.agent_lifter(agent_pos[1], 2)
                    a3 = agent_adhoc.agent_adhoc(a2.pos)

                    env = environment.environment(global_defs.GRID_SIZE,
                                                  obj_pos, False)

                    env.register_agent(a1)
                    env.register_agent(a2)

                    n_steps = 0
                    #print("Target Distance: {}".format(target_dist))
                    #print("Object location {}".format(object_location))
                    print("A1 {}".format(a1.pos))
                    print("A2 {}".format(a2.pos))
                    while (not env.is_terminal):
                        is_terminal, reward = env.step()
                        #ipdb.set_trace()
                        time.sleep(0.1)
                        tp_estimate = a3.respond(env)
                        print("TPESTIMATE STEP", tp_estimate, env.step_count)
                        if env.step_count == 5:
                            a1.tp = 1
                        n_steps += 1
                        #print(n_steps)
                        #print("A1 {}".format(a1.pos))
                        #print("A2 {}".format(a2.pos))
                        #if is_terminal:
                        #print("Reward {}".format(reward))
                    if is_terminal:
                        print("Terminal Reward {}".format(reward))
                        print("N_iters {}".format(n_steps))
Exemple #2
0
    def test_agent_tp_1_respond(self):
        random_pos_array = np.random.randint(
            0, 10, (20, 2))  #Generating 20 random locations
        random_pos_list = [
            global_defs.Point2D(ele[0], ele[1]) for ele in random_pos_array
        ]
        a = agent_lifter.agent_lifter(
            global_defs.Point2D(random_pos_list[0][0], random_pos_list[0][1]),
            1)

        allPos = random_pos_list
        myInd = 0
        loadIndices = range(4, 8)
        random_observation = global_defs.obs(allPos, myInd, loadIndices)

        (action_probs, action_idx) = a.respond(random_observation)

        self.assertTrue(len(action_probs) == 6)
        np.testing.assert_approx_equal(np.sum(action_probs), 1)
        self.assertTrue(action_idx < 6)
Exemple #3
0
from src.agents import agent_adhoc
import random
import numpy as np
from src import global_defs
from src import utils

n_tests = 0

obj_pos, agent_pos = utils.generate_initial_conditions(global_defs.N_TYPES, 2)
i = 0

# ipdb.set_trace()
if True:
    n_tests += 1
    print("-----------Test Iter: {}-------------".format(i))
    a1 = agent_lifter.agent_lifter(agent_pos[0], 2)
    a2 = agent_adhoc.agent_adhoc(agent_pos[1])

    env = environment.environment(global_defs.GRID_SIZE, obj_pos, True)

    env.register_agent(a1)
    env.register_adhoc_agent(a2)

    n_steps = 0
    # print("Target Distance: {}".format(target_dist))
    # print("Object location {}".format(object_location))
    while (not env.is_terminal):
        is_terminal, reward = env.step()
        # ipdb.set_trace()
        time.sleep(0.1)
        last_history = env.history[-1]
    def test_diffType(self):
        n_tests = 0
        total_test = 100
        for i in range(total_test):
            #Agents: Same type.
            #Object: Not on edges.
            #Distance: target_dist steps.

            object_location = np.array(random.sample(range(1, 8), 4)[:2])

            #Put agents target_dist positions away.
            target_dist = 6
            displacement = np.random.randint(-target_dist, target_dist, (2, 2))
            displacement[:, 1] = random.choice(
                [-1, 1]) * (target_dist - np.abs(displacement[:, 0]))

            a1_pos = (object_location[0] + displacement[0][0],
                      object_location[1] + displacement[0][1])
            a2_pos = (object_location[0] + displacement[1][0],
                      object_location[1] + displacement[1][1])

            if not (utils.check_within_boundaries(a1_pos)
                    and utils.check_within_boundaries(a2_pos)):
                continue
            else:
                n_tests += 1
                print("-----------Test Iter: {}-------------".format(i))
                with self.subTest(i=i, msg='Experiment {}'.format(i)):
                    self.assertTrue(
                        np.sum(np.abs(object_location -
                                      a1_pos)) == target_dist)
                    self.assertTrue(
                        np.sum(np.abs(object_location -
                                      a2_pos)) == target_dist)

                    a1 = agent_lifter.agent_lifter(a1_pos, 1)
                    a2 = agent_lifter.agent_lifter(a2_pos, 2)

                    object_location = global_defs.Point2D(
                        object_location[0], object_location[1])

                    env = environment.environment(
                        10, [object_location,
                             global_defs.Point2D(0, 0)], False)
                    env.register_agent(a1)
                    env.register_agent(a2)

                    n_steps = 0
                    print("******** Test 2 **************8")
                    print("Target Distance: {}".format(target_dist))
                    print("Object location {}".format(object_location))

                    while (not env.is_terminal and n_steps < 100):
                        is_terminal, reward = env.step()
                        n_steps += 1

                    if is_terminal:
                        print(n_steps)
                        print("A1 {}".format(a1))
                        print("A2 {}".format(a2))
                        print("Env {}".format(env))
                        print("Reward {} ".format(reward))
                    print("******************************")
                #self.assertTrue(reward==0)
        print("n_tests {} total_tests {}".format(n_tests, total_test))
    def _test_sameType_copy_middleStep(self):
        n_tests = 0
        total_test = 100
        for i in range(total_test):
            #Agents: Same type.
            #Object: Not on edges.
            #Distance: target_dist steps.

            object_location = np.array(random.sample(range(1, 8), 2)[:2])

            #Put agents target_dist positions away.
            target_dist = 6
            displacement = np.random.randint(-target_dist, target_dist, (2, 2))
            displacement[:, 1] = random.choice(
                [-1, 1]) * (target_dist - np.abs(displacement[:, 0]))

            a1_pos = (object_location[0] + displacement[0][0],
                      object_location[1] + displacement[0][1])
            a2_pos = (object_location[0] + displacement[1][0],
                      object_location[1] + displacement[1][1])

            if not (utils.check_within_boundaries(a1_pos)
                    and utils.check_within_boundaries(a2_pos)):
                continue
            else:
                n_tests += 1
                print("-----------Test Iter: {}-------------".format(i))
                with self.subTest(i=i, msg='Experiment {}'.format(i)):
                    self.assertTrue(
                        np.sum(np.abs(object_location -
                                      a1_pos)) == target_dist)
                    self.assertTrue(
                        np.sum(np.abs(object_location -
                                      a2_pos)) == target_dist)

                    a1 = agent_lifter.agent_lifter(a1_pos, 1)
                    a2 = agent_lifter.agent_lifter(a2_pos, 1)

                    object_location = global_defs.Point2D(
                        object_location[0], object_location[1])

                    env = environment.environment(
                        10, [object_location,
                             global_defs.Point2D(0, 0)], False)
                    env.register_agent(a1)
                    env.register_agent(a2)

                    n_steps = 0
                    #print("Target Distance: {}".format(target_dist))
                    #print("Object location {}".format(object_location))
                    print("A1 {}".format(a1.pos))
                    print("A2 {}".format(a2.pos))
                    _, _ = env.step()
                    _, _ = env.step()

                    new_env = env.__copy__()
                    while (not env.is_terminal and n_steps < 10):

                        is_terminal, reward = env.step()
                        is_terminal2, reward2 = new_env.step()

                        self.assertEqual(is_terminal, is_terminal2)
                        self.assertEqual(reward, reward2)

                        s1 = env.__getstate__()
                        s2 = new_env.__getstate__()
                        try:
                            self.assertTrue(utils.compare_env_states(s1, s2))
                        except:
                            pdb.set_trace()

                        n_steps += 1
                        #print(n_steps)
                        #print("A1 {}".format(a1.pos))
                        #print("A2 {}".format(a2.pos))
                        #if is_terminal:
                        #print("Reward {}".format(reward))
                    if is_terminal:
                        print("Terminal Reward {}".format(reward))
                        print("N_iters {}".format(n_steps))
Exemple #6
0
# Distance: target_dist steps.

object_location = np.array(random.sample(range(1, 8), 2)[:2])

# Put agents target_dist positions away.
target_dist = 6
displacement = np.random.randint(-target_dist, target_dist, (2, 2))
displacement[:, 1] = random.choice(
    [-1, 1]) * (target_dist - np.abs(displacement[:, 0]))

a1_pos = (object_location[0] + displacement[0][0],
          object_location[1] + displacement[0][1])
a2_pos = (object_location[0] + displacement[1][0],
          object_location[1] + displacement[1][1])

a1 = agent_lifter.agent_lifter(a1_pos, 1)
a2 = agent_lifter.agent_lifter(a2_pos, 1)

object_location = global_defs.Point2D(object_location[0], object_location[1])

env = environment.environment(
    10, [object_location, global_defs.Point2D(0, 0)], True)
env.register_agent(a1)
env.register_agent(a2)

n_steps = 0
print("A1 {}".format(a1.pos))
print("A2 {}".format(a2.pos))

while (not env.is_terminal and n_steps < 10):