Can Genetic Algorithm in MATLAB Return a 3D Matrix as an Optimal Solution?
6 次查看(过去 30 天)
显示 更早的评论
Hello!
I am currently working on an optimization problem where my optimization variable is a 3D matrix. I am considering using a genetic algorithm (GA) for this task and I have a few questions regarding its capabilities and implementation in MATLAB.
My optimization variable needs to be a 3D matrix due to the nature of the problem I am solving. Is it possible for MATLAB's genetic algorithm functions to handle and return a 3D matrix as the optimal solution? If so, are there any specific considerations or settings I need to be aware of?
Since the calculations and evaluations in my problem are based on this 3D matrix, my fitness function will inherently work with 3D matrices. Are there any best practices or tips for implementing a fitness function in MATLAB that operates on 3D matrices?
Below is a snippet of my current implementation where the optimization variable is a 3D matrix.
numSubTasks = sum(Task_assignment,'all');
num_vehicles = size(Task_assignment, 3);
numSubtasksPerVehicle = size(Updated_Task_assignment, 1);
% Initialize the population matrix
initialPopulation = zeros(num_vehicles, numSubtasksPerVehicle);
for v = 1:num_vehicles
for st = 1:numSubtasksPerVehicle
if st <= size(Updated_Task_assignment, 1)
assignedUAV = find(Updated_Task_assignment(st, :, v), 1);
if isempty(assignedUAV)
assignedUAV = 0;
end
initialPopulation(v, st) = assignedUAV;
else
initialPopulation(v, st) = 0;
end
end
end
% Define GA options
options = optimoptions('ga', ...
'PopulationType', 'doubleVector', ...
'PopulationSize', 200, ...
'MaxGenerations', 100, ...
'CrossoverFraction', 0.8, ...
'EliteCount', 2, ...
'FunctionTolerance', 1e-6, ...
'Display', 'iter');
% fitness = myFitnessFunction(alpha, SR,EU);
function score = myFitnessFunction(initialPopulation)
Offloading_time_Task = calculateOffloadingTime(x_UAV, y_UAV, z_UAV,x_positions,y_positions,x_haps,y_haps,z_haps,num_vehicles,numUAVs, Task_assignment, initialPopulation, all_subtasks,reordered_tasks,initial_data_sizes,dependency_matrix,transmission_time,P_tx,P_Haps,f_c, c, eta_LoS_Ls,numRBs,B_RB_aerial,Noise_aerial,B_RB_dl,Noise_dl,G_u_linear,G_h_linear,h_LoS,kB,Ts,min_value,max_value,Fmax,coverageRadius,a,b,eta_NLoS_Ls);
% Compute Satisfaction Variable
Satisfaction_Variable = zeros(num_vehicles, 1);
for m = 1:num_vehicles
if all(Offloading_time_Task(1, :, m) <= QoS_values(m))
Satisfaction_Variable(m) = 1;
else
Satisfaction_Variable(m) = 0;
end
end
objective1 = MySatisfactionRate(Satisfaction_Variable,reordered_tasks);
objective2 = Compute_Remaining_Energy(E_max,E_HAPS,E_totale);
score = w1 * objective1 + w2 * objective2;
end
numVariables = numSubTasks;
nonlcon = @(initialPopulation) myNonlinearConstraints(initialPopulation,E_totale,Offloading_time_Task, QoS_values, E_max_totale);
[Updated_Task_assignment, fval] = ga(@myFitnessFunction, numVariables, [], [], [], [], [], [], nonlcon,options);
end
I would appreciate any advice, suggestions, or examples from the community regarding the use of genetic algorithms for optimizing 3D matrices in MATLAB.
Thank you in advance for your help and insights!
Best regards,
0 个评论
回答(1 个)
Walter Roberson
2023-12-3
No. If you call ga directly then the function must expect a vector and must return a vector. You can reshape() first thing you do.
If you use Problem Based Optimization it will do the reshaping for you.
8 个评论
Walter Roberson
2023-12-5
Updated_Task_assignment = optimvar('Updated_Task_assignment', [num_vehicles, numSubtasksPerVehicle, numLayers], 'Type', 'integer', 'LowerBound', 0, 'UpperBound', 1);
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!