Parenthesization that minimize values of an expression - dynamic-programming

I need to find a parenthesization for E= c1 O1 c2 O2 .... On-1 cn where c(i) are integers and O(i) could be + or *, that minimize the obtainable value through a parenthesization.
I know that's probably a very basic question, but I just started learning Dynamic Programming.
My main problem is how can I distinguish if O(i) is + or * (or is this useless ? )

Related

Repetition of nested IF statements

I'm struggling to get an Excel query to work. I'm trying to have multiple queries like this within a query and am not sure how to work it. The query so far is this:
=IF(B6=$R$2,B2*$S$2,IF($B$6=$R$3,B2*$S$3,IF(B6=$R$4,B2*$S$4,B2)))
Here is what I'm trying to do, there are the 7 multipliers below, the above statement is for only one of them.
The logic is, that if the cell says Good it is multiplied by 97.5%, if Bad by 102.5%, if OK it's multiplied by 100%, but I need the odds to be multiplied by all 7 areas beneath. So its essentially an if/else statement 7 times within itself:
If B6 = Good * S2, else * S4, then if B7 = Good * S2, else S4, essentially that repeated 7 times down.
How can I do this in Excel?
In B2 and copied down:
=B2*LOOKUP(B6,{"Bad","Good","OK"},{1.025,0.975,1})
might suit but would still require entries into E2 etc.
Might not be as easy to understand but should give the same results (for the three conditions) and is a little shorter:
=B2*(1+.025*((B6="Bad")-(B6="Good")))
It would be better to use VLOOKUP as follows :
=B2 * VLOOKUP(B6,$R$2:$S$4,2) * VLOOKUP(B7,$R$2:$S$4,2) *
VLOOKUP(B8,$R$2:$S$4,2) * VLOOKUP(B9,$R$2:$S$4,2) *
VLOOKUP(B10,$R$2:$S$4,2) * VLOOKUP(B11,$R$2:$S$4,2) *
VLOOKUP(B12,$R$2:$S$4,2)
You could develop a simple User Defined function through VBA though. It would be better and more efficient.

Is h5 = (h1 + h2 + h3 ) / 3 admissible?

I am having a really hard time with the "math" aseptic of heuristic functions. I day dreamed for 3 minutes today in my AI class and I missed the explanation. Can someone explain to me how I can calculate if a heuristic function is admissible? I posted this one ( Is h5 = (h1 + h2 + h3 ) / 3 admissible?) but honestly I it does not have to be this problem. I just understand better by example.
Also, I have the "AI: A modern approach" book with me, but I cannot find an example. If you know where I could find one, I would be grateful.
First, we recall that an heuristic function is is said to be admissible if it never overestimates the cost of reaching the goal. What this means?
In short it means that, if an heuristic function returns a value h for a state x there are no real solution with a lower cost of x. For instance, for pathfinding the euclidean distance between the current point and the destination is admissible because no path can be shorter to go in a straight line! In other word, an admissible heuristic is always optimistic.
Now, we can return to your question. We have three admissible heuristics h1, h2 and h3 and we want to find if the average of these three functions is admissible as well. Now we can call X(s) the best possible cost from a state s to the destination (in other word is the cost of the optimal solution). The value of X is obviously unknown but it will be useful.
Because h1, h2 and h3 are admissible we know that for any state s:
h1(s) < X(s) (remember: h1 never overestimates the optimal cost!)
h2(s) < X(s)
h3(s) < X(s)
Then, because h5 is the average of the other three functions we know for sure that for each state it is bounded between min(h1(s),h2(s),h3(s)) and max(h1(s),h2(s),h3(s)). So we can say that for each state s:
h5(s) <= max(h1(s),h2(s),h3(s)) <= X(s)
And so also h5 is admissible.

Excel Solver: Is there a way to iterate over 2 changing variables?

