why the plot are all zeros?

48 次查看(过去 30 天)
Catherine
Catherine 2024-4-5,18:29
编辑: Dyuman Joshi 2024-4-6,3:32
Why the plot are all zeros and these variables are all zeros: numVehicleTasks, numEdgeTasks, numCloudTasks
clear;
clc;
close all;
% Parameters
numVehicles = 100; % Number of vehicles
numEdges = 3; % Number of edge computing nodes
cloudProcessingTime = 20; % Time taken by the cloud to process a task (in seconds)
edgeProcessingTime = 10; % Time taken by an edge node to process a task (in seconds)
taskGenerationRate = 0.2; % Task generation rate per vehicle (tasks/second)
cloudDelay = 5; % Communication delay for offloading to cloud (in seconds)
edgeDelay = 2; % Communication delay for offloading to edge (in seconds)
simulationTime = 100; % Simulation time (in seconds)
thresholdDistance = 100; % Threshold distance for offloading to neighbor vehicle (meters)
minSpeed = 2; % Minimum speed for random waypoint mobility (m/s)
maxSpeed = 15; % Maximum speed for random waypoint mobility (m/s)
pauseTime = 10; % Pause time for random waypoint mobility (seconds)
numClusters = 5; % Number of clusters in the Things layer
% Initialize vehicles with different computing power
minVehicleComputingPower = 50;
maxVehicleComputingPower = 100;
vehicles = struct('position', [], 'tasks', [], 'computingPower', []);
for i = 1:numVehicles
vehicles(i).position = rand(1, 2) * 1000; % Random initial position
vehicles(i).tasks = [];
vehicles(i).computingPower = minVehicleComputingPower + rand * (maxVehicleComputingPower - minVehicleComputingPower); % Random computing power
end
% Initialize edge nodes with different computing power
minEdgeComputingPower = 50;
maxEdgeComputingPower = 200;
edges = struct('position', [], 'tasks', [], 'computingPower', []);
for i = 1:numEdges
edges(i).position = rand(1, 2) * 1000; % Random position
edges(i).tasks = [];
edges(i).computingPower = minEdgeComputingPower + rand * (maxEdgeComputingPower - minEdgeComputingPower); % Random computing power
end
% Initialize cloud
cloudTasks = [];
% Initialize arrays to store number of processing tasks
numVehicleTasks = zeros(1, numVehicles);
numEdgeTasks = zeros(1, numEdges);
numCloudTasks = 0;
% Initialize clusters in the Things layer
clusters = struct('center', [], 'members', [], 'ipRange', []);
for i = 1:numClusters
clusters(i).center = rand(1, 2) * 1000; % Random center
clusters(i).members = [];
clusters(i).ipRange = ['192.168.', num2str(i), '.']; % IP range for cluster
end
% Random waypoint mobility model for vehicles
for t = 1:simulationTime
% Generate tasks for vehicles
for i = 1:numVehicles
if rand < taskGenerationRate
vehicles(i).tasks = [vehicles(i).tasks; t];
end
end
% Move vehicles (random waypoint mobility model)
for i = 1:numVehicles
if rand < 0.1 % Randomly pause
continue;
end
% Random speed and direction
speed = minSpeed + rand * (maxSpeed - minSpeed);
direction = rand * 2 * pi;
% Update position
vehicles(i).position = vehicles(i).position + [cos(direction), sin(direction)] * speed;
% Wrap-around if outside the area
vehicles(i).position = mod(vehicles(i).position, 1000);
end
% Offload tasks from vehicles to edge nodes, cloud, or neighbor vehicles
for i = 1:numVehicles
if ~isempty(vehicles(i).tasks)
task = vehicles(i).tasks(1);
vehicles(i).tasks(1) = [];
% Decide whether to offload to edge, cloud, or neighbor vehicle
% based on distance, availability, and computing resources
minDistanceEdge = min(arrayfun(@(x) norm(vehicles(i).position - x.position), edges));
if minDistanceEdge <= thresholdDistance && ...
length(cloudTasks) < numVehicles && ...
~isempty(find([edges.tasks], 1)) && ...
vehicles(i).computingPower >= minEdgeComputingPower
% Offload to edge node
[~, minEdge] = min(arrayfun(@(x) length(x.tasks), edges));
edges(minEdge).tasks = [edges(minEdge).tasks; task];
disp(['Task ', num2str(task), ' from vehicle ', num2str(i), ' offloaded to edge ', num2str(minEdge)]);
% Update number of processing tasks on edge
numEdgeTasks(minEdge) = numEdgeTasks(minEdge) + 1;
elseif length(cloudTasks) < numVehicles && ...
vehicles(i).computingPower >= maxEdgeComputingPower
% Offload to cloud
cloudTasks = [cloudTasks; task];
disp(['Task ', num2str(task), ' from vehicle ', num2str(i), ' offloaded to cloud']);
% Update number of processing tasks on cloud
numCloudTasks = numCloudTasks + 1;
else
% Offload to neighbor vehicle within threshold distance
for j = 1:numVehicles
if j ~= i && norm(vehicles(i).position - vehicles(j).position) <= thresholdDistance && ...
length(vehicles(j).tasks) < numVehicles && ...
vehicles(j).computingPower >= maxVehicleComputingPower
vehicles(j).tasks = [vehicles(j).tasks; task];
disp(['Task ', num2str(task), ' from vehicle ', num2str(i), ' offloaded to neighbor vehicle ', num2str(j)]);
% Update number of processing tasks on neighbor vehicle
numVehicleTasks(j) = numVehicleTasks(j) + 1;
break;
end
end
end
end
end
% Process tasks at edge nodes
for j = 1:numEdges
if ~isempty(edges(j).tasks)
completedTasks = edges(j).tasks(edges(j).tasks <= t - edgeDelay - edgeProcessingTime);
edges(j).tasks = setdiff(edges(j).tasks, completedTasks);
disp(['Edge node ', num2str(j), ' processed ', num2str(length(completedTasks)), ' tasks']);
% Update number of processing tasks on edge
numEdgeTasks(j) = numEdgeTasks(j) - length(completedTasks);
end
end
% Process tasks at cloud
if ~isempty(cloudTasks)
completedTasks = cloudTasks(cloudTasks <= t - cloudDelay - cloudProcessingTime);
cloudTasks = setdiff(cloudTasks, completedTasks);
disp(['Cloud processed ', num2str(length(completedTasks)), ' tasks']);
% Update number of processing tasks on cloud
numCloudTasks = numCloudTasks - length(completedTasks);
end
end
% Plot number of processing tasks on each neighbor vehicle, edge, and cloud
figure;
subplot(2, 2, 1);
bar(numVehicleTasks);
title('Number of Processing Tasks on Neighbor Vehicles');
xlabel('Neighbor Vehicle');
ylabel('Number of Tasks');
subplot(2, 2, 2);
bar(numEdgeTasks);
title('Number of Processing Tasks on Edge Nodes');
xlabel('Edge Node');
ylabel('Number of Tasks');
subplot(2, 2, 3);
bar(numCloudTasks);
title('Number of Processing Tasks on Cloud');
xlabel('Cloud');
ylabel('Number of Tasks');

