コード例 #1
0
def formulate(bpp, args):

    prob = DipProblem("Bin Packing")

    assign_vars = LpVariable.dicts("x", [(i, j) for i in bpp.BINS
                                         for j in bpp.ITEMS],
                                   cat=LpBinary)
    use_vars = LpVariable.dicts("y", bpp.BINS, cat=LpBinary)
    waste_vars = LpVariable.dicts("w", bpp.BINS, 0, None)

    prob += lpSum(waste_vars[i] for i in bpp.BINS), "min_waste"

    for j in bpp.ITEMS:
        prob += lpSum(assign_vars[i, j] for i in bpp.BINS) == 1

    for i in bpp.BINS:
        prob.relaxation[i] += (lpSum(bpp.volume[j] * assign_vars[i, j]
                                     for j in bpp.ITEMS) +
                               waste_vars[i] == bpp.capacity * use_vars[i])

    for i in bpp.BINS:
        for j in bpp.ITEMS:
            prob.relaxation[i] += assign_vars[i, j] <= use_vars[i]

    if args.antisymmetryCutsBins:
        for m in range(0, len(bpp.BINS) - 1):
            prob += use_vars[bpp.BINS[m]] >= use_vars[bpp.BINS[m + 1]]

    if args.antisymmetryCutsItems:
        for m in range(0, len(bpp.BINS)):
            for n in range(0, len(bpp.ITEMS)):
                if m > n:
                    i = bpp.BINS[m]
                    j = bpp.ITEMS[n]
                    prob += assign_vars[i, j] == 0

    # Attach the problem data and variable dictionaries
    # to the DipProblem
    prob.bpp = bpp
    prob.assign_vars = assign_vars
    prob.use_vars = use_vars
    prob.waste_vars = waste_vars
    prob.tol = pow(pow(2, -24), old_div(2.0, 3.0))

    return prob
コード例 #2
0
def formulate(args):

    FIXED_COST = None
    ASSIGNMENTS = None
    ASSIGNMENT_COSTS = None

    if args.module:
        m = ilib.import_module(args.module)
        LOCATIONS = m.LOCATIONS
        PRODUCTS = m.PRODUCTS
        DEMAND = m.DEMAND
        CAPACITY = m.CAPACITY
        if hasattr(m, 'FIXED_COST'):
            FIXED_COST = m.FIXED_COST
        if hasattr(m, 'ASSIGNMENTS'):
            ASSIGNMENTS = m.ASSIGNMENTS
        if hasattr(m, 'ASSIGNMENT_COSTS'):
            ASSIGNMENT_COSTS = m.ASSIGNMENT_COSTS
    else:
        LOCATIONS = range(args.numLocations)
        PRODUCTS = range(args.numProducts)
        DEMAND = [int(i) for i in args.demands]
        CAPACITY = args.capacity
        if args.fixedCosts != None:
            FIXED_COST = [int(i) for i in args.fixedCosts]

    if FIXED_COST == None:
        FIXED_COST = {i: 1 for i in LOCATIONS}
    if ASSIGNMENTS == None:
        ASSIGNMENTS = [(i, j) for i in LOCATIONS for j in PRODUCTS]
    if ASSIGNMENT_COSTS == None:
        ASSIGNMENT_COSTS = {i: 0 for i in ASSIGNMENTS}

    prob = DipProblem("Facility Location")

    assign_vars = LpVariable.dicts("x", ASSIGNMENTS, 0, 1, LpBinary)
    use_vars = LpVariable.dicts("y", LOCATIONS, 0, 1, LpBinary)

    prob += (lpSum(use_vars[i] * FIXED_COST[i] for i in LOCATIONS) +
             lpSum(assign_vars[j] * ASSIGNMENT_COSTS[j]
                   for j in ASSIGNMENTS), "min")

    # assignment constraints
    for j in PRODUCTS:
        prob += lpSum(assign_vars[(i, j)] for i in LOCATIONS) == 1

    # Aggregate capacity constraints
    for i in LOCATIONS:
        prob.relaxation[i] += lpSum(
            assign_vars[(i, j)] * DEMAND[j]
            for j in PRODUCTS) <= CAPACITY * use_vars[i]

    # Disaggregate capacity constraints
    for i, j in ASSIGNMENTS:
        prob.relaxation[i] += assign_vars[(i, j)] <= use_vars[i]

    prob.LOCATIONS = LOCATIONS
    prob.PRODUCTS = PRODUCTS
    prob.assign_vars = assign_vars
    prob.use_vars = use_vars

    return prob