Clear Filters
Clear Filters

How to find user combinations in each sector that maximize the value recursively?

1 view (last 30 days)
Hello,
I am writing MATLAB code for finding the best combination in each sector based on minimum or maximum gain value.
Currently i am doing manually taking value of each sector based on their number of sectors and population. For simplification i am taking population value below 100 but actual target is to find the best combinations for a population range till [24 48 72 96 120 144 168 192 216 240 264 288 ]. For simplicity, I have pasted the picture of 24 user population case.
counter = 0;
maxUser_Value = 0.199526231496888;
Ratio = 2.374994702111877e+04;
Time = 2.374994702111877e+04;
for Population = 24:24:300
for Number_of_Sectors = 2:2:8
For sector greater than two it is recursive that best combination of Sector1&2 will serve as an input to find best combination between Best_S12 & Sector 3 and similarly for next till sector 8.
User_Comb = zeros(Number_of_Sectors,1);
User_gains = 1.0e-08 * [
0.1056 0.0012
0.0525 0.0009
0.0357 0.0009
0.0306 0.0009
0.0062 0.0008
0.0057 0.0008
0.0040 0.0007
0.0026 0.0006
0.0025 0.0006
0.0024 0.0006
0.0023 0.0005
0.0021 0.0005]
Sectors_users_index=ones(Number_of_Sectors,1);
if (Number_of_Sectors==2)
while (1)
% Initialize cell array to store combinations
Nusers_per_sector= Population/Number_of_Sectors;
counter = counter+1;
for sector_1_index = 1 : Number_of_Sectors
for sector_2_index = 1: Number_of_Sectors
if sector_1_index == sector_2_index
continue
end
%==============================================================================================================
User_Comb(sector_1_index)= User_Comb(sector_1_index) + User_gains(Sectors_users_index(sector_2_index),sector_2_index)*maxUser_Value;
end
Resources_User_Comb(sector_1_index) = User_gains(Sectors_users_index(sector_2_index),sector_2_index)*maxUser_Value/Ratio;
end
Tot_User_Combinations(counter,1) = sum (User_Comb);
Tot_User_Comb_indices(counter,2:end) = Sectors_users_index;
end
%==============================================================================================================
for userind = 1 : Nusers_per_sector
[max_value(userind),max_ind(userind)] = max(Tot_User_Combinations(:,1));
G1_ind_A2(userind,:) = Tot_User_Combinations(max_ind(userind),2:end);
Tot_User_Combinations_temp = Tot_User_Combinations;
Bst_Usr_maxInd(userind,:) = G1_ind_A2(userind,:);
Totmax_value(userind,:) = max_value(userind);
User_gain_maxComb_S12(userind,:) = [User_gain(G1_ind_IntA2(userind,1),1),User_gain(G1_ind_A2(userind,2),2)];
Tot_User_Combinations_temp(Tot_User_Combinations_temp(:,2)==G1_ind_A2(userind,1),:)=[];
Tot_User_Combinations_temp(Tot_User_Combinations_temp(:,3)==G1_ind_A2(userind,2),:)=[];
Tot_User_Combinations = Tot_User_Combinations_temp;
end
I am writing this code but not able to figure out how the best of S12 will be used as input to Sector 3 as I am doing some mistake, since I have drawn this logic on my paper and done as per logic but still not able to figure out where is the issue. Either indexing or either looping?
%==============================================================================================================
%% I am not able to figure out how to automate this through recursive and using loop
elseif (Number_of_Sectors > 2)
while (1)% Initialize cell array to store combinations
Nusers_per_sector= num_users/Number_of_Sectors;
counter = counter+1;
for sector_1_index = 1 : Number_of_Sectors
for sector_2_index = 1: Number_of_Sectors
if sector_1_index == sector_2_index
continue
end
%==============================================================================================================
User_Comb_S123(sector_1_index)= User_Comb_S123(sector_1_index) + Best_S12_Gains(Sectors_users_index(sector_2_index),sector_2_index)*maxUser_Value;
end
Resources_User_Comb(sector_1_index) = Best_S12_Gains(Sectors_users_index(sector_2_index),sector_2_index)*maxUser_Value/Ratio;
end
Tot_User_Combinations(counter,1) = sum (User_Comb);
Tot_User_Comb_indices(counter,2:end) = Sectors_users_index;
end
%==============================================================================================================
for userind = 1 : Nusers_per_sector
[max_value(userind),max_ind(userind)] = max(Tot_User_Combinations(:,1));
G1_ind_A2(userind,:) = Tot_User_Combinations(max_ind(userind),2:end);
Tot_User_Combinations_temp = Tot_User_Combinations;
Bst_Usr_maxInd(userind,:) = G1_ind_A2(userind,:);
Totmax_value(userind,:) = max_value(userind);
User_gain_maxComb_S12(userind,:) = [User_gain(G1_ind_IntA2(userind,1),1),User_gain(G1_ind_A2(userind,2),2)];
Tot_User_Combinations_temp(Tot_User_Combinations_temp(:,2)==G1_ind_A2(userind,1),:)=[];
Tot_User_Combinations_temp(Tot_User_Combinations_temp(:,3)==G1_ind_A2(userind,2),:)=[];
Tot_User_Combinations = Tot_User_Combinations_temp;
end
end
end
end
User_gains = 12×2
1.0e-08 * 0.1056 0.0012 0.0525 0.0009 0.0357 0.0009 0.0306 0.0009 0.0062 0.0008 0.0057 0.0008 0.0040 0.0007 0.0026 0.0006 0.0025 0.0006 0.0024 0.0006
The end operator must be used within an array index expression.
Kindly guide me as I want to find out for sectors greater than 2 the best combinations of each sector based on their gain values. Looking forward to your kind comments and reply.

