create given number of unique permutations

5 次查看(过去 30 天)
I am preparing a speed-dating type work event where people will be placed into one of five groups to work together for 10mins, then I'll ring a bell and they'll move to a different group activity. All participants will have a card showing them the order that they should move through the activities [e.g., a participant with the card '5, 2, 3, 1, 4' would engage in activity 5 first, and then move to activity 2, etc until they finish on activity 4]. I'm trying to generate these cards for all participants so that everyone has a unique journey through the five activities.
I need:
  • Everyone to attend each activity once only
  • The number of people in each group at one time to be closely matched (don't want loads of people arriving at the same activity at once, while others are relatively empty)
  • Every participant to have a unique order of attending groups
The following code gives me 120 unique ways that a participants can move through the 5 activities. If used this list to make the cards, there would always be 24 people engaging with each activity so the group sizes would be perfectly matched and no two people would have the exact same experience - great!
activities = [1,2,3,4,5];
P = perms(activities)
However, I am only expecting 60 people! I want to cut this list down so that I have 12 people engaging in each activity at a time. If I simply cut this list in half, I lose the balance of people in the different groups. Is there any easy way to cut this to 60 unique permutations with the restriction that I need to balance across the positions?
  2 个评论
Madheswaran
Madheswaran 2024-9-4
Hi Rachael,
If your constraints are
  • At any point of time, each activity must have exactly 12 people
  • For each person, the list of activities must not be repeated and should cover all the activities
Then I suggest this below code without using 'perm' function.
The idea behind the code is simple, divide the 60 people into 5 different batches. For the first activity, allocate the batch 1 to activity 1, batch-2 to activity-2, and so on. For the second activity, cycle the batches onto the next set of activities as explained in the code.
num_people = 60;
num_activities = 5;
cards = zeros(num_people, num_activities); % 60 * 5
batch1 = 1:12;
batch2 = 13:24;
batch3 = 25:36;
batch4 = 37:48;
batch5 = 49:60;
for activity_i = 1:5
cards(batch1, activity_i) = mod(activity_i + 0, num_activities);
cards(batch2, activity_i) = mod(activity_i + 1, num_activities);
cards(batch3, activity_i) = mod(activity_i + 2, num_activities);
cards(batch4, activity_i) = mod(activity_i + 3, num_activities);
cards(batch5, activity_i) = mod(activity_i + 4, num_activities);
end
% replace zeros with 5
cards(cards == 0) = 5;
You can shuffle the cards while delivering it to the participants for the effect of randomness.
Rachael
Rachael 2024-9-4
Thank you but the third contraint is
  • Every participant must have a unique order of attending groups
This is because the event is a speed-dating type excercise designed to let people mix and mingle through the different activities. I don't want any two people going through all activities together. I want everyone to have a unique journey and work with many different people accross the activites.

请先登录,再进行评论。

采纳的回答

Rachael
Rachael 2024-9-4
I asked ChatGPT and it provided a working solution - copied below.
The first solition could produce fewer than 60 permutations, so i asked it to add a section to check that we had 60 permutations in the solution. it works well. :)
% Step 1: Generate all 120 permutations
activities = [1, 2, 3, 4, 5];
P = perms(activities);
% Step 2: Initialize counters to track the number of times each activity
% appears in each position across the selected permutations
counters = zeros(5, 5);
% Step 3: Initialize an empty matrix to store the selected permutations
selected_permutations = [];
% Step 4: Iterate through all permutations and select a balanced subset
for i = 1:size(P, 1)
perm = P(i, :);
can_select = true;
% Check if adding this permutation would exceed 12 occurrences for any activity in any position
for j = 1:5
if counters(j, perm(j)) >= 12
can_select = false;
break;
end
end
% If the permutation can be selected, add it to the selected permutations
if can_select
selected_permutations = [selected_permutations; perm];
for j = 1:5
counters(j, perm(j)) = counters(j, perm(j)) + 1;
end
end
% If we've already selected 60 permutations, break out of the loop
if size(selected_permutations, 1) == 60
break;
end
end
% Ensure we have exactly 60 selected permutations
% If less than 60 permutations are selected, repeat the loop to fill in the remaining
while size(selected_permutations, 1) < 60
for i = 1:size(P, 1)
if size(selected_permutations, 1) == 60
break;
end
perm = P(i, :);
can_select = true;
% Check again if adding this permutation would maintain balance
for j = 1:5
if counters(j, perm(j)) >= 12
can_select = false;
break;
end
end
if can_select
selected_permutations = [selected_permutations; perm];
for j = 1:5
counters(j, perm(j)) = counters(j, perm(j)) + 1;
end
end
end
end
% Step 5: Display the selected permutations
disp('Selected Permutations (Each row represents a participant):');
disp(selected_permutations);
  2 个评论
John D'Errico
John D'Errico 2024-9-4
编辑:John D'Errico 2024-9-4
ALWAYS be very careful with the AI tools, as too often they provide complete garbage, that seems vaguely correct. Test it. Verify it. But if you implicitly trust something just because a computer wrote it, you get what you paid for.
Rachael
Rachael 2024-9-4
thank you - yes, of course :) I have checked the output and it has provided just what I need.

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by