def test_find_random_spots(self):
        walls = extrude(polygon=random_rectangle(delta_x_min=10,
                                                 delta_y_min=10),
                        extrude_boundaries=True,
                        height=2,
                        thickness=0.1)
        walls.pose = [0, 0, 1, 0, 0, 0]
        world = World()
        world.add_model(tag='walls', model=walls)

        self.assertEqual(world.n_models, 1)

        box_model = box(size=[0.05, 0.05, 0.05], pose=[0, 0, 0.025, 0, 0, 0])

        n_spots = random.randint(2, 5)
        poses, free_space_polygon = \
            world.get_random_free_spots(
                box_model,
                n_spots=n_spots,
                ground_plane_models=['walls'],
                dofs=['x', 'y', 'yaw'],
                return_free_polygon=True
            )

        self.assertEqual(len(poses), n_spots)
        self.assertIsNotNone(free_space_polygon)

        self.assertIsNotNone(free_space_polygon)
        self.assertGreater(free_space_polygon.area, 0)
    def test_roulette_picker_no_max_num(self):
        if sys.version_info.major == 2:
            return
        max_num = dict()
        fitness = dict()
        counter = dict()
        for i in range(5):
            name = generate_random_string(5)
            max_num[name] = None
            fitness[name] = random.randint(1, 10)
            counter[name] = 0

        picker = create_picker(tag='roulette',
                               items=list(max_num.keys()),
                               max_num=max_num,
                               fitness=fitness)
        self.assertIsNotNone(picker)

        total_items = 100

        for _ in range(total_items):
            output = picker.get_selection()
            self.assertIsNotNone(output)
            self.assertIn(output, counter.keys())
            counter[output] += 1
    def test_random_picker(self):
        if sys.version_info.major == 2:
            return
        max_num = dict()
        counter = dict()
        total_items = 0
        for i in range(5):
            name = generate_random_string(5)
            max_num[name] = random.randint(1, 10)
            counter[name] = 0

            total_items += max_num[name]

        picker = create_picker(tag='random',
                               items=list(max_num.keys()),
                               max_num=max_num)
        self.assertIsNotNone(picker)

        output = picker.get_selection()

        while output is not None:
            self.assertIsNotNone(output)
            self.assertIn(output, counter.keys())
            counter[output] += 1

            temp_counter = 0
            for tag in counter:
                temp_counter += counter[tag]
            self.assertLessEqual(temp_counter, total_items)
            output = picker.get_selection()

        temp_counter = 0
        for tag in counter:
            temp_counter += counter[tag]
        self.assertEqual(temp_counter, total_items)
Ejemplo n.º 4
0
    def test_init_from_world_sdf(self):
        generator = WorldGenerator(
            world_file=os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                    'worlds', 'test_pcg_example.world'))
        self.assertIsNotNone(generator.world)
        self.assertIsNotNone(generator.world.n_models, 2)
        self.assertIsNotNone(generator.world.n_lights, 1)
        self.assertIn('box', generator.world.models)
        self.assertIn('ground_plane', generator.world.models)
        self.assertIn('sun', generator.world.lights)

        box_name = 'box_' + generate_random_string(5)
        random_box = dict(type='box',
                          args=dict(
                              size="__import__('pcg_gazebo').random.rand(3)",
                              mass="__import__('pcg_gazebo').random.rand()",
                              name=box_name))
        self.assertTrue(
            generator.add_asset(random_box, tag=box_name, type='factory'))

        workspace_name = generate_random_string(5)
        self.assertTrue(
            generator.add_constraint(name=workspace_name,
                                     type='workspace',
                                     geometry_type='circle',
                                     radius=100,
                                     center=[0, 0]))

        num_models = dict()
        num_models[box_name] = random.randint(2, 5)

        engine_name = generate_random_string(5)
        self.assertTrue(
            generator.add_engine(tag=engine_name,
                                 engine_name='random_pose',
                                 models=list(num_models.keys()),
                                 model_picker='random',
                                 no_collision=True,
                                 max_num=num_models,
                                 policies=[
                                     dict(models=list(num_models.keys()),
                                          config=[
                                              dict(dofs=['x', 'y'],
                                                   tag='workspace',
                                                   workspace=workspace_name)
                                          ])
                                 ]))

        generator.run_engines(attach_models=True)

        self.assertEqual(generator.world.n_models, num_models[box_name] + 2)
        self.assertIn('box', generator.world.models)
        self.assertIn('ground_plane', generator.world.models)
        self.assertIn('sun', generator.world.lights)
    def test_seed(self):
        seeds = [random.randint(100) for _ in range(3)]

        for seed in seeds:
            random.init_random_state(seed)
            self.assertEqual(random.PCG_RANDOM_SEED, seed)

            # Test rand
            random.init_random_state(seed)
            ref = [random.rand() for _ in range(5)]

            for _ in range(3):
                random.init_random_state(seed)
                self.assertEqual(random.PCG_RANDOM_SEED, seed)
                self.assertEqual(ref, [random.rand() for _ in range(5)])

            random.init_random_state(seed)
            self.assertEqual(random.PCG_RANDOM_SEED, seed)

            # Test randint
            ref = [random.randint(10) for _ in range(5)]

            for _ in range(3):
                random.init_random_state(seed)
                self.assertEqual(random.PCG_RANDOM_SEED, seed)
                self.assertEqual(ref, [random.randint(10) for _ in range(5)])

            # Test choice
            values = [random.rand() for _ in range(10)]
            random.init_random_state(seed)
            ref = [random.choice(values) for _ in range(5)]
            for _ in range(3):
                random.init_random_state(seed)
                self.assertEqual(random.PCG_RANDOM_SEED, seed)
                self.assertEqual(ref,
                                 [random.choice(values) for _ in range(5)])
 def test_randint(self):
     # Test default output
     output = random.randint(5)
     self.assertTrue(is_integer(output))
     self.assertGreaterEqual(output, 0)
     self.assertLessEqual(output, 5)