Answers (1)

Nipun
Nipun on 21 Dec 2023
Hi Aamir,
I understand that you are trying to find the best combinations of users in each sector based on their gain values. To make your code more efficient and easier to understand, I suggest refactoring it using functions and avoiding infinite loops with appropriate exit conditions.
I have attached the modified code that uses recursive functions to handle different numbers of sectors:
function findBestCombinations(Population, Number_of_Sectors, maxUser_Value, Ratio)
User_gains = 1.0e-08 * [
0.1056 0.0012
0.0525 0.0009
0.0357 0.0009
0.0306 0.0009
0.0062 0.0008
0.0057 0.0008
0.0040 0.0007
0.0026 0.0006
0.0025 0.0006
0.0024 0.0006
0.0023 0.0005
0.0021 0.0005
];
Tot_User_Combinations = [];
Tot_User_Comb_indices = [];
findBestCombinationsRecursive(Population, Number_of_Sectors, maxUser_Value, Ratio, User_gains, [], [], [], Tot_User_Combinations, Tot_User_Comb_indices);
end
Here is the "recursive" version of the function for better modularity
function findBestCombinationsRecursive(Population, Number_of_Sectors, maxUser_Value, Ratio, User_gains, Sectors_users_index, User_Comb, Resources_User_Comb, Tot_User_Combinations, Tot_User_Comb_indices)
Nusers_per_sector = Population / Number_of_Sectors;
if Number_of_Sectors == 2
% Base case for two sectors
for sector_1_index = 1:Number_of_Sectors
for sector_2_index = 1:Number_of_Sectors
if sector_1_index == sector_2_index
continue
end
User_Comb(sector_1_index) = User_Comb(sector_1_index) + User_gains(Sectors_users_index(sector_2_index), sector_2_index) * maxUser_Value;
end
Resources_User_Comb(sector_1_index) = User_gains(Sectors_users_index(sector_2_index), sector_2_index) * maxUser_Value / Ratio;
end
Tot_User_Combinations = [Tot_User_Combinations; sum(User_Comb)];
Tot_User_Comb_indices = [Tot_User_Comb_indices; Sectors_users_index];
% Continue with your code to find the best combination
elseif Number_of_Sectors > 2
% Recursive case for more than two sectors
for sector_1_index = 1:Number_of_Sectors
for sector_2_index = 1:Number_of_Sectors
if sector_1_index == sector_2_index
continue
end
User_Comb(sector_1_index) = User_Comb(sector_1_index) + User_gains(Sectors_users_index(sector_2_index), sector_2_index) * maxUser_Value;
end
Resources_User_Comb(sector_1_index) = User_gains(Sectors_users_index(sector_2_index), sector_2_index) * maxUser_Value / Ratio;
end
Tot_User_Combinations = [Tot_User_Combinations; sum(User_Comb)];
Tot_User_Comb_indices = [Tot_User_Comb_indices; Sectors_users_index];
% Continue with your code to find the best combination
% Recursive call for the next sector
findBestCombinationsRecursive(Population, Number_of_Sectors - 1, maxUser_Value, Ratio, User_gains, Sectors_users_index, User_Comb, Resources_User_Comb, Tot_User_Combinations, Tot_User_Comb_indices);
end
end
This code structure allows you to handle different numbers of sectors in a more modular and recursive way. Please be wary to adjust and complete the code within the base case and recursive case to suit your specific logic for finding the best combination.
Hope this helps.
Regards,
Nipun

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!