python
import numpy as np
# Define the DH parameters
a = np.array([0, 300, 1200, 300, 0, 0])
alpha = np.array([0, -np.pi/2, 0, -np.pi/2, -np.pi/2, -np.pi/2])
d = np.array([600, 0, 0, 1200, 0, 0])
theta = np.array([0, -np.pi/2, 0, np.pi, -np.pi/2, 0])
# Define the transformation matrices
def T(i, theta_i):
return np.array([[np.cos(theta_i), -np.sin(theta_i)*np.cos(alpha[i]), np.sin(theta_i)*np.sin(alpha[i]), a[i]*np.cos(theta_i)],
[np.sin(theta_i), np.cos(theta_i)*np.cos(alpha[i]), -np.cos(theta_i)*np.sin(alpha[i]), a[i]*np.sin(theta_i)],
[0, np.sin(alpha[i]), np.cos(alpha[i]), d[i]],
[0, 0, 0, 1]])
# Calculate the transformation matrices for each joint
def forward_kinematics(theta):
T1 = T(0, theta[0])
T2 = T(1, theta[1]) @ T1
T3 = T(2, theta[2]) @ T2
T4 = T(3, theta[3]) @ T3
T5 = T(4, theta[4]) @ T4
T6 = T(5, theta[5]) @ T5
return T6
# Extract the position of the end effector
def get_end_effector_position(theta):
T6 = forward_kinematics(theta)
x = T6[0, 3]
y = T6[1, 3]
z = T6[2, 3]
return np.array([x, y, z])
# Define the target position
target_position = np.array([1500, 1200, 200])
# Define the bounds for theta
theta_ub = np.array([-8*np.pi/9, -5*np.pi/6, -10*np.pi/9, -np.pi, -2*np.pi/3, -np.pi])
theta_lb = np.array([8*np.pi/9, np.pi/12, 4*np.pi/9, np.pi, -2*np.pi/3, np.pi])
# Define the fitness function
def fitness(theta):
position = get_end_effector_position(theta)
error = np.linalg.norm(position - target_position)
return error
# Genetic Algorithm parameters
population_size = 50
num_generations = 1000
mutation_rate = 0.01
crossover_rate = 0.8
# Initialize the population
population = np.random.uniform(theta_lb, theta_ub, (population_size, 6))
# Genetic Algorithm main loop
for generation in range(num_generations):
# Evaluate the fitness of each individual
fitness_scores = np.array([fitness(ind) for ind in population])
# Select parents using tournament selection
def tournament_selection(population, fitness_scores, k=3):
selected = []
for _ in range(len(population)):
tournament = np.random.choice(len(population), k, replace=False)
winner = tournament[np.argmin(fitness_scores[tournament])]
selected.append(population[winner])
return np.array(selected)
parents = tournament_selection(population, fitness_scores)
# Perform crossover
def crossover(parent1, parent2):
if np.random.rand() < crossover_rate:
crossover_point = np.random.randint(1, len(parent1))
child1 = np.concatenate((parent1[:crossover_point], parent2[crossover_point:]))
child2 = np.concatenate((parent2[:crossover_point], parent1[crossover_point:]))
return child1, child2
return parent1, parent2
new_population = []
for i in range(0, len(parents), 2):
parent1, parent2 = parents[i], parents[i+1]
child1, child2 = crossover(parent1, parent2)
new_population.extend([child1, child2])
# Perform mutation
def mutate(individual):
for i in range(len(individual)):
if np.random.rand() < mutation_rate:
individual[i] = np.random.uniform(theta_lb[i], theta_ub[i])
return individual
population = np.array([mutate(ind) for ind in new_population])
# Elitism: keep the best individual from the previous generation
best_individual = population[np.argmin(fitness_scores)]
population[0] = best_individual
# Print the best solution found
best_individual = population[np.argmin([fitness(ind) for ind in population])]
best_position = get_end_effector_position(best_individual)
best_error = fitness(best_individual)
print("Best solution found:")
print("Theta:", best_individual)
print("End effector position:", best_position)
print("Error:", best_error)