Example #1
0
 def start_sim(self, pb):
     if pb:
         self.sim = pyrosim.Simulator(play_blind=pb, eval_steps=c.eval)
     if pb == False:
         self.sim = pyrosim.Simulator(play_blind=pb, eval_steps=c.eval, play_paused=True)
     self.robot = ROBOT(self.sim, self.genome)
     self.sim.start()
Example #2
0
    def Start_Evaluation(self, env, pb, pp=False):

        #---SIMULATOR---#
        # start the simulator
        self.sim = pyrosim.Simulator(eval_time=c.evalTime,
                                     play_paused=pp,
                                     play_blind=pb,
                                     dt=c.discreteTime)

        #---ROBOTS---#
        # spawn bot
        self.robot = ROBOT(self.sim, self.genome)

        #---ENVIRONMENTS---#
        env.Send_To(self.sim)
        env.Send_Slope(self.sim)

        #---SET COLLISIONS---#
        # here we can assign collisions directly
        # we can also make a predefined collision matrix
        # using sim.create_collision_matrix(collision_type)
        # where collision_type = 'all','none','inter' or 'intra'.
        self.sim.assign_collision(self.robot, env)

        #---SIMULATION---#
        # start the simulation
        self.sim.start()
Example #3
0
 def Evaluate(self):
     sim = pyrosim.Simulator(play_paused=False, eval_time=100)
     robot = ROBOT(sim, self.genome)
     sim.start()
     sim.wait_to_finish()
     sensorData = sim.get_sensor_data(sensor_id=robot.P4, svi=1)
     self.fitness = sensorData[-1]
Example #4
0
    def __init__(self, pb, pp):

        self.envs = {}

        for e in range(c.numEnvs):

            sim = pyrosim.Simulator(play_blind=pb,
                                    play_paused=pp,
                                    eval_time=c.evalTime)

            lightSource_angle = e / c.numEnvs * (2 * math.pi)

            lightSource = sim.send_box(
                x=c.lightSource_dist * math.cos(lightSource_angle),
                y=c.lightSource_dist * math.sin(lightSource_angle),
                z=c.lightSource_size / 2,
                length=c.lightSource_size,
                width=c.lightSource_size,
                height=c.lightSource_size,
                r=1,
                g=1,
                b=1)

            sim.send_light_source(body_id=lightSource)

            self.envs[e] = sim
def evaluate_one(genome, envs, config, fitness_function, show=False, jc=None, cc=None):
    # evaluates one genome in one environment
    # gives possibility of showing
    tree_kwargs = configtools.tree_from_config(config)
    sim_kwargs = configtools.sim_from_config(config)
    envs_kwargs = configtools.envs_from_config(config)
    assert isinstance(envs, list), 'envs must be list'
    env_fitness = np.zeros(len(envs))

    for i, env in enumerate(envs):
        t = treenome.Tree(**tree_kwargs)

        sim = pyrosim.Simulator(play_blind=not show,
                                **sim_kwargs)
        # send tree body and network
        genome.send_to_simulator(sim, t)
        # send environment
        environments.send_to_simulator(sim, env, t, **envs_kwargs)

        sim.start()
        sensor_data = sim.wait_to_finish()
        env_fitness[i] = fitness_function(env, sensor_data, 20)

    # return np.average(env_fitness) - genome.calc_tree_joint_cost(), env_fitness
    return np.mean(env_fitness), env_fitness
 def Start_Evaluation(self, pb):
     self.sim = pyrosim.Simulator(
         play_blind=pb,
         play_paused=False,
         eval_time=100)
     self.robot = ROBOT(self.sim, self.genome)
     self.sim.start()
