diff --git a/@agent/initialize.m b/@agent/initialize.m index e067518..756ec3e 100644 --- a/@agent/initialize.m +++ b/@agent/initialize.m @@ -1,4 +1,4 @@ -function obj = initialize(obj, pos, vel, pan, tilt, collisionGeometry, sensorModel, guidanceModel, comRange, index, label, debug, plotCommsGeometry) +function obj = initialize(obj, pos, vel, pan, tilt, collisionGeometry, sensorModel, guidanceModel, comRange, label, debug, plotCommsGeometry) arguments (Input) obj (1, 1) {mustBeA(obj, 'agent')}; pos (1, 3) double; @@ -6,12 +6,12 @@ function obj = initialize(obj, pos, vel, pan, tilt, collisionGeometry, sensorMod pan (1, 1) double; tilt (1, 1) double; collisionGeometry (1, 1) {mustBeGeometry}; - sensorModel (1, 1) {mustBeSensor} + sensorModel (1, 1) {mustBeSensor}; guidanceModel (1, 1) {mustBeA(guidanceModel, 'function_handle')}; - comRange (1, 1) double = NaN; + comRange (1, 1) double; label (1, 1) string = ""; debug (1, 1) logical = false; - plotCommsGeometry (1, 1) logical = true; + plotCommsGeometry (1, 1) logical = false; end arguments (Output) obj (1, 1) {mustBeA(obj, 'agent')}; @@ -28,6 +28,12 @@ function obj = initialize(obj, pos, vel, pan, tilt, collisionGeometry, sensorMod obj.debug = debug; obj.plotCommsGeometry = plotCommsGeometry; + if isempty(obj.label) + obj.label = sprintf("Agent %d"); + end + + + % Add spherical geometry based on com range obj.commsGeometry = obj.commsGeometry.initialize(obj.pos, comRange, REGION_TYPE.COMMS, sprintf("%s Comms Geometry", obj.label)); diff --git a/@miSim/initialize.m b/@miSim/initialize.m index cd62a73..67a480c 100644 --- a/@miSim/initialize.m +++ b/@miSim/initialize.m @@ -52,6 +52,11 @@ function obj = initialize(obj, domain, objective, agents, minAlt, timestep, part % Define agents obj.agents = agents; obj.constraintAdjacencyMatrix = logical(eye(size(agents, 1))); + for ii = 1:size(obj.agents, 1) + if isempty(char(obj.agents{ii}.label)) + obj.agents{ii}.label = sprintf("Agent %d", ii); + end + end % Compute adjacency matrix and lesser neighbors obj = obj.updateAdjacency(); diff --git a/test/test_miSim.m b/test/test_miSim.m index c2968a5..e96052e 100644 --- a/test/test_miSim.m +++ b/test/test_miSim.m @@ -162,7 +162,7 @@ classdef test_miSim < matlab.unittest.TestCase sensor = sensor.initialize(tc.alphaDistMin + rand * (tc.alphaDistMax - tc.alphaDistMin), tc.betaDistMin + rand * (tc.betaDistMax - tc.betaDistMin), NaN, NaN, tc.alphaTiltMin + rand * (tc.alphaTiltMax - tc.alphaTiltMin), tc.betaTiltMin + rand * (tc.betaTiltMax - tc.betaTiltMin)); % Initialize candidate agent - newAgent = tc.agents{ii}.initialize(candidatePos, zeros(1,3), 0, 0, candidateGeometry, sensor, @gradientAscent, tc.comRange, sprintf("Agent %d", ii)); + newAgent = tc.agents{ii}.initialize(candidatePos, zeros(1,3), 0, 0, candidateGeometry, sensor, @gradientAscent, tc.comRange); % Make sure candidate agent doesn't collide with % domain @@ -296,7 +296,7 @@ classdef test_miSim < matlab.unittest.TestCase sensor = sensor.initialize(tc.alphaDistMin + rand * (tc.alphaDistMax - tc.alphaDistMin), tc.betaDistMin + rand * (tc.betaDistMax - tc.betaDistMin), NaN, NaN, tc.alphaTiltMin + rand * (tc.alphaTiltMax - tc.alphaTiltMin), tc.betaTiltMin + rand * (tc.betaTiltMax - tc.betaTiltMin)); % Initialize candidate agent - newAgent = tc.agents{ii}.initialize(candidatePos, zeros(1,3), 0, 0, candidateGeometry, sensor, @gradientAscent, tc.comRange, sprintf("Agent %d", ii), false, false); + newAgent = tc.agents{ii}.initialize(candidatePos, zeros(1,3), 0, 0, candidateGeometry, sensor, @gradientAscent, tc.comRange); % Make sure candidate agent doesn't collide with % domain @@ -378,14 +378,14 @@ classdef test_miSim < matlab.unittest.TestCase % Initialize agents tc.agents = {agent; agent}; - tc.agents{1} = tc.agents{1}.initialize(tc.domain.center + dh + [d, 0, 0], zeros(1,3), 0, 0, geometry1, sensor, @gradientAscent, 3*d, sprintf("Agent %d", 1)); - tc.agents{2} = tc.agents{2}.initialize(tc.domain.center + dh - [d, 0, 0], zeros(1,3), 0, 0, geometry2, sensor, @gradientAscent, 3*d, sprintf("Agent %d", 2)); + tc.agents{1} = tc.agents{1}.initialize(tc.domain.center + dh + [d, 0, 0], zeros(1,3), 0, 0, geometry1, sensor, @gradientAscent, 3*d); + tc.agents{2} = tc.agents{2}.initialize(tc.domain.center + dh - [d, 0, 0], zeros(1,3), 0, 0, geometry2, sensor, @gradientAscent, 3*d); % Optional third agent along the +Y axis geometry3 = rectangularPrism; geometry3 = geometry3.initialize([tc.domain.center + dh - [0, d, 0] - tc.collisionRanges(1) * ones(1, 3); tc.domain.center + dh - [0, d, 0] + tc.collisionRanges(1) * ones(1, 3)], REGION_TYPE.COLLISION, sprintf("Agent %d collision volume", 3)); tc.agents{3} = agent; - tc.agents{3} = tc.agents{3}.initialize(tc.domain.center + dh - [0, d, 0], zeros(1, 3), 0, 0, geometry3, sensor, @gradientAscent, 3*d, sprintf("Agent %d", 3)); + tc.agents{3} = tc.agents{3}.initialize(tc.domain.center + dh - [0, d, 0], zeros(1, 3), 0, 0, geometry3, sensor, @gradientAscent, 3*d); % Initialize the simulation tc.testClass = tc.testClass.initialize(tc.domain, tc.domain.objective, tc.agents, tc.minAlt, tc.timestep, tc.partitoningFreq, tc.maxIter, cell(0, 1), tc.makeVideo); @@ -415,7 +415,7 @@ classdef test_miSim < matlab.unittest.TestCase % Initialize agents tc.agents = {agent}; - tc.agents{1} = tc.agents{1}.initialize([tc.domain.center(1:2), 3], zeros(1,3), 0, 0, geometry1, sensor, @gradientAscent, 3, sprintf("Agent %d", 1)); + tc.agents{1} = tc.agents{1}.initialize([tc.domain.center(1:2), 3], zeros(1,3), 0, 0, geometry1, sensor, @gradientAscent, 3); % Initialize the simulation tc.testClass = tc.testClass.initialize(tc.domain, tc.domain.objective, tc.agents, tc.minAlt, tc.timestep, tc.partitoningFreq, tc.maxIter, cell(0, 1), tc.makeVideo); @@ -445,7 +445,7 @@ classdef test_miSim < matlab.unittest.TestCase % Initialize agents tc.agents = {agent}; - tc.agents{1} = tc.agents{1}.initialize([tc.domain.center(1:2)-tc.domain.dimensions(1)/3, 3], zeros(1,3), 0, 0, geometry1, sensor, @gradientAscent, 3, sprintf("Agent %d", 1), true); + tc.agents{1} = tc.agents{1}.initialize([tc.domain.center(1:2)-tc.domain.dimensions(1)/3, 3], zeros(1,3), 0, 0, geometry1, sensor, @gradientAscent, 3, "", true); % Initialize the simulation tc.testClass = tc.testClass.initialize(tc.domain, tc.domain.objective, tc.agents, tc.minAlt, tc.timestep, tc.partitoningFreq, tc.maxIter, cell(0, 1), tc.makeVideo); @@ -481,8 +481,8 @@ classdef test_miSim < matlab.unittest.TestCase % Initialize agents tc.agents = {agent; agent}; - tc.agents{1} = tc.agents{1}.initialize(tc.domain.center + d, zeros(1,3), 0, 0, geometry1, sensor, @gradientAscent, 3, sprintf("Agent %d", 1), false); - tc.agents{2} = tc.agents{2}.initialize(tc.domain.center - d, zeros(1,3), 0, 0, geometry2, sensor, @gradientAscent, 3, sprintf("Agent %d", 2), false); + tc.agents{1} = tc.agents{1}.initialize(tc.domain.center + d, zeros(1,3), 0, 0, geometry1, sensor, @gradientAscent, 3); + tc.agents{2} = tc.agents{2}.initialize(tc.domain.center - d, zeros(1,3), 0, 0, geometry2, sensor, @gradientAscent, 3); % Initialize the simulation tc.testClass = tc.testClass.initialize(tc.domain, tc.domain.objective, tc.agents, tc.minAlt, tc.timestep, tc.partitoningFreq, tc.maxIter, cell(0, 1), tc.makeVideo, tc.makePlots); @@ -516,8 +516,8 @@ classdef test_miSim < matlab.unittest.TestCase % Initialize agents tc.agents = {agent; agent;}; - tc.agents{1} = tc.agents{1}.initialize(tc.domain.center - d + [0, radius * 1.5, 0], zeros(1,3), 0, 0, geometry1, sensor, @gradientAscent, 10, sprintf("Agent %d", 1), false, false); - tc.agents{2} = tc.agents{2}.initialize(tc.domain.center - d - [0, radius * 1.5, 0], zeros(1,3), 0, 0, geometry2, sensor, @gradientAscent, 10, sprintf("Agent %d", 2), false, false); + tc.agents{1} = tc.agents{1}.initialize(tc.domain.center - d + [0, radius * 1.5, 0], zeros(1,3), 0, 0, geometry1, sensor, @gradientAscent, 10); + tc.agents{2} = tc.agents{2}.initialize(tc.domain.center - d - [0, radius * 1.5, 0], zeros(1,3), 0, 0, geometry2, sensor, @gradientAscent, 10); % Initialize obstacles obstacleLength = 1; @@ -557,8 +557,8 @@ classdef test_miSim < matlab.unittest.TestCase % Initialize agents commsRadius = 5; tc.agents = {agent; agent;}; - tc.agents{1} = tc.agents{1}.initialize(tc.domain.center - [d, 0, 0], zeros(1,3), 0, 0, geometry1, sensor, @gradientAscent, commsRadius, sprintf("Agent %d", 1), false); - tc.agents{2} = tc.agents{2}.initialize(tc.domain.center - [0, d, 0], zeros(1,3), 0, 0, geometry2, sensor, @gradientAscent, commsRadius, sprintf("Agent %d", 2), false); + tc.agents{1} = tc.agents{1}.initialize(tc.domain.center - [d, 0, 0], zeros(1,3), 0, 0, geometry1, sensor, @gradientAscent, commsRadius); + tc.agents{2} = tc.agents{2}.initialize(tc.domain.center - [0, d, 0], zeros(1,3), 0, 0, geometry2, sensor, @gradientAscent, commsRadius); % Initialize obstacles obstacleLength = 1.5; @@ -605,14 +605,14 @@ classdef test_miSim < matlab.unittest.TestCase % Initialize agents commsRadius = d; tc.agents = {agent; agent; agent; agent; agent;}; - tc.agents{1} = tc.agents{1}.initialize(tc.domain.center + [d, 0, 0], zeros(1,3), 0, 0, geometry1, sensor, @gradientAscent, commsRadius, sprintf("Agent %d", 1), false); - tc.agents{2} = tc.agents{2}.initialize(tc.domain.center, zeros(1,3), 0, 0, geometry2, sensor, @gradientAscent, commsRadius, sprintf("Agent %d", 2), false); - tc.agents{3} = tc.agents{3}.initialize(tc.domain.center + [-d, d, 0], zeros(1,3), 0, 0, geometry3, sensor, @gradientAscent, commsRadius, sprintf("Agent %d", 3), false); - tc.agents{4} = tc.agents{4}.initialize(tc.domain.center + [-2*d, d, 0], zeros(1,3), 0, 0, geometry4, sensor, @gradientAscent, commsRadius, sprintf("Agent %d", 4), false); - tc.agents{5} = tc.agents{5}.initialize(tc.domain.center + [0, d, 0], zeros(1,3), 0, 0, geometry5, sensor, @gradientAscent, commsRadius, sprintf("Agent %d", 5), false); + tc.agents{1} = tc.agents{1}.initialize(tc.domain.center + [d, 0, 0], zeros(1,3), 0, 0, geometry1, sensor, @gradientAscent, commsRadius); + tc.agents{2} = tc.agents{2}.initialize(tc.domain.center, zeros(1,3), 0, 0, geometry2, sensor, @gradientAscent, commsRadius); + tc.agents{3} = tc.agents{3}.initialize(tc.domain.center + [-d, d, 0], zeros(1,3), 0, 0, geometry3, sensor, @gradientAscent, commsRadius); + tc.agents{4} = tc.agents{4}.initialize(tc.domain.center + [-2*d, d, 0], zeros(1,3), 0, 0, geometry4, sensor, @gradientAscent, commsRadius); + tc.agents{5} = tc.agents{5}.initialize(tc.domain.center + [0, d, 0], zeros(1,3), 0, 0, geometry5, sensor, @gradientAscent, commsRadius); % TODO - % make agent label and ID optional, they can be derived from index + % make agent label optional, can be derived from index % Consider how to do the same for collision geometry label % make @gradientAscent always the choice % Build collision geometry initialization into agent initialization?