Exemplo n.º 1
0
def isaffordable(c, p, budget):
    tol = 10**(-6)
    money = ds.dotproduct(c, p)
    if (abs(budget - money) <= tol):
        return True
    elif (money <= budget + tol):
        return True
    else:
        return False
Exemplo n.º 2
0
def gbestprice(eps, istar, alpha, numf, minprice, btprice, maxtot, budget,
               diff):
    ms = ds.dotproduct(alpha, minprice)
    tol = 10**(-6)
    bestprice = copy.deepcopy(minprice)
    g = istar - numf

    #print("minprice =" + str(minprice))
    #print("btprice =" + str(btprice))
    #print(maxtot)

    if isfeasibleprice(alpha, minprice, budget) and (ms <= maxtot + tol):
        if not btprice:  # empty
            #print("++++++++++++ gbest price: TODO 1")
            bestprice[g] = gfindmaxprice(eps, g, alpha, minprice, maxtot,
                                         budget)

        else:  # non-empty
            #print("++++++++++++ gbest price: TODO 2")
            if ds.breaktievector(minprice, btprice):
                jstar = breaktieindex(minprice, btprice)
                #print("jstar = " + str(jstar))

                if (g < jstar):
                    bestprice[g] = btprice[g]  # TODO
                elif (g == jstar):
                    #print("THIS CASE")
                    if (btprice[g] >= eps - 10**(-8)):
                        bestprice[g] = btprice[g] - eps
                    else:
                        bestprice = []
                else:  # set the price as high as possible
                    bestprice[g] = gfindmaxprice(eps, g, alpha, minprice,
                                                 maxtot, budget)

            else:  # No
                bestprice = []

    else:  # No
        bestprice = []

    if (bestprice == diff):
        bestprice = []

    #print("Set bestprice = " + str(bestprice))
    return bestprice
Exemplo n.º 3
0
def gfindmaxprice(eps, g, alpha, minprice, maxtot, budget):
    #bestprice = copy.deepcopy(minprice)
    tol = 10**(-6)
    #print(maxtot)
    #print(budget)
    #print(alpha)
    #print(min(maxtot, budget) - ds.dotproduct(alpha, minprice))
    #print(alpha[g] * eps)
    beta = math.floor(
        (min(maxtot, budget) - ds.dotproduct(alpha, minprice) + tol) /
        (alpha[g] * eps))
    #print("beta = " + str(beta))

    if beta < 0:
        print("++++++++ gfindmaxprice: no feasible price")

    bestprice = minprice[g] + beta * eps  # TODO: check no rounding error

    return bestprice
Exemplo n.º 4
0
def getinfeasiblecolsone(row, rmin, order, numf, minprice, maxtms, fb2col):
    #print("order = " + str(list(map(lambda x: fb2col[x], order))))
    #print(order)
    # Ignore the price of rmin
    rm = (rmin[0], rmin[1])
    #print(rm)
    # Find the index of rm in the order
    index = order.index(rm)

    fc = []
    type = getcoltype(rmin, row, numf)
    #print ("type = " + str(type) + " ", end ='')
    if (type == 1):  # rmin can't be a non-active slack variable
        print("getfeasiblecolsone: Something wrong!!!!!")
    elif (type == 2):  # non-slack with zero coefficient
        # Remove everying after the index, any price is OK
        fc = order[index + 1:]
    elif (type == 3):  # non-slack with non-zero coefficient
        #print("type 3")
        if (row < numf):  # family case
            #print("family case")
            # Remove all the cols that is less preferred than rmin
            fc = order[index + 1:]
            #print("index = " + str(index))
            #print(order)
            # the total money is at most ...
            maxtms[row] = ds.dotproduct(rmin[1], rmin[2])
        else:  # game case, price matters
            #print("game case")
            g = row - numf
            fc = order[len(order) - 1]
            # The price of game g is at least as expensive as the price of game g at rmin
            minprice[g] = rmin[2][g]
    else:  # active slack variable
        #print("type 4")
        fc = []  #order[len(order)-1]		# any col, any price is OK

    #print("row = " + str(row) + ": " + "fc =" + str(list(map(lambda x: fb2col[x], fc))))
    return fc, minprice, maxtms
Exemplo n.º 5
0
def fbestprice(istar, alpha, minprice, btprice, maxtot, budget):
    #print("Debug")
    #print(minprice)
    #print(btprice)
    #print(maxtot)

    ms = ds.dotproduct(alpha, minprice)
    tol = 10**(-6)

    if isfeasibleprice(alpha, minprice, budget) and (ms <= maxtot + tol):
        if not btprice:  # empty
            return minprice
        else:  # non-empty

            if ds.breaktievector(minprice, btprice):
                #jstar = breaktieindex(minprice, btprice)
                return minprice

            else:  # No
                return []

    else:  # No
        return []
Exemplo n.º 6
0
def mul(A, x):
    return [ds.dotproduct(a, x) for a in A]
Exemplo n.º 7
0
def isfeasibleprice(alpha, price, budget):
    tol = 10**(-6)
    if (ds.dotproduct(alpha, price) <= budget + tol):
        return True
    else:
        return False