def simulate(individual, blind, time, height):
    sim = pyrosim.Simulator(eval_time=time,
                            play_blind=blind,
                            debug=False,
                            xyz=[-1.2, -1.2, 1.2],
                            hpr=[50, -35, 0.0],
                            use_textures=True)
    builder = Stairs.StairBuilder(sim, [0.7, 0.7, 0], 0.2)
    builder.build(20, height)

    weight_matrix = np.reshape(individual, MATRIX_SHAPE)
    if ROBOT_TYPE == "Robot":
        robot = Robot.Robot(sim, weight_matrix)
    elif ROBOT_TYPE == "OriginalRobot":
        robot = OriginalRobot.Robot(sim, weight_matrix)
    elif ROBOT_TYPE == "SimpleRobot":
        robot = SimpleRobot.Robot(sim, weight_matrix)
    elif ROBOT_TYPE == "DenseRobot":
        robot = DenseRobot.Robot(sim, weight_matrix)
    fitness_sensor, body = robot.build()
    if not blind:
        sim.film_body(body)
    sim.create_collision_matrix('intra')
    sim.start()
    results = sim.wait_to_finish()

    return min(
        sim.get_sensor_data(fitness_sensor, svi=1)[-1],
        sim.get_sensor_data(fitness_sensor, svi=0)[-1])
Example #8
0
 def Start_Evaluation(self, env, pb=True, pp=True):
     """
     Evaluates an individual over a simulation environment in pyrosim.
     env: Pyrosim simulation env (object of ENVIRONMENT class)
     play_blind   : bool, optional
         If True the simulation runs without graphics (headless) else if
         False the simulation runs with graphics (the default is False)
     play_paused : bool, optional
         If True the simulation starts paused else if False the simulation
         starts running. With simulation window in focus use Ctrl-p to
         toggle pausing the simulation. (the default is False)
     return:None
     """
     self.sim = pyrosim.Simulator(eval_time=c.evalTime, play_blind=pb, play_paused=pp, xyz=[0, 7.5, 0.8], hpr=[270,0,0.0])
     
     # add robot to sim
     self.robot = ROBOT(self.sim,genome = self.genome, hidden_genome = self.hidden_genome, WHEEL_RADIUS = self.WHEEL_RADIUS, SPEED=self.SPEED, MASS=self.MASS)
    
     # add environment to sim
     env.Send_To(sim = self.sim)
     
     # define collisions in sim
     self.sim.assign_collision('ball', 'robot')
     self.sim.assign_collision('goalpost', 'robot')
     self.sim.assign_collision('ball', 'goalpost')
 
     self.sim.start()
     # retrieve the id of the ball position sensor
     self.ball_psensor_id = env.ball_psensor_id
     self.robot_position = self.robot.position
Example #9
0
 def Start_Evaluation(self, env, pp, pb):
     self.sim = pyrosim.Simulator(play_paused=pp,
                                  eval_time=c.evalTime,
                                  play_blind=pb)
     env.Send_To(self.sim)
     self.robot = ROBOT(self.sim, self.genome)
     self.sim.start()
Example #10
0
 def Start_Evaluation(self, env, pp, pb, evalTime):
     self.sim = pyrosim.Simulator(play_paused=pp,
                                  eval_time=evalTime,
                                  play_blind=pb)
     self.robotWhegged = ROBOTWHEGGED(self.sim, self.genome,
                                      self.genomeTest, env)
     env.Send_To(self.sim)
     self.sim.start()
Example #11
0
 def Start_Evaluation(self, env, pp, pb):
     self.sim = pyrosim.Simulator(play_paused=pp,
                                  eval_steps=c.evalTime,
                                  play_blind=pb)
     env.Send_To(self.sim)
     #self.sim.set_camera((-4, -1, 2), (15, -15, 0), tracking = 'none', body_to_track = 0)
     self.robot = ROBOT(self.sim, self.genome)
     self.sim.start()
Example #12
0
 def Start_Evaluation(self, pb, tower_y):
     self.pb = pb
     self.sim = pyrosim.Simulator( play_paused=True, eval_time=c.eval_time, play_blind=pb)
     self.robot = ROBOT(self.sim, self.genome)
     self.tower = TOWER(self.sim, tower_y)
     self.sim.assign_collision("tower", "tower")
     self.sim.assign_collision("tower", "robot")
     self.sim.start()