Ejemplo n.º 7
0
    def test_generator_seed(self):
        if sys.version_info[0] < 3:
            return
        generator = ModelGroupGenerator()
        # Set random generator's seed
        generator.seed = np.random.randint(low=0, high=10000)

        box_name = 'box_' + generate_random_string(5)
        random_box = dict(type='box',
                          args=dict(
                              size="__import__('pcg_gazebo').random.rand(3)",
                              mass="__import__('pcg_gazebo').random.rand()",
                              name=box_name))
        self.assertTrue(
            generator.add_asset(random_box, tag=box_name, type='factory'))

        cylinder_name = 'cyl_' + generate_random_string(5)
        random_cyl = dict(type='cylinder',
                          args=dict(
                              radius="__import__('pcg_gazebo').random.rand()",
                              length="__import__('pcg_gazebo').random.rand()",
                              mass="__import__('pcg_gazebo').random.rand()",
                              name=cylinder_name))
        self.assertTrue(generator.add_asset(random_cyl, tag=cylinder_name))

        sphere_name = 'sphere_' + generate_random_string(5)
        random_sphere = dict(
            type='sphere',
            args=dict(radius="__import__('pcg_gazebo').random.rand()",
                      mass="__import__('pcg_gazebo').random.rand()",
                      name=sphere_name))
        self.assertTrue(generator.add_asset(random_sphere, tag=sphere_name))

        workspace_name = generate_random_string(5)
        self.assertTrue(
            generator.add_constraint(name=workspace_name,
                                     type='workspace',
                                     geometry_type='circle',
                                     radius=100,
                                     center=[0, 0]))

        num_models = dict()
        num_models[box_name] = random.randint(1, 3)
        num_models[cylinder_name] = random.randint(1, 3)
        num_models[sphere_name] = random.randint(1, 3)

        total_num_models = 0
        for tag in num_models:
            total_num_models += num_models[tag]

        engine_name = generate_random_string(5)
        self.assertTrue(
            generator.add_engine(
                tag=engine_name,
                engine_name='random_pose',
                models=list(num_models.keys()),
                model_picker='random',
                no_collision=True,
                max_num=num_models,
                policies=[
                    dict(models=list(num_models.keys()),
                         config=[
                             dict(dofs=['x', 'y'],
                                  tag='workspace',
                                  workspace=workspace_name),
                             dict(dofs=['roll', 'pitch', 'yaw'],
                                  tag='uniform',
                                  min=-2 * np.pi,
                                  max=2 * np.pi)
                         ])
                ]))

        ref = generator.run('test')
        self.assertIsNotNone(ref)
        self.assertEqual(ref.n_models, total_num_models)

        for _ in range(3):
            model = generator.run('test')
            self.assertIsNotNone(model)
            self.assertEqual(ref.to_sdf('model'), model.to_sdf('model'))
Ejemplo n.º 8
0
    def test_multiple_engines(self):
        generator = WorldGenerator()
        box_name = 'box_' + generate_random_string(5)

        random_box = dict(type='box',
                          args=dict(
                              size="__import__('pcg_gazebo').random.rand(3)",
                              mass="__import__('pcg_gazebo').random.rand()",
                              name=box_name))
        self.assertTrue(
            generator.add_asset(random_box, tag=box_name, type='factory'))
        self.assertIn(box_name, generator.assets.tags)

        cyl_name = 'cyl_' + generate_random_string(5)
        random_cyl = dict(type='cylinder',
                          args=dict(
                              radius="__import__('pcg_gazebo').random.rand()",
                              length="__import__('pcg_gazebo').random.rand()",
                              name=cyl_name))
        self.assertTrue(
            generator.add_asset(random_cyl, tag=cyl_name, type='factory'))
        self.assertIn(cyl_name, generator.assets.tags)

        sphere_name = 'sphere_' + generate_random_string(5)
        random_sphere = dict(
            type='sphere',
            args=dict(radius="__import__('pcg_gazebo').random.rand()",
                      name=sphere_name))
        self.assertTrue(
            generator.add_asset(random_sphere, tag=sphere_name,
                                type='factory'))
        self.assertIn(sphere_name, generator.assets.tags)

        workspace_name = generate_random_string(5)
        self.assertTrue(
            generator.add_constraint(name=workspace_name,
                                     type='workspace',
                                     geometry_type='circle',
                                     radius=100,
                                     center=[0, 0]))

        models = [box_name, cyl_name, sphere_name]
        total_models = 0
        for item in models:
            max_num = dict()
            max_num[item] = random.randint(1, 3)
            total_models += max_num[item]
            self.assertTrue(
                generator.add_engine(tag=item + '_engine',
                                     engine_name='random_pose',
                                     models=[item],
                                     model_picker='random',
                                     no_collision=True,
                                     max_num=max_num,
                                     policies=[
                                         dict(models=[item],
                                              config=[
                                                  dict(
                                                      dofs=['x', 'y'],
                                                      tag='workspace',
                                                      workspace=workspace_name)
                                              ])
                                     ]))
        self.assertTrue(generator.run_engines())
        self.assertEqual(len(generator.world.models), total_models)