Uncapacited Facility location covering - python

I am trying to solve this problem using pulp :
This is my code, There is a problem, because the result should be to only keep the second Location :
# Import PuLP modeler functions
from pulp import *
# Set of locations J
Locations = ["A", "B","C"]
# Set of demands I
Demands = ["1", "2", "3", "4", "5"]
# Set of distances ij
dt = [ # Demands I
# 1 2 3 4 5
[2, 23, 30, 54, 1], # A Locations J
[3, 1, 2, 2, 3], # B
[50,65,80,90,100] # C distances are very long
]
# Max value to get covered
s = 5
# Theses binaries values should be generated by code from the dt array ... I write it down directly for simplification.
# Demand I is served by location J If distance is <= 5 ( 0 = KO , 1 = OK)
covered = [
[1,0,0,0,1],
[1,1,1,1,1] # This shows that we only need Location B , not A
[0,0,0,0,0] # This shows we can't use Location C, it's too far
]
# Creates the 'prob' variable to contain the problem data
prob = LpProblem("Set covering", LpMinimize)
# # Problem variables
J = LpVariable.dicts("location", Locations, cat='Binary')
# The distance data is made into a dictionary
distances = makeDict([Locations, Demands], covered, 0)
# The objective function
# Minimize J, which is the number of locations
prob += lpSum(J["A"]+J["B"]+J["C"])
# The constraint
# Is it covered or not ?
for w in Locations:
for b in Demands:
if(distances[w][b] > 0):
prob += int(distances[w][b]) * J[w] >= 1
# Or eventually this instead :
#for w in Locations:
# prob += (lpSum([distances[w][b] * J[w] for b in Demands]) >= 1)
# or that :
# prob += 1 * J["A"] >= 1
# prob += 1 * J["A"] >= 1
# prob += 1 * J["B"] >= 1
# prob += 1 * J["B"] >= 1
# prob += 1 * J["B"] >= 1
# prob += 1 * J["B"] >= 1
# prob += 1 * J["B"] >= 1
# The problem data is written to an .lp file
prob.writeLP("SetCovering.lp")
# The problem is solved using PuLP's choice of Solver
prob.solve()
# The status of the solution is printed to the screen
print("Status:", LpStatus[prob.status])
# Each of the variables is printed with it's resolved optimum value
for v in prob.variables():
print(v.name, "=", v.varValue)
# The optimised objective function value is printed to the screen
print("Total Locations = ", value(prob.objective))
# Show constraints
constraints = prob.constraints
print(constraints)
#Status: Optimal
#location_A = 1.0
#location_B = 1.0
#location_C = 0.0
#Total Locations = 2.0
The result should be :
location_A = 0.0
location_B = 1.0
location_C = 0.0
because location B covers all of our needs.
I wonder where is the problem, there is the maths code , I hope I wrote enough:
Thanks , it's nice if you have a solution, I have also tried lpSum with no luck
Edit : Modified the code a few, you can see 'optimal solution', but It's not the solution I want + Added a "Location_C"
EDIT : This is my new code, added a secondary continuous pulp dict for arcs(links) generation (ser_customer) . The solver should only pick Fac-2 in this case, because it's near all of the customers, and other facilities are way too far:
# Lists (sets / Array) of Customers and Facilities
Customer = [1,2,3,4,5]
Facility = ['Fac-1', 'Fac-2', 'Fac-3']
# Dictionary of distances in kms
distance = {'Fac-1' : {1 : 54, 2 : 76, 3 : 5, 4 : 76, 5 : 76},
'Fac-2' : {1 : 1, 2 : 3, 3 : 1, 4 : 8, 5 : 1},
'Fac-3' : {1 : 45, 2 : 23, 3 : 54, 4 : 87, 5 : 88}
}
# Setting the Problem
prob = LpProblem("pb", LpMinimize)
# Defining our Decision Variables
use_facility = LpVariable.dicts("Use Facility", Facility, 0, 1, LpBinary)
ser_customer = LpVariable.dicts("Service", [(i,j) for i in Customer for j in Facility], 0)
# Setting the Objective Function = Minimize amount of facilities and arcs
prob += lpSum(use_facility['Fac-1']+use_facility['Fac-2']+use_facility['Fac-3']) + lpSum(distance[j][i]*ser_customer[(i,j)] for j in Facility for i in Customer)
# Constraints,At least 1 arc must exist between facilities and customers
for i in Customer:
prob += lpSum(ser_customer[(i,j)] for j in Facility) >= 1
prob.solve()
# Print the solution of Decision Variables
for v in prob.variables():
print(v.name, "=", v.varValue)
# Print the solution of Binary Decision Variables
Tolerance = 0.0001
for j in Facility:
if use_facility[j].varValue > Tolerance:
print("Establish Facility at site = ", j)
The result seems to show good arcs(links), but there is no facility selection, I wonder if somebody have any idea, is there any way to force use_facility[index] to be > 0 , Is adding arcs decisions variables a good idea ? I have tried to moove the arcs as a constraint too instead of being into the objective function, with no luck. :
Service_(1,_'Fac_1') = 0.0
Service_(1,_'Fac_2') = 1.0
Service_(1,_'Fac_3') = 0.0
Service_(2,_'Fac_1') = 0.0
Service_(2,_'Fac_2') = 1.0
Service_(2,_'Fac_3') = 0.0
Service_(3,_'Fac_1') = 0.0
Service_(3,_'Fac_2') = 1.0
Service_(3,_'Fac_3') = 0.0
Service_(4,_'Fac_1') = 0.0
Service_(4,_'Fac_2') = 1.0
Service_(4,_'Fac_3') = 0.0
Service_(5,_'Fac_1') = 0.0
Service_(5,_'Fac_2') = 1.0
Service_(5,_'Fac_3') = 0.0
Use_Facility_Fac_1 = 0.0
Use_Facility_Fac_2 = 0.0
Use_Facility_Fac_3 = 0.0
I also have tried the AirSquid solution, ,I think I maybe miss sources decisions variables who should be minimized but don' t know how to do, I guess covered are arcs (links), anyway It is a good exercise, harder than a simple product mix, hi hi :
prob = LpProblem('source minimzer', LpMinimize)
dist_limit = 5
sources = ['A', 'B','C'] # the source locations
# note this is zero-indexed to work with the list indexes in dist dictionary...
destinations = list(range(5)) # the demand locations 0, 1, 2, 3, 4
dist = { 'A': [2, 23, 30, 54, 1],
'B': [3, 1, 2, 2, 3],
'C':[24,54,12,56,76]}
covered = LpVariable.dicts('covered', [(s, d) for s in sources for d in destinations], cat='Binary')
# The objective function
# Minimize the number of sources
prob += lpSum(covered[s, d])
# set up constraint to limit covered if the destination is "reachable"
for s in sources:
for d in destinations:
prob += covered[s, d] * dist[s][d] <= dist_limit
# add one more constraint to make sure that every destination is "covered"...
# The problem is solved using PuLP's choice of Solver
prob.solve()
# The status of the solution is printed to the screen
print("Status:", LpStatus[prob.status])
# The optimised objective function value is printed to the screen
print("Location Selection = ", prob.objective)
The solution displayed, while it should print "B" :
Status: Optimal
Total Locations = covered_('C',_4)

