function [number_edges_store,InitialMatrix,FinalMatrix] = Organization_Control_An_Evolving_Interdependent_Population_Code(N,n_l,p,b,num_reproduce,numGen,tau,num_selected,beta,initMatrix,storeInitialMatrix,storeFinalMatrix) % N = number of cells % n_l = initial number of links % p = number of mutations is approximately p*N^2 % b = benefit to fitness for taking vs. giving % num_reproduce = number that reproduce (by replacement) % numGen = number of generations % initMatrix = given initial. Input of [] will generate a random one % tau = when to apply dose % num_selected = number of individuals selected in medicine step % beta = probabilistic number of links affected % initialize if isempty(initMatrix) connectivitymatrix = spalloc(N,N,N+n_l); one_idx = randperm(N^2,n_l); % choose n_l random nodes (allowing diagonal entries to be included) connectivitymatrix(one_idx) = 1; % make n_l random links else connectivitymatrix = sparse(initMatrix); end if tau == 1 affected_cells = randperm(N,num_selected); for j = 1:length(affected_cells) choicevector = (rand(1,N) > beta); connectivitymatrix(affected_cells(j),:) = connectivitymatrix(affected_cells(j),:).*choicevector; % turn beta percent of 1s to 0 in the row end end if storeInitialMatrix == 'y' InitialMatrix = connectivitymatrix; else InitialMatrix = []; % save blank end number_edges_store = zeros(1,numGen); % number of edges at a given timestep number_edges_store(1) = nnz(connectivitymatrix); % note: we treat diagonal elements (self-loops) as 1 INCOMING arrow and 1 OUTGOING arrow (0 benefit, increase in complexity) for i = 2:numGen if i == tau affected_cells = randperm(N,num_selected); for j = 1:length(affected_cells) choicevector = (rand(1,N) > beta); connectivitymatrix(affected_cells(j),:) = connectivitymatrix(affected_cells(j),:).*choicevector; % turn beta percent of 1s to 0 in the row (affect giving) end end % Mutation connectivitymatrix = abs(connectivitymatrix-logical(sprand(N,N,p))); % Note: p should generally be relatively small compared to 1/N: if p*N^2 is large, there may be significantly fewer zeros generated by this function than expected % Fitness calculation connectivitymatrix = connectivitymatrix - diag(diag(connectivitymatrix)); % remove diagonals fitness_vector = b*sum(connectivitymatrix,1)-(sum(connectivitymatrix,2))'; % Updating [~,I] = sort(fitness_vector); min_n = I(1,(1:num_reproduce)); max_n = I(1,(end-num_reproduce+1:end)); connectivitymatrix(min_n,:) = 0; connectivitymatrix(:,min_n) = 0; connectivitymatrix(min_n,:) = connectivitymatrix(max_n,:); connectivitymatrix(:,min_n) = connectivitymatrix(:,max_n); % record number of edges number_nonzero_elements = nnz(connectivitymatrix); number_edges_store(i) = number_nonzero_elements; end if storeFinalMatrix == 'y' FinalMatrix = connectivitymatrix; else FinalMatrix = []; end