Beispiel #1
0
def get_small_world() -> World:
    mat_ground = Lambertian(Color(.8, .8, .3))
    ground = Sphere(Vector(0, -100.5, -1), 100, mat_ground)

    mat_center = Lambertian(Color(.7, .3, .3))
    center = Sphere(Vector(0, 0, -1), .5, mat_center)

    mat_left = Metal(Color(.8, .8, .8))
    left = Sphere(Vector(-1, 0, -1), .5, mat_left)

    mat_right = Dielectric(1.5)
    right = Sphere(Vector(1, 0, -1), .5, mat_right)

    return World([ground, center, left, right])
Beispiel #2
0
def get_world() -> World:
    world = []
    glass = Dielectric(1.5)

    # Ground
    ground_mat = Lambertian(Color(0.5, 0.5, 0.5))
    world.append(Sphere(Vector(0, -1000, 0), 1000, ground_mat))

    # Marbles
    for x in range(-11, 11):
        for z in range(-11, 11):
            choose_material = random()
            material: Material

            center = Vector(x + .9 * random(), .2, z + .9 * random())

            if (center - Vector(4, .2, 0)).norm() <= .9:
                continue

            if choose_material < .8:
                # Diffuse
                albedo = Color(*(random(3) * random(3)))
                material = Lambertian(albedo)
            elif choose_material < .95:
                # Metal
                albedo = Color(*(random(3) * .5 + .5))
                fuzz = random() * .4
                material = Metal(albedo, fuzziness=fuzz)
            else:
                # Glass
                material = glass

            world.append(Sphere(center, .2, material))

    # Big Spheres
    world.append(Sphere(Vector(4, 1, 0),  1.0, glass))
    world.append(Sphere(Vector(-4, 1, 0), 1.0, Lambertian(Color(.6, .1, .1))))
    world.append(Sphere(Vector(0, 1, 0),  1.0, Metal(Color(.7, .6, .5))))

    return World(world)
Beispiel #3
0
def get_colors():
    checkpointing = True
    checkpoint_filename = "checkpoints/image_in_progress.npy"
    world_filename = "checkpoints/world.pickle"

    aspect_ratio = 1.6
    image_height = 600
    image_width = int(aspect_ratio * image_height)

    samples_per_pixel = 8
    alias_block_size = np.ceil(np.sqrt(samples_per_pixel))
    max_depth = 15

    look_from, look_at = Vector(13, 2, 3), Vector(0, 0, 0)
    focus_dist = 10
    camera = Camera(image_width, image_height,
                    look_from, look_at,
                    vfov=20, aperture_width=.1, focus_dist=focus_dist)

    if checkpointing and os.path.exists(checkpoint_filename):
        colors = np.load(checkpoint_filename)
    else:
        colors = np.ones((image_width, image_height, 3)) * -1

    if checkpointing:
        if os.path.exists(world_filename):
            with open(world_filename, 'rb') as f:
                world = World(pickle.load(f))
        else:
            world = get_world()
            with open(world_filename, 'wb') as f:
                pickle.dump(world.items, f)
    else:
        world = get_world()

    for j in tqdm(range(image_height)):
        for i in tqdm(range(image_width)):

            if colors[i, j, 0] != -1:
                continue

            color = Vector(0, 0, 0)
            for offset in range(samples_per_pixel):
                dx = (offset % alias_block_size) / alias_block_size
                dy = (offset / alias_block_size) / samples_per_pixel

                horizontal_component = (i + dx) / (image_width - 1)
                vertical_component = (j + dy) / (image_height - 1)
                ray = camera.get_ray(horizontal_component, vertical_component)
                color += get_color(ray, world, max_depth)

            color = clamp(color, samples_per_pixel)
            colors[i, j, :] = color.arr

        if checkpointing:
            np.save(checkpoint_filename, colors)

    if checkpointing:
        os.remove(checkpoint_filename)
        os.remove(world_filename)
    return colors
Beispiel #4
0
                '''
                self.path = np.vstack((djk1.path, djk2.path))
                self.smoothed_path = self.smoothing(self.path)
                # '''

                self.path_list.append(self.smoothed_path)

        if verbose:
            t2 = time.time()
            print('Generate multiple paths: {}\n'.format(t2 - t1))


if __name__ == '__main__':

    # --- world class
    world = World()
    world.generate_frame([-pi, pi], [-pi, pi])

    # --- Set start/goal point
    world.start = np.array([-3.0, -3.0])
    world.goal = np.array([3.0, 3.0])

    # --- Set objects
    og = ObjectGenerator(world)
    og.generate_object_sample1()
    og.set_object_type()

    # '''
    # --- Generate 10x10 valid roadmaps
    t0 = time.time()
    prm_list = [PRM(world, 100, 5) for i in range(10)]
            if self.best_ind.fitness <= self.fitness_thresh:
                print('The best fitness reaches the threshold value.')
                break

            if g >= 5:
                diff = np.abs(self.history[-5][1] - self.history[-1][1])
                if diff < 1e-2:
                    print('The best fitness does not change for 10 steps.')
                    break

            self.gtot += 1


if __name__ == '__main__':
    # World
    world = World()
    world.generate_frame([-10, 10], [-10, 10])
    world.type = 'cartesian'

    # Objects in the cartesian space
    og = ObjectGenerator(world)
    og.generate_object([[3.5, 6.5], [3.5, 3.5], [6.5, 3.5], [6.5, 6.5]])

    # Robot
    robot2R = RobotArm(base=[0, 0], lengths=[3.0, 5.0])
    robot2R.start = np.array([pi / 2, -pi / 7])
    robot2R.goal = np.array([pi / 5, -pi / 7])

    # C-space
    cspace = World()
    cspace.robot = robot2R
            if g >= 5:
                diff = np.abs(self.history[-5][1] - self.history[-1][1])
                if diff < 1e-2:
                    print('The best fitness does not change for 10 steps.')
                    break

            self.gtot += 1