You are on the right track! A couple things will help...
First, you overlooked a key piece of information in your output in that the solver says your formulation is INFEASIBLE!
Status: Infeasible
So whatever came out in the variables is gibberish and you must figure that part out first.
So, why is it infeasible? Take a look at your constraint. You are trying to force the impossible if your distance value is zero this cannot be true:
prob += int(distances[w][b]) * J[w] >= 1
So, you need to reformulate! You are missing a concept here. You actually need 2 constraints for this problem.
You need to constrain the selection of a source-destination if the route is too long
You need to enforce that every destination is covered.
You also need a double-indexed decision variable. Why? Well, lets say that source 'A' covers destination 1, 2; and 'B' covers 2, 3, 4, 5.... You will be able to know that all the destinations are "covered" with one variable, but you will not know which sources were used, so you need to keep track of both to get the full picture.
Here is a start, along with a couple edits. I'd suggest the variable names source and destination as that is kinda standard. You do not have a specific demand in this particular problem, just the need for a connection. You might also want to use dictionaries more than nested lists, I think it is clearer. Below is an example start with the first constraint. Note the trick here in limiting the covered variable. If the distance is less than the limit, s, then this constraint is satisfiable. For instance, if the distance is 3:
3 * 1 <= s
Anyhow, here is a recommended start. The other constraint is not implemented. You will need to sum across all the sources to ensure the destination is "covered". Comment back if your are stuck.
prob = LpProblem('source minimzer', LpMinimize)
dist_limit = 5
sources = ['A', 'B'] # the source locations
# note this is zero-indexed to work with the list indexes in dist dictionary...
destinations = list(range(5)) # the demand locations 0, 1, 2, 3, 4
dist = { 'A': [2, 23, 30, 54, 1],
'B': [3, 1, 2, 2, 3]}
covered = LpVariable.dicts('covered', [(s, d) for s in sources for d in destinations], cat='Binary')
# set up constraint to limit covered if the destination is "reachable"
for s in sources:
for d in destinations:
prob += covered[s, d] * dist[s][d] <= dist_limit
# add one more constraint to make sure that every destination is "covered"...

Related

Split variables into groups, each constrained to hold a specific number of variables, while optimizing group sums towards specific values

