Effacer les filtres
Effacer les filtres

Monte Carlo- Trying to give each car a driving pattern

2 vues (au cours des 30 derniers jours)
Amir Hussain
Amir Hussain le 15 Jan 2023
Réponse apportée : Surya le 16 Mai 2023
hey, currently trying to give each car in my selection a driving pattern based on current data. i know cars will drive from 7am-10pm and that i each car has a random number of trips and distance based on distribution. i want to run each car and when the drive it will reduce it battery capacity based on equations i am yet to implement. How can i fix my code as unable to give driving patterns. want it stored in km of hourly chunks based on the distance travelled. thanks
QatarEV = 174670;
sedanShare = 0.46;
time = 1:288; %There are 288 intervals (5 min), from 00:00am to 23:55pm
sedanTypes = ["Tesla3", "MercedesEQS", "TeslaS", "Hyundai"];
suvTypes = ["Mercedes", "Nissan", "Toyota", "Renault", "VW"];
% Calculate the number of sedans and SUVs
num.sedans = QatarEV * sedanShare;
num.sedans = round(num.sedans);
num.suvs = 174670 - num.sedans;
% Assign sedan types randomly
sedanAssignments = sedanTypes(randi(numel(sedanTypes),1,num.sedans))';
% Assign SUV types randomly
suvAssignments = suvTypes(randi(numel(suvTypes),1,num.suvs))';
% Concatenate the sedan and SUV assignments
carAssignments = [sedanAssignments; suvAssignments];
batterySizes = table(["Tesla3", "MercedesEQS", "TeslaS", "Hyundai", ...
"Mercedes", "Nissan", "Toyota", "Renault", "VW"]', ...
[54;107.8;103;64;28;40;40;41;35.8]*1e3, ...
VariableNames=["carAssignments","battery size"]);
carTable = table([repmat("sedan",num.sedans,1); repmat("suv",num.suvs,1)], carAssignments);
carTable = outerjoin(carTable,batterySizes,"RightVariables",...
"battery size");
carTable.Properties.VariableNames(1) = "carType";
% here we give each battery pack a state to begin simulation
state = rand(height(carTable),1) .* carTable.("battery size");
% create a column of random values multiplied by the battery pack sizes
carTable = [carTable table(state)];
% now determine number of trips in the day by each EV by checking if car is
% sedan or suv then giving values
sedanr= floor(normrnd(8.473,6.855));
suvr=floor(normrnd(5.731,4.274));
% Assign a random number of trips to each car based on its type
numTrips = zeros(height(carTable),1);
numTrips(strcmp(carTable.carType,'sedan')) = normrnd(8.473,6.855,sum(strcmp(carTable.carType,'sedan')),1);
numTrips(strcmp(carTable.carType,'suv')) = normrnd(5.731,4.274,sum(strcmp(carTable.carType,'suv')),1);
numTrips = floor(numTrips);
numTrips(numTrips<0) = 0;
% Add the number of trips as a new column in the carTable
carTable = [carTable table(numTrips)];
carTable.Properties.VariableNames(end) = "numTrips";
% making any negative number zero
maximumDistance = 250;
distanceTable = table(repelem(["suvs";"sedans"],4,1),...
[15.38 34.07 47.25 3.3 5.56 52.22 40 2.22]'./100,...
repmat([0 40 90 180]',2,1),repmat([40 100 180 300]',2,1),...
VariableNames=["type","perc","from","to"]);
num_days = 1;
types = unique(distanceTable.type)';
for tidx = types
driving_distances.(tidx) = rand(num_days * num.(tidx),1);
nextRow = 1;
for idx = find(distanceTable.type == tidx)'
thisDistanceRange = distanceTable(idx,:);
lastRow = min(nextRow + ceil(numel(driving_distances.(tidx)) * thisDistanceRange.perc),numel(driving_distances.(tidx)));
driving_distances.(tidx)(nextRow:lastRow) = floor(driving_distances.(tidx)(nextRow:lastRow) * (thisDistanceRange.to - thisDistanceRange.from) + thisDistanceRange.from);
nextRow = lastRow + 1;
end
driving_distances.(tidx) = driving_distances.(tidx)(randperm(numel(driving_distances.(tidx))));
driving_distances.(tidx) = reshape(driving_distances.(tidx),[],num_days);
end
carTable = [carTable array2table([driving_distances.sedans; driving_distances.suvs],VariableNames = "July " + (1:num_days))];
% for each car give them a average speed
% cars start driving from 7am-10pm (84:264)
% speeds range from 50km/h to 150km/h, I made this up
for i = 1:height(carTable)
car = carTable(i,:);
avgSpeeds_kmph = rand(N,1)*100 + 50;
endTime = 0;
proportions = rand(car.numTrips, 1);
proportions = proportions / sum(proportions);
total_distance= carTable.("July 1");
car.trip_distance = proportions * total_distance;
% for j = 1:car.numTrips
% startTime = randi([84, 264]);
% while startTime <= endTime
% startTime = randi([84, 264]);
% end
% duration = (car.trip_distance(j) / avgSpeeds_kmph ) * (60/5);
% endTime = startTime + duration;
end
% end

Réponses (1)

Surya
Surya le 16 Mai 2023
Hi,
Based on the code you have provided, I see that you have generated random distances for each car and stored them in the carTable with column names like "July 1", "July 2", etc. You want to convert these distances into a driving pattern, in kilometers per hour, for every hour of the day between 7 AM and 10 PM.
One way to do this would be to loop through every hour between 7 AM and 10 PM and for each hour, loop through every car to calculate its average speed during that hour. You can then use the average speed and distance to calculate the driving pattern for that hour. Here is some code that you can use as a starting point:
intv_per_hour = 12;
% Hourly driving pattern, in kilometers
driving_patterns = zeros(15, height(carTable));
% Loop through every hour of the day from 7AM to 10PM
for h = 7*intv_per_hour : 22*intv_per_hour
% Hourly average speeds, in kilometers per hour
avg_speeds_kph = rand(height(carTable), 1) * 100 + 50;
% Loop through every car
for i = 1:height(carTable)
% Get the distance traveled by the car during the current hour
distance = carTable(i, round(h/intv_per_hour)).Variables;
% Calculate the time taken to travel the distance based on the average speed
time_hours = distance / avg_speeds_kph(i);
% Calculate the kilometers per hour for the current hour and store it in the driving pattern array
driving_patterns(h/intv_per_hour-6, i) = distance / time_hours;
end
end
The above code calculates the average speed of each car during each hour of the day and uses it to calculate the driving pattern for that hour. The driving patterns are stored in a new array driving_patterns where each row corresponds to an hour of the day and each column corresponds to a car in carTable.
Note that the driving patterns use an index of h/intv_per_hour-6 to store the driving pattern for a given hour in the driving_patterns array. This ensures that the 7th hour (which starts at index 7intv_per_hour = 84 in your time variable) corresponds to the first row of the driving_patterns array, the 8th hour corresponds to the second row, and so on, up to the 22nd hour (which starts at index 22intv_per_hour = 264) corresponding to the 15th row of the driving_patterns array.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by