This may sound a bit odd and maybe I'm just missing the forest through the trees on this question, but is there a way to force the Excel Solver to return only one instance of a result? As a short example imagine that we have some results on the likability of various objects (colors, animals, and shapes). We want the solver to return the three most preferred objects from this list.
Red (400)
Dog (120)
Circle (100)
Red (400)
Cat (90)
Square (75)
Blue (90)
Horse (60)
Triangle (70)
Green (80)
Snake (30)
Rectangle (40)
Yellow (40)
Rabbit (20)
Pentagon (15)
The problem is, of course, simplified in this example. Basically, my issue arises in that I want one of each type, namely Red, Dog, and Circle but I keep getting Red, Red (again), and Dog because the total is higher. I want to define a way to prevent Solver from returning two values named the same. I just can't seem to figure it out and Google doesn't seem to produce any viable responses either.
It's unclear how your data is setup, and this could affect how you setup the Solver problem, but here is one method (nb - this method will only work if you have 200 or fewer values to choose from).
Make Column A for "Category". This would have values such as "Color", "Animal", and "Shape".
Column B would be for "Type", and contain the information you provided. (e.g. Dog, Cat, ... Red, Blue, ... Circle, Square, ...)
Column C is the Value or Score for the type shown in Column B, again the information you provided.
Column D has fields that Solver will manipulate, let's call it "Selected". Selected will be a 0 or a 1.
Column E is the result of selection, a simple calculation, =C2*D2, filled down.
Make Cell H2 the sum of Column E. This will be your objective for Solver.
Make G3 through G5 the values in "Category" (Color, Animal, Shape).
Make H3 through H5 the total selected values in each category. That is =SUMIF($A$2:$A$16,"="&G3,$D$2:$D$16) filled down.
The workbook looks like this ...
... from this, you can setup Solver with the following ...
Set Objective: is $H$2
To: is set to Max. (i.e. you are looking for the most preferred)
By Changing Variable Cells: is set to $D$2:$D$16
Subject to the Constraints: has four entries. $D$2:$D$16 = binary; $H$3 = 1; $H$4 = 1; $H$5 = 1
Select a Solving Method: is set to Evolutionary. You can use GRG Nonlinear, but it takes longer.
The dialog looks like this ...
... with the following result, which meets your criteria ...
Related
I am currently trying to check if a number in a comma-separated string is within a number interval. What I am trying to do is to check if an area code (from the comma-separated string) is within the interval of an area.
The data:
AREAS
Area interval
Name
Number of locations
1000-1499
Area 1
?
1500-1799
Area 2
?
1800-1999
Area 3
?
GEOLOCATIONS
Name
Areas List
Location A
1200, 1400
Location B
1020, 1720
Location C
1700, 1920
Location D
1940, 1950, 1730
The result I want here is the number of unique locations in the "Areas list" within the area interval. So Location D should only count ONCE in the 1800-1999 "area", and the Location A the same in the 1000-1499 location. But location B should count as one in both 1000-1499 and one in 1500-1799 (because a number from each interval is in the comma-separated string in "Areas list"):
Area interval
Name
Number of locations
1000-1499
Area 1
2
1500-1799
Area 2
3
1800-1999
Area 3
2
How is this possible?
I have tried with a COUNTIFS, but it doesnt seem to do the job.
Here is one option using FILTERXML():
Formula in C2:
=SUM(FILTERXML("<x><t>"&TEXTJOIN("</s></t><t>",,"1<s>"&SUBSTITUTE(B$7:B$10,", ","</s><s>"))&"</s></t></x>","//t[count(.//*[.>="&SUBSTITUTE(A2,"-","][.<=")&"])>0]"))
Where:
"<x><t>"&TEXTJOIN("</s></t><t>",,"1<s>"&SUBSTITUTE(B$7:B$10,", ","</s><s>"))&"</s></t></x>" - Is the part where we construct a valid piece of XML. The theory here is that we use three axes here. Each t-node will be named a literal 1 to make sure that once we return them with xpath we can sum the result. The outer x-nodes are there to make sure Excel will handle the inner axes correctly. If you are curious to know how this xml-syntax looks at the end, it's best to step through using the 'Evaluate Formula' function on the Data-tab;
//t[count(.//*[.>="&SUBSTITUTE(A2,"-","][.<=")&"])>0]")) - Basically means that we collect all t-nodes where the count of child s-nodes that are >= to the leftmost number and <= to the rightmost number is larger than zero. For A2 the xpath would look like //t[count(.//*[.>=1000][.<=1499])>0]")) after substitution. In short: //t - Select t-nodes, where count(.//* select all child-nodes where count of nodes that fullfill both requirements [.>=1000][.<=1499] is larger than zero;
Since all t-nodes equal the number 1, the SUM() of these t-nodes equals the amount of unique locations that have at least one area in its Areas List;
Important to note that FILTERXML() will result into an error if no t-nodes could be found. That would mean we need to wrap the FILTERXML() in an IFERROR(...., 0) to counter that and make the SUM() still work correctly.
Or, wrap the above in BYROW():
Formula in C2:
=BYROW(A2:A4,LAMBDA(a,SUM(FILTERXML("<x><t>"&TEXTJOIN("</s></t><t>",,"1<s>"&SUBSTITUTE(B$7:B$10,", ","</s><s>"))&"</s></t></x>","//t[count(.//*[.>="&SUBSTITUTE(a,"-","][.<=")&"])>0]"))))
Using MMULT and TEXTSPLIT:
=LET(rng,TEXTSPLIT(D2,"-"),
tarr,IFERROR(--TRIM(TEXTSPLIT(TEXTJOIN(";",,$B$2:$B$5),",",";")),0),
SUM(--(MMULT((tarr>=--TAKE(rng,,1))*(tarr<=--TAKE(rng,,-1)),SEQUENCE(COLUMNS(tarr),,1,0))>0)))
I am in very distinguished company but will add my version anyway as byrow probably is a slightly different approach
=LET(range,B$2:B$5,
lowerLimit,--#TEXTSPLIT(E2,"-"),
upperLimit,--INDEX(TEXTSPLIT(E2,"-"),2),
counts,BYROW(range,LAMBDA(r,SUM((--TEXTSPLIT(r,",")>=lowerLimit)*(--TEXTSPLIT(r,",")<=upperLimit)))),
SUM(--(counts>0))
)
Here the ugly way to do it, with A LOT of helper columns. But not so complicated 🙂
F4= =TRANSPOSE(FILTERXML("<m><r>"&SUBSTITUTE(B4;",";"</r><r>")&"</r></m>";"//r"))
F11= =TRANSPOSE(FILTERXML("<m><r>"&SUBSTITUTE(A11;"-";"</r><r>")&"</r></m>";"//r"))
F16= =SUM(F18:F21)
F18= =IF(SUM(($F4:$O4>=$F$11)*($F4:$O4<=$G$11))>0;1;"")
G18= =IF(SUM(($F4:$O4>=$F$12)*($F4:$O4<=$G$12))>0;1;"")
H18= =IF(SUM(($F4:$O4>=$F$13)*($F4:$O4<=$G$13))>0;1;"")
My son found this math game app and I've been playing it. Essentially you have a 5x5 grid of numbers from 0-9, and then at the end of each column and row is a target sum. Every number in the grid can be turned on or off, and when the correct sum is achieved that row or column is highlighted. You can actually play on harder levels, but for an excel problem I decided to try 5x5 just to see if I could get solver to find the answer.
The way I designed this is as follows:
(a) Cells B2-F6 have the number values from the grid.
(b) Cells H2-L6 I put in all zeroes.
(c) Cells N2-S6 I put in as the product of B2 and H2, B3 and H3, etc.
(d) Cells N8-R8 are the sum of N2-N6, O2-O6, etc.
(e) Cells S2-S6 are the sums of N2-R2, N3-R3, etc.
(f) Cells N9-R9 are the target identified by the game.
(g) Cells N10-R10 are N9-N8, O9-O8, etc.
(h) Cells T2-T6 are the target identified by the game.
(i) Cells U2-U6 are T2-S2, T3-S3, etc.
(j) Cell W2 is =MAX(T2:T6,N9:R9)
(k) Cell T12 is =N10*W2^9+O10*W2^8+P10*W2^7+Q10*W2^6+R10*W2^5+U2*W2^4+U3*W2^3+U4*W2^2+U5*W2^1+U6*W2^0
In solver, I set the object of $T$12 to be value of 0, with constraints on variable cells $H$2:$L$6 as <=1, >=0, integer.
However, I can't seem to get solver to find me a solution. I solved it in the game first so I had a solution. When I put that in, solver will tell me it found a solution after changing nothing, or on the evolutionary one it will tell me it hasn't found anything better...of course not, I've already given you the solution. But if you change just one of the 5x5 grid to be wrong, it still can't find a solution. Is this just too complex for solver? It's weird to me that when it fails it has non-integer values in the cells. I keep wondering why it's trying non-integer values when I told it not to (and I did check to be sure my Ignore Integer Constraints box is unchecked). I understand that there are 2^25 different possibilities it should have to try, but there's an infinite number more (okay, maybe finite if it has a maximum number of decimals in the data type, but for all practical, non-quantum purposes, it's infinite) if it ignores my integer requirements. Maybe it's just not comfortable with a brute-force approach and is trying to head a particular direction and just keeps finding that unsatisfactory. I also recognize that my cell T12 formula may be too much for it to handle. It just won't let me have multiple targets so I was trying to come up with a way for it to guarantee no false positives (e.g., if you just add all the difference cells in N10-S10 and U2-U6, which should all be zero, you can actually get solutions where some of the columns are non-zero but add up to zero because of positives and negatives...and those are result cells, so I can't similarly constrain them, I think).
Anyhow, here's the number set:
2 9 3 2 3
9 9 8 4 1
2 6 5 2 6
3 1 6 8 7
6 8 3 6 9
Row totals = 13, 4, 7, 19, 32 from top to bottom
Column totals = 13, 18, 8, 20, 16
I'd give you the answer...but where's the fun in that?
I've structured a formula that should spit out a numerical answer that is basically a simple sum formula with IF statements assigning numerical values to qualitative inputs. Apologies for the length, I am looking at a number of different factors.Formula is:
Cell F36 contains:
=IF(F32="Medium-High", 18, IF(F32="High", 24, SUM(F16, IF(F20="High",5,IF(F20="Medium-High",4,IF(F20="Medium",3,IF(F20="Low-medium",2,IF(F20="Low",1))))),IF(F24="High",5,IF(F24="Medium-High",4,IF(F24="Medium",3,IF(F24="Low-Medium",2,IF(F24="Low",1))))),IF(F28="High",5,IF(F28="Medium-High",4,IF(F28="Medium",3,IF(F28="Low-Medium",2,IF(F28="Low",1,IF(F28="N/A",0)))))),IF(F30="N/A",0,IF(F30="High",5,IF(F30="Medium-High",4,IF(F30="Medium",3,IF(F30="Low-Medium",2,IF(F30="Low",1)))))),IF(F32="High",5,IF(F32="Medium-High",4,IF(F32="Medium",3,IF(F32="Low-Medium",2,IF(F32="Low",1,IF(F32="N/A",0)))))))))
F16 - contains values from 1 to 5
F20, F24, F28, F30, F32 - Are dropdowns, where you choose a value: Low, Low-Medium, Medium, Medium-High, High;
Right now, based on a test input, where: F16 = 5, F20 = Low (1), F24= Medium-High (4), F28 = N/A (0), F30 = Medium (3), F32 = Medium (3) my output in F36 is 11, however doing simple adding, I should be at 5+1+4+0+3+3 = 16.
Where am I losing 5 points?
You can create a table allocating numerical output to each qualitative inputs and using vlookup to get your output. For example, for a table created on K19:L25, the following can be used:
=SUM(F16,VLOOKUP(F20,K19:L25,2,FALSE),VLOOKUP(F24,K19:L25,2,FALSE),VLOOKUP(F28,K19:L25,2,FALSE),VLOOKUP(F30,K19:L25,2,FALSE),VLOOKUP(F32,K19:L25,2,FALSE))
EDIT:
Tried your formula and it gives the correct result. Maybe your 5 in F16 is not a numerical value? You can test it by using
=ISNUMBER(F16)
and it should give a TRUE if it is a number.
Debug embedded IFs is just hell.
What you are doing is recode a 5 levels scale. You may use alternative solutions.
a. place the 5 text values (from "Low" to "High") somewhere in a sheet, say in T1:T5, even better, give this range a name
then you can retrieve the values you search with
MATCH(F20,$T$1:$T$5,0)
b. if it's suitable for your use, you may use combo boxes, presenting the 5 choices and giving the desired answer (from 1 to 5) in a linked cell.
(maybe not the answer, but I can't comment…)
(This should be a comment but I needed the formatting capabilities of an answer)
This is the current logic of your formula:
Check cell F32: "Medium-High" outputs 18, "High" outputs 24
If F32 is neither of those, then SUM the following:
F16
Lookup for F20, F24, F28, F30, and F32 where the lookup for each is:
"High" = 5
"Medium-High" = 4
"Medium" = 3
"Low-Medium" = 2
"Low" = 1
I find it strange that you first check cell F32 only to check it again later in the sum. Because this current logic produces incorrect results, we can't really advise how to fix it without sample data and expected results. My guess is you are summing too many lookups by including the F32 in there, but that's just speculation without data.
I am trying to write a formula.
The variables are:
1 - Type (Copper or Aluminum)
2 - Amps (100, 150 or 200)
3 - Distance (in feet).
Determining the type of wire, the amps the wire will be pushing, and the distance determines the wire type.
Example: if I am using Aluminum type wire, for 100 Amps that is less than 150 ft, I would use type 1 wire.
Second Example: if I am using Aluminum, for 100 Amps that is greater than 150 ft but less than 200 feet, I would use 1/0 wire.
Any suggestions? Thanks!
Dimitri
Here is an example formula using all three variables as you requested. The scant information in your example is not inclusive enough to construct a formula that will account for all the variables involved.
Column "A" has the wire (aluminum); Column "B" has the amperage and "C" has the length in feet. D1 has the formula: =IF(AND(A1="Aluminum",B1=100,C1<150),"Type 1",IF(AND(A1="Aluminum",B1=100,(AND(C1>150,C1<200))),"1/0","?")).
Two issues: The "1/0" text will produce an error flag in the cell because of an interpretation for a two-year date. Simply click "ignore error" for all these cells. Secondly, the question mark is there because of not knowing what you would want to do with the other variables if the ones within the formula both prove false. This formula is only a partial answer that sees your examples.
=IF(AND(D10=100,$L$4=1),VLOOKUP(I10,$A$136:$C$158,3,TRUE),IF(AND(D10=150,$L$4=1),VLOOKUP(I10,$E$136:$G$154,3,TRUE),IF(AND(D10=200,$L$4=1),VLOOKUP(I10,$H$136:$J$149,3,TRUE),IF(AND(D10=100,$L$5=1),VLOOKUP(I10,$K$136:$M$158,3,TRUE),IF(AND(D10=150,$L$5=1),VLOOKUP(I10,$N$136:$P$158,3,TRUE),IF(AND(D10=200,$L$5=1),VLOOKUP(I10,$Q$136:$S$154,3,TRUE),"UNKNOWN"))))))
That ended up being my formula. Worked Great.
I have established a datasheet with all 4th grade students. This data includes name, race, economically challenged, and their latest state assessment scores.
Column D is race - N if White / Y if other than White
Column E is economic situation of child - if in trouble Y / otherwise N
Column G is their test score.
Here is what I cannot get to work...
If any student scores less than a 70, we need the text to be RED (which I can do easily).
However, I need to figure out a way to recognize this...
If D is = Y, AND the grade is less than 70... I need to fill the cell with red and have white text. Any student that is D = N and still less than 70... text should remain red.
That is one problem. I need a separate rule to recognize when D = Y and E = Y AND score is less than 70... I need to fill that cell with black.
Our goal is to recognize quickly the deficiencies in our non-White and very low income students.
Is there anyone who may be able to help?
You can do this with conditional formatting.
Under conditional formating go to Manage Rules. You are going to add three rules. In the end it will look like this:
The formulas are:
=AND($D2 = "Y",$E2 = "Y",$G2 <70)
=AND($D2 = "Y",$G2 <70)
=$G2 <70
Add the three rules with the corresponding formats. Then make sure they are in the order that is shown in the picture. Also make sure that the "Applies to" cover the entire needed range.