3

I am trying to get a matching between two sets of vertices, one represents meets between two teams and the other time slots when the meets could happen. The adjacency map represents both teams' availability to meet at any given time slot. This would be bipartite matching. The problem is that I want to constraint this so that any team will not be assigned to consecutive time slots, more specifically to time slots in two consecutive days.

So, I've tried adding a check right before adding an edge to the match, at the end of the augmenting path, that checks the previous day's and following day's time slots, if they have been matched to a meet with any of the teams in the meet about to match. I tried it with Hopcroft-Karp and with DFS. It didn't work.

Is this not bipartite matching anymore?

Any help will be greatly appreciated.

EDIT:

As suggested in the answer below, I am trying to implement this as an integer linear program. The following code is heavily based on the example from Google OR-tools on assignment matching As it is, it seems to work correctly for matching, but without my specified constraint.

// [START program]
// [START import]
import com.google.ortools.linearsolver.MPConstraint;
import com.google.ortools.linearsolver.MPObjective;
import com.google.ortools.linearsolver.MPSolver;
import com.google.ortools.linearsolver.MPVariable;
// [END import]

/** MIP example that solves an assignment problem. */
public class GameMatching {
    static {
        System.loadLibrary("jniortools");
    }

    public static void main(String[] args) {
        // Data
        // [START data_model]
        // Adjacency matrix represents which games can happen on which dates
        int[][] adj = {
          {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
          {1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
          {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
          {0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
        };

        int numGames = adj.length;
        int numDates = adj[0].length;

        //represents which game time slots are on a given day (4 games on sundays, 2 on weekdays)
        int[][] days = {
              {0, 1, 2, 3},
              {4, 5},
              {6, 7},
              {8, 9, 10, 11},
              {12, 13},
              {14, 15},
              {16, 17},
              {18, 19},
              {20, 21, 22, 23},
              {24, 25},
              {26, 27},
              {28, 29},
              {30, 31},
              {32, 33, 34, 35},
              {36, 37},
              {38, 39},
              {40, 41},
              {42, 43},
              {44, 45, 46, 47},
              {48, 49},
              {50, 51},
              {52, 53, 54, 55},
              {56, 57},
              {58, 59},
              {60, 61},
              {62, 63},
              {64, 65, 66, 67},
              {68, 69},
              {70, 71},
              {72, 73},
              {74, 75},
              {76, 77, 78, 79}
        };
        //represents what day of the week is a day, a team can play thursday and sunday, but not sunday and monday 0 is sunday, 1 is monday...
        int[] weekDays = {0, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0};

        // teamToGames[i][j] represents a team i's, games j
        int[][] teamToGames = {
              {1, 3, 9, 16, 18, 26},
              {0, 8, 12, 16, 23, 28},
              {1, 5, 7, 13, 21, 27},
              {2, 5, 14, 17, 22, 26},
              {7, 15, 19, 21, 24, 28},
              {3, 10, 14, 20, 27, 29},
              {2, 6, 9, 13, 23, 29},
              {6, 8, 11, 18, 19, 25},
              {8, 4, 10, 11, 17, 24},
              {4, 12, 15, 20, 22, 25},
        };
       
        // [END data_model]

        // Solver
        // [START solver]
        // Create the linear solver with the CBC backend.
        MPSolver solver = new MPSolver("AssignmentMip", MPSolver.OptimizationProblemType.CBC_MIXED_INTEGER_PROGRAMMING);
        // [END solver]

        // Variables
        // [START variables]
        // x[i][j] is an array of 0-1 variables, which will be 1
        // if a game i is assigned to date j.
        MPVariable[][] match = new MPVariable[numGames][numDates];
        for (int i = 0; i < numGames; ++i) {
            for (int j = 0; j < numDates; ++j) {
                match[i][j] = solver.makeIntVar(0, 1, "");
            }
        }

        // [END variables]

        // Constraints
        // [START constraints]
        // Each game is assigned to at most one date.
        for (int i = 0; i < numGames; ++i) {
            MPConstraint constraint = solver.makeConstraint(0, 1, "");
            for (int j = 0; j < numDates; ++j) {
                constraint.setCoefficient(match[i][j], 1);
            }
        }
        // Each date is assigned to at most one game.
        for (int j = 0; j < numDates; ++j) {
            MPConstraint constraint = solver.makeConstraint(0, 1, "");
            for (int i = 0; i < numGames; ++i) {
                constraint.setCoefficient(match[i][j], 1);
            }
        }
        // Can only assign respecting adj matrix
        for (int i = 0; i < numGames; ++i) {
            for (int j = 0; j < numDates; ++j) {
                MPConstraint constraint = solver.makeConstraint(0, adj[i][j], "");
                constraint.setCoefficient(match[i][j], 1);
            }
        }

        // Cannot assign team to consecutive dates
        for (int i = 0; i < teamToGames.length; ++i) {
            for (int j = 0; j < days.length - 1; ++j) {
                if (weekDays[j] != 4) {
                    MPConstraint constraint = solver.makeConstraint(0, 1, "");
                    for (int k = 0; k < teamToGames[i].length; ++k) {
                        for (int l = 0; l < days[j].length; ++l) {
                            constraint.setCoefficient(match[teamToGames[i][k]][l], 1);
                        }
                        for (int l = 0; l < days[j+1].length; ++l) {
                            constraint.setCoefficient(match[teamToGames[i][k]][l], 1);
                        }
                    }
                }
            }
        }

        // [END constraints]

        // Objective
        // [START objective]
        MPObjective objective = solver.objective();
        for (int i = 0; i < numGames; ++i) {
            for (int j = 0; j < numDates; ++j) {
                objective.setCoefficient(match[i][j], 1);
            }
        }

        objective.setMaximization();
        // [END objective]

        // Solve
        // [START solve]
        MPSolver.ResultStatus resultStatus = solver.solve();
        // [END solve]

        // Print solution.
        // [START print_solution]
        // Check that the problem has a feasible solution.
        if (resultStatus == MPSolver.ResultStatus.OPTIMAL || resultStatus == MPSolver.ResultStatus.FEASIBLE) {
            System.out.println("Total matches: " + objective.value() + "\n");
            for (int i = 0; i < numGames; ++i) {
                for (int j = 0; j < numDates; ++j) {
                    // Test if x[i][j] is 0 or 1 (with tolerance for floating point
                    // arithmetic).
                    if (match[i][j].solutionValue() > 0.5) {
                        System.out.println("Game " + i + " assigned to date " + j);
                    }
                }
            }
        } else {
            System.err.println("No solution found.");
        }
        // [END print_solution]
    }

    // private GameMatching() {
    // }
}
// [END program]

EDIT This is the apparently working code. There is one last thing I need this to do. It is preferable to have two games on one same day (if other constraints allow) rather than one game in each of two separate days. My first thought was finding a way to constrain the games per day to either 0 or 2 (or more on a Sunday), but that may not always be possible. So any help on how I could do this will be appreciated.

Thank you in advance.

vsoued
  • 45
  • 4
  • 1
    Please clarify your question and mention what you have done in algo or code – Nitin Singhal Jun 24 '20 at 09:31
  • Sorry @גלעדברקן, that was just something I was trying, I didn't get tu put up the working code. I'm on it now. – vsoued Jun 30 '20 at 07:44
  • Good idea you had to try to make the consecutive day constraint work. To get more attention, you might consider posing the additional - nice to have two games on one day - as another question on SO, including the work so far. (In general, editing the question to another question doesn't usually garner much attention because most people have read it and moved on already.) – גלעד ברקן Jun 30 '20 at 11:26
  • To prefer two games per date, one idea might be to have a variable, `reward`, that is set to be maximised as the objective. We maintain a constraint that every "game" (the specific team pair) must be assigned (value of associated constraint must be 1). We create all game pairs we deem as valid two-game dates. Then each assignment of a game from these pairs is associated with a greater value added to `reward` than is an assignment done as it is now. Those would need an added constraint that either both are assigned or neither. – גלעד ברקן Jun 30 '20 at 12:52

1 Answers1

1

We could have an integer linear program, where each constraint for a team to meet includes two additional before variables. For example, for team A to meet on day 4:

{
  A_meets_on_4: 1,
  A_meets_before_4: 1,
  A_meets_before_5: 1
}

If we try to assign A to 3, we get:

{
  A_meets_on_3: 1,
  A_meets_before_3: 1,
  A_meets_before_4: 1
}

And if we try to assign A to 5, we get:

{
  A_meets_on_5: 1,
  A_meets_before_5: 1,
  A_meets_before_6: 1
}

Now if we try to assign, say 3 and 4, together we get:

{
  A_meets_on_3: 1,
  A_meets_on_4: 1,
  A_meets_before_3: 1,
  A_meets_before_4: 2
  A_meets_before_5: 1
}

But if we constrain all the before variables, like A_meets_before_4, to be less than 2, this would not be allowed.

If we try to assign A to 3 and 5, together we get:

{
  A_meets_on_3: 1,
  A_meets_on_5: 1,
  A_meets_before_3: 1,
  A_meets_before_4: 1
  A_meets_before_5: 1
  A_meets_before_6: 1
}

which is allowed.

גלעד ברקן
  • 23,602
  • 3
  • 25
  • 61
  • Thank you for replying. I am not familiar with linear programming. I found this https://developers.google.com/optimization/assignment/assignment_example which seems like a good place to start. From what I understand, for every team, I'd have to add an integer variable for every time slot, the one before, and the one after, with a value of 1, and then add the constraint that all variables have to be less than 2, is this correct? But in this case how would I handle that each meet involves 2 teams? – vsoued Jun 24 '20 at 18:29
  • @vsoued as far as I am aware (and I also have little experience with integer linear programming, having used it only once in a practical setting), assigning any one meet would mean two of the series of variables like the one I mentioned, one for each team; those in addition to any other constraints such as total meets per team. – גלעד ברקן Jun 24 '20 at 19:27
  • @vsoued the solver I used in a practical setting was [this one](https://github.com/JWally/jsLPSolver#readme) -- the README file there shows the variable declarations as JSON similar to the example I gave. For a regular ILP solver, we would write the formulation slightly differently. I'm not sure how to write that off the top of my head but I'm sure others on SO could help with that. – גלעד ברקן Jun 24 '20 at 19:31
  • If I understand correctly, @גלעד ברקן, that would mean that the values of two separate matches would need to somehow overlap and be added in order to set a constraint on the sum, correct? I'm breaking my head over this. Looking at you examples in json does make this easier to visualize but I still lack basic understanding of lp – vsoued Jun 25 '20 at 06:48
  • @vsoued not sure I follow. What do you mean by "two separate matches?" What's a "match?" – גלעד ברקן Jun 25 '20 at 09:52
  • Sorry, I meant matrices, one being x[i][j] = (0,1) which I called 'match' in the code above, that contains the variables that will be 1 if a game is matched to a date, and 0 if not, and the other matrix would be x[i][j] = (something?) That would contain the before and after variables? – vsoued Jun 25 '20 at 13:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216639/discussion-between-vsoued-and--). – vsoued Jun 25 '20 at 13:31