I have a number of variables each assigned an integer value. I need to split these variables in three groups with a predefined number of variables going into each group while optimizing towards predefined sums of the values in each group. Each group sum should be as close as possible to the predefined value, but can be above or below. All variables should be used and each variable can only be used once.
For example, I might have 10 variables...
Variable
Value
A1
98
A2
20
A3
30
A4
50
A5
18
A6
34
A7
43
A8
21
A9
32
A10
54
...and the goal could be to create three groups:
Group
#Variables
Sum optimized towards
X
6
200
Y
2
100
Z
2
100
So group X should hold 6 variables and their sums should be as close as possible to 200 - but I need to optimize for each of the groups simultanously.
I've tried to set up PuLP to perform this task. I seem to have found a solution for creating a single group, but I cannot figure out how to split the variables into groups and optimize the assignments based on the sums for each group. Is there a way to do this?
Below is my code for producing the first group with the presented variables.
from pulp import LpMaximize, LpMinimize, LpProblem, lpSum, LpVariable, PULP_CBC_CMD, value, LpStatus
keys = ["A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10"]
data = [98,20,30,50,20,34,43,21,32,54]
problem_name = 'repex'
prob = LpProblem(problem_name, LpMaximize)
optiSum = 200 # Optimize towards this sum
variableCount = 6 # Number of variables that should be in the group
# Create decision variables
decision_variables = []
for i,n in enumerate(data):
variable = i
variable = LpVariable(str(variable), lowBound = 0, upBound = 1, cat= 'Binary')
decision_variables.append(variable)
# Add constraints
sumConstraint = "" # Constraint on sum of data elements
for i, n in enumerate(decision_variables):
formula = data[i]*n
sumConstraint += formula
countConstraint = "" # Constrain on number of elements used
for i, n in enumerate(decision_variables):
formula = n
countConstraint += formula
prob += (sumConstraint <= optiSum)
prob += (countConstraint == variableCount)
prob += sumConstraint
# Solve
optimization_result = prob.solve(PULP_CBC_CMD(msg=0))
prob.writeLP(problem_name + ".lp" )
print("Status:", LpStatus[prob.status])
print("Optimal Solution to the problem: ", value(prob.objective))
print ("Individual decision_variables: ")
for v in prob.variables():
print(v.name, "=", v.varValue)
Which produces the following output:
Status: Optimal
Optimal Solution to the problem: 200.0
Individual decision_variables:
0 = 0.0
1 = 1.0
2 = 0.0
3 = 1.0
4 = 0.0
5 = 1.0
6 = 1.0
7 = 1.0
8 = 1.0
9 = 0.0
This seems to be a fairly standard "assignment" problem.
Let z_ij be a set of binary variable representing if object i is assigned to group j.
Your objective then is to minimise the absolute value of deviations of the group-sums from their target values - working example code below:
from pulp import LpMaximize, LpMinimize, LpProblem, lpSum, LpVariable, PULP_CBC_CMD, value, LpStatus
data = [98,20,30,50,20,34,43,21,32,54]
n_object = len(data)
#object_keys = ["A" + str(i) for i in range(1, n_object + 1)]
object_keys = range(n_object)
group_sum_targets = [200, 100, 100]
group_n_objects = [6, 2, 2]
n_group = len(group_sum_targets)
group_keys = range(n_group)
problem_name = 'repex'
# Seek to minimise absolute deviation from the target sums
prob = LpProblem(problem_name, LpMinimize)
# Primary Decision variables - the assignments
z = LpVariable.dicts('z',
indexs = [(i, j) for i in object_keys for j in group_keys],
cat='Binary')
# Aux. decision variables
group_sums = LpVariable.dicts('group_sums', indexs=group_keys,
cat='Continuous')
group_abs_error = LpVariable.dicts('group_abs_error', indexs=group_keys,
cat='Continuous')
# Objective - assumes all groups evenly penalised for missing
# their target sum, and penalty for 'over' and 'under' have same
# weighting
prob += lpSum([group_abs_error[j] for j in group_keys])
# Constraints on groups
for j in group_keys:
prob += group_sums[j] == lpSum([z[(i, j)]*data[i] for i in object_keys])
prob += group_abs_error[j] >= group_sums[j] - group_sum_targets[j]
prob += group_abs_error[j] >= group_sum_targets[j] - group_sums[j]
# Constrain number of objects used
prob += lpSum([z[(i, j)] for i in object_keys]) == group_n_objects[j]
# Constraints of objects
for i in object_keys:
# Every object used exactly once
prob += lpSum([z[(i, j)] for j in group_keys]) == 1
# Solve
optimization_result = prob.solve(PULP_CBC_CMD(msg=0))
print("Status:", LpStatus[prob.status])
print("Optimal Solution to the problem: ", value(prob.objective))
print ("Individual decision_variables: ")
for v in prob.variables():
print(v.name, "=", v.varValue)
Which gives me the following (only printing the non-0 z's). As you can see groups have 6, 2, 2 objects as desired, and the sums are somewhat close to the targets.
Status: Optimal
Optimal Solution to the problem: 34.0
Individual decision_variables:
group_abs_error_0 = 9.0
group_abs_error_1 = 18.0
group_abs_error_2 = 7.0
group_sums_0 = 191.0
group_sums_1 = 118.0
group_sums_2 = 93.0
z_(0,_1) = 1.0
z_(1,_0) = 1.0
z_(2,_0) = 1.0
z_(3,_2) = 1.0
z_(4,_1) = 1.0
z_(5,_0) = 1.0
z_(6,_2) = 1.0
z_(7,_0) = 1.0
z_(8,_0) = 1.0
z_(9,_0) = 1.0

calculating average artist entropy given user prediction and tracks in recommender systems

I have to calculate average artist entropy of users. I have solved this task on a test case but I am not able to generalize it to more task cases.
Shannon Entropy formula was used calculation the entropy of users.
def get_average_entropy_score(predictions: np.ndarray, item_df: pd.DataFrame, topK=10) -> float:
"""
predictions - np.ndarray - predictions of the recommendation algorithm for each user.
item_df - pd.DataFrame - information about each song with columns 'artist' and 'track'.
returns - float - average entropy score of the predictions.
"""
score = None
# TODO: YOUR IMPLEMENTATION.
l = []
for i in item_df['artist']:
l.append(i)
prob = 0
prob2 = 0
prob3 = 0
prob4 = 0
for i in range(len(predictions)):
for j, v in enumerate(predictions[i]):
if l[v] == 'A1':
p = 1/len(predictions[i])
prob += p
if l[v] == 'A2':
p = 1/len(predictions[i])
prob2 += p
if l[v] == 'A3':
p = 1/len(predictions[i])
prob3 += p
if l[v] == 'A4':
p = 1/len(predictions[i])
prob4 += p
if v != -1:
continue
entro1 = (prob*np.log2(prob))
entro2 = -(prob2*np.log2(prob2) + prob3*np.log2(prob3) + prob4*np.log2(prob4))
add = entro1 + entro2
entropy_over_users = add/4 # number of items/user
score = entropy_over_users
print(entropy_over_users)
return score
Now imagine I have a dataframe of artist - track like the following:
item_df = pd.DataFrame({'artist': ['A1', 'A1', 'A1', 'A1', 'A2', 'A3', 'A4']})
And I have a prediction of recommender system predicting items in position 0 1 2 or 3 like the following:
predictions = np.array([[0, 1, 2, 3], [6, 5, 4, 3], [-1, -1, -1, -1]])
From predictions e.g. the user 1 has been recommended item 0 first, item 1 second, item 2 third and item 3 fourth. A prediction of -1 means I should ignore this value because this item has not been seen by the user and should not be included in to calculation at all.
Now the question is I can't get it to work for general case where for example I don't know the A1, A2 and so on. or better Imagine you don't know the track names. Also see that item 0 in the prediction means that it is the first track in item_df, item 1 means the second and so on. Please help me. I don't know how to progress further! Please ask if something is unclear! Thanks!
Additional remark: solving the test case on paper gave me 0.5 if I normalize it.

Transportation optimization task with carrier constraints

I have a transportation problem with constraints on the load a robot can carry.
There are robots in different places in a warehouse.
And I am optimizing based on distances from robot to stations where the robot can collect some boxes.
The constraint is that a robot can carry 2 boxes IF the height of the first box is below a threshold.
Otherwise it can carry only a single box.
And every station has only 1 box to take.
I succeeded in planning for robots without the load constraint.
So that every robot can carry 2 boxes.
In the example below robot 1 gets station 2 and 3 and robot 2 gets station 1.
So this is fine.
But I am struggling on how to add the box_heights constraint which affects the loadable boxes on the robot.
How would I design this in pulp?
This is my test inout data:
optimizer costs: [[1, 2, 3], [4, 5, 6]]
optimizer boxes_to_take: {1: 1, 2: 1, 3: 1}
optimizer box_heights: {1: 1, 2: 2, 3: 1}
optimizer available_space: {1: 2, 2: 2}
optimizer station_ids: [1, 2, 3]
Costs explained:
# Stations
# 1 2 3
[1, 2, 3], # S1 Robots
[4, 5, 6], # S2
So the cost from S2 to Station 3 is 6.
And this is my script: ( copy - paste executable python 3.8 code)
import logging
import threading
import typing
from pulp import *
logger = logging.getLogger()
logger.level = logging.INFO
stream_handler = logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)
class TransportOptimizer:
def __init__(self):
logging.info(f"Initializing TransportOptimizer")
# Takes stations and robots to optimize and returns a new set of optimized stations
def optimize(self):
station_ids = [1, 2, 3]
robot_ids = [1, 2]
# Creates a dictionary for the number of units a robot can load
available_space = {}
for robot_id in robot_ids:
available_space[robot_id] = 2
# Creates a dictionary for the number of available boxes
boxes_to_take = {}
box_heights = {}
for station_id in station_ids:
boxes_to_take[station_id] = 1
box_heights[station_id] = 1 if station_id % 2 else 2
logging.debug(f"boxes_to_take: {boxes_to_take}")
# Creates a list of costs of each transportation path
costs = [ # Stations
# 1 2 3
[1, 2, 3], # S1 Robots
[4, 5, 6], # S2
]
logging.info(f"optimizer costs: {costs}")
logging.info(f"optimizer boxes_to_take: {boxes_to_take}")
logging.info(f"optimizer box_heights: {box_heights}")
logging.info(f"optimizer available_space: {available_space}")
logging.info(f"optimizer station_ids: {station_ids}")
# The cost data is made into a dictionary
costs = makeDict([robot_ids, station_ids], costs, 0)
# Creates the 'problem' variable to contain the problem data
problem = LpProblem("FullCostOptimizer", LpMinimize)
# Creates a list of tuples containing all the possible routes for transport
Routes = [(robot_id, station_id) for robot_id in robot_ids for station_id in station_ids]
# A dictionary called 'Vars' is created to contain the referenced variables(the routes)
vars = LpVariable.dicts("Route", (robot_ids, station_ids), 0, None, LpInteger)
# The objective function is added to 'problem' first
problem += (
lpSum([vars[robot_id][station_id] * costs[robot_id][station_id] for (robot_id, station_id) in Routes]),
"Sum_of_Transporting_Costs",
)
# The maximum loadable boxes constraints are added to problem for each robot
for robot_id in robot_ids:
problem += lpSum([vars[robot_id][station_id] for station_id in station_ids]) <= available_space[
robot_id], f"sum_of_boxes_robot {robot_id}_can_load"
# The minimum boxes constraints are added to problem for each station
for station_id in station_ids:
problem += lpSum([vars[robot_id][station_id] for robot_id in robot_ids]) >= boxes_to_take[
station_id], "sum_of_boxes_on_station_%s" % station_id
station_id = 1
robot_id = 1
logging.info(
f"robot {robot_id} and station {station_id}: {vars[robot_id][station_id]}, sum={lpSum([vars[robot_id][station_id] for station_id in station_ids]) <= available_space[robot_id]}")
logging.info(f"optimizer problem: {problem}")
# The problem is solved using PuLP's choice of Solver
problem.solve(PULP_CBC_CMD(msg=False))
# The status of the solution is printed to the screen
logging.debug(f"Status: {LpStatus[problem.status]}")
# Each of the variables is printed with it's resolved optimum value
for v in problem.variables():
logging.debug(f"{v.name} = {v.varValue}")
# The optimised objective function value is printed to the screen
logging.debug(f"Total Cost of Transportation = {value(problem.objective)}")
logging.debug(problem.sol_status)
logging.info(f"Total cost: {problem.objective.value()}")
logging.info(f"vars: {problem.variables()}")
for v in problem.variables():
route, robot, station = str.split(v.name, sep='_')
logging.info(f"robot {robot} to station {station} = {v.varValue} {'valid' if v.varValue == 1 else ''}")
if problem.sol_status != 1:
raise RuntimeError(f"Solver failed with status {problem.sol_status}")
if __name__ == '__main__':
optimizer = TransportOptimizer()
optimized_tasks = optimizer.optimize()

