Can Genetic Algorithm in MATLAB Return a 3D Matrix as an Optimal Solution?

6 views (last 30 days)
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,

Answers (1)

Walter Roberson
Walter Roberson on 3 Dec 2023
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 Comments
Maria
Maria on 5 Dec 2023
Hello!
I defined Updated_Task_assignment as an optimvar for the GA, which requires it to be in a vectorized form. However, my calculation functions, like calculateOffloadingTime, require it in its original 3D matrix format. I'm struggling with how to handle this transformation within the GA's fitness function. Is there a recommended approach to reshape this vectorized matrix back to 3D within the context of a GA optimization?
numSubtasksPerVehicle = size(Task_assignment, 1);
numLayers = size(Task_assignment, 2);
num_vehicles = size(Task_assignment, 3);
% Define optimization variable
Updated_Task_assignment = optimvar('Updated_Task_assignment', num_vehicles * numSubtasksPerVehicle, numLayers, 'Type', 'integer', 'LowerBound', 0, 'UpperBound', 1);
Task_assignment = customReshape3Dto2D(Task_assignment);
% Define GA options
options = optimoptions('ga', ...
'PopulationType', 'doubleVector', ...
'PopulationSize', 200, ...
'MaxGenerations', 100, ...
'CrossoverFraction', 0.8, ...
'EliteCount', 2, ...
'FunctionTolerance', 1e-6, ...
'Display', 'iter');
[Energy_Total_UAV,Total_HAPS_energy_consumption,E_totale] = Calculate_Total_Energy_Consumption(x_UAV, y_UAV, z_UAV,x_positions,y_positions,x_haps,y_haps,z_haps,num_vehicles,numUAVs, Task_assignment, Updated_Task_assignment, 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,total_time);
function score = myFitnessFunction(Updated_Task_assignment)
% Compute Satisfaction Variable
Satisfaction_Variable = zeros(num_vehicles, 1);
[Offloading_time_subtask, 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, Updated_Task_assignment, 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);
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 = @(Updated_Task_assignment) myNonlinearConstraints(Updated_Task_assignment,E_totale,Offloading_time_Task, QoS_values, E_max_totale);
% Run GA
[Updated_Task_assignment, fval] = ga(@myFitnessFunction, numVariables, [], [], [], [], [], [], nonlcon,options);
end
Any advice or guidance on how to effectively manage these issues in MATLAB would be greatly appreciated.
Walter Roberson
Walter Roberson on 5 Dec 2023
Updated_Task_assignment = optimvar('Updated_Task_assignment', [num_vehicles, numSubtasksPerVehicle, numLayers], 'Type', 'integer', 'LowerBound', 0, 'UpperBound', 1);

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!