Example #13
0
 def Evaluate(self, pb):
     sim = pyrosim.Simulator(play_blind=pb)
     robot = ROBOT(sim, self.genome)
     sim.start()
     sim.wait_to_finish()
     x = sim.get_sensor_data(sensor_id=robot.P4, svi=0)
     y = sim.get_sensor_data(sensor_id=robot.P4, svi=1)
     z = sim.get_sensor_data(sensor_id=robot.P4, svi=2)
     self.fitness = y[-1]
Example #14
0
 def Start_Evaluation(self, pb):
     self.sim = pyrosim.Simulator(play_blind=pb,
                                  eval_time=1000,
                                  debug=False,
                                  play_paused=False,
                                  use_textures=True,
                                  xyz=[-25, -50, 0])
     self.robot = ROBOT(self.sim, self.genomehidden, self.genomeoutput)
     self.sim.start()
Example #15
0
 def compute_work(self, serial=False):
     for i in range(10):
       sim = pyrosim.Simulator(eval_steps=COEVOLVE.TIME_STEPS, play_blind=True, play_paused=False, dt=COEVOLVE.DT)
       #print("Simulating aggregate", self.aggregate_key, "with controller", self.controller_key)
       self.fitness = self.aggregate.evaluate(sim, self.controller, idNum=self.keys, debug=False)
       if self.fitness > -1:
         break
       #print("fitness of aggregate", self.aggregate_key, "and controller", self.controller_key, "retrieved")
     if self.fitness < 0: self.fitness = 0
Example #16
0
    def Evaluate(self):
        sim = pyrosim.Simulator(play_paused=False, eval_time=500)
        robot = ROBOT(sim, random.random() * 2 - 1)

        sim.start()
        sim.wait_to_finish()

        #gets x,y,z coord of individual's red cylinder.
        y = sim.get_sensor_data(sensor_id=robot.P4, svi=1)

        self.fitness = y[-1]  #gets last element in y array (last y pos)
Example #17
0
    def Start_Evaluation(self, pb):

        # starts simulation
        self.sim = pyrosim.Simulator(play_paused=False,
                                     eval_time=500,
                                     play_blind=pb)

        # call to function ROBOT which generates the scissor robot
        self.robot = ROBOT(self.sim, self.genome)

        # starts simulator
        self.sim.start()
 def Start_Evaluation(self, env, pp, pb):
     #run simulator without pausing
     self.sim = pyrosim.Simulator(eval_time = c.evalTime, play_blind = pb, play_paused = pp)
     
     env.Send_To( self.sim )
     #create instance of the robot class
     #sends a random value between -1 and 1 for each iteration
     self.robot = Predator(self.sim, self.genome)
     self.collided = self.sim.assign_collision(self.robot, env.source)
     
     
     #start the simulator
     self.sim.start()
    def start_evaluation(self, blind_mode=False):
        '''
        :param blind_mode: Flag to disable the visualization

        Creates an instance of pyrosim and ROBOT
        '''
        self.sim = pyrosim.Simulator(play_paused=False,
                                     eval_time=1000,
                                     window_size=(1900, 1080),
                                     play_blind=blind_mode)
        self.robot = ROBOT(self.sim, self.genome)

        self.sim.start()
Example #20
0
    def playback(self, play_all=False):
        '''
        for review purposes, plays the best aggregate and controller, with play_blind off
        '''

        fit = 0
        eindex = 0
        for j in range(len(self.contrs.p)):
            if abs(self.contrs.p[j].fitness) > abs(fit):
                print("fit")
                print(j)
                fit = self.contrs.p[j].fitness
                eindex = j

        if play_all:
            for a in self.aggrs.p:
                aggr = a
                elmt = self.contrs.p[eindex]
                sim = pyrosim.Simulator(eval_steps=1000, play_blind=False, play_paused=False, dt=.01)
                try:
                    print(aggr.evaluate(sim, elmt, debug=True))
                except Exception as e:
                    print("error")
                    pass
        else:
            
            fit = 99999
            aindex = -1
            elmt = self.contrs.p[eindex]
            
            for i in range(len(elmt.scores)):
                if elmt.scores[i] < fit:
                    fit = elmt.scores[i]
                    aindex = i

            aggr = self.aggrs.p[aindex]
            print(aindex, eindex)
            sim = pyrosim.Simulator(eval_steps=1000, play_blind=False, play_paused=True, dt=.01, use_textures=True)
            aggr.evaluate(sim, elmt, debug=False)