I have an issue with solver as follows (simplified version):
So I have a nested If statement that describes condition for 2 changing variables(x,y). For example:
In one cell: IF(AND((x<=2),(x>=0.5),(y<=10),(y>=5)),1,0
The cell below it: IF(AND((x<=2.5),(x>=1.9),(y<=11),(y>=9)),1,0
The objective function is the sum of these 2 variables
Solver or goal seek (unless i give it the awnser) can't seem to get an awnser other than 0,0.
My actual problem is that i have 6 of these IF cells and I'm trying to find an (x,y) that maximizes my objective function. I want excel to go through as many combinations it can.
Any thoughts or other ways to do this? Thanks.
The reason that the Solver does not find the optimal solution in this toy problem is because the use of IF and AND statements make the problem non convex. For non-convex problems, the GRG Nonlinear solution method (the default used by solver) does not guarantee an optimal solution, as it can be trapped in locally best solutions which are not optimal.
Having said that, there is a way to formulate your problem as a mixed integer program, which, although still non-convex, can be solved with the "Simplex LP" method of Solver, and give a guaranteed maximum.
Model Setup
Here is a screenshot of the spreadsheet setup:
For convenience, I have used named ranges for the several quantities.
In particular:
- B2 --> x_var
- C2 --> x_UB1
- D2 --> x_LB1
- E2 --> x_UB2
- F2 --> x_LB2
and for row 3 I use the same convention, but instead of x_ we have y_.
The red cells (B4 and E4) have the conditions you described, and the blue cell (B5) has their sum.
For example, the condition for B4 reads
=IF(AND(x_var<=x_UB1,x_var>=x_LB1,y_var<=y_UB1,y_var>=y_LB1),1,0)
We are going to replace these expressions with two binary variables, which equal one if each expression is satisfied and zero otherwise.
The logic is that instead of an IF expression we can impose the constraints:
LB_x * z <= x <= UB_x * z
LB_y * z <= y <= UB_y * z
z is binary
then z = 1 ==> LB_x <= x <= UB_x
LB_y <= y <= UB_y
and because we maximize the sum of the two z variables, the x and y will try to fit i the corresponding ranges so that as many z as possible equal 1.
The green cells H2, J2 have the two new binary varibles, called cond1_true, cond2_true respectively. The other cells have the constraints described above:
For example, for the first expression:
J2: =x_var-cond1_true*x_UB1
J3: =y_var-cond1_true*y_UB1
K2: =x_LB1*cond1_true-x_var
K3: =y_LB1*cond1_true-y_var
All these cells need to be <= 0 in the solver model.
Solver Model:
In the mode, the objective function cell is the sum of the binary variables. The decision variables are x_var, y_yar, cond1_true, cond2_true. The constraints are all in expression <= 0 format. Here is the worksheet: https://www.dropbox.com/s/uek2k9gownhh3ni/excel-solver-is-there-a-way-to-iterate-over-2-changing-variables.xlsx?dl=0
Using this formulation, the solver goes through many combinations of variables and tries to pick up the best one. It can often guarantee an optimal solution (which is almost always the case for small problems)
UPDATE
If the intervals are non overlapping we need to modily
LB_x * z <= x <= UB_x * z
to
min(LB_x) * (1-z) + LB_x * z <= x <= UB_x * z + max(UB_x) * (1-z)
Where min(LB_x) is the minimum lower bound across all intervals (likewise for UB and for y). This way, if an x does not fall into the interval (z=0) it is only forced to fall in some other interval.
I hope this helps!

What is the easiest way of composing this polynomial expressions in Excel?

This is trivial in programming languages, but when I need to do something quick & dirty and to see whether I got the coefficients right, I usually go for Excel.
So, what have I got,
c1(i) c2(i) c3(i)
0,321323232 1 0
0,32132122 0 2
0,321214324 1 2
...
..
.
The polynomial expression is
KT = SUM( AEA0 * c1(i) + J * c2(i) + PD * c3(i) )
i=1,37
One approach would be to make columns with AEA0, J, PD (btw, these are constants) and then multiply & sum them with the coefficient columns. I don't like this one because it adds a lot of extra stuff, and it messes up my copy pasting later (when I copy paste the coefficients to a text file).
Second approach would be to manually form an expression clicking all the cells one by one,
KT = AEA0 cell * A1 + J * B1 + PD * C1 + AEA0 * A2 + J * B2 + PD * C2 + ...
you get the point. Is there a way to improve this approach in some way so it takes a range of A1 to A37?
Is there some better approach to forming this kind of expressions?
Put the value for AEA0 in Z1. Put the value for J in Z2. put the value for PD in Z3.
Then you can use:
=SUM(A1:A37)*Z1+SUM(B1:B37)*Z2+SUM(C1:C37)*Z3

Can I use a decision tree to compare values for pairs of attributes?

I would like to use a decision tree for binary classification. I would like to know if my approach is a valid approach for decision trees.
Each instance in my data set has pairs of attributes, and I have identified that for some pairs, I can compare the values to make a decision. For example, an instance may have the following attributes:
instance = {A1, A2, A3, A4, B1, B2, B3, B4}
A1 and B1 have different values, but refer to the same feature--this is that I meant when I referred to them as a pair. What I would like to do is have nodes in the tree that compare values of a pair:
(A1 > B1)
/ \
(A2 < B2) (A3 > B3)
/ \ / \
...
Is this a valid approach using decision trees?
Is there a better learning approach for this type of problem?
This is a valid approach indeed. All you need is to create new binary features like
C[i] = 1 if A[i] > B[i] else 0
or just
C[i] = A[i] - B[i]
and feed them to an ordinary decision tree algorithm, like rpart in R on sklearn.tree.DecisionTreeClassifier in Python.

Resources