コード例 #1
0
def solve_seer_ilp(companies,
                   facilities,
                   customers,
                   facilityDemands,
                   costs,
                   alpha=1,
                   log_verbose='Quiet'):
    assert alpha >= 0  # ensure trace-off factor is >= 0, which show the contribution of representative cost
    assert log_verbose in ['Quiet', 'Terse', 'Normal', 'Verbose']
    n_company = len(companies)
    n_facility = len(facilities)
    n_customer = len(customers)
    n_type = len(facilityDemands)

    mdl = CpoModel()

    customerSupplier = mdl.integer_var_list(n_customer, 0, n_facility - 1,
                                            'customerSupplier')
    openFacility = mdl.integer_var_list(n_facility, 0, 1, 'openFacility')
    facilityType = mdl.integer_var_list(n_facility, 0, n_type, 'facilityType')
    customerType = mdl.integer_var_list(n_customer, 0, n_type - 1,
                                        'customerType')
    openCompany = mdl.integer_var_list(n_company, 0, 1, 'openCompany')
    company2id = {companies[i].name: i for i in range(n_company)}
    type2id = {facilityDemands[i].type: i for i in range(n_type)}
    facilityCompany = [
        company2id[facilities[i].company] for i in range(n_facility)
    ]

    validFacilityType = np.zeros((n_facility, n_type + 1))
    validFacilityType[:, n_type] = 1
    for i in range(n_facility):
        validFacilityType[i, type2id[facilities[i].type]] = 1

    for i in range(n_customer):
        mdl.add(mdl.element(openFacility, customerSupplier[i]) == 1)
        mdl.add(
            mdl.element(customerType, i) == mdl.element(
                facilityType, customerSupplier[i]))
        mdl.add(
            mdl.element(mdl.element(customerType, i), validFacilityType[i]) ==
            1)

    for i in range(n_facility):
        mdl.add(
            mdl.element(mdl.element(facilityType, i), validFacilityType[i]) ==
            1)
        mdl.add(
            mdl.element(openFacility, i) <= mdl.element(
                openCompany, facilityCompany[i]))
        mdl.add((mdl.element(openFacility, i) == 1) == (
            mdl.element(facilityType, i) < n_type))

    for i in range(n_type):
        mdl.add(mdl.count(facilityType, i) == facilityDemands[i].demand)

    # Objective
    total_cost = mdl.scal_prod(openCompany, [c.cost for c in companies])
    for i in range(n_customer):
        total_cost += mdl.element(customerSupplier[i], alpha * costs[i])
    mdl.add(mdl.minimize(total_cost))

    # -----------------------------------------------------------------------------
    # Solve the model and display the result
    # -----------------------------------------------------------------------------

    # Solve model
    msol = mdl.solve(TimeLimit=TIME_LIMIT, LogVerbosity=log_verbose)

    selectedFacilities = []
    selectedCompanies = []
    if msol:
        for i in range(n_facility):
            if msol[facilityType[i]] < n_type:
                selectedFacilities.append(i)
                if facilityCompany[i] not in selectedCompanies:
                    selectedCompanies.append(facilityCompany[i])
    return msol, selectedFacilities, selectedCompanies
コード例 #2
0
# Create one variable per warehouse to indicate if it is open (1) or not (0)

open = mdl.integer_var_list(NB_WAREHOUSES, 0, 1, "open")

# Add constraints stating that the supplying warehouse of each store must be open

for s in supplier:

    mdl.add(mdl.element(open, s) == 1)

# Add constraints stating that the number of stores supplied by each warehouse must not exceed its capacity

for wx in range(NB_WAREHOUSES):

    mdl.add(mdl.count(supplier, wx) <= WAREHOUSES[wx].capacity)

# Build an expression that computes total cost

total_cost = mdl.scal_prod(open, [w.cost for w in WAREHOUSES])

for sx in range(NB_STORES):

    total_cost = total_cost + mdl.element(supplier[sx], SUPPLY_COST[sx])

# Minimize total cost

mdl.add(mdl.minimize(total_cost))

#-----------------------------------------------------------------------------
コード例 #3
0
mdl.add(sum(mdl.nbBus[b] * b[busSize] for b in Buses) >= nbKids)

# Objective
mdl.minimize(sum(mdl.nbBus[b] * b[busCost] for b in Buses))

msol = mdl.solve()

# Dislay solution
for b in Buses:
    print(msol[mdl.nbBus[b]], " buses with ", b[busSize], " seats")

#Add a constraint
# Number of different quantity should be less than 1

mdl.add(
    mdl.sum(((mdl.count(mdl.nbBus.values(), q) >= 1)
             for q in range(0, maxquantity + 1))) <= 1)

#mdl.add(mdl.sum((mdl.sum((q==mdl.nbBus[b]) for b in Buses)>=1) for q in range(0,maxquantity+1)) <=1)
# works too but less CP style

msol2 = mdl.solve()

print()
print("Number of sizes where we have 1 possible quantity ")
print()

# Display solution

for b in Buses:
    print(msol2[mdl.nbBus[b]], " buses with ", b[busSize], " seats")
コード例 #4
0
              [47, 65, 55, 71, 95]]

# Define model.
wl = CpoModel()

# Define decision variables.
openwarehouse = wl.integer_var_list(nbWarehouses, 0, 1, "openwarehouse")
warehouseservesstore = wl.integer_var_list(nbStores, 0, nbWarehouses - 1,
                                           "warehouseservesstore")

# Define constraints.
for s in warehouseservesstore:
    wl.add(wl.element(openwarehouse, s) == 1)

for w in range(nbWarehouses):
    wl.add(wl.count(warehouseservesstore, w) <= capacity[w])

# Define objective.
fixedArray = []
for i in range(nbWarehouses):
    fixedArray.append(fixed)
total_cost = wl.scal_prod(openwarehouse, fixedArray)
for s in range(nbStores):
    total_cost = total_cost + wl.element(warehouseservesstore[s],
                                         supplyCost[s])

wl.add(wl.minimize(total_cost))

# Solve and print solution.
wlsol = wl.solve()
for w in range(nbWarehouses):