From d30fd9ccaa004439b66a505c4fe540b1076b6d7c Mon Sep 17 00:00:00 2001 From: Kevin D Date: Mon, 1 Dec 2025 22:58:38 -0800 Subject: [PATCH] fixed performance plot after 50th timestep --- @agent/run.m | 25 ++++++++++--------------- @miSim/plotPerformance.m | 7 +++++-- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/@agent/run.m b/@agent/run.m index 3fc1a24..16566c1 100644 --- a/@agent/run.m +++ b/@agent/run.m @@ -29,17 +29,17 @@ function obj = run(obj, domain, partitioning, t) obj.performance = [obj.performance, sum(C(~isnan(C)))]; % Compute gradient on agent's performance - [gradSensorPerformanceX, gradSensorPerformanceY] = gradient(S, domain.objective.discretizationStep); % grad S_n - [gradObjectiveX, gradObjectiveY] = gradient(F, domain.objective.discretizationStep); % grad f - - gradS = cat(3, gradSensorPerformanceX, gradSensorPerformanceY, zeros(size(gradSensorPerformanceX))); % grad S_n - gradF = cat(3, gradObjectiveX, gradObjectiveY, zeros(size(gradObjectiveX))); % grad f - [gradCX, gradCY] = gradient(C, domain.objective.discretizationStep); % grad C; gradC = cat(3, gradCX, gradCY, zeros(size(gradCX))); % temp zeros for gradCZ nGradC = vecnorm(gradC, 2, 3); if obj.debug + % Compute additional component-level values for diagnosing issues + [gradSensorPerformanceX, gradSensorPerformanceY] = gradient(S, domain.objective.discretizationStep); % grad S_n + [gradObjectiveX, gradObjectiveY] = gradient(F, domain.objective.discretizationStep); % grad f + gradS = cat(3, gradSensorPerformanceX, gradSensorPerformanceY, zeros(size(gradSensorPerformanceX))); % grad S_n + gradF = cat(3, gradObjectiveX, gradObjectiveY, zeros(size(gradObjectiveX))); % grad f + ii = 8; hold(obj.debugFig.Children(1).Children(ii), "on"); imagesc(obj.debugFig.Children(1).Children(ii), F./max(F, [], 'all')); @@ -96,26 +96,21 @@ function obj = run(obj, domain, partitioning, t) end end - % grad(s*f) = grad(f) * s + f * grad(s) - product rule (f scalar field, s vector field) - % gradC = S .* abs(gradF) + F .* abs(gradS); % second term provides altitude - % normalize in x3 dimension and find the direction which maximizes ascent - % nGradC = vecnorm(gradC, 2, 3); - [xNextIdx, yNextIdx] = find(nGradC == max(nGradC, [], 'all')); % find direction of steepest increase + % Use largest grad(C) value to find the direction of the next position + [xNextIdx, yNextIdx] = find(nGradC == max(nGradC, [], 'all')); roundingScale = 10^-log10(domain.objective.discretizationStep); pNext = [floor(roundingScale .* mean(unique(domain.objective.X(:, xNextIdx))))./roundingScale, floor(roundingScale .* mean(unique(domain.objective.Y(yNextIdx, :))))./roundingScale, obj.pos(3)]; % have to do some unfortunate rounding here soemtimes + % Determine next position vDir = (pNext - obj.pos)./norm(pNext - obj.pos, 2); rate = 0.2 - 0.004 * t; nextPos = obj.pos + vDir * rate; % Move to next position - % (dynamics not modeled at this time) obj.lastPos = obj.pos; obj.pos = nextPos; - % Calculate movement - d = obj.pos - obj.collisionGeometry.center; - % Reinitialize collision geometry in the new position + d = obj.pos - obj.collisionGeometry.center; obj.collisionGeometry = obj.collisionGeometry.initialize([obj.collisionGeometry.minCorner; obj.collisionGeometry.maxCorner] + d, obj.collisionGeometry.tag, obj.collisionGeometry.label); end \ No newline at end of file diff --git a/@miSim/plotPerformance.m b/@miSim/plotPerformance.m index 5dff115..27b6305 100644 --- a/@miSim/plotPerformance.m +++ b/@miSim/plotPerformance.m @@ -15,14 +15,17 @@ function obj = plotPerformance(obj) % Plot current cumulative performance hold(obj.fPerf.Children(1), 'on'); o = plot(obj.fPerf.Children(1), obj.perf(end, :)); - o.XData = NaN(size(o.XData)); % correct time will be set at runtime + warning('off', 'MATLAB:gui:array:InvalidArrayShape'); % suppress this warning to avoid polluting output + o.XData = NaN(1, obj.maxIter); % correct time will be set at runtime + o.YData = [0, NaN(1, obj.maxIter - 1)]; hold(obj.fPerf.Children(1), 'off'); % Plot current agent performance for ii = 1:(size(obj.perf, 1) - 1) hold(obj.fPerf.Children(1), 'on'); o = [o; plot(obj.fPerf.Children(1), obj.perf(ii, :))]; - o(end).XData = NaN(size(o(end).XData)); % correct time will be set at runtime + o(end).XData = NaN(1, obj.maxIter); % correct time will be set at runtime + o(end).YData = [0, NaN(1, obj.maxIter - 1)]; hold(obj.fPerf.Children(1), 'off'); end