if __name__ == '__main__':
    '''
    World setting
    '''

    # World
    world = World()
    world.generate_frame([-pi, pi], [-pi, pi])
    world.type = 'cartesian'

    # Objects in the cartesian space
    og = ObjectGenerator(world)
    og.generate_object_sample1()

    # --- Set start/goal point
    world.start = np.array([-pi / 2, -pi / 2]) * 1.9
    world.goal = np.array([pi / 2, pi / 2]) * 1.9
    '''
    Functions
    '''
    NGEN = 1000
    n_ind = 100
Beispiel #7
0
'''
Parameter setting
'''
n_ind = 100   # The number of individuals in a population.
n_elite = 10
NGEN = 1000   # The number of generation loop.
fitness_thresh = 0.1
verbose = True
# random.seed(64)

'''
World setting
'''

# --- world class
world = World()

# --- Set frame
world.frame = np.array(
    [[-pi, -pi],
     [-pi, pi],
     [pi, pi],
     [pi, -pi],
     [-pi, -pi]])

# --- Set start/goal point
world.start = np.array([-pi / 2, -pi / 2]) * 1.9
world.goal = np.array([pi / 2, pi / 2]) * 1.9

# --- Generate objects
og = ObjectGenerator(world)
from math import pi
import seaborn as sns
import matplotlib.pyplot as plt

from geometry import World
from geometry import ObjectGenerator
from viewer import GraphDrawer
from collision_checker import CollisionChecker
sns.set()

'''
World setting
'''

# --- world class
world = World()

# --- Set frame
world.frame = np.array(
    [[-pi, -pi],
     [-pi, pi],
     [pi, pi],
     [pi, -pi],
     [-pi, -pi]])

# --- Set start/goal point
world.start = np.array([-pi / 2, -pi / 2]) * 1.9
world.goal = np.array([pi / 2, pi / 2]) * 1.9

# --- Generate objects
og = ObjectGenerator(world)
Beispiel #9
0
import matplotlib.pyplot as plt

from geometry import World
from geometry import ObjectGenerator
from viewer import GraphDrawer
from dijkstra import Dijkstra
from collision_checker import CollisionChecker
import post_process

sns.set()
'''
World setting
'''

# --- world class
world = World()

# --- Set frame
world.frame = np.array([[-pi, -pi], [-pi, pi], [pi, pi], [pi, -pi], [-pi,
                                                                     -pi]])

# --- Set start/goal point
world.start = np.array([-pi / 2, -pi / 2]) * 1.9
world.goal = np.array([pi / 2, pi / 2]) * 1.9

# --- Generate objects
og = ObjectGenerator(world)
world.objects = og.example()

# --- CollisionChecker
cc = CollisionChecker(world)
            if not path_is_valid:
                count_failure += 1

                if count_failure > 1000:
                    print('fail')
                    break

        return self.path_list


# In[]:
if __name__ == '__main__':

    # --- world class
    world = World()

    # --- Set frame and objects
    og = ObjectGenerator(world)
    og.generate_frame([-pi, pi], [-pi, pi])
    og.generate_object_sample1()
    og.set_object_type()

    # world_margin = world.mcopy(rate=1.05)
    world.update_objects([[0.5, 0.0], [-0.5, 0.0]])

    # --- Set start/goal point
    world.start = np.array([-3.0, -3.0])
    world.goal = np.array([3.0, 3.0])

    cd = CellDecomposition(world)
            if self.best_ind.fitness <= self.fitness_thresh:
                print('The best fitness reaches the threshold value.')
                break

            if g >= 100:
                diff = np.abs(self.history[-100][1] - self.history[-1][1])
                if diff < 1e-2:
                    print('The best fitness does not change for 10 steps.')
                    break

            self.gtot += 1


if __name__ == '__main__':
    # Set world
    world = World()
    world.generate_frame([-2, 10], [-2, 10])
    world.type = 'cartesian'

    # Objects in the cartesian space
    og = ObjectGenerator(world)
    og.generate_object([[4.5, 5.5], [4.5, 4.5], [5.5, 4.5], [5.5, 5.5]])
    og.generate_object([[5.0, 4.5], [5.0, 0.0], [5.2, 0.0], [5.2, 4.5]])
    world_margin = world.mcopy(rate=1.05)

    # Robot
    robot_parameter = {
        'base': [0, 0],
        'length': [3.0, 5.0],
        'mass': [None, None],
        'I': [None, None],
Beispiel #12
0
# np.random.seed(1)
'''
Parameter setting
'''
n_ind = 100  # The number of individuals in a population.
n_elite = 10
NGEN = 100  # The number of generation loop.
fitness_thresh = 0.1
verbose = True
# random.seed(64)
'''
World setting
'''

# --- world class
world = World()
world.generate_frame([-pi, pi], [-pi, pi])
world.type = 'cartesian'

# Objects in the cartesian space
og = ObjectGenerator(world)
world.objects = og.generate_object_sample1()

# --- Set start/goal point
world.start = np.array([-pi / 2, -pi / 2]) * 1.9
world.goal = np.array([pi / 2, pi / 2]) * 1.9

# --- Generte Collision Checker
cc = CollisionChecker(world)
'''
Functions