TroglodyteDerivations's picture
Updated lines 39-46 with: [hashtag removal] target_pattern = np.array([ [25.76815, -80.1868], [25.7743, -80.1937], [25.762, -80.18], [25.76815, -80.1868], [25.7743, -80.18], [25.762, -80.1937] ]) | Updated lines 48-57 with: [added hashtag notation on target_pattern coordinates] # Define the target pattern [Flying in the shape of an ∞-Pattern] #target_pattern = np.array([ #[25.76815, -80.1868], # Start of the figure-eight #[25.765, -80.183], # Top of the left loop #[25.762, -80.18], # Bottom of the left loop #[25.765, -80.183], # Middle of the figure-eight #[25.768, -80.18], # Bottom of the right loop #[25.7743, -80.1868], # Top of the right loop #[25.76815, -80.1868] # End of the figure-eight #])
9436204 verified
import streamlit as st
import random
import numpy as np
from deap import base, creator, tools, algorithms
import folium
from streamlit_folium import folium_static
st.title('UAV Genetic Algorithm (Evolutionary Algorithm) X-Shaped Flight Pattern')
st.image('UAV_GA_Path_4.jpeg', caption='UAV Genetic Algorithm Path 4')
st.write('一。 Nico Maulen B2B Lean Gatti - Techengue Vol 1 - Tech & Latin House')
st.write('δΊŒγ€‚ GUAY [TECH HOUSE REMIX] Ozuna, Bad Gyal')
st.write('三。 KAROL G, Feid, DFZM ft. Ovy On The Drums, J Balvin, Maluma, Ryan Castro, Blessd - +57')
st.write('四。 Dei V - Quickie (Official Video)')
st.image('UAV_GA_Path_2.webp', caption='UAV Genetic Algorithm Path 2')
# Define the target M-shape pattern in Miami
#target_pattern = np.array([
# [25.7743, -80.1937], # Top left of M
# [25.76815, -80.1868], # Bottom left of M
# [25.765, -80.183], # Middle top of M
# [25.762, -80.18], # Bottom middle of M
# [25.765, -80.177], # Middle top of M (right side)
# [25.768, -80.18], # Bottom right of M
# [25.7743, -80.1868] # Top right of M
# ])
# Target Pattern set of coordinates does not form the letter M shape
# Define the target M-shape pattern in Miami
#target_pattern = np.array([
# [25.76815, -80.1868], # Top left of M
# [25.762, -80.18], # Bottom left of M
# [25.765, -80.183], # Middle of M
# [25.768, -80.18], # Bottom right of M
# [25.7743, -80.1868] # Top right of M
# ])
# Target Pattern set of coordinates does not form the letter M shape
# Define the target pattern [Flying in the shape of an X-Pattern]
target_pattern = np.array([
[25.76815, -80.1868],
[25.7743, -80.1937],
[25.762, -80.18],
[25.76815, -80.1868],
[25.7743, -80.18],
[25.762, -80.1937]
])
# Define the target pattern [Flying in the shape of an ∞-Pattern]
#target_pattern = np.array([
#[25.76815, -80.1868], # Start of the figure-eight
#[25.765, -80.183], # Top of the left loop
#[25.762, -80.18], # Bottom of the left loop
#[25.765, -80.183], # Middle of the figure-eight
#[25.768, -80.18], # Bottom of the right loop
#[25.7743, -80.1868], # Top of the right loop
#[25.76815, -80.1868] # End of the figure-eight
#])
# Define the number of points in the optimized pattern
num_points = len(target_pattern)
# Define the range for latitude and longitude
lat_range = (25.76, 25.78)
lon_range = (-80.20, -80.17)
# Create the fitness function and individual
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create("Individual", list, fitness=creator.FitnessMin)
toolbox = base.Toolbox()
# Attribute generator
toolbox.register("attr_lat", random.uniform, lat_range[0], lat_range[1])
toolbox.register("attr_lon", random.uniform, lon_range[0], lon_range[1])
# Structure initializers
toolbox.register("individual", tools.initCycle, creator.Individual,
(toolbox.attr_lat, toolbox.attr_lon), n=num_points)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
# Define the objective function
def objective_function(optimized_pattern, target_pattern):
return np.sum((np.array(optimized_pattern) - np.array(target_pattern)) ** 2)
def eval_pattern(individual):
optimized_pattern = np.array(individual).reshape(-1, 2)
return objective_function(optimized_pattern, target_pattern),
toolbox.register("evaluate", eval_pattern)
toolbox.register("mate", tools.cxBlend, alpha=0.5)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=0.1, indpb=0.2)
toolbox.register("select", tools.selTournament, tournsize=3)
# Genetic Algorithm Parameters
pop_size = 100
ngen = 500
cxpb = 0.7
mutpb = 0.2
# Initialize population
pop = toolbox.population(n=pop_size)
# Capture fitness values over generations
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", np.mean)
stats.register("min", np.min)
logbook = tools.Logbook()
# Run the genetic algorithm
algorithms.eaSimple(pop, toolbox, cxpb, mutpb, ngen, stats=stats, halloffame=None, verbose=True)
# Get the best individual
best_individual = tools.selBest(pop, k=1)[0]
best_pattern = np.array(best_individual).reshape(-1, 2)
# Print the original target pattern
st.write("Original Target Pattern:")
st.write(target_pattern)
st.image('UAV_GA_Path_3.jpeg', caption='UAV Genetic Algorithm Path 3')
# Print the optimized pattern
st.write("Optimized Pattern:")
st.write(best_pattern)
st.write("Objective Function Value:", objective_function(best_pattern, target_pattern))
st.image('UAV_GA_Path_1.jpeg', caption='UAV Genetic Algorithm Path 1')
# Create a Folium map centered around the target pattern
m = folium.Map(location=[25.76815, -80.1868], zoom_start=13)
# Create feature groups for the target pattern and optimized pattern
target_group = folium.FeatureGroup(name='Target Pattern')
optimized_group = folium.FeatureGroup(name='Optimized Pattern')
# Add markers for the target pattern to the target group
for point in target_pattern:
folium.Marker(location=point, icon=folium.Icon(color='red', icon='info-sign')).add_to(target_group)
# Add markers for the optimized pattern to the optimized group
for point in best_pattern:
folium.Marker(location=point, icon=folium.Icon(color='blue', icon='info-sign')).add_to(optimized_group)
# Add a Polyline for the optimized pattern to the optimized group
folium.PolyLine(locations=best_pattern, color='blue', weight=3, opacity=1).add_to(optimized_group)
# Add labels for the target pattern and optimized pattern
folium.Marker(
location=[25.76815, -80.1868],
icon=folium.DivIcon(
icon_size=(150,36),
icon_anchor=(0, -30), # Shift south by 30 pixels
html='<div style="font-size: 18pt; color: red;">Target Pattern</div>',
)
).add_to(target_group)
folium.Marker(
location=[25.7743, -80.18],
icon=folium.DivIcon(
icon_size=(150,36),
icon_anchor=(30, 0), # Shift right by 30 pixels
html='<div style="font-size: 18pt; color: blue;">Optimized Pattern</div>',
)
).add_to(optimized_group)
# Add the feature groups to the map
target_group.add_to(m)
optimized_group.add_to(m)
# Add layer control to the map
folium.LayerControl().add_to(m)
st.image('UAV_GA_Path_6.jpeg', caption='UAV Genetic Algorithm Path 6')
# Display the Folium map in Streamlit
st.write("Map of Target and Optimized Patterns:")
folium_static(m)
st.image('UAV_GA_Path_5.jpeg', caption='UAV Genetic Algorithm Path 5')