AnsweredAssumed Answered

Problem in numpy formulation of LP

Question asked by nicolas.winc on Jan 30, 2018
Latest reply on Jan 30, 2018 by pietrobelotti@fico.com

Hi,

 

I am trying to resolve the following problem in XPress python. A_ub constraint is written either with numpy (in this case the best optimum is 0) or with a loop and the best optimum is -40000 vs 0 (objective is to minimize hence this is much better). It all looks to me as if using the numpy expression adds implicit constraints.... Is this normal and how can this be avoided to write everything in a clear numpy formulation?

 

two data text files attached to test the code below.

 

minimize:     c^T * x

              subject to:   A_ub * x <= b_ub

              A_eq * x == b_eq

               lb<= x <= ub

 

Many thanks for your help,

 

Nicolas

 

 

 

global generalFolder

    generalFolder='C:\\xxxxxxxxxxx\\'

   

    nb_dogs=6

    A_ub=np.round(np.loadtxt(generalFolder+'A_ub.txt'),4)

    A_eq=np.round(np.loadtxt(generalFolder+'A_eq.txt'),4)

 

 

    '''build ibjective function. we want to minimize the last variable'''

    obj=np.zeros((12*nb_dogs+10*nb_dogs*(nb_dogs-1)+1,1))

    obj[12*nb_dogs+10*nb_dogs*(nb_dogs-1)][0]=1

    c=np.ndarray.flatten(obj)

 

 

    b_ub=np.ndarray.flatten(np.zeros((A_ub.shape[0],1))) #b_ub is a vector of 0 except last two rows

    b_ub[nb_dogs*(nb_dogs-1)]=-498

    b_ub[nb_dogs*(nb_dogs-1)+1]=500

    b_eq=np.ndarray.flatten(np.zeros((A_eq.shape[0],1)))

 

 

   

    bounds=tuple(((0,100) for i in range(0,nb_dogs)))

    bounds=bounds+tuple((0,0) for i in range(nb_dogs,8*nb_dogs+4*nb_dogs*(nb_dogs-1)))

    bounds=bounds+tuple(((0,None) for _ in range(0,4*nb_dogs+6*nb_dogs*(nb_dogs-1))))

    bounds=bounds+tuple((None,None)for _ in range(0,1))

 

 

    '''converts list of tuples to vectors for XPress'''

    lb=[]

    ub=[]

    for b in bounds:

        if b[0]==None:

            lb.append(-xp.infinity)

        else:

            lb.append(b[0])

        if b[1]==None:

            ub.append(xp.infinity)

        else:

            ub.append(b[1])

 

 

    '''

    writes problem for xPress and solves

    '''

    print('##################    XPRESS 1    #####################')

    p = xp.problem ('prob1')

    x = np.array ([xp.var(lb=-xp.infinity,ub=xp.infinity, vartype=xp.continuous) for i in range(A_ub.shape[1])])

    p.addVariable ( x )

    p.addConstraint (xp.Dot(A_ub,x) <= b_ub)

    p.addConstraint (xp.Dot(A_eq,x) == b_eq)

    p.addConstraint(x<=ub)

    p.addConstraint(lb<=x)

    p.setObjective (x[-1],sense=xp.minimize)

    p.solve()

 

 

    xs=p.getSolution()

    obj_xp=p.getObjVal()   # objective is 0

    print(xs)

   

 

 

    ################################################################################################################################################

    print('##################    XPRESS 2    #####################')

    A_ub_const=np.array([xp.constraint() for i in range(0,A_ub.shape[0])])

    for i in range(0,A_ub.shape[0]):

        A_ub_const[i]= xp.Sum([A_ub[i,j]*x[j] for j in range(0,A_ub.shape[1])]) <= b_ub[i]

 

 

    p2 = xp.problem ('prob2')

    p2.addVariable ( x )

    p2.addConstraint (A_ub_const) #only change is here. adding this constraint with a loop instead of numpy

    p2.addConstraint (xp.Dot(A_eq,x) == b_eq)

    p2.addConstraint(x<=ub)

    p2.addConstraint(lb<=x)

    p2.setObjective (x[-1],sense=xp.minimize)

    p2.solve()

 

 

    xs=p2.getSolution()

    obj_xp2=p2.getObjVal()   # objective is -39467 instead of 0

    print(xs)

Attachments

Outcomes