Given one (1xN) list of positive weights (not necessarily integers i.e. floats) and an equaled-length (1xN) list of corresponding costs I want to find the subset of the weight list that sums exactly to a given sum S and has the lowest cost (sum of costs*weights corresponding to the subset in the weight list). Written in Python would be best (if possible) since I am not that good with other languages!
Example:
w = [2.5, 3.0, 1.0, 5.5] # Weight list
c = [1.0, 1.5, 2.0, 3.0] # Cost list
S = 6.5 # Target sum
For this case we have two possible subsets that sums to S:
sub1 = [2.5, 3.0, 1.0]
sub2 = [1.0, 5.5]
The costs of these subsets are:
cost1 = 2.5*1.0+3.0*1.5+1.0*2.0 = 9.0
cost2 = 1.0*2.0+5.5*3.0 = 18.5
Since subset 1 has the lowest cost (9.0) this is the subset I want.
One possible solution would of course be to calculate all possible combinations and then just pick the minimum of the calculated costs. I was hoping that there is a more efficient solution to this problem.
I have search for different solutions but I could only find solutions for Python which solve the problem of equal sum and not simultaneously getting the lowest cost. Here is an example of such a solution: Algorithm to find which number in a list sum up to a certain number.