Exemplo n.º 8
0
def findbestprice(eps, c, istar, rmins, numf, minprice, maxtms, budget,
                  fbmins):
    numg = len(rmins) - numf
    fbtprice = [0] * numg  # family break tie price
    gbtprice = [0] * numg  # game break tie price
    diff = []  # the bestprice must be different from diff

    currminprice = [0] * (len(rmins) - numf)  #copy.deepcopy(minprice)
    currmaxtms = [0] * numf  #copy.deepcopy(maxtms)

    cc = (c[0], c[1], currminprice)

    if c not in fbmins:
        for row in range(len(rmins)):
            #if (row < numf) and (row != istar):		# family case
            #	print("Do nothing 1")

            if (row >= numf) and (row != istar):  # game case
                g = row - numf
                ctype = getcoltype(cc, row, numf)
                mtype = getcoltype(rmins[row], row, numf)

                if (ctype == 3) and (mtype == 3):
                    if not ds.breaktie(cc, rmins[row]):
                        currminprice[
                            g] = rmins[row][2][g] + eps  # must be eps higher
                        #print("---- TODO")
                    else:
                        currminprice[g] = rmins[row][2][g]

        return bestprice(eps, istar, c[1], currminprice, [], budget + 1,
                         budget, numf, diff)
    # For a row that already have a contract without price (f,b) in the basis
    else:
        #print("-------------------------- Interesting Case")
        index = fbmins.index(c)
        if (index == istar):
            for i in range(len(fbmins)):
                if (i != istar) and (fbmins[i] == c):
                    index = i

        #print("###################index = " + str(index))
        #print("###################istar = " + str(istar))

        # best price must be differrent from the existing one
        diff = rmins[index][2]

        for row in range(len(rmins)):
            #if (row < numf) and (row != istar) and (row != index):		# family case
            #	print("---- Family case")

            if (row >= numf) and (row != istar) and (row !=
                                                     index):  # game case
                #print("---- Game case")
                g = row - numf
                ctype = getcoltype(cc, row, numf)
                mtype = getcoltype(rmins[row], row, numf)

                if (ctype == 3) and (mtype == 3):
                    #print("--- Check point")
                    if not ds.breaktie(cc, rmins[row]):
                        currminprice[
                            g] = rmins[row][2][g] + eps  # must be eps higher
                    else:
                        currminprice[g] = rmins[row][2][g]
        # same family and same bundle, need to break tie carefuly here
        ctype = getcoltype(cc, index, numf)  # verify cc
        mtype = getcoltype(rmins[index], index, numf)

        if (index < numf):  # family case
            #print("---------------- Family case")

            if (ctype == 3) and (mtype == 3):  # non-zero coefficient
                fbtprice = rmins[index][2]
                currmaxtms[index] = ds.dotproduct(c[1], rmins[index][2])

                temp = bestprice(eps, istar, c[1], currminprice, fbtprice,
                                 currmaxtms[index], budget, numf, diff)
                if not temp:
                    currmaxtms[index] = ds.dotproduct(c[1],
                                                      rmins[index][2]) - eps
                    fbtprice = []
                    return bestprice(eps, istar, c[1], currminprice, fbtprice,
                                     currmaxtms[index], budget, numf, diff)
                else:
                    return temp
            elif (ctype == 2) and (mtype == 2):
                fbtprice = rmins[index][2]
                return bestprice(eps, istar, c[1], currminprice, fbtprice,
                                 budget + 1, budget, numf, diff)
            else:
                return bestprice(eps, istar, c[1], currminprice, [],
                                 budget + 1, budget, numf, diff)

        else:  # game case
            #print("---------------- Finally touched game case")
            # break tie based on price
            g = index - numf
            if (istar >= numf):
                currminprice[istar - numf] = 0

            if (ctype == 3) and (mtype == 3):  # non-zero coefficient
                #print("*********** Degbug Here")
                #print("price = " + str(currminprice))

                if (istar < numf):
                    gbtprice = rmins[index][2]
                    currminprice[g] = rmins[index][2][g]
                    temp = bestprice(eps, istar, c[1], currminprice, gbtprice,
                                     budget + 1, budget, numf, diff)
                    if not temp:
                        gbtprice = []
                        currminprice[g] = rmins[index][2][g] + eps
                        return bestprice(eps, istar, c[1], currminprice,
                                         gbtprice, budget + 1, budget, numf,
                                         diff)
                    else:
                        return temp
                else:
                    #print("&&&&&&&&&&&&&&&&&&&&&&&& Check")
                    gbtprice = rmins[index][2]
                    currminprice[g] = rmins[index][2][g]
                    temp1 = bestprice(eps, istar, c[1], currminprice, gbtprice,
                                      budget + 1, budget, numf, diff)

                    gbtprice = []
                    currminprice[g] = rmins[index][2][g] + eps
                    temp2 = bestprice(eps, istar, c[1], currminprice, gbtprice,
                                      budget + 1, budget, numf, diff)

                    #print("temp1 =" + str(temp1))
                    #print("temp2 =" +str(temp2))

                    if (temp1 == []) and (temp2 == []):
                        return []
                    elif (temp1 == []) and (temp2 != []):
                        return temp2
                    elif (temp1 != []) and (temp2 == []):
                        return temp1
                    else:
                        tol = 10**(-6)
                        if not ds.isequalprice(temp1, temp2):
                            if (temp1[istar - numf] + tol >=
                                    temp2[istar - numf]):
                                return temp1
                            else:
                                return temp2
                        else:
                            return temp1
            elif (ctype == 2) and (mtype == 2):
                gbtprice = rmins[index][2]
                return bestprice(eps, istar, c[1], currminprice, gbtprice,
                                 budget + 1, budget, numf, diff)
            else:
                return bestprice(eps, istar, c[1], currminprice, [],
                                 budget + 1, budget, numf, diff)