回答(2 个)

the cyclist
the cyclist 2024-4-5,19:42
If you set a breakpoint at the code
% Update number of processing tasks on neighbor vehicle
numVehicleTasks(j) = numVehicleTasks(j) + 1;
you will see that you never reach this line, so numVehicleTasks is always zero.
This implies that you never satisfy the condition
norm(vehicles(i).position - vehicles(j).position) <= thresholdDistance && ...
length(vehicles(j).tasks) < numVehicles && ...
vehicles(j).computingPower >= maxVehicleComputingPower
I didn't try to debug it any deeper than that.
  2 个评论
Catherine
Catherine 2024-4-5,20:01
i know this but my question what is the solution or inother words why the conditions aren't satisfied
Dyuman Joshi
Dyuman Joshi 2024-4-6,3:30
编辑:Dyuman Joshi 2024-4-6,3:32
"why the conditions aren't satisfied"
Because the values are so.
Why the values are so? Because of the operations you have performed in the code. And as you get an unexpected result, it is likely that something in your code is not correct.
What that might be, we do not know.

请先登录,再进行评论。


Voss
Voss 2024-4-5,23:09
Here a few things I notice that don't make sense to me. Coincidentally, each of them gives a condition that is always false, and each is in one of the three if/elseif/else branches for offloading a task to an edge node, the cloud, or a neighboring vehicle, respectively.
1. The condition for offloading a task to an edge node includes:
~isempty(find([edges.tasks], 1))
Each element of edges has tasks initially empty, so [edges.tasks] is empty and that condition is initially false. I assume that's what's intended. But the problem is that the only place in the code where a new element of tasks in the edges array is added is in this same "Offload to edge node" section, which only executes if that condition is true at some point. That is to say, if all edges have empty tasks at first, but you need to have at least one non-empty tasks in some elements of edges in order to add another one, then they're all going to remain empty forever. How can you ever add the first one, if there needs to be at least one in there before you add it? Do you see what I mean?
2. The condition for offloading a task to the cloud includes
vehicles(i).computingPower >= maxEdgeComputingPower
but that will always be false given the particular parameter values you have defined. maxEdgeComputingPower is 200 and maxVehicleComputingPower is 100, so no vehicle's computingPower will ever be >= 200.
3. The condition for offloading a task to a neighbor vehicle includes
vehicles(j).computingPower >= maxVehicleComputingPower
but vehicles(j).computingPower is a random number strictly between minVehicleComputingPower and maxVehicleComputingPower, because rand returns a number strictly between 0 and 1. Specifically, rand never returns exactly 1, so vehicles(j).computingPower is always strictly less than maxVehicleComputingPower. Thus that condition is always false.
Commenting out those three always-false conditions produces a situation where the else branch is never reached (because the elseif condition is always true). Commenting out only the first and third produces some non-zero results (and zero cloud tasks still, because of conditon #2), which are shown below, but I have no idea whether those results are correct.
In general, you should think about the interaction between those sets of conditions, and the order that they are checked in: are you sure that you should check for offloading to an edge node first, then check for offloading to the cloud, and finally as a last resort offloading to a neighbor vehicle? (I have no idea.)
clear;
clc;
close all;
% Parameters
numVehicles = 100; % Number of vehicles
numEdges = 3; % Number of edge computing nodes
cloudProcessingTime = 20; % Time taken by the cloud to process a task (in seconds)
edgeProcessingTime = 10; % Time taken by an edge node to process a task (in seconds)
taskGenerationRate = 0.2; % Task generation rate per vehicle (tasks/second)
cloudDelay = 5; % Communication delay for offloading to cloud (in seconds)
edgeDelay = 2; % Communication delay for offloading to edge (in seconds)
simulationTime = 100; % Simulation time (in seconds)
thresholdDistance = 100; % Threshold distance for offloading to neighbor vehicle (meters)
minSpeed = 2; % Minimum speed for random waypoint mobility (m/s)
maxSpeed = 15; % Maximum speed for random waypoint mobility (m/s)
pauseTime = 10; % Pause time for random waypoint mobility (seconds)
numClusters = 5; % Number of clusters in the Things layer
% Initialize vehicles with different computing power
minVehicleComputingPower = 50;
maxVehicleComputingPower = 100;
vehicles = struct('position', [], 'tasks', [], 'computingPower', []);
for i = 1:numVehicles
vehicles(i).position = rand(1, 2) * 1000; % Random initial position
vehicles(i).tasks = [];
vehicles(i).computingPower = minVehicleComputingPower + rand * (maxVehicleComputingPower - minVehicleComputingPower); % Random computing power
end
% Initialize edge nodes with different computing power
minEdgeComputingPower = 50;
maxEdgeComputingPower = 200;
edges = struct('position', [], 'tasks', [], 'computingPower', []);
for i = 1:numEdges
edges(i).position = rand(1, 2) * 1000; % Random position
edges(i).tasks = [];
edges(i).computingPower = minEdgeComputingPower + rand * (maxEdgeComputingPower - minEdgeComputingPower); % Random computing power
end
% Initialize cloud
cloudTasks = [];
% Initialize arrays to store number of processing tasks
numVehicleTasks = zeros(1, numVehicles);
numEdgeTasks = zeros(1, numEdges);
numCloudTasks = 0;
% Initialize clusters in the Things layer
clusters = struct('center', [], 'members', [], 'ipRange', []);
for i = 1:numClusters
clusters(i).center = rand(1, 2) * 1000; % Random center
clusters(i).members = [];
clusters(i).ipRange = ['192.168.', num2str(i), '.']; % IP range for cluster
end
% Random waypoint mobility model for vehicles
for t = 1:simulationTime
% Generate tasks for vehicles
for i = 1:numVehicles
if rand < taskGenerationRate
vehicles(i).tasks = [vehicles(i).tasks; t];
end
end
% Move vehicles (random waypoint mobility model)
for i = 1:numVehicles
if rand < 0.1 % Randomly pause
continue;
end
% Random speed and direction
speed = minSpeed + rand * (maxSpeed - minSpeed);
direction = rand * 2 * pi;
% Update position
vehicles(i).position = vehicles(i).position + [cos(direction), sin(direction)] * speed;
% Wrap-around if outside the area
vehicles(i).position = mod(vehicles(i).position, 1000);
end
% Offload tasks from vehicles to edge nodes, cloud, or neighbor vehicles
for i = 1:numVehicles
if ~isempty(vehicles(i).tasks)
task = vehicles(i).tasks(1);
vehicles(i).tasks(1) = [];
% Decide whether to offload to edge, cloud, or neighbor vehicle
% based on distance, availability, and computing resources
minDistanceEdge = min(arrayfun(@(x) norm(vehicles(i).position - x.position), edges));
if minDistanceEdge <= thresholdDistance && ...
length(cloudTasks) < numVehicles && ...
...~isempty(find([edges.tasks], 1)) && ...
vehicles(i).computingPower >= minEdgeComputingPower
% Offload to edge node
[~, minEdge] = min(arrayfun(@(x) length(x.tasks), edges));
edges(minEdge).tasks = [edges(minEdge).tasks; task];
disp(['Task ', num2str(task), ' from vehicle ', num2str(i), ' offloaded to edge ', num2str(minEdge)]);
% Update number of processing tasks on edge
numEdgeTasks(minEdge) = numEdgeTasks(minEdge) + 1;
elseif length(cloudTasks) < numVehicles && ...
vehicles(i).computingPower >= maxEdgeComputingPower
% Offload to cloud
cloudTasks = [cloudTasks; task];
disp(['Task ', num2str(task), ' from vehicle ', num2str(i), ' offloaded to cloud']);
% Update number of processing tasks on cloud
numCloudTasks = numCloudTasks + 1;
else
% Offload to neighbor vehicle within threshold distance
for j = 1:numVehicles
if j ~= i && norm(vehicles(i).position - vehicles(j).position) <= thresholdDistance && ...
length(vehicles(j).tasks) < numVehicles% && ...
...vehicles(j).computingPower >= maxVehicleComputingPower
vehicles(j).tasks = [vehicles(j).tasks; task];
disp(['Task ', num2str(task), ' from vehicle ', num2str(i), ' offloaded to neighbor vehicle ', num2str(j)]);
% Update number of processing tasks on neighbor vehicle
numVehicleTasks(j) = numVehicleTasks(j) + 1;
break;
end
end
end
end
end
% Process tasks at edge nodes
for j = 1:numEdges
if ~isempty(edges(j).tasks)
completedTasks = edges(j).tasks(edges(j).tasks <= t - edgeDelay - edgeProcessingTime);
edges(j).tasks = setdiff(edges(j).tasks, completedTasks);
disp(['Edge node ', num2str(j), ' processed ', num2str(length(completedTasks)), ' tasks']);
% Update number of processing tasks on edge
numEdgeTasks(j) = numEdgeTasks(j) - length(completedTasks);
end
end
% Process tasks at cloud
if ~isempty(cloudTasks)
completedTasks = cloudTasks(cloudTasks <= t - cloudDelay - cloudProcessingTime);
cloudTasks = setdiff(cloudTasks, completedTasks);
disp(['Cloud processed ', num2str(length(completedTasks)), ' tasks']);
% Update number of processing tasks on cloud
numCloudTasks = numCloudTasks - length(completedTasks);
end
end
% Plot number of processing tasks on each neighbor vehicle, edge, and cloud
figure;
subplot(2, 2, 1);
bar(numVehicleTasks);
title('Number of Processing Tasks on Neighbor Vehicles');
xlabel('Neighbor Vehicle');
ylabel('Number of Tasks');
subplot(2, 2, 2);
bar(numEdgeTasks);
title('Number of Processing Tasks on Edge Nodes');
xlabel('Edge Node');
ylabel('Number of Tasks');
subplot(2, 2, 3);
bar(numCloudTasks);
title('Number of Processing Tasks on Cloud');
xlabel('Cloud');
ylabel('Number of Tasks');

标签

Community Treasure Hunt

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

Start Hunting!

Translated by