メインコンテンツへスキップ

Gurobi add an if condition

進行中

コメント

7件のコメント

  • Xin Zhi
    • First Question
    • Conversationalist

     


    0
  • Ronald van der Velden
    • Gurobi Staff Gurobi Staff

    Hi,

    If I understand the puzzle correctly:

    • You need to choose a value -1 or +1 for each row
    • Then you need to select a subset of rows
    • For those rows, the sum of each -1/+1 multiplied by a given constant Column3 must add up to 6

    It sounds like you could simplify this by saying we should select an integer value between -1 and 1 (including 0), where the sum of -1/0/+1 multiplied by the constants adds up to 6.

    import gurobipy as gp
    from gurobipy import GRB
    data = [3, -1, 2, -2, 4]
    with gp.Model() as model:
    N=len(data)
    X=model.addVars(N, lb=-1, ub=1, vtype=GRB.INTEGER)
    model.addConstr(gp.quicksum(data[i] * X[i] for i in range(N)) == 6)
    model.optimize()
    for i in range(N):
    print(f'X[{i}] = {X[i].X}')

    Kind regards,
    Ronald

    0
  • Xin Zhi
    • First Question
    • Conversationalist

    Hi Ronald,

    Thanks a lot for your reply. My problem becomes more complicated than this. Since, Column4_Vars can take values in 1,2,3,4, in the end I have four subsets based on this problem.  for example 

     

    if Column4_Vars  == 1,  
    model.addConstr(gp.quicksum(data[i] * X[i] for i in range(N)) == 6)
    if Column4_Vars  == 2,   
    model.addConstr(gp.quicksum(data[i] * X[i] for i in range(N)) == 10)
    if Column4_Vars  == 3, 
    model.addConstr(gp.quicksum(data[i] * X[i] for i in range(N)) == 20)
    if Column4_Vars  == 4, 
    model.addConstr(gp.quicksum(data[i] * X[i] for i in range(N)) == -5)
    0
  • Xin Zhi
    • First Question
    • Conversationalist

    so in the end I need to find X and Column4_Vars at the same time

     

    0
  • Ronald van der Velden
    • Gurobi Staff Gurobi Staff

    The full puzzle is still not entirely clear now. Is the challenge now to partition the rows into four groups, where each row gets a +/- sign, and the product/sum within each group must match a certain value? E.g. "Divide the numbers [3, -1, 2, -2, 4] into four partitions; multiply some of them by -1, then take the sum within each group and make sure the sums equal 6, 10, 20, -5? For this particular sample data I think there is no solution. Also, the code you shared in your previous comment sums all items and not just the one in a partition/subset.

    0
  • Xin Zhi
    • First Question
    • Conversationalist

    Hi, Yes your understanding is correct, but I didn't design the numerical example such that it adds up. but it is what you described

    1
  • Xin Zhi
    • First Question
    • Conversationalist
    import pandas as pd
    
    data = {
        "Column3": [2, 56, 3, 3, 2, 6, 8, 8, 43, 32, 2, 3, 6, 34, 2, 6],
        "Y1": [1, -1, 1, 1, 1, -1, 1, -1, 1, 1, -1, -1, 1, 1, 1, -1],
        "Column4_Vars": [1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4],
        "Column3_times_Y1": [2, -56, 3, 3, 2, -6, 8, -8, 43, 32, -2, -3, 6, 34, 2, -6],
        "sum": [-48, -48, -48, -48, 39, 39, 39, 39, 39, 27, 27, 27, 36, 36, 36, 36]
    }
    
    df = pd.DataFrame(data)


    The above example is a valid example.


    if Column4_Vars  == 1,  
    model.addConstr(gp.quicksum(data[i] * X[i] for i in range(N)) == -48)
    if Column4_Vars  == 2,   
    model.addConstr(gp.quicksum(data[i] * X[i] for i in range(N)) == 39)
    if Column4_Vars  == 3,  
    model.addConstr(gp.quicksum(data[i] * X[i] for i in range(N)) == 27)
    if Column4_Vars  == 4,  
    model.addConstr(gp.quicksum(data[i] * X[i] for i in range(N)) == 36)


    I need to learn Y1 and Column4_Vars at the same time. 'Column3' and 'sum' are training data.
    0

サインインしてコメントを残してください。