I have an optimization problem where I have 5 variables: A, B1, B2, C1, C2. I am trying to optimize these 5 variables to get the smallest root sum square value I can. I have a few optimization techniques that are working ok, but this one in particular is giving me some trouble.
I want to explore all 32 options of changing the variables and pick the smallest RSS value. What I mean by this is each variable can either be +/- an increment. And each choice leads to 2 more choices, with 5 variables thats 32 choices. ( 2^5 )
To clarify, I am not adding my variables: A, B1, B2 etc to each other, I am incrementing / decrementing them by an arbitrary amount. A +/- X, B1+/- X2, etc. And I am trying to figure out which combination of incrementation/ decrementation of my 5 variables will return the lowest Root sum square value.
A
+/ \-
B1 B1
+/\- +/\-
B2 B2 B2 B2
So on and so forth till all 5 levels are complete. I am not sure even where to start to attempt to solve this. What kind of data structure would be best for storing this? Is it an iterative, or a recursive solution. I don’t need the answer to the problem, rather somewhere to look or somewhere to start. Thank you again for taking the time to look at this.
To clarify further confusion this is my optimization method. I ahve 5 variables, and 5 increments, each increment matches to a variable.
(a,b,c,d,e,f) —> (incA, incB, incC, indD, incE, incF)
I want to find the best combination of +/- incX to X ( x being one of the 5 variables )ie:
the solution might be something like:
a+incA , B-incB, c+incC, d+incD, e+incE, f-incF.
There are 32 possibilities of combinations, after reading through all the answers below I have settled on this possible algorithm. ( see my answer below ) make edits and questions as necessary. This is not a perfect algorithm, it is for clarification and ease of understanding, i know it can be condensed.
//Settign all possible solutions to be iterated through later.
double[] levelA = new double[2];
double[] levelB = new double[2];
double[] levelC = new double[2];
double[] levelD = new double[2];
double[] levelE = new double[2];
levelA[0] = a + incA;
levelA[1] = a - incA;
levelB[0] = b + incB;
levelB[1] = b - incB;
levelC[0] = c + incC;
levelC[1] = c - incC;
levelD[0] = d + incD;
levelD[1] = d - incD;
levelE[0] = e + incE;
levelE[1] = e - incE;
double[] rootSumAnswers = new double[32];
int count = 0;
for(int i = 0; i < 2; i ++)
{
for(int k = 0; k < 2; k++)
{
for(int j = 0; j < 2; j ++)
{
for(int n = 0; n < 2; n++)
{
for(int m = 0; m < 2; m++)
{
rootSumAnswers[count++] = calcRootSum(levelA[i], levelB[k], levelC[j], levelD[n], levelE[m]);
}
}
}
}
}
//Finally, i find the minimum of my root sum squares and make that change permanent, and do this again.
You can produce a set containing all possible sets of operations (e.g. {-,-,-,-}, {-,-,-,+}, {-,-,+,-} etc.) using the following function which will output a list of bool arrays where true and false represent + and -. The
varsparameter indicates the number of variables being combined. Note that for 5 variables, there are only 16 combinations (not 32 as you state) since there are only 4 operators when combining 5 variables (assuming the first variable cannot be negated):Once you have this list, you can use it to combine the variables in all possible ways. First we define a helper function to take a set of variables and a single
bool[]combination and applies it (it assumes that there are the correct number of elements in the combination for the number of variables passed):You can also format the combination nicely using:
Putting it all together to test all possible combinations:
This will output:
Edit:
Per the changes to your question I have updated my answer. As others have mentioned, it may not be necessary to use a full search such as this, but you may find the method useful anyway.
GetOperators()changes slightly to return 2n combinations (rather than 2n-1 as before):The
Combine()method is changed to take a set of increments in addition to the variables and the combination to be used. For each element of the combination, if it istrue, the increment is added to the variable, if it is false, it is subtracted:And
FormatCombination()is also updated to display the new combination style:Putting it all together:
Output (abridged):