I’m trying to generate semi-random subsets with some restrictions.
Here are the variable descriptions with example values:
- ObjCount – the number of objects (12)
- VisibleCount (AKA SetSize) – the number of objects per set (6)
- SetCount – the number of sets (12)
- ObjAppearances – the number of set in which an object appears =
SetCount * VisibleCount / ObjCount
I need to produce a given number of sets (SetCount) that follow these rules:
- Each set is a collection of objects, but no object can be in a single set more than once.
- Furthermore, each object should be in the same number of sets. If it doesn’t devide evenly, then the number sets in which an object appears can be off by 1 (some objects are in 4 sets, and others are in 5). I’ll try to avoid this situation, so it’s not critical.
It’s turning out to be far less trivial than I initially thought. Could anyone help me with some code/psuedocode? A solution to a generalized version would be very helpful too.
Thanks in advance.
Edit: VisibleCount is the set size. The number of times an object appears (ObjAppearances) is SetCount * VisibleCount / ObjCount
Edit2: I should also add that I want the sets to be fairly random. If the sets all have sequential objects (e.g. set1:5,6,7 set2:3,4,5 set3:10,11,0), the solution isn’t useful. Sorry for not making that clear.
Edit3: Here is a solution that DOES NOT work. (In C#)
static void Main(string[] args)
{
var ObjectCount = 12;
var SetSize = 6;
var SetCount = 12;
var Sets = Enumerable.Range(0, SetCount).Select(i => new List<int>()).ToArray(); // a SetCount-sized array of lists
var ObjectAppearances = SetSize * SetCount / ObjectCount;
var rand = new Random();
// fill the sets
for (int obj = 0; obj < ObjectCount; obj++)
{
for (int a = 0; a < ObjectAppearances; a++)
{
// get the collection of sets that are not full
var nonFullSets = Sets.Where(s => s.Count < SetSize);
// get the collection of non-full sets without obj
var setsWithoutObj = nonFullSets.Where(s => !s.Contains(obj));
///////////////////////
// Here is the problem. All of the non-full sets may already
// have a copy of obj
///////////////////////
// choose a set at random
var currentSetIndex = rand.Next(setsWithoutObj.Count());
var currentSet = setsWithoutObj.ElementAt(currentSetIndex);
// add the object
currentSet.Add(obj);
}
}
// randomize the order within each set and output each
for (int i = 0; i < SetCount; i++)
{
var randomlyOrderedSet = Sets[i].OrderBy(obj => rand.Next());
Sets[i] = new List<int>(randomlyOrderedSet);
// output
foreach (var obj in Sets[i])
Console.Write(string.Format("{0}, ", obj));
Console.WriteLine();
}
Console.ReadLine();
}
Here’s the Solution – An implementation of MizardX’s answer
static void Main(string[] args)
{
var ObjectCount = 12;
var SetSize = 6;
var SetCount = 10;
var rand = new Random();
// make a matrix [SetCount][ObjectCount]
var Matrix = new int[SetCount][];
for (int s = 0; s < SetCount; s++)
Matrix[s] = Enumerable.Repeat(0, ObjectCount).ToArray();
// put approximately the same number of objects in each set by
// adding sequential objects to sequential sets (not random)
for (int s = 0; s < SetCount; s++)
{
var firstObject = (int)Math.Ceiling((double)s * ObjectCount / SetCount);
for (int i = 0; i < SetSize; i++)
{
var o = (firstObject + i) % ObjectCount;
Matrix[s][o] = 1;
}
}
// output the result
for (int s = 0; s < SetCount; s++)
{
for (int o = 0; o < ObjectCount; o++)
{
Console.Write(string.Format("{0}, ", Matrix[s][o]));
}
Console.WriteLine();
}
Console.WriteLine();
// shuffle sets
Matrix = Matrix.OrderBy(s => rand.Next()).ToArray();
// make a new matrix for shuffle objects
var objOrder = Enumerable.Range(0, ObjectCount).OrderBy(o => rand.Next()).ToArray();
var MatrixSuffled = new int[SetCount][];
for (int s = 0; s < SetCount; s++)
MatrixSuffled[s] = Enumerable.Repeat(0, ObjectCount).ToArray();
for (int o = 0; o < ObjectCount; o++)
{
var oldObj = o;
var newObj = objOrder[o];
for (int s = 0; s < SetCount; s++)
{
MatrixSuffled[s][newObj] = Matrix[s][oldObj];
}
}
// check and output the result
var objectCounters = Enumerable.Repeat(0, ObjectCount).ToArray();
for (int s = 0; s < SetCount; s++)
{
var objectsInThisSet = 0;
for (int o = 0; o < ObjectCount; o++)
{
objectsInThisSet += MatrixSuffled[s][o];
objectCounters[o] += MatrixSuffled[s][o];
Console.Write(string.Format("{0}, ", MatrixSuffled[s][o]));
}
Console.Write(string.Format(" {0}", objectsInThisSet));
Console.WriteLine();
}
// output object count
Console.WriteLine();
for (int o = 0; o < ObjectCount; o++)
Console.Write(string.Format("{0} ", objectCounters[o]));
Console.ReadLine();
}
For 12 objects, 6 sets and 3 visible, the initial matrix might look like this:
And after shuffle:
Resulting in sets: