1

I'm fairly new at programming, so I'm hoping someone will be able to point me in the right direction on this.

I have a list of ~2400 people and each person has at least one of 23 conditions (the condition for each person is either 1 if they have the condition or 0 if they don't).

E.G. If Jon has conditions 1, 5, and 6 Jon's output would be {1,0,0,0,1,1,0,0...}.

I will then be given a list of how many people with each condition is desired. So if condition 1's desired number is 10 people and condition 2's desired number is 5 people, I would want 10 people who have condition 1 and 5 people who have condition 2 (A person can count towards condition 1 and condition 2 if they have both).

Also, I must select exactly 30 people and try to get as close to the desired number of conditions as possible, with a strict limitation that a single condition must have at least three people and no more than 10 people.

Is there a way that this can be done and if so how would I go about doing it? I tried brute forcing this, but the large number of combinations prevented me from getting a solution.

Edit: Here is a small example with a list of 5 people and 4 conditions:

Person 1: {1,0,0,1} Person 2: {0,1,1,1} Person 3: {0,0,0,1} Person 4: {1,0,0,0} Person 5: {1,0,0,1}

Desired number of conditions {2,1,1,2}. The idea is that I need to select 3 people and get as close to the desired number of conditions as possible. In this example person 1,2, and 4 would be selected. I have to expand this idea to a list of 2400 people with 23 conditions and selecting 30 people (though it should be able to expand to any list size and any number of conditions) and it is not known if there is a combination of 30 members that will result in an exact match.

Did that help clarify things?

  • The problem specification sounds a bit ambiguous. Are there two types of problems, in one of which you need to select `x` people satisfying condition `i`, and in the other of which you need to select 30 people satisfying conditions `i`, `j`, `k`, .. or as many as conditions among them as possible? The first type seems easy. – norio Nov 01 '17 at 00:28
  • You should explain what you want with a small example of the process. For example, you can use 5 people and 3 conditions. – Alperen Nov 01 '17 at 08:07
  • You want to do this in pure *python-3.x* code, or are you willing to use some off-the-shelf library like, e.g., *z3py* or [pysmt](https://github.com/pysmt/pysmt)? – Patrick Trentin Nov 06 '17 at 14:42
  • I'm willing to use anything that will help me get this to work. – Jeremy Kaplan Nov 08 '17 at 19:56

1 Answers1

0

This problem could be easily solved by constraint programming. Since you are using Python you could use Google's CP solver. In your case with 2400 people, you would have 30 decision variables, with each variable having a domain of 2400 values. Your objective would be to minimize the difference between your solution and the theoretical optimal solution (a score of 0 would mean that all 23 conditions are met perfectly). By using constraint programming this could be achieved with a few dozen lines of code. The solver will take care of the search strategy and other details.