An algorithm for the mellon-selling farmer - algorithm

Question i saw on the net:
A melon-selling farmer has n melons. The weight of each melon, an integer (lbs), is distinct. A customer asks for exactly m pounds of uncut melons. Now, the farmer has the following problem: If it is possible to satisfy the customer, he should do so by finding the appropriate melons as efficiently as possible, or else tell the customer that it is not possible to fulfill his request.
note: This is not homework btw, i just need guidance.
My Answer:
This seems similar to the coin change problem(knapsack) and the subset problem (backtracking).
Coin-change: i can put the weights into a set w = {5, 8, 3 , 2,....} then solve and the same goes for the Subset problem.
So basically i can use either method to solve this problem?

This is exactly an integer knapsack problem where solutions have zero wasteage. There is a good dynamic programming/memoization strategy to help you solve it. See either of these links:
http://www.cs.ship.edu/~tbriggs/dynamic/
https://en.wikipedia.org/wiki/Knapsack_problem
Indeed, the subset-sum problem IS the 0/1 knapsack problem where the weight equals the value of each item.

Related

Solving Minimum time target problem by solving HJB equation numerically

Here is the problem where I am trying to solve by solving HJB equation, where the following term is coming:
To solve this, what I am doing is from one point I am iterating for all u than deciding h that stencil remains in the 9 point neighborhood, and then taking the 5 top most weight (based on distance) and lastly deciding the max value to update the value function but it is actually diverging.
https://colab.research.google.com/drive/1J805FxYLRBnRQAbvRRDVKwyqGO9wkR_M#scrollTo=ykFMv5YNKGBQ
I have written everything in function form please give a look, it is easy to understand, if anybody have any suggestion please weigh in

Which mathematical optimization problem is this?

I have an combinational optimization problem and I do not know its name in literature.
My problem is the following:
I have n sets containing exclusive elements, so each element is present only in a set.
An element is characterized by 2 constraints values, and one profit.
I have to choose an element from each set in order to maximize the sum of the profits, while keeping the sum of each constraint below a a specified limit.
Is this an already studied problem? WHich is its name?
Can I assimilate it to an already studied problem?
Thanks to #Berthur & #mrBen replies, I discovered that this is a multiple-constrained knapsack problem where you have to create an indicator variable to force that only one element will be chosen by each set
The problem you're describing is know as the multiple-choice knapsack problem. In your case, as you have 2 constraints, it's actually a 2-dimentional multiple-choice knapsack problem.
With the keywords multi-dimentional multiple-choice knapsack problem (sometime abbreviated as MMKP) you should be able to find the corresponding literature.

Booking System is NP Complete

I have to show that the following problem is NP-Complete and need some helpful hints on how to proceed.
The problem:
We're looking at a meeting booking system. The input is a list n of possible times as well as m lists (where m <= n), one list per person containing their choice of possible meeting times. For each possible time, a priority number is also given. For each reservation time in the list of n, a cost is also given. (Cost of booking the room). The algorithm should assign times so that the combined priority for those who have booked should be as small as possible while the total cost of booking should not be higher than M.
NP
So first to show that it's in NP we should show that given a correct solution it can be verified that it is indeed correct. I guess it should verify that that the cost is below the threshold of K and that the priority of the correct solution is indeed the minimum - both of which can be done in Polynomial time I assume. We traverse through the lists of people, assert that each one has a time granted to them, add up the cost in a variable and at the end of this list assert that the cost is below K. The priority can be dealt with in similar fashion I suppose?
NP Hard
Then to show it's NP Hard I can use the Knapsack Problem since they're rather similar. With input S, size of bag, a list of items with weight w and value v as well as the goal W which is the goal-value. I guess it's clear that S can correlate to cost and that W correlate to the priority? So we want S, the size, to be below S i.e we have the similar condition for the problem above where the cost has to be below K. Then W, the total value should generally exceed W, but in our case we want it to be as low as possible which seems doable.
I'm afraid I might've gone the wrong way when it comes to verifying the problem. Also the reduction to show it's NP Hard is perhaps not thought out all the way. Some pointers would be very helpful! Thanks
NP
When you are proving the problem is in NP, you must first turn your problem into a decision problem. Then you can verify your certificate in polynomial time as you started to describe.
NP Hard
You need to transform the Knapsack problem into your meeting problem. You are going the right way because you are transforming size and weight from Knapsack into the meeting problem. Once you figure out the transformation, you must verify that it can be done in polynomial time. Finally, you can show that the solution to Knapsack is a solution to meeting problem and vice versa.