Example #21
0
    def Start_Evaluation(self, pp, pb):
        #run simulator without pausing
        self.sim = pyrosim.Simulator(eval_time=c.evalTime,
                                     play_blind=pb,
                                     play_paused=pp)

        #create instance of the robot class
        #sends a random value between -1 and 1 for each iteration
        self.predator = Predator(self.sim, self.predatorGenome)
        self.prey = Prey(self.sim, self.preyGenome)

        #start the simulator
        self.sim.start()
Example #22
0
    def Start_Evaluation(self, env, pp, pb):

        # starts simulation
        self.sim = pyrosim.Simulator(play_paused=pp,
                                     eval_time=c.evalTime,
                                     play_blind=pb)

        # call to function ROBOT which generates the scissor robot
        self.robot = ROBOT(self.sim, self.genome)

        env.Send_To(self.sim)

        # starts simulator
        self.sim.start()
Example #23
0
 def non_MPI_exhaustive(self):
     """
     non-parallel exhaustive evaluation
     """
     
     for j in range(len(self.contrs.p)):
         contr = self.contrs.p[j]
         for i in range(len(self.aggrs.p)):
             aggr = self.aggrs.p[i]
             sim = pyrosim.Simulator(eval_steps=COEVOLVE.TIME_STEPS, play_blind=True, play_paused=False, dt=COEVOLVE.DT)
             fit = aggr.evaluate(sim, self.contrs.p[j], idNum = [i,j,0])
             self.contrs.p[j].scores.append(fit)
             
     self.calculate_fitness()
Example #24
0
    def evaluate(self, genomes, play_blind=True, play_paused=False):
        robots = [
            Robot(weights=genomes[i, 1:], **self.kwargs)
            for i in range(len(genomes))
        ]
        envs = [
            SpatialScaffoldingStairsEnv(temp=genomes[i, 0], **self.kwargs)
            for i in range(len(genomes))
        ]
        fitnesses = np.zeros(len(genomes))  # fitnesses

        # process simulations in batches
        batch_size = len(
            genomes) if self.max_parallel is None else self.max_parallel
        for start_i in range(0, len(genomes), batch_size):
            end_i = min(start_i + batch_size, len(genomes))
            batch_robots = robots[start_i:end_i]
            batch_env = envs[start_i:end_i]
            batch_sims = [
                pyrosim.Simulator(play_paused=play_paused,
                                  eval_time=self.eval_time,
                                  play_blind=play_blind,
                                  dt=0.025)  # default dt=0.05
                for i in range(start_i, end_i)
            ]

            # start a batch of simulations
            for robot, env, sim in zip(batch_robots, batch_env, batch_sims):
                env.send_to(sim)
                robot.send_to(sim)
                sim.assign_collision(robot.group, env.group)
                sim.start()

            for j, (robot, env,
                    sim) in enumerate(zip(batch_robots, batch_env,
                                          batch_sims)):
                sim.wait_to_finish()
                # distance to trophy fitness
                x_pos = sim.get_sensor_data(sensor_id=robot.p4, svi=0)[-1]
                y_pos = sim.get_sensor_data(sensor_id=robot.p4, svi=1)[-1]
                z_pos = sim.get_sensor_data(sensor_id=robot.p4, svi=2)[-1]
                robot_pos = np.array([x_pos, y_pos, z_pos])
                goal_pos = np.array(env.trophy_pos)
                dist = np.sqrt(
                    np.dot(robot_pos - goal_pos, robot_pos - goal_pos))
                fitness = 1 / (dist + 1)
                fitnesses[start_i + j] = fitness

        return fitnesses
