Find the combination that maximize the objective function values from the data set.
8 views (last 30 days)
Show older comments
Hello,
I have a written a small piece of code to find the unique combinations from the excel file which has data from my objective optimzation problem.
The file has six columns other hidden columns are not important. The first column in the green shows the objective value, the second and third column with blue indicates the constraint outputs, and fourth, fifth and sixth column with the yellow represents the combinations that are maximizing my objective function.
I have to choose the maximum (green) value from first column each time in the iteration, which will then select the constraint values (blue) from second and third column, whatever they may be. Additionally, each time, it should select a unique combination (yellow) from fourth fifth and sixth column different from the previous combination that maximized the value. No combination value in column should be repeated like if in first iteration it selected [1 8 1] then in next it should [2 6 2] or [3 7 1] or [4 6 5] but every time based on the maximum value it should have unique combination which is maximizing the objective.
My code snippet is here as I am not able to figure out how to choose unique value in every column.
filename = 'Objective Optimization MATLAB.xlsx';
data = readtable(filename);
% Initialize variables
previous_combination = [0 0 0];
% Number of iterations (adjust as needed)
num_iterations = 10;
for i = 1:num_iterations
% Find the maximum objective value
[max_value, max_index] = max(data{:, 1});
% Get the corresponding constraint values and combination
constraint_value1 = data{max_index, 2};
constraint_value2 = data{max_index, 3};
combination = data{max_index, 4:6};
% Check if the combination is unique in each column
while ~isempty(previous_combination) && any(combination == previous_combination)
% Remove the current maximum value to find the next one
data{max_index, 1} = -inf;
[max_value, max_index] = max(data{:, 1});
constraint_value1 = data{max_index, 2};
constraint_value2 = data{max_index, 3};
combination = data{max_index, 4:6};
end
% Ensure no two column values are identical
while length(unique(combination)) < length(combination)
% Remove the current maximum value to find the next one
data{max_index, 1} = -inf;
[max_value, max_index] = max(data{:, 1});
constraint_value1 = data{max_index, 2};
constraint_value2 = data{max_index, 3};
combination = data{max_index, 4:6};
end
% Store the combination to avoid repetition
previous_combination = combination;
% Display the results
fprintf('Iteration %d:\n', i);
fprintf('Max Objective Value: %f\n', max_value);
fprintf('Constraint Value 1: %f\n', constraint_value1);
fprintf('Constraint Value 2: %f\n', constraint_value2);
fprintf('Combination: %s\n\n', mat2str(combination));
uniquecombinations(i,:) = combination;
% Remove the current maximum value to find the next one in the next iteration
data{max_index, 1} = -inf;
end
% Getting these combinations
1 8 2
3 7 1
1 3 2
3 6 1
1 2 3
3 5 1
7 8 1
3 2 4
3 1 2
4 8 1
% Since in first column 3 and 1 are repeated
% In 2nd column 8 is repeated
% In 3rd column 2 and 1 are repeated.
1 Comment
Accepted Answer
Torsten
on 20 Aug 2024
filename = 'Objective Optimization MATLAB.xlsx';
data = xlsread(filename);
data(:,7:end) = [];
n = 1;
while(size(data,1) > 0)
[~,idx] = max(data(:,1));
taken(n,:) = data(idx,:);
jdx = data(:,4) == data(idx,4) | data(:,5) == data(idx,5) | data(:,6) == data(idx,6);
data(jdx,:) = [];
n = n+1;
end
taken
2 Comments
More Answers (0)
See Also
Categories
Find more on Denoising and Compression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!