Contents
Equigrouping Demo
%This function is the Main executable for 1-D equigrouping. The %input 'm' represents the number of groups, 'p' the size of the groups, and %'r' the radius of the rule to be used by the rule. S is the number of internal states an agent can %have. The output 'x' is a matrix whose first component is the timestep and for which x(i,:) is the %vector of agent positions at timestep i.
Initialize executables
clear functions; %always clear functions at the top of your main executable because there might have %been a previously-called executable which used different functions with the same names addpath('../common_infrastructure'); %this loads common infrastructure functions addpath('../motion_rules'); %this loads the motion rules
Initialize state variables
m = 3; p = 3; % n is the number of agents, equal to the number of groups times the size of each group n = m*p; % here is where you set the value of the radius of your local rule. r = 2*p + 1; %now we choose an initial configuration of stateless agents that is guaranteed to be r-connected x(1,1:n) = initial_config(n,r,1); %or can input a hard-coded test initial configuration instead of random generation %x(1,1:n) = [1 2 3 (r+2) (r+3) (r+4)]; %initializes the halting check variable 'done' and timestep counter 'i' done = 0; i = 1;
Initialize image creation
%set the bounds of the image window (play with these to get better results xmin = x(1,1) - r; xmax = x(1,n) + r; ymin = 0; ymax = 2; %produce an image of the initial condition figure axis([xmin xmax ymin ymax]); size = 150; %this command sets the size of the agent-dots scatter(x(i,:), ones(1,n), size, 'filled') pause(5)

the main iteration
while (~done) %while we're NOT done, for k = 1:n %then for all agents between 1 and n b = cut_out(x(i,:),ones(1,n),k,r); %compute the local view around agent k pos_action(k) = enact(equigrouping_rule(b,p),b); %and compute the action at that agent k would take (this where the main thing is done) end norm_action = sum(abs(pos_action)); %the compute the norm of action, i.e. the number of agents that would take non-Stay actions in the given configuration if (norm_action == 0) %if the norm of action is zero, i.e. nobody would take a non-stay action, done = 1; %then we're at a fixed point, and we set the Done variable to 1 to stop the time loop else %otherwise, i = i+1; %we increment the time counter x(i,:) = x(i-1,:); %initialize the position configuration at time i to be the same as the configuration at the previous time step j = agent_choice(i,x(i-1,:),ones(1,n)); %pick an agent to act via the function 'agent_choice', which can be random, ordered, whatever x(i,j) = x(i,j) + pos_action(j); %and make the chosen agent take its action (which might of course be to Stay) if (x(i,j) ~= x(i-1,j)) %if an agent has changed position, pause (0.005) %we take a short pause makes it easier to see what's going on scatter(x(i,:), ones(1,n), size,'filled') % and the plot the current state of the system end end end
