Excel Solver Finding a Target Sum from Subset of Number Set

老子叫甜甜 提交于 2019-12-23 02:05:15

问题


I want to use Solver to find a target sum from a subset of a larger set. (10 number subset of 16 numbers). I'm not very familiar with Solver and I'm unsure of the best way to set my constraints.

I have 5 categories of numbers within the set, A, B, C, D, E. The set of 16 may include different combinations of these categories, but the target subset total has a defined makeup: A, B, B, C, C, C, (B or C), (B or C),D, E

So my question is, how would I introduce these rules into Solver?

Secondarily, I know Solver comes up with 1 solution, is there a way to get a range of solutions that are closest to the target? So, if I have a set that doesn't quite produce the target, I could see 3 solutions that are nearest the target (plus or minus)?

Thanks.


回答1:


TL/DR - Here's a way to setup the problem you have. Getting a series of answers that are "close" is out of scope for Solver. You could do it with VBA, if you define additional criteria to search around. (Edit - added set up for defined makeup)

Comments

It seems there is no criteria about "a number must be selected from each category", or "a number must be selected from category X". Rather, you have a bunch of numbers, each belongs to a category, but you want to find the combination of numbers that provide a certain total - then investigate the categories afterward.

Initial Problem Setup

From the information you provided in your comment, I put together the following ...

Column A (Category) and Column B (Value) is the information you provided. Column C (Selected) is just 0 or 1. Column D (Result) is the multiplication =B2*C2 filled down.

F2 is the sum of Column D. G2 is the target value for this sum (as you provided). H2 is the squared error of the calculation =(F2-G2)^2.

This is the solver setup ...

  • Set Objective: is set to $H$2 - the squared error of the calculation
  • To: is set to Min. You could choose Value Of: 0, but if there is no exact answer, it may fail.
  • By Changing Variable Cells: is set to $C$2:$C$17 or Column C.
  • Subject to the Constraints: includes $C$2:$C$17 = binary. This forces the values to be either 0 or 1.
  • Select a Solving Method: is set to Evolutionary. GRG Nonlinear can sometimes solve this type of problem, but it takes much longer than Evolutionary.

Below is the result - different from what you reported. When I force your result, I get 106.61 for CalcTotal (perhaps a transcription error somewhere) ...

Finding Near Results

Solver is an optimizer, so it will find "the answer" that best fits your objective.

In order for it to provide you with different answers, you need to provide it with different objectives.

For example, you may want to force a solution that uses C1, but is still closest to your objective. The setup might look like this...

... with this solution ...

In that example, I excluded $C$10 from the "By Changing Variable Cells:" and from the "Subject To Constraints:" fields. Another way to accomplish the same thing would have been to keep the original field values unchanged, but to add an additional constraint of $C$10 = 1.

Or, as another example, perhaps you want a solution that chooses exactly two values from Category B. You could modify the setup to include sums of values selected from each category, and add a constraint. Here is the setup ...

... and the corresponding result ...

Making an Algorithm

If you determine criteria for the range of solutions you are looking for, you could setup a VBA sub to loop through setting up Solver, getting the solution, and storing the results of the "Selected Column" for review.

If you want to pursue this track, I suggest you look at other solutions provided on this site, try to set something up, and ask a new question if you have problems.

Edit - missed one of your criteria

I missed addressing your statement - "the target subset total has a defined makeup: A, B, B, C, C, C, (B or C), (B or C),D, E"

In this instance, using the setup containing sums for each category, you can specify these constraints ...

  • $G$4 = 1
  • $G$5 <= 4
  • $G$5 >= 2
  • $G$6 <= 5
  • $G$6 >= 3
  • $G$7 = 1
  • $G$8 = 1

On first pass, it provided this result: A1, B2, B3, C1, C3, C4, C5, C6, D1, E1 with a total of 106.38.



来源:https://stackoverflow.com/questions/41653105/excel-solver-finding-a-target-sum-from-subset-of-number-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!