Skip to main content

Issue with Adding Constraints for Binary Variables

Answered

Comments

7 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try Gurobot?
  • Jaromił Najman
    • Gurobi Staff

    Hi Katherine,

    You could write your model to an LP file

    mod.write("myModel.lp")

    and analyze the constraints by hand to check whether they look as expected.

    I think that the constraints

    # Active members (j) can't be paired with more than one rushee (i) in each event (k)
    mod.addConstrs(gb.quicksum(var_dict[x,y,z] for y in j) <= 1 for z in K for y in j)

    should read

    mod.addConstrs(gb.quicksum(var_dict[x,y,z] for x in i) <= 1 for z in K for y in j)

    So for a fixed event and active member the sum over all rushees cannot exceed 1. With the description you provided, I am not sure about the event condition. Maybe you have to move the \(\texttt{for z in K}\) loop into the \(\texttt{quicksum}\).

    I think that the constraints

    # Rushees (i) must be paired with exactly one active member (j) for their assigned event (k)
    mod.addConstrs(gb.quicksum(var_dict[x,y,z] for x in i) == 1 for z in K for y in j)

    should read

    mod.addConstrs(gb.quicksum(var_dict[x,y,z] for y in j) == 1 for z in K for x in i)

    So for a fixed event and a fixed rushee, the number of active member is exactly 1. Again, with the description you provided, I am not sure about the event condition. Maybe you have to move the \(\texttt{for z in K}\) loop into the \(\texttt{quicksum}\).

    Best regards, 
    Jaromił

    0
  • Katherine Atwater
    • Gurobi-versary
    • First Comment
    • First Question

    Hi,

    I have tried both of your suggestions, but both return that the model in infeasible. I have included my model down below for clarification.


    import pandas as pd

    num_parties = 23

    # Import PNM data, then parse the data into lists
    rusheedata = pd.read_excel(r"FinalFinalData.xlsx", sheet_name = "Rushee Data",dtype=int)
    i = rusheedata["i"].tolist()
    k = rusheedata["k"].tolist()
    K = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]

    memberdata = pd.read_excel(r"FinalFinalData.xlsx", sheet_name = "Member Data",dtype=int)
    j = memberdata["j"].tolist()

    # Creating dictionary of rushees i and what party k they are in
    rushee_party_list = dict(zip(rusheedata.i, rusheedata.k))


    ## START MODEL

    import gurobipy as gb
    from gurobipy import *

    # Create model
    mod = Model("Model 1")

    # Create variables Xijk
    var_dict = {}
    for x in i:
        for y in j:
            for z in K:
              var_dict[x,y,z] = mod.addVar(vtype="B",name=f"X[{x},{y},{z}]")

    mod.update()

    # Constraints
    # Rushees must be in their assigned parties (k)
    for x in i:
        for y in j:
            for z in K:
              if rushee_party_list[x] != z:
                    mod.addConstr(var_dict[x,y,z] == 0)

    # Active members can't be paired with more than one rushee each party
    mod.addConstrs(gb.quicksum(var_dict[x,y,z] for x in i for z in K) <= 1 for y in j)
             
             
    # Rushees must be paired with exactly one active member
    mod.addConstrs(gb.quicksum(var_dict[x,y,z] for y in j for z in K) == 1 for x in i)
    0
  • Jaromił Najman
    • Gurobi Staff

    Please note that your code snippet does not help because I don't have access to the file \( \texttt{FinalFinalData.xlsx} \).

    In order to resolve the infeasibility, please refer to How do I determine why my model is infeasible?

    0
  • Katherine Atwater
    • Gurobi-versary
    • First Comment
    • First Question

    I have included a screenshot example of my data below to clarify. 

    i  = 1, 2, 3, ..., 1610

    j = 1, 2, 3, ..., 100

    k is in range(1 - 23), randomly assigned to each i value

     

    0
  • Jaromił Najman
    • Gurobi Staff

    A screenshot is not helpful as it is not possible to copy+paste the data into your code snippet. If you want to provide the data, please either rewrite as a Python data structure which you can add to your code snippet or upload the data file as described in Posting to the Community Forum.

    Note that it would be best if you first try to determine the source of infeasibility yourself as described in How do I determine why my model is infeasible?

    0
  • Katherine Atwater
    • Gurobi-versary
    • First Comment
    • First Question

    I have figured out my issue, I needed to include "rushee_party_list[x] == z" in my constraint that limits rushees to exactly 1 event.

    0

Post is closed for comments.