0/1 knapsack with dependent item weight?

The standard 0/1 knapsack requires that the weight of every item is independent to others. Then DP is a efficient algorithm towards the solution. But now I met a similar but extensions of this problem, that
the weight of new items are dependent on previous items already in
the knapsack.
For example, we have 5 items a, b, c, d and e with weight w_a, ..., w_e. item b and c have weight dependency.
When b is already in the knapsack, the weight of item c will be smaller than w_c because it can share some space with b, i.e. weight(b&c) < w_b + w_c. Symmetrically, when c is already in the knapsack, the weight of b will be smaller than w_b.
This uncertainty results a failure of original DP algorithm, since it depend on the correctness of previous iterations which may not correct now. I have read some papers about knapsack but they either have dependencies subjected to profit (quadratic knapsack problem), or have variable weight which follows a random distribution (stochastic knapsack problem). I have also aware of the previous question 1/0 Knapsack Variation with Weighted Edges, but there is only a very generic answer available, and no answer about what is the name of this knapsack.
One existing solution:
I have also read one approximate solution in a paper about DBMS optimizations, where they group the related items as one combined item for knapsack. If use this technique into our example, the items for knapsack will be a, bc, d, e, therefore there is no more dependencies between any two of these four items. However it is easy to construct an example that does not get optimal result, like when an item with "small weight and benefit" is grouped with another item with "large weight and benefit". In this example, the "small" item should not be selected in solution, but is selected together with the "large" item.
Question:
Is there any kind of efficient solving techniques that can get optimal result, or at least with some error guarantee? Or am I taking the wrong direction for modelling this problem?
Could you not have items a, b, c, bc, d and e? Possibly with a constraint that b and bc can't be both in the knapsack and similarly so with c and bc? My understanding is that that would be a correct solution since any solution that has b and c can be improved by substituting both by bc (by definition). The constraints on membership should take care of any other cases.
This is a very interesting problem and I have been working on this for a while. The first thing to consider is that binary knapsack problem with dependent item weights/value is not trivial at all. You may consider using Bayesian networks, Markov models, and other similar techniques for solving this problem. Nonetheless, any practical approach to this problem has to make some assumptions either about the optimization model or its input. Here is an example of formulating the binary knapsack problem with value-dependent items. https://arxiv.org/pdf/1702.06662.pdf
In this work, authors have proposed modeling the input (value-related dependencies) using fuzzy graphs and then using the proposed integer linear programming model to solve the optimization problem. An extended version of the work has been accepted for publication and will be soon available online.
Please do not hesitate to contact me if you needed further information. I can also provide you with the source code of the model if needed.
In the end I managed to solve the problem with the B&B method proposed by #Holt. Here is the key settings:
(0) Before running the B&B algorithm, group all items depend on their dependency. All items in one partition have weight dependency with all other items in the same group, but not with items in other groups.
Settings for B&B:
(1) Upper-bound: assume that the current item has the minimum weight, i.e. assume all dependencies exist.
(2) Lower-bound: assume that the current item has the maximum weight, i.e. assume all dependencies do not exist.
(3) Current weight: Calculate the real current weight.
All the above calculations can be done in a linear time by playing around with the groups we get in step 0. Specifically, when obtaining those weights, scanning only items in current group (the group which the current item be in) is enough - items in other groups have no dependencies with the current one, so it will not change the real weight of current item.

KNapsack variant. Ideas?

I have the following knapsack problem variant:
I want to buy X units of a product at min cost, and there are m farmers that offer:
-
and I can choose at most one option from each farmer.
Formally, I want to
Could you please let me know if this problem resembles a variant of 0-1 knapsack problem?
I would be grateful if you could provide any references where I can find more information about this or any relevant algorithms.
i think you can relate this to those (0-1 Knapsack) kind of problems. here also you will have to select one option (the max number of units) from a farmer, possibly the one having the least cost/unit ratio, and then call the programm recursively, this time having one less farmer (the one from which the items are chosen from) the last time.

Resources