Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
pythongurobi:检索MIP的线性算法的解决方案_Python_Callback_Mathematical Optimization_Gurobi - Fatal编程技术网

pythongurobi:检索MIP的线性算法的解决方案

pythongurobi:检索MIP的线性算法的解决方案,python,callback,mathematical-optimization,gurobi,Python,Callback,Mathematical Optimization,Gurobi,我有一个MIP,我试图通过回调来恢复线性松弛的解,但我不能。我使用下面的代码来提取变量Z,就在我的MIP线性松弛重新曝光之后 # Definition de la fonction callback def mycallback(model, where): if where == GRB.Callback.MIPNODE: if model.cbGet(GRB.Callback.MIPNODE_STATUS) == GRB.Status.OPTIMAL:

我有一个MIP,我试图通过回调来恢复线性松弛的解,但我不能。我使用下面的代码来提取变量Z,就在我的MIP线性松弛重新曝光之后

# Definition de la fonction callback
def mycallback(model, where):
    if where == GRB.Callback.MIPNODE:
        if model.cbGet(GRB.Callback.MIPNODE_STATUS) == GRB.Status.OPTIMAL:
            sol_Z=model.cbGetNodeRel(model.getAttr('x', vZ))
            FTe=range(1,FT+1)
            for cb in FCBloc: 
                for e in FOpEq:
                    for t in FTe:
                        Bl_ZDonnees.update({(cb,e,t):sol_Z[cb[0],cb[1],t,e]})
            NomFichier="M_1_Relax.csv"
            fichier = open(NomFichier, 'w')
            cw = csv.writer(fichier, delimiter=';')
            for bl1 in Bl_ZDonnees:
                cw.writerow([bl1,Blst_Donnees[bl1]])
            fichier.close()
            model.terminate()

函数
Model.cbGetNodeRel()
。因此,您需要编写类似于
sol_Z=model.cbGetNodeRel(vZ)
的函数
model.cbGetNodeRel()
。因此,您需要编写类似于
sol_Z=model.cbGetNodeRel(vZ)
的代码。我测试这个方法,它是可以的

    #**********************************************************************
# Optimise relaxation, then recover the values that are near to 1   
    def M_Relax(model,FF,Fb0,Fb1,FBlocs,FBlocsR,FBlocs_FOP,FCBloc,FT,FOp_,FOpEq,FVarOrdProd):
        FTe=range(1,FT+1)
        Fbif=[Fb0,Fb1]
        N=len(FBlocs)
        mr=model.copy()
        mr.setParam('OutputFlag',0)
        # Create variables déplacements des équipements
        vZ={}
        for Cb in FCBloc:
            for t in FTe:
                for e in FOpEq:
                    vZ[Cb[0],Cb[1],t,e]= model.getVarByName(name="vZ_{}_{}_{}_{}".format(Cb[0],Cb[1],t,e))
        EntVars=[]
        for var in mr.getVars():
            if var.vType !=GRB.CONTINUOUS:
                EntVars+=[var]
                var.vType=GRB.CONTINUOUS

        mr.optimize()
        Blst_Donnees={}
        status = mr.status
        if mr.SolCount>0 or status == GRB.Status.OPTIMAL:
            sol_Z=mr.getAttr('x', vZ)
            for bl in FBlocsR:
                 for e in [e1 for e1 in FOpEq if e1[0]==op]:                            
                     for cb in [cb1 for cb1 in FCBloc if cb1[1]==bl]:
                          if len([t for t in FTe if sol_Z[cb[0],bl,t,e]>0.8])>=1:
                              t1=min([t for t in FTe if sol_Z[cb[0],bl,t,e]>0.8])                                            
                                    Blst_Donnees.update({('Z',cb[0],cb[1],e):t1})
            NomFichier="M_Relaxation.csv"
            fichier = open(NomFichier, 'w')
            cw = csv.writer(fichier, delimiter=';')
            for bl1 in Blst_Donnees:
                cw.writerow([bl1,Blst_Donnees[bl1]])
            fichier.close()
            return Blst_Donnees
        elif status == GRB.Status.INFEASIBLE:
            return [0,0]

我测试这个方法,没问题

    #**********************************************************************
# Optimise relaxation, then recover the values that are near to 1   
    def M_Relax(model,FF,Fb0,Fb1,FBlocs,FBlocsR,FBlocs_FOP,FCBloc,FT,FOp_,FOpEq,FVarOrdProd):
        FTe=range(1,FT+1)
        Fbif=[Fb0,Fb1]
        N=len(FBlocs)
        mr=model.copy()
        mr.setParam('OutputFlag',0)
        # Create variables déplacements des équipements
        vZ={}
        for Cb in FCBloc:
            for t in FTe:
                for e in FOpEq:
                    vZ[Cb[0],Cb[1],t,e]= model.getVarByName(name="vZ_{}_{}_{}_{}".format(Cb[0],Cb[1],t,e))
        EntVars=[]
        for var in mr.getVars():
            if var.vType !=GRB.CONTINUOUS:
                EntVars+=[var]
                var.vType=GRB.CONTINUOUS

        mr.optimize()
        Blst_Donnees={}
        status = mr.status
        if mr.SolCount>0 or status == GRB.Status.OPTIMAL:
            sol_Z=mr.getAttr('x', vZ)
            for bl in FBlocsR:
                 for e in [e1 for e1 in FOpEq if e1[0]==op]:                            
                     for cb in [cb1 for cb1 in FCBloc if cb1[1]==bl]:
                          if len([t for t in FTe if sol_Z[cb[0],bl,t,e]>0.8])>=1:
                              t1=min([t for t in FTe if sol_Z[cb[0],bl,t,e]>0.8])                                            
                                    Blst_Donnees.update({('Z',cb[0],cb[1],e):t1})
            NomFichier="M_Relaxation.csv"
            fichier = open(NomFichier, 'w')
            cw = csv.writer(fichier, delimiter=';')
            for bl1 in Blst_Donnees:
                cw.writerow([bl1,Blst_Donnees[bl1]])
            fichier.close()
            return Blst_Donnees
        elif status == GRB.Status.INFEASIBLE:
            return [0,0]

展示一些代码和自己的尝试,否则这个问题可能会被解决。这是一个合理的问题。Gurobi没有提供很多关于回调函数的文档。展示一些代码和自己的尝试,否则这个问题可能会被解决。这是一个合理的问题。古罗比没有提供很多关于回调函数的文档。谢谢你,格雷格·格洛克纳,我将应用你的建议。但我尝试了另一种方法(见下文)。谢谢格雷格·格洛克纳先生,我将应用你的建议。但我尝试了另一种方法(见下文)。