I have 10 warehouses across the US. Each of them may or may not have product A, B, C, D, E in stock. Someone orders all five items from my website.
I want to minimize th
Your problem is exactly the classical set cover optimization problem, which is NP-hard; the "universe" is the set of items that the user wants, and the candidate sets are the warehouses.
The algorithm proposed by @user384706 and @Kickaha is the greedy algorithm discussed there, which is an okay approximation.
If you want to find the actual optimal solution, your best bet is to use the integer linear programming formulation and plug into some ILP solver.
You say you don't care about distances, but if you did the set cover problem also accounts for a cost per warehouse (c(s)
on the wiki page) that would represent farther-away warehouses being less optimal to pick.
Here is my psudeocode that does not take into account that the search depth could be more than 1, but I believe the basic algorithm is sound.
ItemList = (A,B,C,D,E,F)
//Iterate until all items have been scheduled
do while length of Itemlist > 0
{
var1=0;
Warehouse="";
//Search all the warehouses to find which has the most items in the list
For w = 1 to 10
{
val2 = function[select no of items available from Itemlist](ItemList,w);
if var2 > var1 then
{var1=var2;
Warehouse=w;}
}
//Return a list of the items available from the warehouse with the most items available
DBrecordset = function[select no of items available from Itemlist](ItemList,Warehouse);
//schedule them
Output to file (Warehouse + DB recordset contents)
//remove the scheduled items from the list
function [Remove items from ItemList](ItemList,DBRecordset)
}
How about the following:
IF a warehouse has all items THEN pick that warehouse
ELSE
Find how many items each warehouse has e.g. NY (2) Boston(3) Chicago(2)
Sort them by items Boston(3) NY(2) Chicago (2)
Current Max is the warehouse with the most products i.e. (3)
Step 1: Start making shipments starting from the warehouse with the current max i.e. Boston.
Step 2: As you go on check if you have all the packets and not e.g. NY has 2 and Chicago also has 2 but they are the same 2. Keep this tuple as potential.
Step 3: Once you finished with all warehouses, update current max to be the next highest number of products a warehouse has. In this case (2)
IF no warehouse left you are done
ELSE go to Step 1
Continue until you have shipments for all items
As the number of locations is rather small, you could prepare a list with shipment options in ascending order of shipments:
10 x shipment alternatives from 1 location
45 x shipment alternatives from 2 locations
120 x shipment alternatives from 3 locations
210 x shipment alternatives from 4 locations
252 x shipment alternatives from 5 locations
For a given order, you'd scan your table and check for every option whether it is feasible for the given order. The first match is a viable solution. Assuming that a customer cannot order more than five products, no more than five shipments have to be taken into account.
In real, you'd take into consideration that customers might order larger quantities. Your warehouses might have a non-zero but limited number of products. So, the checking might be cumbersome in the end. Which brings us back to the ILP solver mentioned obove.
Well, what I could think of on top of my head is that you need 2 matrices. first one will have columns as the place of your warehouse and rows will be products A,B,C..ect. Like for the given case it will be a 5X3 matrix. Another matrix will contain the distance vectors from your warehouse to the destination. Like columns could be your warehouse place - New York, Chicago, etc and rows could be the places where you want to ship. Now to figure out which warehouse will send which product should be the outcome of the minimum distance from 2nd matrix and maximum products available from the first matrix. there are definitely some method to get it from two matrix , you just need to search and do some maths.