예제 #1
0
def DistanceVelocity(genotype):
    """Third version of the fitness function. This is the first one used in Kadihasanoglu et al. 2015. 
    Here we evolve agents based on minimizing their final distance and final velocity."""

    final_distances = np.empty(ntrials)
    final_velocities = np.empty(ntrials)
    jerks = np.empty(ntrials)
    i = 0
    agent = AgentEnv(genotype, Size, WeightRange, BiasRange, TimeConstMin,
                     TimeConstMax, InputWeightRange, Dt)
    for ts in target_size:
        for d in initial_distance:
            for v in initial_velocity:
                agent.setInitialState(v, d, ts)
                agent.Optical_variable = optical_variable
                accelerations = []
                # While agent hasn't crashed, agent is still moving forward significantly, and not too much time has elapsed
                while (agent.Distance > 0) and (agent.Velocity > 0.005) and (
                        agent.Time < trial_length):
                    agent.sense()
                    agent.think()
                    agent.act()
                    accelerations.append(agent.Acceleration)

                if agent.Distance < 0:  # If agent crashed, reset distance to starting position
                    agent.Distance = d

                if agent.Velocity < 0:  # If agent finishes moving backwards, reset velocity to starting velocity
                    agent.Velocity = v

                final_distances[i] = agent.Distance / d
                final_velocities[i] = agent.Velocity / v
                jerks[i] = jerk(accelerations)
                i += 1

    jweight = 0.
    fitness = (
        (1 - np.average(final_distances)) +
        (1 - np.average(final_velocities))) / 2 - jweight * np.average(jerks)
    return fitness
예제 #2
0
def FinalDistanceNoCrash(genotype):
    """Second version of the fitness function. Here we evolve agents on their ability to end the 
    trial near the target, WITHOUT crashing. Crashes result in a default fitness of the initial 
    distance."""

    final_distances = np.empty(ntrials)
    i = 0
    agent = AgentEnv(genotype, Size, WeightRange, BiasRange, TimeConstMin,
                     TimeConstMax, InputWeightRange, Dt)
    for ts in target_size:
        for d in initial_distance:
            for v in initial_velocity:
                agent.setInitialState(v, d, ts)
                agent.Optical_variable = optical_variable
                # While agent hasn't crashed, agent is still moving forward significantly, and not too much time has elapsed
                while (agent.Distance > 0) and (agent.Velocity > 0.005) and (
                        agent.Time < trial_length):
                    agent.sense()
                    agent.think()
                    agent.act()

                if agent.Distance < 0:  # if agent crashed, reset distance to starting position
                    agent.Distance = d

                final_distances[
                    i] = agent.Distance / d  # fitness is proportion of distance
                i += 1

    return (1 - np.average(final_distances))
예제 #3
0
def FinalDistance(genotype):
    """First version of the fitness function. Here we evolve agents simply on their ability to end 
    the trial near the target - crashes count, too!"""

    final_distances = np.empty(ntrials)
    i = 0
    agent = AgentEnv(genotype, Size, WeightRange, BiasRange, TimeConstMin,
                     TimeConstMax, InputWeightRange, Dt)
    for ts in target_size:
        for d in initial_distance:
            for v in initial_velocity:
                agent.setInitialState(v, d, ts)
                agent.Optical_variable = optical_variable
                # While distance is still positive, agent is still moving forward significantly, and not too much time has elapsed
                while (agent.Distance > 0) and (agent.Velocity > 0.005) and (
                        agent.Time < trial_length):
                    agent.sense()
                    agent.think()
                    agent.act()

                final_distances[i] = agent.Distance
                i += 1

    return (1 - np.average(final_distances))
예제 #4
0
        save(filename, mga)
        print('%f sec elapsed so far \n' % (time.time() - start))


# ===========================================    RUNTIME   =========================================

#run_continue('DistanceVelocityJerk_V2_P150_T168_G175_2021-04-28', 25, 25)

# Set up
start = time.time()
print('Number of Evaluation Trials: %i' % ntrials)
# Run simulation
mga = Microbial(fitnessFunction, Population, GenotypeLength, RecombProb,
                MutatProb)
