compute from a set of parameters

1 view (last 30 days)
Elysi Cochin
Elysi Cochin on 13 Mar 2020
Edited: Elysi Cochin on 15 Mar 2020
C = 60; % capacity of a truck
demand = [30 35 24 15 14 48]; % goods to deliver
m = 2; % no of avaible trucks
i wanted to compute how many trucks needed to deliver the goods
and also the number of trips taken by each truck
we will take demand of each person, and check if the sum of demand is greater than the capacity of the truck,
eg: 30 + 35 > 60, in our example, yes - so cant load both to one truck
if it is greater than the capacity we can check if any other demand can fit into the truck, else we need to load the demand into a new truck
how can i compute the number of trucks used and the number of trips
from the above example, output will be
number_of_trucks_used = 2
number_of_trips = [2 1];
i.e. first truck 2 trips and second truck 1 trip
i also wanted to store the goods loaded into the truck in a cell array, and the position taken from vector demand in another cell array like
cell_array_value = { [30 15 14] [48] ; [35 24] }
cell_array_pos = { [1 4 5] [6] ; [2 3] }
please can some one help me to solve it in a simple way

Accepted Answer

Ameer Hamza
Ameer Hamza on 15 Mar 2020
I posted a response to your other question: https://www.mathworks.com/matlabcentral/answers/510925-how-to-split-a-vector-into-small-subvectors-based-on-condition. I am also posting the code here so that anyone coming here can find it.
N = 60;
V = [30 35 24 15 14 48];
a = mat2cell(repmat([0 1], numel(V), 1), ones(size(V)), 2);
combs = logical(combvec(a{:})'); % create all possible combinations
combs(1, :) = []; % remove a trivial combination
cost = sum(combs.*V, 2);
valid_index = cost < N;
valid_combs = combs(valid_index, :);
valid_costs = cost(valid_index);
[valid_costs, index] = sort(valid_costs, 'descend');
valid_combs = valid_combs(index, :);
optimal_combs = logical([]);
while ~isempty(valid_combs)
current_comb = valid_combs(1,:);
optimal_combs = [optimal_combs; current_comb];
index = any(valid_combs(:, current_comb) == valid_combs(1, current_comb), 2);
valid_combs(index, :) = [];
end
result = {};
for i=1:size(optimal_combs,1)
result{i} = V(optimal_combs(i,:));
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!