Skip to main content

How to sum variables that are not dimensionally incomplete

Answered

Comments

1 comment

  • Ahmed Azab
    • Gurobi Staff Gurobi Staff

    Hi 子龙 王,

    The problem you're encountering occurs because your code is trying to find certain keys in the x dictionary that actually don't exist, resulting in a KeyError.

    This issue pops up due to the way your loop is set up—it goes through a set range of numbers (from 1 to 4), implicitly assuming that for every possible combination of (i, j, k), there's a corresponding entry in your x dictionary. However, the reality, as outlined by your operations_machines setup, is that not every (j, k) pair is appropriate for each i. That mismatch leads to the error you see because the code attempts to look up keys that simply aren't there.

    Here is one possible way to fix it. 

    from gurobipy import Model, GRB

    # Indices for operations/items and phases/stages
    I = [1, 2]
    J = [1, 2, 3, 4]

    # Let Valid k values for some (i, j) combination
    operations_machines = {
        (1, 1): [1, 2],
        (1, 4): [3, 4],
        (2, 2): [1, 3],
        (2, 3): [2, 4]
    }

    # Initialize the model
    model = Model("ExampleModel")

    # Decision variables x[i,j,k]
    x = {}
    for i in I:
        for j in J:
            for k in range(1, 5):  # Assuming k should also iterate over 1 to 4
                x[i, j, k] = model.addVar(vtype=GRB.BINARY, name=f"x[{i},{j},{k}]")

    for i in I:
        for j in J:
            if (i, j) in operations_machines:  # Only proceed if (i, j) has valid k values
                valid_k_values = operations_machines[(i, j)]
                for k in valid_k_values:  # Iterate only over valid k values
                    model.addConstr(sum(x[i, j, k_prime] for k_prime in valid_k_values) == 1, 
                                    f"constraint_{i}_{j}_{k}")

    You can check the constraint expression for this example 

    # Print constraint expressions
    model.update()  # Ensure the model is updated before accessing constraints
    constraints = model.getConstrs()  # Retrieve list of constraints
    for constr in constraints:
        expr = model.getRow(constr)  # Get the linear expression for the constraint
        print(f"{constr.ConstrName}: {expr} == 1")

    You can also use more concise ways of defining the constraints using tupledict and inserting an if statement inside Model.addConstrs()

    I hope this helps 

    Thanks

    - Ahmed

     

    0

Please sign in to leave a comment.