Example #25
0
    def Start_Evaluation(self, env, pp, pb):
        self.sim = pyrosim.Simulator(play_paused=pp,
                                     play_blind=pb,
                                     eval_time=c.evalTime)
        # self.sim.make_movie(movie_name= '/home/iskander/PycharmProjects/EvoBotProject/genomes/a.mpg')

        self.robot = ROBOT(self.sim, self.genome)

        env.SendTo(self.sim)
        env.SendID(self.sim, self.ID)

        # print('num bodies', self.sim.get_num_bodies())

        self.sim.assign_collision('obstacles', 'robot')
        self.sim.assign_collision('obstacles', 'supports')
        self.sim.start()
Example #26
0
	def Start_Evaluation(self, env, pb, pp = False):
		
		#---SIMULATOR---#
		# start the simulator
		self.sim = pyrosim.Simulator(eval_time = c.evalTime, play_paused = pp, play_blind = pb)

		#---ROBOTS---#
		# spawn bot
		self.robot = ROBOT(self.sim, self.genome)
		
		#---ENVIRONMENTS---#
		env.Send_To(self.sim)
		
		#---SIMULATION---#
		# start the simulation
		self.sim.start()
Example #27
0
    def Start_Evaluation(self, pb):

        # create a Pyrosim simulator
        self.sim = pyrosim.Simulator(play_paused=False,
                                     eval_time=1500,
                                     play_blind=pb,
                                     xyz=[1.0, -2.6, 1.5],
                                     window_size=(415, 280),
                                     hpr=[110, -18, 0])

        # create a robot inside the simulator called sim
        self.robot = ROBOT(
            self.sim, self.genome, self.A, self.B,
            self.leg)  # create a class instance of the ROBOT class

        # start the simulation
        self.sim.start()
Example #28
0
    def evaluate(self):
        sim = pyrosim.Simulator(play_paused=False,
                                eval_time=500,
                                window_size=(1900, 1080))
        robot = ROBOT(sim, self.genome)

        # Starts simulation
        sim.start()
        sim.wait_to_finish()

        # Position sensor returns [x,y,z] coordinates
        # svi 0 -> x
        # positionData_x = sim.get_sensor_data(sensor_id=robot.position_red, svi=0)
        positionData_y = sim.get_sensor_data(sensor_id=robot.position_red,
                                             svi=1)
        # positionData_z = sim.get_sensor_data(sensor_id=robot.position_red, svi=2)

        self.fitness = positionData_y[-1]
Example #29
0
    def Start_Evaluation(self, pb, pp):

        #---SIMULATOR---#

        # start the simulator
        self.sim = pyrosim.Simulator(eval_time=1000,
                                     play_paused=pp,
                                     play_blind=pb)

        #---ROBOTS---#

        # spawn bot
        self.robot = ROBOT(self.sim, self.genome)

        #---SIMULATION---#

        # start the simulation
        self.sim.start()
Example #30
0
      def Start_Evaluation(self, pb):
        # seconds = 150
        # dt = 0.05
        # eval_time = int(seconds/dt)
        # self.sim = pyrosim.Simulator(xyz=[50,60,0], use_textures=True, eval_time= eval_time, dt = dt ,play_paused=False, play_blind=pb)
        seconds = 120.0
        dt = 0.2
        eval_time = int(seconds/dt)
        # print(eval_time)
        gravity = -1.0

        self.sim = pyrosim.Simulator(eval_time=eval_time, debug=False,
                                   play_paused=False,
                                   gravity=gravity,
                                   play_blind=pb,
                                   use_textures=True,
                                   capture=False,
                                   dt=dt)

        self.snakeservo= ROBOT(self.sim, self.genomeh, self.genomeop)
        self.sim.start()