how to set up two different populations in a segregation model code?

3 vues (au cours des 30 derniers jours)
Wenchen Liu
Wenchen Liu le 2 Déc 2021
I have a general code for the segregation model like the following:
%% n = grid_size;
grid_size = 50;
n = grid_size;
n2 = n^2;
grids = zeros(n);
emp = round((percent_empty/100)*n2);
two = round((percent_two/100)*n2);
tot = emp + two;
rand_loc = randperm(n2);
init_emp = rand_loc(1:emp);
init_two = rand_loc(emp+1:tot);
init_one = rand_loc(tot+1:end);
grids(init_emp) = 3;
grids(init_two) = 2;
grids(init_one) = 1;
figure, imagesc(grids), colorbar, axis([1 n 1 n]);
% frame = getframe;
% writeVideo(writerObj,frame);
how could I insert two populations in this code, that is population 1 are unsatis ed with their location if the number of their neighbours that are from population 2 is u1 or higher, and population 2 occupants are unsatis ed with their location if the number of their neighbours that are from population 1 is u2 or higher. Beside this, p1+p2 is less than 100; the rest can be empty.

Réponses (1)

Abhinaya Kennedy
Abhinaya Kennedy le 15 Fév 2024
Hi,
To modify the existing code to include two populations with specific satisfaction conditions, you would need to add functionality to:
  • Define the satisfaction thresholds u1 and u2 for populations 1 and 2, respectively.
  • Calculate the number of neighbours of each type surrounding each cell.
  • Move unsatisfied occupants to empty cells until all occupants are satisfied or no more moves can be made.
Here's an outline of how you can modify the code to include these steps:
% Define the grid size and the percentages for each population
grid_size = 50;
percent_empty = 10; % You can set this value as needed
percent_one = 45; % Population 1 percentage
percent_two = 45; % Population 2 percentage
% Define satisfaction thresholds
u1 = 3; % Threshold for population 1
u2 = 3; % Threshold for population 2
% Initialize the grid
n = grid_size;
n2 = n^2;
grids = zeros(n);
% Calculate the number of each type
emp = round((percent_empty/100)*n2);
one = round((percent_one/100)*n2);
two = round((percent_two/100)*n2);
% Randomly assign initial locations
rand_loc = randperm(n2);
init_emp = rand_loc(1:emp);
init_one = rand_loc(emp+1:emp+one);
init_two = rand_loc(emp+one+1:end);
% Fill the grid with populations and empty spaces
grids(init_emp) = 0; % Empty cells
grids(init_one) = 1; % Population 1
grids(init_two) = 2; % Population 2
% Display the initial grid
figure, imagesc(grids), colorbar, axis square;
% Main loop to move unsatisfied occupants
max_iterations = 1000; % Set a maximum number of iterations to prevent infinite loop
iteration = 0;
while iteration < max_iterations
iteration = iteration + 1;
moved = false;
for x = 1:n
for y = 1:n
if ~check_satisfaction(grids, x, y, u1, u2)
% Find an empty cell to move to
empty_cells = find(grids == 0);
if ~isempty(empty_cells)
new_loc = empty_cells(randi(length(empty_cells)));
grids(new_loc) = grids(x, y); % Move occupant to the new location
grids(x, y) = 0; % Set old location to empty
moved = true;
end
end
end
end
% Update the display
figure, imagesc(grids), colorbar, axis square;
% If no one moved in this iteration, everyone is satisfied
if ~moved
break;
end
end
% Display the final grid
figure, imagesc(grids), colorbar, axis square;
% Function definitions must appear at the end of the file
% Function to calculate the number of neighbors of a given type
function count = count_neighbors(grids, x, y, neighbor_type)
[rows, cols] = size(grids);
count = 0;
for i = -1:1
for j = -1:1
if ~(i == 0 && j == 0) && x+i > 0 && x+i <= rows && y+j > 0 && y+j <= cols
if grids(x+i, y+j) == neighbor_type
count = count + 1;
end
end
end
end
end
% Function to determine if an occupant is satisfied
function is_satisfied = check_satisfaction(grids, x, y, u1, u2)
if grids(x, y) == 1
is_satisfied = count_neighbors(grids, x, y, 2) < u1;
elseif grids(x, y) == 2
is_satisfied = count_neighbors(grids, x, y, 1) < u2;
else
is_satisfied = true;
end
end

Catégories

En savoir plus sur Blue dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by