% 定義無人機起始位置和目標位置
start_point = [0, 0, 0]; % 起始位置 [x, y, z]
target_point = [100, 100, 100]; % 目標位置 [x, y, z]
% 定義城市地形和障礙物信息
city_map = imread(‘city_map.png’); % 城市地形圖像
obstacles = [
20, 30, 10; % 障礙物1位置 [x, y, z]
50, 60, 20; % 障礙物2位置 [x, y, z]
80, 90, 15; % 障礙物3位置 [x, y, z]
];
% 定義TGA參數
num_trees = 50; % 樹木數量
max_iterations = 100; % 最大迭代次數
step_size = 1; % 步長
% 定義適應度函數
fitness_func = @(x) objective_func(x, start_point, target_point, city_map, obstacles);
% 初始化樹木
trees = unifrnd(0, 1, num_trees, 3); % 初始位置隨機分布
best_solution = [];
best_fitness = Inf;
% 迭代優化
for iteration = 1:max_iterations
% 計算適應度值
fitness_values = fitness_func(trees);
% 更新最優解
[min_fitness, min_index] = min(fitness_values);
if min_fitness < best_fitnessbest_solution = trees(min_index, :);best_fitness = min_fitness;
end% 生長新樹
new_trees = zeros(num_trees, 3);
for i = 1:num_trees% 選擇父樹parent_index = select_parent(fitness_values);% 生長新樹new_trees(i, :) = trees(parent_index, :) + step_size * randn(1, 3);% 限制位置范圍new_trees(i, :) = max(new_trees(i, :), 0);new_trees(i, :) = min(new_trees(i, :), 100);
end% 更新樹木
trees = new_trees;% 顯示當前迭代結果
disp(['Iteration: ', num2str(iteration), ' Best Fitness: ', num2str(best_fitness)]);
end
% 顯示最優解
disp(‘Optimization finished.’);
disp(['Best Fitness: ', num2str(best_fitness)]);
disp(‘Best Solution:’);
disp(best_solution);
% 可視化航跡規劃結果
visualize_solution(start_point, target_point, city_map, obstacles, best_solution);