Linear programming solution for minimum number of resources

I am trying to solve this problem using linear programming using Pulp in python.
We have mango packs with each having different number of mangoes.
We should be able to serve the demand using the minimum number of packets and if possible serve the whole bag.
# Packet Names and the count of mangoes in each packet.
mangoe_packs = {
"pack_1": 2,
"pack_2": 3,
"pack_3": 3,
"pack_4": 2
}
For example,
Based on the demand we should get the correct packets. Ie., if the demand is 2, we give the packet with 2 mangoes. If the demand is 5, we serve packets with 2 and 3 mangoes. If your demand is 2 and we don't have any packet with 2 mangoes we can serve packet with 3 mangoes. In this case, we will have one remnant mango. Our purpose is to have the least number of remnant mangoes while serving the demand.
# Packet Names and the count of mangoes in each packet.
mangoe_packs = {
"pack_1": 2,
"pack_2": 3,
"pack_3": 3,
"pack_4": 2
}
Based on the data provided above,
If the demand is 2, The solution is pack_2 (can be pack_4 also).
If the demand is 4, The solution is pack_2 + pack_4.
If the demand is 5, The solution is pack_1 + pack_2
I am new to Linear programming and stuck at the problem. Tried few solutions and they are not working.
I am unable to come up with the correct objective function and constraints to solve this problem. Need help with that. Thank you.
Here is the code I tried.
from pulp import *
prob = LpProblem("MangoPacks", LpMinimize)
# Number of Mangoes in each packet.
mangoe_packs = {
"pack_1": 2,
"pack_2": 3,
"pack_3": 3,
"pack_4": 2
}
# Define demand variable.
demand = LpVariable("Demand", lowBound=2, HighBound=2, cat="Integer")
pack_count = LpVariable.dicts("Packet Count",
((i, j) for i in mangoe_packs.values() for j in ingredients),
lowBound=0,
cat='Integer')
pulp += (
lpSum([
pack_count[(pack)]
for pack, mango_count in mangoe_packs.iteritems()])
)
pulp += lpSum([j], for pack, j in mangoe_packs.iteritems()]) == 350 * 0.05
status = prob.solve()
Thank you.
Here are some considerations:
The variables of the problem are whether or not a pack should be opened. These variables are thus either 0 or 1 (keep closed, or open).
The main objective of the problem is to minimise the number of remnant mangoes. Or otherwise put: to minimise the total number of mangoes that are in the opened packs. This is the sum of the values of the input dictionary, but only of those entries where the corresponding LP variable is 1. Of course, a multiplication (with 0 or 1) can be used here.
In case of a tie, the number of opened packs should be minimised. This is simply the sum of the above mentioned variables. In order to combine this into one, single objective, multiply the value of the first objective with the total number of packets and add the value of this second objective to it. That way you get the right order in competing solutions.
The only constraint is that the sum of the number of mangoes in the opened packs is at least the number given in the input.
So here is an implementation:
def optimise(mango_packs, mango_count):
pack_names = list(mango_packs.keys())
prob = LpProblem("MangoPacks", LpMinimize)
# variables: names of the mango packs. We can either open them or not (0/1)
lp_vars = LpVariable.dicts("Open", pack_names, 0, 1, "Integer")
# objective: minimise total count of mangoes in the selected packs (so to
# minimise remnants). In case of a tie, minimise the number of opened packs.
prob += (
lpSum([mango_packs[name]*lp_vars[name] for name in pack_names]) * len(mango_packs)
+ lpSum([lp_vars[name] for name in pack_names])
)
# constraint: the opened packs need to amount to a minimum number of mangoes
prob += lpSum([mango_packs[name]*lp_vars[name] for name in pack_names]) >= mango_count
prob.solve()
In order to visualise the result, you could add the following in the above function:
print("Status:", LpStatus[prob.status])
# Each of the variables is printed with it's resolved optimum value
for i, v in enumerate(prob.variables()):
print("{}? {}".format(v.name, ("no","yes")[int(v.varValue)]))
Call the function like this:
# Packet Names and the count of mangoes in each packet.
mango_packs = {
"pack_1": 10,
"pack_2": 2,
"pack_3": 2,
"pack_4": 2
}
optimise(mango_packs, 5)
Output (when you added those print statements)
Status: Optimal
Open_pack_1? no
Open_pack_2? yes
Open_pack_3? yes
Open_pack_4? yes
See it run here -- give it some time to temporarily install the pulp module.
Here is a simple model that minimizes the total number of remnant mangoes. Instead of specifying the exact packages available the model just specifies the number of packages available per size (here 5 of size 2 and 15 of size 4):
from pulp import *
# PROBLEM DATA:
demand = [3, 7, 2, 5, 9, 3, 2, 4, 7, 5] # demand per order
packages = [0, 5, 0, 15] # available packages of different sizes
O = range(len(demand))
P = range(len(packages))
# DECLARE PROBLEM OBJECT:
prob = LpProblem('Mango delivery', LpMinimize)
# VARIABLES
assigned = pulp.LpVariable.dicts('assigned',
((o, p) for o in O for p in P), 0, max(demand), cat='Integer') # number of packages of different sizes per order
supply = LpVariable.dicts('supply', O, 0, max(demand), cat='Integer') # supply per order
remnant = LpVariable.dicts('remnant', O, 0, len(packages)-1, cat='Integer') # extra delivery per order
# OBJECTIVE
prob += lpSum(remnant) # minimize the total extra delivery
# CONSTRAINTS
for o in O:
prob += supply[o] == lpSum([p*assigned[(o, p)] for p in P])
prob += remnant[o] == supply[o] - demand[o]
for p in P:
# don't use more packages than available
prob += packages[p] >= lpSum([assigned[(o, p)] for o in O])
# SOLVE & PRINT RESULTS
prob.solve()
print(LpStatus[prob.status])
print('obj = ' + str(value(prob.objective)))
print('#remnants = ' + str(sum(int(remnant[o].varValue) for o in O)))
print('demand = ' + str(demand))
print('supply = ' + str([int(supply[o].varValue) for o in O]))
print('remnant = ' + str([int(remnant[o].varValue) for o in O]))
If the demand cannot be fulfilled this model will be infeasible. Another option in this case would be to maximize the number of orders fulfilled with a penalty for remnant mangoes. Here is the adapted model:
from pulp import *
# PROBLEM DATA:
demand = [3, 7, 2, 5, 9, 3, 2, 4, 7, 5] # demand per order
packages = [0, 5, 0, 5] # available packages of different sizes
O = range(len(demand))
P = range(len(packages))
M = max(demand) # a big enough number
# DECLARE PROBLEM OBJECT:
prob = LpProblem('Mango delivery', LpMaximize)
# VARIABLES
assigned = pulp.LpVariable.dicts('assigned',
((o, p) for o in O for p in P), 0, max(demand), cat='Integer') # number of packages of different sizes per order
supply = LpVariable.dicts('supply', O, 0, max(demand), cat='Integer') # supply per order
remnant = LpVariable.dicts('remnant', O, 0, len(packages)-1, cat='Integer') # extra delivery per order
served = LpVariable.dicts('served', O, cat='Binary') # whether an order is served
diff = LpVariable.dicts('diff', O, -M, len(packages)-1, cat='Integer') # difference between demand and supply
# OBJECTIVE
# primary objective is serve orders, secondary to minimize remnants
prob += 100*lpSum(served) - lpSum(remnant) # maximize served orders with a penalty for remnants
# CONSTRAINTS
for o in O:
prob += supply[o] == lpSum([p*assigned[(o, p)] for p in P])
prob += diff[o] == supply[o] - demand[o]
for p in P:
# don't use more packages than available
prob += packages[p] >= lpSum([assigned[(o, p)] for o in O])
for o in O:
# an order is served if supply >= demand
# formulation adapted from https://cs.stackexchange.com/questions/69531/greater-than-condition-in-integer-linear-program-with-a-binary-variable
prob += M*served[o] >= diff[o] + 1
prob += M*(served[o]-1) <= diff[o]
prob += lpSum([assigned[(o, p)] for p in P]) <= M*served[o]
for o in O:
# if order is served then remnant is supply - demand
# otherwise remnant is zero
prob += remnant[o] >= diff[o]
prob += remnant[o] <= diff[o] + M*(1-served[o])
# SOLVE & PRINT RESULTS
prob.solve()
print(LpStatus[prob.status])
print('obj = ' + str(value(prob.objective)))
print('#served = ' + str(sum(int(served[o].varValue) for o in O)))
print('#remnants = ' + str(sum(int(remnant[o].varValue) for o in O)))
print('served = ' + str([int(served[o].varValue) for o in O]))
print('demand = ' + str(demand))
print('supply = ' + str([int(supply[o].varValue) for o in O]))
print('remnant = ' + str([int(remnant[o].varValue) for o in O]))