mga.runTournaments(Tournaments)
# Save data
ffname = str(mga.fitnessFunction.__name__)
generation = int(mga.generationsRun)
filename = '%s_V%i_test' % (ffname, optical_variable)
save(filename, mga)
#Report runtime
print('TOTAL TIME ELAPSED: %i sec' % (int(time.time() - start)))
# Show graphs and save them too
mga.showFitness()
mga.showDiversity()
# Show trajectories of best individual
agent = AgentEnv(mga.bestIndividual, Size, WeightRange, BiasRange,
                 TimeConstMin, TimeConstMax, InputWeightRange, Dt)
agent.showTrajectory(optical_variable, target_size[0], initial_distance[0],
                     initial_velocity[0])
예제 #5
0
    def trialAnalysis(self, traj_params=(), show=True):
        """Suggested value for jweight are 0 or 1000 (KBB15). traj_params should be a
        tuple of (initial velocity, initial distance, target size)."""

        # Step 1: Get genotype of best individual
        genotype = self.bestIndividual

        # Step 2: Run simulation
        final_distances = np.empty(ntrials)
        final_velocities = np.empty(ntrials)
        jerks = np.empty(ntrials)
        trials = 0
        successes = 0
        crashes = 0
        early_stops = 0
        timeouts = 0
        agent = AgentEnv(genotype, Size, WeightRange, BiasRange, TimeConstMin, TimeConstMax, InputWeightRange, Dt)
        for ts in target_size:
            for d in initial_distance:
                for v in initial_velocity:
                    agent.setInitialState(v, d, ts)
                    agent.Optical_variable = self.Optical_variable
                    acceleration = []
                    distance = []
                    velocity = []
                    optical = []
                    # Simulation loop
                    i = 0
                    while (agent.Distance > 0) and (agent.Velocity > 0.005) and (agent.Time < trial_length):
                        agent.sense()
                        agent.think()
                        agent.act()
                        i += 1
                        acceleration.append(agent.Acceleration)    # Record data
                        distance.append(agent.Distance)
                        velocity.append(agent.Velocity)
                        optical.append(agent.Optical_info[agent.Optical_variable])

                    if show == True and (v, d, ts) == traj_params: # If this is the trial we want to visualize, record data for plotting
                        series = [distance, velocity, acceleration, optical]
                        time = agent.Time

                    if agent.Velocity < 0:   # If agent finishes moving backwards, reset velocity to starting velocity
                        agent.Velocity = v
                        
                    if agent.Distance > 15:  # If agent stopped prematurely
                        early_stops += 1

                    elif agent.Distance < 0:   # If agent crashed, reset distance to starting position
                        agent.Distance = d
                        crashes += 1
                        
                    elif agent.Time > trial_length+Dt:  # If agent never stops/times out
                        timeouts += 1
                    else:
                        successes += 1
                    final_distances[trials] = agent.Distance/d   # Record performance data for fitness function
                    final_velocities[trials] = agent.Velocity/v
                    jerks[trials] = jerk(acceleration)
                    trials += 1

        # Plot data
        if show == True:
            for i in range(len(series)):    # This is necessary because of an inconsistent error I was getting
                time = np.arange(0, trial_length, agent.Dt)
                l = len(series[i])
                time = time[:l]
                plt.plot(time, series[i])
                ov_labels = ['Image size', 'Image expansion rate', 'Tau', 'Tau-dot', 'Proportional Rate']
                labels = ['Distance (m)', 'Velocity (m/sec)', 'Acceleration (m/sec^2)]', ov_labels[agent.Optical_variable] ]
                plt.xlabel('Time (sec)')
                plt.ylabel(labels[i])
                plt.title('%s-Guided Agent' % (ov_labels[agent.Optical_variable]))
                plt.show()

        self.successes = successes
        self.crashes = crashes
        self.earlyStops = early_stops
        self.timeouts = timeouts
