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

Change sets in a optimization loop

進行中

コメント

8件のコメント

  • Maliheh Aramon
    Gurobi Staff Gurobi Staff

    Hi, 

    I am not sure that I fully understand your question. Given your snippet, it is not clear what you changed on the model in each iteration of the "for" loop.

    Would you like to build and solve a separate model for each key-value pair in your \(\texttt{dict_F_I}\) Python dictionary? Or should the model for the next pair also contain the variables associated with the previous pairs?

    If it is the former, you need to instantiate a new model object for each pair inside the loop:

    for key, value in dict_F_I.items()
        model = gp.Model(f"model_key}")
        # Build the model by adding variables/constraint
     
        # Write the model into file and optimizer it
        model.write(f"model-{key}.mps")
        model.optimize()

    # Dispose the model object
      model.dispose()

    Best regards,

    Maliheh

    0
  • Cassiano Tavares
    Conversationalist
    First Question
    Gurobi-versary

    I need to build the large model once and, solved it in each iteration only the variables associated to pair (key, value) of my dictionary. This strategy is based in decoposition. My problem has ten clusters and, I need to solve a cluster for eatch iteration.

    It´s possible? 

    My model has 9MM variables

    0
  • Maliheh Aramon
    Gurobi Staff Gurobi Staff

    I need to build the large model once and, solved it in each iteration only the variables associated to pair (key, value) of my dictionary.

    What happens to the variables that are not part of the key-value pair in each iteration? Are you planning to fix these variables to specific values, or remove them from the model?

    In each iteration, consider creating a copy of the large model using the Model.copy() method. You can then work on this copied model, either by fixing or removing the variables that are not part of the associated key-value pair. At the end of each iteration, dispose of the copied model and start the next iteration with a fresh copy.

    To fix a variable to a specific value, set its lower and upper bounds to that value. To remove a variable from the copied model, use the Model.remove() method.

    Note that Gurobi has a partition heuristic that uses large-neighborhood search to improve the current incumbent solution. You can enable using this heuristic when solving your large model by setting the Partition variable attributes and the PartitionPlace solver parameter. While I'm not certain this heuristic will be effective for your large model, it may be worth trying.

    Maliheh

    0
  • Cassiano Tavares
    Conversationalist
    First Question
    Gurobi-versary
    %%time
    global Z1,Z2,Z3,Z4

     

    # Build the new objective function
    Z1= gp.quicksum(theta[v,l,t] for v in V for l in L for t in T)
    Z2 =  gp.quicksum((ViolaUmin[v,l,t]+ViolaUmax[v,l,t])*PenEqVar[t]  for v in V for l in L for t in T)
    Z3 = gp.quicksum((ViolaWmin[g,t]+ViolaWmax[g,t])*PenEqIdade[t] for g in G for t in T)
    Z4 = gp.quicksum((ViolaVmax[f,t]*PenMaxVar[t] ) for f in F for t in T)

     

    objective = Z1 - (Z2 + Z3 + Z4)

     

    model.setObjective(objective,sense=GRB.MAXIMIZE)

     

    model.update()
     
    for polo, fazendas in dict_F_l.items():
       
        print("Copying the original model")
        copy = model.copy()
       
       
        print("Redefining the sets F and L")

     

        # Uptadeting the sets fazendas e polos

     

        F = set(fazendas)
        L = set([polo])

     

        print(f'The new set F is: ', F)
        print(f'The new set L is: ', L)



        # Atualizando o modelo
        copy.update()

     

        #model.write(f'saida_polo={polo}.lp')

     

        print ('\nIniciando laço para o polo', L, 'com fazendas', F,'\n')

     

        # Parametrizando os testes computacionais

     

        copy.setParam("TimeLimit", 3600*0.25)

     

        copy.setParam("MIPGap", 0.01)

     

        print('Executando o solver...')
       
        # Write the model into file and optimizer it
        copy.write(f"model-{polo}.mps")

     

        # Executa o solver
        copy.optimize()

     

        if copy.Status == gp.GRB.OPTIMAL:

     

             
            print('Uptadeting X, Y e Z')
            # Adiciona os resultados da iteração para um array de solução
            #for l in range(polos+1):
            for t in range(Tmax+1):
                for e in E:
                    for f in F:
                        if f in fazendas:
                            for i in I:
                           
                                if x[e,i,f,t].x > 1.E-3:
                                    X_eift[e][i][f][t] = x[e,i,f,t].x
                                else:
                                    X_eift[e][i][f][t] = 0
                                if y[e,i,f,t].x > 1.E-3:
                                    Y_eift[e][i][f][t] += y[e,i,f,t].x
                                else:
                                    Y_eift[e][i][f][t] = 0
                                if z[e,i,f,t].x > 1.E-3:
                                    Z_eift[e][i][f][t] += z[e,i,f,t].x
                                else:
                                    Z_eift[e][i][f][t] = 0

     

            print('Updating Theta...')
            for t in range(Tmax+1):
                for v in V:
                    if theta[v,polo,t].x > 1.E-3:
                        Theta_vlt[v][polo][t] = theta[v,polo,t].x
                    else:
                        Theta_vlt[v][polo][t] = 0
         
           
            #coleting the residual value of this iteration for uptade constraint C16
            for e in E:
                for f in F:
                    if f in K_e[e]:
                        for t in range(Tmax+1):
                            plantio_realizado[t] += x[e,0,f,t].x

     

            print(f'O plantio total até o polo [{polo}] é:', np.round(plantio_realizado,2))

     

            #coleting the residual value of this iteration for uptade constraint C22
            for t in range(Tmax+1):
                for e in E:
                    for i in I:
                        for f in F:
                            if f in K_e[e]:
                                if t>1:
                                    if i>1:
                                        if z[e,i,f,t].x < z[e,i-1,f,t-1].x:
                                            estratos_erradicados[t] +=  x[e,i-1,f,t-1].x
                                            area_estratos_erradicados[t] += y[e,i-1,f,t-1].x



            #print(f'A área total até o polo [{polo}] é:', np.round(A_polo_max[polo],2))                
            #print(f'A erradicação total até o polo [{polo}] é:', np.round(estratos_erradicados,2 ))
            print(f'A area erradicada total até o polo [{polo}] é:', np.round(area_estratos_erradicados,2 ))
     
       
        else:
            print("Infeasible solution, interrupting the iteration")

     

            break



        producao[polo] = Z1.getValue()
        print(f'A produção do polo {polo} foi: {producao[polo]}')

     

        desvio_var[polo] = Z2.getValue()
       
        print(f'O desvio varietal do polo {polo} foi: {desvio_var[polo]}')
         
        desvio_et[polo] = Z3.getValue()
        print(f'O desvio etario do polo {polo} foi: {desvio_et[polo]}')
         
        # Dispose the model object
        copy.dispose()
     
    Unfortanelly this strategy don´t work to mee.... I give this error:
    AttributeError: Unable to retrieve attribute 'x'
     
     
    0
  • Cassiano Tavares
    Conversationalist
    First Question
    Gurobi-versary

    Sorry... I believe that the message above is not clear enough...

    Unfortanelly this strategy (model.copy) don´t work to mee.... I give this error:
    AttributeError: Unable to retrieve attribute 'x'
     
    Following my methaeuristic code
    global Z1,Z2,Z3,Z4

     

    # Build the new objective function
    Z1= gp.quicksum(theta[v,l,t] for v in V for l in L for t in T)
    Z2 =  gp.quicksum((ViolaUmin[v,l,t]+ViolaUmax[v,l,t])*PenEqVar[t]  for v in V for l in L for t in T)
    Z3 = gp.quicksum((ViolaWmin[g,t]+ViolaWmax[g,t])*PenEqIdade[t] for g in G for t in T)
    Z4 = gp.quicksum((ViolaVmax[f,t]*PenMaxVar[t] ) for f in F for t in T)

     

    objective = Z1 - (Z2 + Z3 + Z4)

     

    model.setObjective(objective,sense=GRB.MAXIMIZE)

     

    model.update()
     
    for polo, fazendas in dict_F_l.items():
       
        print("Copying the original model")
        copy = model.copy()
       
       
        print("Redefining the sets F and L")

     

        # Uptadeting the sets fazendas e polos

     

        F = set(fazendas)
        L = set([polo])

     

        print(f'The new set F is: ', F)
        print(f'The new set L is: ', L)



        # Atualizando o modelo
        copy.update()

     

        #model.write(f'saida_polo={polo}.lp')

     

        print ('\nIniciando laço para o polo', L, 'com fazendas', F,'\n')

     

        # Parametrizando os testes computacionais

     

        copy.setParam("TimeLimit", 3600*0.25)

     

        copy.setParam("MIPGap", 0.01)

     

        print('Executando o solver...')
       
        # Write the model into file and optimizer it
        copy.write(f"model-{polo}.mps")

     

        # Executa o solver
        copy.optimize()

     

        if copy.Status == gp.GRB.OPTIMAL:

     

             
            print('Uptadeting X, Y e Z')
            # Adiciona os resultados da iteração para um array de solução
            #for l in range(polos+1):
            for t in range(Tmax+1):
                for e in E:
                    for f in F:
                        if f in fazendas:
                            for i in I:
                           
                                if x[e,i,f,t].x > 1.E-3:
                                    X_eift[e][i][f][t] = x[e,i,f,t].x
                                else:
                                    X_eift[e][i][f][t] = 0
                                if y[e,i,f,t].x > 1.E-3:
                                    Y_eift[e][i][f][t] += y[e,i,f,t].x
                                else:
                                    Y_eift[e][i][f][t] = 0
                                if z[e,i,f,t].x > 1.E-3:
                                    Z_eift[e][i][f][t] += z[e,i,f,t].x
                                else:
                                    Z_eift[e][i][f][t] = 0

     

            print('Updating Theta...')
            for t in range(Tmax+1):
                for v in V:
                    if theta[v,polo,t].x > 1.E-3:
                        Theta_vlt[v][polo][t] = theta[v,polo,t].x
                    else:
                        Theta_vlt[v][polo][t] = 0
         
           
            #coleting the residual value of this iteration for uptade constraint C16
            for e in E:
                for f in F:
                    if f in K_e[e]:
                        for t in range(Tmax+1):
                            plantio_realizado[t] += x[e,0,f,t].x

     

            print(f'O plantio total até o polo [{polo}] é:', np.round(plantio_realizado,2))

     

            #coleting the residual value of this iteration for uptade constraint C22
            for t in range(Tmax+1):
                for e in E:
                    for i in I:
                        for f in F:
                            if f in K_e[e]:
                                if t>1:
                                    if i>1:
                                        if z[e,i,f,t].x < z[e,i-1,f,t-1].x:
                                            estratos_erradicados[t] +=  x[e,i-1,f,t-1].x
                                            area_estratos_erradicados[t] += y[e,i-1,f,t-1].x



            #print(f'A área total até o polo [{polo}] é:', np.round(A_polo_max[polo],2))                
            #print(f'A erradicação total até o polo [{polo}] é:', np.round(estratos_erradicados,2 ))
            print(f'A area erradicada total até o polo [{polo}] é:', np.round(area_estratos_erradicados,2 ))
     
       
        else:
            print("Infeasible solution, interrupting the iteration")

     

            break



        producao[polo] = Z1.getValue()
        print(f'A produção do polo {polo} foi: {producao[polo]}')

     

        desvio_var[polo] = Z2.getValue()
       
        print(f'O desvio varietal do polo {polo} foi: {desvio_var[polo]}')
         
        desvio_et[polo] = Z3.getValue()
        print(f'O desvio etario do polo {polo} foi: {desvio_et[polo]}')
         
        # Dispose the model object
        copy.dispose()
     
    In my previous code, I only used model.update() instead model.copy()
     
    There is any wayout for my issue?
    0
  • Riley Clement
    Gurobi Staff Gurobi Staff

    I think you are misunderstanding the relationship between the sets and the model.  I will make the following analogy:  Let's say you take a photo of yourself wearing a hat.  Then you take the hat off.  Does the photo magically change - or are you still wearing a hat in the photo?  Changing the F and L sets after a model has been created is not going to change the model.

    0
  • Cassiano Tavares
    Conversationalist
    First Question
    Gurobi-versary

    I see...thank you soo much for this analogy! It's very clear to me now.

    There is anyway that is possible to define sets dynamically into the loop? (Uptading all model)

    0
  • Cassiano Tavares
    Conversationalist
    First Question
    Gurobi-versary

    I have to solve in each iteration a cluster that is in dict_F_l. This dictionary has the cluster key and the values ​​of the farms that I need to solve in each iteration. Thus, in the first iteration:
    key =0, values ​​= [6, 8, 9], I only need to solve these farms (6,8,9). And so... onwards...

     

    dict_F_l_items(

    [(0, [6, 8, 9]), # first iteration

    (1, [5, 7]), # second iteration

    (2, [10]),

    (3, [0, 1, 2, 3, 4, 16]),

    (4, [11, 12]),

    (5, [18, 26]),

    (6, [15, 22, 24]),

    (7, [21, 25]),

    (8, [13, 14, 17, 19, 20]),

    (9, [23, 27])

    ])

    There is any way to solve this values in each iteration?

    0

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