Fill order from smaller packages?

The input is an integer that specifies the amount to be ordered.
There are predefined package sizes that have to be used to create that order.
e.g.
Packs
3 for $5
5 for $9
9 for $16
for an input order 13 the output should be:
2x5 + 1x3
So far I've the following approach:
remaining_order = 13
package_numbers = [9,5,3]
required_packages = []
while remaining_order > 0:
found = False
for pack_num in package_numbers:
if pack_num <= remaining_order:
required_packages.append(pack_num)
remaining_order -= pack_num
found = True
break
if not found:
break
But this will lead to the wrong result:
1x9 + 1x3
remaining: 1
So, you need to fill the order with the packages such that the total price is maximal? This is known as Knapsack problem. In that Wikipedia article you'll find several solutions written in Python.
To be more precise, you need a solution for the unbounded knapsack problem, in contrast to popular 0/1 knapsack problem (where each item can be packed only once). Here is working code from Rosetta:
from itertools import product
NAME, SIZE, VALUE = range(3)
items = (
# NAME, SIZE, VALUE
('A', 3, 5),
('B', 5, 9),
('C', 9, 16))
capacity = 13
def knapsack_unbounded_enumeration(items, C):
# find max of any one item
max1 = [int(C / item[SIZE]) for item in items]
itemsizes = [item[SIZE] for item in items]
itemvalues = [item[VALUE] for item in items]
# def totvalue(itemscount, =itemsizes, itemvalues=itemvalues, C=C):
def totvalue(itemscount):
# nonlocal itemsizes, itemvalues, C
totsize = sum(n * size for n, size in zip(itemscount, itemsizes))
totval = sum(n * val for n, val in zip(itemscount, itemvalues))
return (totval, -totsize) if totsize <= C else (-1, 0)
# Try all combinations of bounty items from 0 up to max1
bagged = max(product(*[range(n + 1) for n in max1]), key=totvalue)
numbagged = sum(bagged)
value, size = totvalue(bagged)
size = -size
# convert to (iten, count) pairs) in name order
bagged = ['%dx%d' % (n, items[i][SIZE]) for i, n in enumerate(bagged) if n]
return value, size, numbagged, bagged
if __name__ == '__main__':
value, size, numbagged, bagged = knapsack_unbounded_enumeration(items, capacity)
print(value)
print(bagged)
Output is:
23
['1x3', '2x5']
Keep in mind that this is a NP-hard problem, so it will blow as you enter some large values :)
You can use itertools.product:
import itertools
remaining_order = 13
package_numbers = [9,5,3]
required_packages = []
a=min([x for i in range(1,remaining_order+1//min(package_numbers)) for x in itertools.product(package_numbers,repeat=i)],key=lambda x: abs(sum(x)-remaining_order))
remaining_order-=sum(a)
print(a)
print(remaining_order)
Output:
(5, 5, 3)
0
This simply does the below steps:
Get value closest to 13, in the list with all the product values.
Then simply make it modify the number of remaining_order.
If you want it output with 'x':
import itertools
from collections import Counter
remaining_order = 13
package_numbers = [9,5,3]
required_packages = []
a=min([x for i in range(1,remaining_order+1//min(package_numbers)) for x in itertools.product(package_numbers,repeat=i)],key=lambda x: abs(sum(x)-remaining_order))
remaining_order-=sum(a)
print(' '.join(['{0}x{1}'.format(v,k) for k,v in Counter(a).items()]))
print(remaining_order)
Output:
2x5 + 1x3
0
For you problem, I tried two implementations depending on what you want, in both of the solutions I supposed you absolutely needed your remaining to be at 0. Otherwise the algorithm will return you -1. If you need them, tell me I can adapt my algorithm.
As the algorithm is implemented via dynamic programming, it handles good inputs, at least more than 130 packages !
In the first solution, I admitted we fill with the biggest package each time.
I n the second solution, I try to minimize the price, but the number of packages should always be 0.
remaining_order = 13
package_numbers = sorted([9,5,3], reverse=True) # To make sure the biggest package is the first element
prices = {9: 16, 5: 9, 3: 5}
required_packages = []
# First solution, using the biggest package each time, and making the total order remaining at 0 each time
ans = [[] for _ in range(remaining_order + 1)]
ans[0] = [0, 0, 0]
for i in range(1, remaining_order + 1):
for index, package_number in enumerate(package_numbers):
if i-package_number > -1:
tmp = ans[i-package_number]
if tmp != -1:
ans[i] = [tmp[x] if x != index else tmp[x] + 1 for x in range(len(tmp))]
break
else: # Using for else instead of a boolean value `found`
ans[i] = -1 # -1 is the not found combinations
print(ans[13]) # [0, 2, 1]
print(ans[9]) # [1, 0, 0]
# Second solution, minimizing the price with order at 0
def price(x):
return 16*x[0]+9*x[1]+5*x[2]
ans = [[] for _ in range(remaining_order + 1)]
ans[0] = ([0, 0, 0],0) # combination + price
for i in range(1, remaining_order + 1):
# The not found packages will be (-1, float('inf'))
minimal_price = float('inf')
minimal_combinations = -1
for index, package_number in enumerate(package_numbers):
if i-package_number > -1:
tmp = ans[i-package_number]
if tmp != (-1, float('inf')):
tmp_price = price(tmp[0]) + prices[package_number]
if tmp_price < minimal_price:
minimal_price = tmp_price
minimal_combinations = [tmp[0][x] if x != index else tmp[0][x] + 1 for x in range(len(tmp[0]))]
ans[i] = (minimal_combinations, minimal_price)
print(ans[13]) # ([0, 2, 1], 23)
print(ans[9]) # ([0, 0, 3], 15) Because the price of three packages is lower than the price of a package of 9
In case you need a solution for a small number of possible
package_numbers
but a possibly very big
remaining_order,
in which case all the other solutions would fail, you can use this to reduce remaining_order:
import numpy as np
remaining_order = 13
package_numbers = [9,5,3]
required_packages = []
sub_max=np.sum([(np.product(package_numbers)/i-1)*i for i in package_numbers])
while remaining_order > sub_max:
remaining_order -= np.product(package_numbers)
required_packages.append([max(package_numbers)]*np.product(package_numbers)/max(package_numbers))
Because if any package is in required_packages more often than (np.product(package_numbers)/i-1)*i it's sum is equal to np.product(package_numbers). In case the package max(package_numbers) isn't the one with the samllest price per unit, take the one with the smallest price per unit instead.
Example:
remaining_order = 100
package_numbers = [5,3]
Any part of remaining_order bigger than 5*2 plus 3*4 = 22 can be sorted out by adding 5 three times to the solution and taking remaining_order - 5*3.
So remaining order that actually needs to be calculated is 10. Which can then be solved to beeing 2 times 5. The rest is filled with 6 times 15 which is 18 times 5.
In case the number of possible package_numbers is bigger than just a handful, I recommend building a lookup table (with one of the others answers' code) for all numbers below sub_max which will make this immensely fast for any input.
Since no declaration about the object function is found, I assume your goal is to maximize the package value within the pack's capability.
Explanation: time complexity is fixed. Optimal solution may not be filling the highest valued item as many as possible, you have to search all possible combinations. However, you can reuse the possible optimal solutions you have searched to save space. For example, [5,5,3] is derived from adding 3 to a previous [5,5] try so the intermediate result can be "cached". You may either use an array or you may use a set to store possible solutions. The code below runs the same performance as the rosetta code but I think it's clearer.
To further optimize, use a priority set for opts.
costs = [3,5,9]
value = [5,9,16]
volume = 130
# solutions
opts = set()
opts.add(tuple([0]))
# calc total value
cost_val = dict(zip(costs, value))
def total_value(opt):
return sum([cost_val.get(cost,0) for cost in opt])
def possible_solutions():
solutions = set()
for opt in opts:
for cost in costs:
if cost + sum(opt) > volume:
continue
cnt = (volume - sum(opt)) // cost
for _ in range(1, cnt + 1):
sol = tuple(list(opt) + [cost] * _)
solutions.add(sol)
return solutions
def optimize_max_return(opts):
if not opts:
return tuple([])
cur = list(opts)[0]
for sol in opts:
if total_value(sol) > total_value(cur):
cur = sol
return cur
while sum(optimize_max_return(opts)) <= volume - min(costs):
opts = opts.union(possible_solutions())
print(optimize_max_return(opts))
If your requirement is "just fill the pack" it'll be even simpler using the volume for each item instead.

Categories

Resources