예제 #6
0
 def perturbationAnalysis(self, ptype, perturbations, traj_params=(), show=True):
     """Valid value for ptype are 'Position', 'Velocity', 'Mapping', and 'Delay'. Perturbations
     should be a vector of perturbation values to be iterated through and applied at every step. The
     length should be trial_length/Dt. The exception for this is in the case of Delay perturbations,
     in which case perturbations should be an integer representing the number of time steps to delay
     motor output by. Suggested value for jweight are 0 or 1000 (KBB15). traj_params should be a
     tuple of (initial velocity, initial distance, target size)."""
 
     # Step 1: Get genotype of best individual
     genotype = self.bestIndividual
 
     # Step 2: Run simulation
     final_distances = np.empty(ntrials)
     final_velocities = np.empty(ntrials)
     jerks = np.empty(ntrials)
     trials = 0
     crashes = 0
     early_stops = 0
     timeouts = 0
     agent = AgentEnv(genotype, Size, WeightRange, BiasRange, TimeConstMin, TimeConstMax, InputWeightRange, Dt)
     for ts in target_size:
         for d in initial_distance:
             for v in initial_velocity:
                 agent.setInitialState(v, d, ts)
                 agent.Optical_variable = self.Optical_variable
                 acceleration = []
                 distance = []
                 velocity = []
                 optical = []
                 if ptype == 'Delay':
                     outputs = []
                     for o in range(perturbations):
                         outputs.append(0)
 
                 # Simulation loop
                 i = 0
                 while (agent.Distance > 0) and (agent.Velocity > 0.005) and (agent.Time < trial_length):
                     agent.sense()
                     agent.think()
                     if ptype == 'Delay':
                         outputs.append(agent.output)   # Take self.output and append it to end of the list
                         agent.output = outputs.pop(0)  # Remove first element from list and set it to self.output
 
                     agent.act()
                     if ptype == 'Position':
                         agent.Distance += perturbations[i]
                     if ptype == 'Velocity':
                         agent.Velocity += perturbations[i]
                     if ptype == 'Mapping':
                         agent.brake_effectiveness = perturbations[i]
 
                     i += 1
                     acceleration.append(agent.Acceleration)    # Record data
                     distance.append(agent.Distance)
                     velocity.append(agent.Velocity)
                     optical.append(agent.Optical_info[agent.Optical_variable])
 
                 if show == True and (v, d, ts) == traj_params:  # If this is the trial we want to visualize, record data for plotting
                     series = [distance, velocity, acceleration, optical]
                     time = agent.Time
 
                 if agent.Distance < 0:   # If agent crashed, reset distance to starting position
                     agent.Distance = d
                     crashes += 1
 
                 if agent.Distance > 15:  # If agent stopped prematurely
                     early_stops += 1
 
                 if agent.Time > trial_length:  # If agent never stops/times out
                     timeouts += 1
 
                 if agent.Velocity < 0:   # If agent finishes moving backwards, reset velocity to starting velocity
                     agent.Velocity = v
 
 
                 final_distances[trials] = agent.Distance/d   # Record performance data for fitness function
                 final_velocities[trials] = agent.Velocity/v
                 jerks[trials] = jerk(acceleration)
                 trials += 1
 
     # Plot data
     fitness= ( (1-np.average(final_distances)) + (1-np.average(final_velocities)) )/2 - 1000*np.average(jerks)
     if show == True:
         for i in range(len(series)):    # This is necessary because of an inconsistent error I was getting
             time = np.arange(0, trial_length, agent.Dt)
             l = len(series[i])
             time = time[:l]
             plt.plot(time, series[i])
             ov_labels = ['Image size', 'Image expansion rate', 'Tau', 'Tau-dot', 'Proportional Rate']
             labels = ['Distance (m)', 'Velocity (m/sec)', 'Acceleration (m/sec^2)]', ov_labels[agent.Optical_variable] ]
             plt.xlabel('Time (sec)')
             plt.ylabel(labels[i])
             plt.title('%s Perturbation (%s agent)' % (ptype, ov_labels[agent.Optical_variable]))
             plt.show()
         print('Original Fitness: %f' % self.bestFitness)
         print('Perturbed Fitness: %f' % fitness)
 
     successes = trials - crashes - early_stops - timeouts
     if ptype=='Delay':
         self.delayPerturbStats = fitness, successes, crashes, early_stops, timeouts
         self.delayPerturbation = perturbations
     elif ptype=='Position':
         self.positionPerturbStats = fitness, successes, crashes, early_stops, timeouts
         self.positionPerturbation = perturbations
     elif ptype=='Velocity':
         self.velocityPerturbStats = fitness, successes, crashes, early_stops, timeouts
         self.velocityPerturbation = perturbations
     elif ptype=='Mapping':
         self.mappingPerturbStats = fitness, successes, crashes, early_stops, timeouts
         self.mappingPerturbation = perturbations
     else:
         print('Ptype argument not recognized!')