2024年“华数杯”数学竞赛A题 | 代码&技术文档(论文)

机器臂关节角路径的优化设计

问题一


代码

  • Matlab
clc
clear
% 参数定义
a = [0, 300, 1200, 300, 0, 0];
alpha = [0, -90, 0, -90, -90, -90];
d = [600, 0, 0, 1200, 0, 0];
theta_min = [-160, -150, -200, -180, -120, -180];
theta_max = [160, 15, 80, 180, 120, 180];
P_target = [1500, 1200, 200]; % 目标位置
lambda = 1; % 权重因子

% 初始种群生成
population_size = 100;
num_generations = 100;
crossover_rate = 0.8;
mutation_rate = 0.1;

% 随机生成初始种群
population = zeros(population_size, 6);
for i = 1:population_size
    for j = 1:6
        population(i, j) = theta_min(j) + (theta_max(j) - theta_min(j)) * rand;
    end
end

% 遗传算法主程序
for generation = 1:num_generations
    % 适应度计算
    fitness_values = zeros(population_size, 1);
    for i = 1:population_size
        fitness_values(i) = fitness(population(i, :), P_target);
    end
    
    % 将适应度值转换为非负值
    min_fitness = min(fitness_values);
    fitness_values = fitness_values - min_fitness + 1; % 确保所有适应度值为正
    
    % 选择(轮盘赌算法)
    probabilities = fitness_values / sum(fitness_values);
    selected_indices = randsample(1:population_size, population_size, true, probabilities);
    selected_population = population(selected_indices, :);
    
    % 交叉
    new_population = zeros(population_size, 6);
    for i = 1:2:population_size
        if rand < crossover_rate
            crossover_point = randi([1, 5]);
            new_population(i, :) = [selected_population(i, 1:crossover_point), selected_population(i + 1, crossover_point + 1:end)];
            new_population(i + 1, :) = [selected_population(i + 1, 1:crossover_point), selected_population(i, crossover_point + 1:end)];
        else
            new_population(i, :) = selected_population(i, :);
            new_population(i + 1, :) = selected_population(i + 1, :);
        end
    end
    
    % 变异
    for i = 1:population_size
        if rand < mutation_rate
            mutation_point = randi([1, 6]);
            new_population(i, mutation_point) = theta_min(mutation_point) + (theta_max(mutation_point) - theta_min(mutation_point)) * rand;
        end
    end
    
    population = new_population;
end

% 输出最优解
fitness_values = zeros(population_size, 1);
for i = 1:population_size
    fitness_values(i) = fitness(population(i, :), P_target);
end

[~, best_index] = min(fitness_values);
best_solution = population(best_index, :)
best_fitness = fitness_values(best_index)
% 正运动学计算函数
function T = forward_kinematics(theta)
    a = [0, 300, 1200, 300, 0, 0];
    alpha = [0, -90, 0, -90, -90, -90];
    d = [600, 0, 0, 1200, 0, 0];
    T = eye(4);
    for i = 1:6
        A = [cosd(theta(i)), -sind(theta(i)) * cosd(alpha(i)), sind(theta(i)) * sind(alpha(i)), a(i) * cosd(theta(i));
             sind(theta(i)), cosd(theta(i)) * cosd(alpha(i)), -cosd(theta(i)) * sind(alpha(i)), a(i) * sind(theta(i));
             0, sind(alpha(i)), cosd(alpha(i)), d(i);
             0, 0, 0, 1];
        T = T * A;
    end
end

% 适应度计算函数
function E_error = fitness(theta, P_target)
    T = forward_kinematics(theta);
    P_actual = T(1:3, 4)';
    E_error = norm(P_target - P_actual);
end
  • Python
import numpy as np
from scipy.optimize import minimize

# D-H参数
a = [0, 300, 1200, 300, 0, 0]
alpha = [0, -np.pi/2, 0, -np.pi/2, -np.pi/2, -np.pi/2]
d = [600, 0, 0, 1200, 0, 0]
theta_ranges = [(-160, 160), (-150, 15), (-200, 80), (-180, 180), (-120, 120), (-180, 180)]

# 目标位置
target = np.array([1500, 1200, 200])

# 计算D-H矩阵
def dh_matrix(a, alpha, d, theta):
    return np.array([
        [np.cos(theta), -np.sin(theta)*np.cos(alpha), np.sin(theta)*np.sin(alpha), a*np.cos(theta)],
        [np.sin(theta), np.cos(theta)*np.cos(alpha), -np.cos(theta)*np.sin(alpha), a*np.sin(theta)],
        [0, np.sin(alpha), np.cos(alpha), d],
        [0, 0, 0, 1]
    ])

# 计算末端位姿
def end_effector_position(thetas):
    T = np.eye(4)
    for i in range(6):
        T = T @ dh_matrix(a[i], alpha[i], d[i], np.radians(thetas[i]))
    return T[:3, 3]

# 目标函数
def objective(thetas):
    pos = end_effector_position(thetas)
    return np.linalg.norm(pos - target)

# 初始猜测
initial_guess = [0, -90, 0, 180, -90, 0]

# 约束
bounds = [(low, high) for low, high in theta_ranges]

# 优化
result = minimize(objective, initial_guess, bounds=bounds, method='SLSQP')
optimal_thetas = result.x

print("最优关节角度:", optimal_thetas)
print("最小化误差:", result.fun)

# 打印末端位置
end_position = end_effector_position(optimal_thetas)
print("末端位置:", end_position)

技术文档

在这里插入图片描述

完整内容请看下方~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
华数杯给出的目是一个基于雅鲁藏布江未来五十发展规划的一个优化类问。这与我们美赛的MCM型大致相同。MCM是一种偏向自然、理工的竞赛,目一般较具体,表述简洁,要求明确,并且通常会给出大量的表格数据进行数据处理。而华数杯的A需要参赛者自行收集数据进行建模规划。因此,对于本次的A,收集数据是一个重要的问。你可以分享自己收集来的数据以方便大家进行建模。2023华数杯如期开赛,作为美赛的模拟赛,赛和比赛时间都与美赛高度相似,参赛者完全可以将其作为一次美赛之前的练习赛进行。比赛的发时间与华数杯一致,都是早晨六点。已经将机器翻译的初步翻译结果分享给大家,以帮助大家更好地选。机器人在组装过程中,由于需求是随时间变化的,因此企业为了获得最佳生产效益,就要在整个生产过程中逐日地根据库存和需求决定生产计划。对于这个问,我们只需要将多阶段决策问转化为一系列单阶段最优化问,逐个求解即可。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [2023华数杯”国际大学生数学建模A完整思路](https://blog.csdn.net/qq_33690821/article/details/128860643)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [备战2023华数杯数学建模,解析学习华数杯](https://blog.csdn.net/weixin_45499067/article/details/131967612)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值