In Fractional Knapsack problem,
1. prepare the third array, value per weight array, dividing the weight of each item by its corresponding value
2. sort the items in descending order according to their value per weight
the reason behind the step 1 and 2?
Because it is more profitable to fill all possible volume with substance having the highest value/weight ratio. So use up all given quantity of item with the best value/weight ratio, then with the second and so on.
Just think - if you believe that it is possible to fill some part with less valuable item (while more valuable one is available) - change it to more valuable and you'll have got some additional money
In fractional knapsack you can add part/fraction of item in your knapsack(answer) to maximize the total value of item in your bag. As the goal is to maximize the total value in knapsack we should put items such that the value of weight being put is high and it takes as less space as possible , so that more can be added to the knapsack. Hence, the weight per value is required to calculate. The capacity of knapsack is not unlimited , hence we need value per weight to utilize the space optimally.
For example if we have weight ={10,20,30,} and value {60,100,120} and bag can hold 50 at Max then if don't divide the item then we can have only 1 item of 20 and 1 of 30 . So the total value will be 220.
But as per fractional knapsack we divide value by weight and get the array {6,5,4} . Sort it(already sorted ) . Now the order of item becomes i1,i2, i3
Take all 10 kg of i1=6*10
Take all 20kg of i2= 5*20
Take the remaining 20kg from i3= 4*((2/3)*30)
Total value is= 60+100+80= 240
Hence, we need value per weight.
Refer the link: https://www.geeksforgeeks.org/fractional-knapsack-problem/
This is related to basic mathematics. Let's take a constant, which has numerator and denominator.
In this case:
Constant: value/weight ratio.
Numerator: value
Denominator: weight
As the Constant and Numerator have direct relation, when the constant is bigger, the numerator is also bigger. Simultaneously, when the constant is bigger, the denominator is smaller, as they have indirect relation.
So, when we're arranging ratios in descending order, we're trying to sort entries of maximum value and less weight relatively and put them in our bag with given capacity in that order.
Related
I have an array of integers. Lets denote it by A. There is another array W containing weights associated with each entry of A.
In other words, associated with each entry A[i] is a certain weight entry W[i]. (Note that the weights are out of a limited set S_w = {w1,w2,w3,w4} so only few possible values)
The problem statement is as follows: Pick a random number of entries out of A such that when summed together, give you the highest value (SUM_A) under the constraint that the sum of their respective weights (SUM_W) doesn't exceed a threshold, W_threshold.
One possibility is brute force: Compute all permutations of A and for each permutation, select first n entries such that their sum weight SUM_W doesn't exceed W_threshold. Finally, the permutation that gives the maximum SUM_A shall be selected. But the complexity of this scheme is very high due to permutation computation step since the length of A is not constrained.
One other (sub-optimal) possibility is to sort A in descending order and then select first n entries such that their SUM_W doesn't exceed W_threshold. This has lower complexity but the result would be suboptimal.
Could someone give me tips if their already exists an algorithm to resolve the above stated problem? Or if anyone has ideas better than the ones I described above. Many thanks
I'm not a computer science major, so I have fairly limited knowledge of algorithms. Lately I was thinking of a market bot of some sort and I have a key question that I cannot handle with brute-force.
Question: Let there be a list of items, where number of items are greater
than 10000. Each item has a value in between 0 and 1, and a price. Value and
price are independent of each other. You must choose the cheapest 10 items
where their average (or total) value is greater or equal than a given value.
I thought of several algorithms such as:
-Sort the list by price
-Divide the list in 5 item chunks, reducing the brute
force steps from 10000nCr10 to 2000nCr2.
Obviously it will not give the true cheapest combination, but hopefully close enough? I appreciate any help.
You can solve it using integer linear programming. Let the value of item i be v[i] and its cost c[i]. Let x[i] be the number of each item you buy (which can take the values 0 or 1), and V be the minimum acceptable total value. The 0/1 constraint on x[i] makes this an integer linear program rather than a simpler linear program.
Then you want so minimize sum(c[i]*x[i]) such that sum(v[i]*x[i]) >= V and sum(x[i]) = 10, which is a problem of the right form to be solved as an ILP.
Here's a good open-source solver: https://www.gnu.org/software/glpk/
Suppose I have an ordered list of weights, having length M. I want to divide this list into N ordered non-empty sublists, where the sum of the weights in each sublist are as close to each other as possible. Finally, the length of the list will always be greater than or equal to the number of partitions.
For example:
A reader of epoch fantasy wants to read the entire Wheel of Time series in N = 90 days. She wants to read approximately the same amount of words each day, but she doesn't want to break a single chapter across two days. Obviously, she also doesn't want to read it out of order either. The series has a total of M chapters, and she has a list of the word counts in each.
What algorithm could she use to calculate the optimum reading schedule?
In this example, the weights probably won't vary much, but the algorithm I'm seeking should be general enough to handle weights that vary widely.
As for what I consider optimum, I would say that given the choice between having two or three partitions vary in weight a small amount from the average would be better than having one partition vary a lot. Or in other words, She would rather have several days where she reads a few hundred more or fewer words than the average, if it means she can avoid having to read a thousand words more or fewer than the average, even once. My thinking is to use something like this to compute the score of any given solution:
let W_1, W_2, W_3 ... w_N be the weights of each partition (calculated by simply summing the weights of its elements).
let x be the total weight of the list, divided by its length M.
Then the score would be the sum, where I goes from 1 to N of (X - w_i)^2
So, I think I know a way to score each solution. The question is, what's the best way to minimize the score, other than brute force?
Any help or pointers in the right direction would be much appreciated!
As hinted by the first entry under "Related" on the right column of this page, you are probably looking for a "minimum raggedness word wrap" algorithm.
I want to rank item types by comparing the ratio of frequency in basket 1 over frequency in another basket 2.
For example, if item type A has about 5 counts in basket 1 and 0 counts in basket 2, this should rank much higher than type B with say 10 items in basket 1 and 10 items in basket 2. I use the odds ratio abs(log(freq in basket1/freq in basket2)), however this doesn't capture the fact that I should prioritize abs(log(10/100)) as abs(log(1/10)).
I'm thinking whether to add multiply this result by their the total count e.g (10+100)abs(log(10/100)) but then again this amount seems to overwhelm the log value.
What would be a good suggestion to weigh the log values?
The standard approach to these types of tasks is to model the item as a biased coin that produces baskets, B1 with probability p and B2 with 1 - p. Intuitively this means that an item type has an underlying true ratio of baskets which produces a particular split of items between baskets. So a "90% A" might produce [9,1] but also [10,0] or even [0,10] although this result with a pretty low probability.
Then you can look at a sample like [5,0] and [10,1] and calculate a confidence interval for the parameter p, then rank the item types by the lower bound of the interval. This way [10,2] will sort above [5,1]. Even though proportions in both samples are the same, [10,2] will have a narrower confidence interval and thus its lower bound will be higher.
The idea and some more detailed formulas is described at: http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
Say I have the following for a bunch of items.
item position
item size
item length
A smaller position is better, but a larger length and size are better.
I want to find the item that has the smallest position, largest length and size.
Can I simply calculate a value such as (total - position) * size * length for each item, and then find the item with the largest value? Would it be better to work off percentages?
Either add a fourth item, which is your calculated value of 'goodness', and sort by that OR if your language of choice allows, override the comparason operators for sorting to use your formula and then sort. Note that the latter approach means that the function to determine betterness will be applied multiple times per item in the list, but it has the advantage of ease of making a procedural comparason possible (eg first look at the position, then if that is equal, look at size then length) - athough this could also be expressed as a formula resulting in a single number to sort by.
As for your proposed formula, note that each item has the same numerical weight even though they are measured on completely unrelated scales. Furthermore, all items with either position=total, size=0 or length=0 evaluate to zero.
If what you want is that position is the most important thing, but given equal positions, size is the next most important thing, but given equal positions and sizes, then go by length, this can be formulated into a single number as follows:
(P-position)*(S*L) + size*L + length
where L is a magic number that is greater than the maximum possible length value, S is a number greater than the maximum possible size value, and P is a number greater than the maximum possible position value.
If, on the other hand, what you want is some scale where the items are of whatever relative importances, one possible formula looks like this:
((P-position)/P)*pScale * (size/S)*sScale * (length/L)*lScale
In this version, P, S and L have much the same definitions as before - but it is very inmportant that the values of P, S and L are meaningful in a compatible way, e.g all very close to expected maximum values. pScale, sScale and lScale are there so you can essentially specify the relative importance of each item. They could all be 1 if all atems are equally important, in which case you could leave them out entirely.
As previously answered, though, there are also a potentially infinite number of other ways you could choose to code this. As a random example, for large sizes, length could become less important; those possibilities would require much additional thought as to what is actually meant by such a vague statement.