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

Help in gurobipy

回答済み

コメント

24件のコメント

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Objects \(\texttt{a,l}\) are lists of lists- Thus, you have to access their values via \(\texttt{a[p][t]}\) and \(\texttt{l[p][t]}\).

    The constraint

    m.addConstrs((S1('*',t) for t in weeks ))

    does not say anything. Do you want to add something like \(\sum_t S1_{p,t} =1 \)? You could use the quicksum function for this.

    Moreover, \(\texttt{ModelSence}\) should read \(\texttt{ModelSense}\).

    0
  • ghada ouerfelli
    Gurobi-versary
    Curious
    Conversationalist

    I have an other error :   m.addConstrs((S1('*',t) for t in weeks) <=L)
    TypeError: '<=' not supported between instances of 'generator' and 'int'

    import gurobipy as gp
    from gurobipy import GRB

    m=gp.Model("Log")

    weeks = range(0,4)
    products=range(1,2)
    L=10
    k=2
    a=[[0,0,10,0,0],
    [0,20,0,0,0],
    ]
    l=[[0,2,3,1,2],
    [0,9,5,1,2],
    ]

    S=m.addVars(products,weeks,name='S')
    S1=m.addVars(products,weeks,name='S1')
    S2=m.addVars(products,weeks,obj=k,name='S2')


    l1=m.addVars(products,weeks,name='l1')
    l2=m.addVars(products,weeks,obj=k,name='l2')
    l3=m.addVars(products,weeks,obj=k,name='l2')

    a1=m.addVars(products,weeks,name='a1')
    a2=m.addVars(products,weeks,obj=k,name='a2')

    S1[1,0]=1
    S1[2,0]=2
    S2[1,0]=3
    S2[2,0]=4

    m.addConstrs((S[p,t-1]+a[p][t] == l[p][t]+S[p,t] for p in products for t in weeks if t>1))
    m.addConstrs((S1[p,t-1]+a1[p,t] == l1[p,t]+S1[p,t]-l3[p,t] for p in products for t in weeks if t>0))
    m.addConstrs((S2[p,t-1]+a2[p,t] == l2[p,t]+S2[p,t]+l3[p,t] for p in products for t in weeks if t>0))
    m.addConstrs((S[p,t-1]+a[p][t] == l[p][t]+S[p,t] for p in products for t in weeks if t>0))
    m.addConstrs((S[p,t] == S1[p,t]+S2[p,t] for p in products for t in weeks ))
    m.addConstrs((a[p][t] == a1[p,t]+a2[p,t] for p in products for t in weeks ))
    m.addConstrs((l[p][t] == l1[p,t]+l2[p,t] for p in products for t in weeks ))
    m.addConstrs((S1('*',t) for t in weeks) <=L)

    m.ModelSence=GRB.MINIMIZE
    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    What are you trying to formulate with this constraint?

    The constraint

    m.addConstrs((S1('*',t) for t in weeks ) <= L)

    does not say anything. Do you want to formulate something like \(\sum_{p} S_{p,t} \leq L \,\forall t\) ? You could use the quicksum function for this.

    m.addConstrs(gp.quicksum(S1[p,t] for p in products) <= 1 for t in weeks)
    0
  • ghada ouerfelli
    Gurobi-versary
    Curious
    Conversationalist

    thanks but I didn'i find the same result with CPLEX

    the right solution is // solution (optimal) with objective 106

    l2 = [[0
                 0 0 0 0]
                 [0 0 0 0 0]];
    l3 = [[0 1 0 2 0]
                 [0 0 0 0 4]];
    S2 = [[3 2 4 2 2]
                 [4 7 7 7 3]];
    a2 = [[0 0 2 0 0]
                 [0 3 0 0 0]];
    S = [[4 2 9 8 6]
                 [6 17 12 11 9]];
    S1 = [[1 0 5 6 4]
                 [2 10 5 4 6]];
    a1 = [[0 0 8 0 0]
                 [0 17 0 0 0]];
    l1 = [[0 2 3 1 2]
                 [0 9 5 1 2]];

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Note that you are fixing values for S1 and S2

    S1[1,0]=1
    S1[2,0]=2
    S2[1,0]=3
    S2[2,0]=4

    which already lead to a different solution.

    You can write your model to a human-readable LP file and analyze the generated file whether the model looks like you would expect it to. You can use the write method for this.

    m.write("myLP.lp")
    0
  • ghada ouerfelli
    Gurobi-versary
    Curious
    Conversationalist
    S1[1,0]=1
    S1[2,0]=2
    S2[1,0]=3
    S2[2,0]=4
    it is my initial stock in two warehouses in week zero
    0
  • ghada ouerfelli
    Gurobi-versary
    Curious
    Conversationalist

    I will send you the model to understand the default it will better 

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    You state that in your optimal solution you have

    S2 = [[3 ,2 ,4 ,2 ,2],
          [4 ,7 ,7 ,7 ,3]];
    S1 = [[1 ,0 ,5 ,6 ,4],
          [2 ,10 ,5 ,4 ,6]];

    In this solution it is

    print(S1[1][0])
    print(S2[1][0])
    > 2
    > 4

    Moreover, in your formulation you don't even have an optimization variable for S1[2,0] and S2[2,0] because the first index of variables S1, S2 goes over products which is given as \(\texttt{range(1,2) = 1}\).

    Maybe you meant to use

    products=range(0,2)
    [...]
    S1[0,0]=1
    S1[1,0]=2
    S2[0,0]=3
    S2[1,0]=4
    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    I will send you the model to understand the default it will better

    You don't have to send your model. You already posted the code snippet producing it which most likely has indexing issues.

    You should write your model to a human-readable LP file and analyze the generated file whether the model looks like you would expect it to. You can use the write method for this.

    m.write("myLP.lp")
    0
  • ghada ouerfelli
    Gurobi-versary
    Curious
    Conversationalist

    My Model is infeasble

    import gurobipy as gp
    from gurobipy import GRB

    m=gp.Model("Log")

    weeks = range(0,4)
    #weeks = [0,1,2,3,4]
    products=range(1,2)
    #products=[1,2]
    L=10
    k=2
    a=[[0,0,10,0,0],
    [0,20,0,0,0],
    ]
    l=[[0,2,3,1,2],
    [0,9,5,1,2],
    ]

    S=m.addVars(products,weeks,name='S')
    S1=m.addVars(products,weeks,name='S1')
    S2=m.addVars(products,weeks,obj=k,name='S2')


    l1=m.addVars(products,weeks,name='l1')
    l2=m.addVars(products,weeks,obj=k,name='l2')
    l3=m.addVars(products,weeks,obj=k,name='l2')

    a1=m.addVars(products,weeks,name='a1')
    a2=m.addVars(products,weeks,obj=k,name='a2')

    S1[0,0]=1
    S1[1,0]=2
    S2[0,0]=3
    S2[1,0]=4

    m.addConstrs((S[p,t-1]+a[p][t] == l[p][t]+S[p,t] for p in products for t in weeks if t>0))
    m.addConstrs((S1[p,t-1]+a1[p,t] == l1[p,t]+S1[p,t]-l3[p,t] for p in products for t in weeks if t>0))
    m.addConstrs((S2[p,t-1]+a2[p,t] == l2[p,t]+S2[p,t]+l3[p,t] for p in products for t in weeks if t>0))
    m.addConstrs((S[p,t-1]+a[p][t] == l[p][t]+S[p,t] for p in products for t in weeks if t>0))
    m.addConstrs((S[p,t] == S1[p,t]+S2[p,t] for p in products for t in weeks ))
    m.addConstrs((a[p][t] == a1[p,t]+a2[p,t] for p in products for t in weeks ))
    m.addConstrs((l[p][t] == l1[p,t]+l2[p,t] for p in products for t in weeks ))
    m.addConstrs(gp.quicksum(S1[p,t] for p in products) <= 1 for t in weeks)

    m.optimize()
    m.write("myLP.lp")
    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    My Model is infeasble

    In this case, please refer to How do I determine why my model is infeasible?

    There are two things which pop up.

    In your comment you want to define weeks as

    #weeks = [0,1,2,3,4]

    but you use

    weeks = range(0,4)

    which equals \(\texttt{[0,1,2,3]}\).

    So this should read

    weeks = range(0,5)

    Then, in your first comment you wanted to formulate the last constraints as \(\leq L\) but it is \(\leq 1\). So it probably should be

    m.addConstrs(gp.quicksum(S1[p,t] for p in products) <= L for t in weeks)

    Again, I can only recommend to have a look at the model file generated called \(\texttt{myLP.lp}\). This is the easiest way to find out what is wrong with the model.

    0
  • ghada ouerfelli
    Gurobi-versary
    Curious
    Conversationalist

    sorry  for this type of error but I still have another result different from Cplex

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    The issue is that you are adding variables

    S1[0,0],S1[1,0],S2[0,0],S2[1,0]

    to the objective function but later you don't use them, because you overwrite the dictionary entries by

    S1[0,0]=1
    S1[1,0]=2
    S2[0,0]=3
    S2[1,0]=4

    To properly fix variables to some values, you have to fix their lower and upper bounds.

    S1[0,0].lb=1
    S1[0,0].ub=1
    S1[1,0].lb=2
    S1[1,0].ub=2
    S2[0,0].lb=3
    S2[0,0].ub=3
    S2[1,0].lb=4
    S2[1,0].ub=4

    With this, the solution is 106. The solution point is a different one. This is possible because your model might have multiple optimal solutions. Still, you should double check that.

    Moreover, I think that in your first constraints set, it should be \(\texttt{if t > 0}\) instead of \(\texttt{if t > 1}\) but this is something you have to decide.

    Also note that there is a constraint missing

    m.addConstrs(gp.quicksum(S[p,t] for p in products) <= L + gp.quicksum(S2[p,t] for p in products) for t in weeks)
    0
  • ghada ouerfelli
    Gurobi-versary
    Curious
    Conversationalist

    thanks a lot a lot Sir your guide 

    but there is something I wanna ask for it :

    when I write print (l2)

    print (l3) 

    the program shows only l2 in double 

    Warning: variables 40 and 50 have the same name "l2[0,0]"
    Warning: to let Gurobi read it back, use rlp format


    <gurobi.Model Continuous instance Log: 72 constrs, 80 vars, No parameter changes>
    {(0, 0): <gurobi.Var l2[0,0] (value 0.0)>, (0, 1): <gurobi.Var l2[0,1] (value 0.0)>, (0, 2): <gurobi.Var l2[0,2] (value 0.0)>, (0, 3): <gurobi.Var l2[0,3] (value 0.0)>, (0, 4): <gurobi.Var l2[0,4] (value 0.0)>, (1, 0): <gurobi.Var l2[1,0] (value 0.0)>, (1, 1): <gurobi.Var l2[1,1] (value 0.0)>, (1, 2): <gurobi.Var l2[1,2] (value 0.0)>, (1, 3): <gurobi.Var l2[1,3] (value 0.0)>, (1, 4): <gurobi.Var l2[1,4] (value 0.0)>}
    {(0, 0): <gurobi.Var l2[0,0] (value 0.0)>, (0, 1): <gurobi.Var l2[0,1] (value 1.0)>, (0, 2): <gurobi.Var l2[0,2] (value 0.0)>, (0, 3): <gurobi.Var l2[0,3] (value 0.0)>, (0, 4): <gurobi.Var l2[0,4] (value 0.0)>, (1, 0): <gurobi.Var l2[1,0] (value 0.0)>, (1, 1): <gurobi.Var l2[1,1] (value 0.0)>, (1, 2): <gurobi.Var l2[1,2] (value 0.0)>, (1, 3): <gurobi.Var l2[1,3] (value 2.0)>, (1, 4): <gurobi.Var l2[1,4] (value 4.0)>}

    Process finished with exit code 0

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    You give your variables the same name as indicated by the warning

    l2=m.addVars(products,weeks,obj=k,name='l2')
    l3=m.addVars(products,weeks,obj=k,name='l2') # should be 'l3'
    0
  • ghada ouerfelli
    Gurobi-versary
    Curious
    Conversationalist

    thanks a lot

    0
  • ghada ouerfelli
    Gurobi-versary
    Curious
    Conversationalist

    When I change range by a list , I found some errors like  S1[0,0].lb=1
    KeyError: (0, 0) what should I do 

    weeks = range(0,5)
    by
    weeks = [0,1,2,3,4]
    and
    products=range(0,2)
    by
    products=[1,2]


    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    As the error states, there is no entry \(\texttt{S1[0,0]}\). This is because now your products list starts with a 1. Thus, you have to adjust your product indices to also start with 1 when explicitly using them.

    S1[1,0].lb=1
    [..]
    S2[2,0].ub=4

    I would recommend having a look at our Gurobi Python webinar to cover the basics of gurobipy. I would also recommend having a look at some introductory Python tutorials to get used to Python data structures and objects.

    0
  • ghada ouerfelli
    Gurobi-versary
    Curious
    Conversationalist

    Yes I m a beginner in python and optimization , I will try to improve my self

    import gurobipy as gp
    from gurobipy import GRB

    m=gp.Model("Log")

    #weeks = range(0,5)
    weeks = [0,1,2,3,4]
    #products=range(0,2)
    products=[1,2]
    L=10
    k=2
    a=[[0,0,10,0,0],
    [0,20,0,0,0],
    ]
    l=[[0,2,3,1,2],
    [0,9,5,1,2],
    ]

    S=m.addVars(products,weeks,name='S')
    S1=m.addVars(products,weeks,name='S1')
    S2=m.addVars(products,weeks,obj=k,name='S2')


    l1=m.addVars(products,weeks,name='l1')
    l2=m.addVars(products,weeks,obj=k,name='l2')
    l3=m.addVars(products,weeks,obj=k,name='l3')

    a1=m.addVars(products,weeks,name='a1')
    a2=m.addVars(products,weeks,obj=k,name='a2')

    #S1[0,0]=1
    #S1[1,0]=2
    #S2[0,0]=3
    #S2[1,0]=4

    S1[1,0].lb=1
    S1[1,0].ub=1
    S1[2,0].lb=2
    S1[2,0].ub=2
    S2[1,0].lb=3
    S2[1,0].ub=3
    S2[2,0].lb=4
    S2[2,0].ub=4

    #S1[0,0].lb=1
    #S1[0,0].ub=1
    #S1[1,0].lb=2
    #S1[1,0].ub=2
    #S2[0,0].lb=3
    #S2[0,0].ub=3
    #S2[1,0].lb=4
    #S2[1,0].ub=4

    #m.addConstrs((S[p,t-1]+a[p][t] == l[p][t]+S[p,t] for p in products for t in weeks if t>0))
    m.addConstrs(S[p,weeks[weeks.index(t)-1]]+a[p][t] == l[p][t]+S[p][t] for p in products for t in weeks if t!=weeks[0])

    m.addConstrs((S1[p,t-1]+a1[p,t] == l1[p,t]+S1[p,t]-l3[p,t] for p in products for t in weeks if t>0))
    m.addConstrs((S2[p,t-1]+a2[p,t] == l2[p,t]+S2[p,t]+l3[p,t] for p in products for t in weeks if t>0))
    m.addConstrs((S[p,t-1]+a[p][t] == l[p][t]+S[p,t] for p in products for t in weeks if t>0))
    m.addConstrs((S[p,t] == S1[p,t]+S2[p,t] for p in products for t in weeks ))
    m.addConstrs((a[p][t] == a1[p,t]+a2[p,t] for p in products for t in weeks ))
    m.addConstrs((l[p][t] == l1[p,t]+l2[p,t] for p in products for t in weeks ))
    m.addConstrs(gp.quicksum(S1[p,t] for p in products) <= L for t in weeks)
    m.addConstrs(gp.quicksum(S[p,t] for p in products) <= L + gp.quicksum(S2[p,t] for p in products) for t in weeks)

    m.optimize()
    m.write("myLP.lp")
    print(m)
    0
  • ghada ouerfelli
    Gurobi-versary
    Curious
    Conversationalist

     File "C:\Users\Ghada\PycharmProjects\Logistika Program\Logistika Program.py", line 55, in <genexpr>
        m.addConstrs(S[p,weeks[weeks.index(t)-1]]+a[p][t] == l[p][t]+S[p][t] for p in products for t in weeks if t!=weeks[0])
    KeyError: 1

    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    \(\texttt{S}\) is a tupledict of optimization variables. Thus you have to access it as such via \(\texttt{S[p,t]}\).

    Moreover, your constant lists \(\texttt{a,l}\) are indexed over \([0,1]\times [0,1,2,3,4]\) but you changed your products list to \([1,2]\). Thus, you have to subtract 1 from the p index for every access to \(\texttt{a}\) and \(\texttt{l}\).

    m.addConstrs(S[p,t-1]+a[p-1][t] == l[p-1][t]+S[p,t] for p in products for t in weeks if t>0)
    #[...]
    m.addConstrs((S[p,t-1]+a[p-1][t] == l[p-1][t]+S[p,t] for p in products for t in weeks if t>0))
    # [...]
    m.addConstrs((a[p-1][t] == a1[p,t]+a2[p,t] for p in products for t in weeks ))
    m.addConstrs((l[p-1][t] == l1[p,t]+l2[p,t] for p in products for t in weeks ))
    0
  • ghada ouerfelli
    Gurobi-versary
    Curious
    Conversationalist

    Sorry but I still have the same problem

    import gurobipy as gp
    from gurobipy import GRB

    m=gp.Model("Log")

    weeks = [0,1,2,3,4]

    products=[1,2]
    L=10
    k=2
    a=[[0,0,10,0,0],
    [0,20,0,0,0],
    ]
    l=[[0,2,3,1,2],
    [0,9,5,1,2],
    ]

    S=m.addVars(products,weeks,name='S')
    S1=m.addVars(products,weeks,name='S1')
    S2=m.addVars(products,weeks,obj=k,name='S2')


    l1=m.addVars(products,weeks,name='l1')
    l2=m.addVars(products,weeks,obj=k,name='l2')
    l3=m.addVars(products,weeks,obj=k,name='l3')

    a1=m.addVars(products,weeks,name='a1')
    a2=m.addVars(products,weeks,obj=k,name='a2')

    #S1[0,0]=1
    #S1[1,0]=2
    #S2[0,0]=3
    #S2[1,0]=4

    S1[1,0].lb=1
    S1[1,0].ub=1
    S1[2,0].lb=2
    S1[2,0].ub=2
    S2[1,0].lb=3
    S2[1,0].ub=3
    S2[2,0].lb=4
    S2[2,0].ub=4


    m.addConstrs(S[p,t-1]+a[p-1][t] == l[p-1][t]+S[p,t] for p in products for t in weeks if t>0)
    m.addConstrs(S1[p,t-1]+a1[p-1][t] == l1[p-1][t]+S1[p,t]-l3[p,t] for p in products for t in weeks if t>0)
    m.addConstrs(S2[p,t-1]+a2[p-1][t] == l2[p-1][t]+S2[p,t]+l3[p,t] for p in products for t in weeks if t>0)
    m.addConstrs((S[p-1,t] == S1[p,t]+S2[p,t] for p in products for t in weeks ))
    m.addConstrs((a[p-1][t] == a1[p,t]+a2[p,t] for p in products for t in weeks ))
    m.addConstrs((l[p-1][t] == l1[p,t]+l2[p,t] for p in products for t in weeks ))
    m.addConstrs(gp.quicksum(S1[p,t] for p in products) <= L for t in weeks)
    m.addConstrs(gp.quicksum(S[p,t] for p in products) <= L + gp.quicksum(S2[p,t] for p in products) for t in weeks)

    m.optimize()
    m.write("myLP.lp")
    print(m)
    0
  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    The index changes in your products list only affect the constant lists \(\texttt{a,l}\). They don't affect the optimization variables \(\texttt{a1,a2,l1,l2,S}\). Thus you made a few too many changes. The following should work

    m.addConstrs(S1[p,t-1]+a1[p,t] == l1[p,t]+S1[p,t]-l3[p,t] for p in products for t in weeks if t>0)
    m.addConstrs(S2[p,t-1]+a2[p,t] == l2[p,t]+S2[p,t]+l3[p,t] for p in products for t in weeks if t>0)
    m.addConstrs((S[p,t] == S1[p,t]+S2[p,t] for p in products for t in weeks ))
    0
  • ghada ouerfelli
    Gurobi-versary
    Curious
    Conversationalist

    Thanks a lot for your support 

    0

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