//Method called every time a new numericUpDown (+ associated Label) has to be added to pnlPredInputs public void setInputNumUpDown(int count, Variable curVariable) { NumericUpDown curNum = new NumericUpDown(); Label curLabel = new Label(); if (count == 0) { curNum = numPredInput0; curNum.Visible = true; curLabel = lblPredInp0; curLabel.Visible = true; } else { //---- New numUpDown curNum.Name = "numPredInput" + count.ToString(); curNum.TextAlign = numPredInput0.TextAlign; curNum.Width = numPredInput0.Width; curNum.Font = numPredInput0.Font; curNum.Cursor = numPredInput0.Cursor; curNum.ValueChanged += new System.EventHandler(numPredInput_ValueChanged); pnlPredInputs.Controls.Add(curNum); curNum.Location = new Point(numPredInput0.Location.X, numPredInput0.Location.Y + 50 * count); //----- //---- New Label curLabel.Name = "lblPredInp" + count.ToString(); curLabel.Font = lblPredInp0.Font; curLabel.ForeColor = lblPredInp0.ForeColor; pnlPredInputs.Controls.Add(curLabel); curLabel.Location = new Point(lblPredInp0.Location.X, lblPredInp0.Location.Y + 50 * count); //---- } curNum.Tag = curVariable; updateMinMaxInputNum(curNum); curLabel.Text = curVariable.input.displayedShortName + ":"; curLabel.Refresh(); }
public List<double> realVals; //Y (independent variable) values as read from the input file #endregion Fields #region Constructors public ValidCombination() { realVals = new List<double>(); calcVals = new List<RowVal>(); errors = new List<double>(); coeffs = new PolCoeffs(); dependentVars = new Combination(); independentVar = new Variable(); assessment = new Assessment(); }
public CombinationItem() { variable = new Variable(); operation = new Operation(); }
//Method actually creating a new ValidCombination variable and performing preliminary validity checks private ValidCombination newCombination(Combination curCombination, Variable yVar, List<Input> inputs, List<ValidCombination> allValidCombinations, Config curConfig) { //Performing the regression and the corresponding analysis to determine whether this specific combination should be stored or not CombValues xVals = Common.getCombinationListVals(curCombination, inputs); CombValues yVals = new CombValues(); yVals.combination = new Combination(); int maxDec = inputs[yVar.index].vals.Max(x => x <= Int32.MaxValue ? ((decimal)x - Convert.ToInt32((decimal)x)).ToString().Length - 2 : 0); if (maxDec < 0) maxDec = 0; Variable curVariable = new Variable() { index = yVar.index, input = inputs[yVar.index], noDec = maxDec }; CombinationItem curItem = new CombinationItem() { variable = curVariable, operation = new Operation(), exponent = 1.0 }; yVals.combination.items.Add(curItem); for (int row = 0; row < inputs[yVar.index].vals.Count; row++) { RowVal curRowVal = new RowVal(); curRowVal.value = inputs[yVar.index].vals[row]; curRowVal.weights.Add(1.0); yVals.values.Add(curRowVal); } Analysis curAnalysis = new Analysis(); ValidCombination curValidCombination = curAnalysis.createValidComb(curCombination, inputs, xVals, yVals, curConfig, true); if (curValidCombination != null && allValidCombinations.Count > 0) { if (alreadyStored(curValidCombination, allValidCombinations)) { curValidCombination = null; } } return curValidCombination; }
//Method starting the whole combinatorics process (and, subsequently, the calculations one) for the given independent variable and all the remaining "Input" (columns) private Results mainCombinations(Results curResults, List<Input> inputs, Variable indepVar) { //Loop accounting for combinations consisting in just one variable for (int col = 0; col < inputs.Count; col++) { if (cancelSim) return curResults; if (col != indepVar.index) curResults.combinations = addCombination(inputs, new List<int> { col }, curResults.config, curResults.combinations, indepVar); } //Main loop combining all the variables among them, together with all the exponents and operations for (int col = 0; col < inputs.Count - 1; col++) //All the columns from the start until the previous to the last one { if (cancelSim) return curResults; if (col == indepVar.index) continue; MainCalcs.bgwMain.ReportProgress(0, "Independent variable: " + indepVar.input.displayedName + " - Analysing all the combinations involving: " + inputs[col].displayedName + Environment.NewLine + "Valid solutions so far: " + curResults.combinations.Count.ToString()); for (int col2 = col; col2 < inputs.Count; col2++) //All the columns from "col" to the last one { if (cancelSim) return curResults; if (col2 == indepVar.index) continue; int maxComb = inputs.Count - 1 - col2; for (int comb = 0; comb < maxComb; comb++) // All the combinations of columns. For example: with 1, 2, 3, 4, when col is 1, there are upto 3 (e.g., 2-3-4) { if (cancelSim) return curResults; int col3 = col2; List<int> curList = new List<int>(); curList.Add(col); for (int comb2 = 0; comb2 <= comb; comb2++) { col3 = col3 + 1; if (col3 != indepVar.index) { curList.Add(col3); //List including the variables being accounted in the current combination } } curResults.combinations = addCombination(inputs, curList, curResults.config, curResults.combinations, indepVar); } } } return curResults; }
//Method called from the main combinatorics loops for multivariate cases above (addMulti). Its whole purpose is reducing the size of the loops. //It includes the "more internal loops", the ones creating the corresponding ValidCombination and adding it to the list of all the combinations so far private List<ValidCombination> internalLoop(int[] curOpers, int[] curExps, List<ValidCombination> allCombinations, Config curConfig, List<int> indices, List<Input> inputs, Variable indepVar) { for (int rel = 0; rel < curConfig.operations.Count; rel++) { if (cancelSim) return allCombinations; Combination curCombination = new Combination(); curOpers[indices.Count - 2] = rel; for (int i = 0; i < indices.Count; i++) { int genIndex = indices[i]; //The cast to decimal type is included in order to avoid problems with the double floating point (e.g., without casting to decimal, 10.55 wouldn't be found) int maxDec = inputs[genIndex].vals.Max(x => x <= Int32.MaxValue ? ((decimal)x - Convert.ToInt32((decimal)x)).ToString().Length - 2 : 0); if (maxDec < 0) maxDec = 0; Variable curVariable = new Variable { index = genIndex, input = inputs[genIndex], noDec = maxDec }; CombinationItem curItem = new CombinationItem() { variable = curVariable, exponent = curConfig.exponents[curExps[i]], operation = curConfig.operations[curOpers[i]] }; curCombination.items.Add(curItem); } allCombinations = addToAllCombinations(curCombination, inputs, curConfig, allCombinations, indepVar); } return allCombinations; }
//This method creates a new "Variable" instance for the given independent variable (y) from the original "Input" one and starts the combinating/calculating processes private Results combsForIndep(List<Input> allInputs, int curIndex, Results curResults) { int maxDec = allInputs[curIndex].vals.Max(x => x <= Int32.MaxValue ? ((decimal)x - Convert.ToInt32((decimal)x)).ToString().Length - 2 : 0); if (maxDec < 0) maxDec = 0; Variable indepVar = new Variable { index = curIndex, input = allInputs[curIndex], noDec = maxDec }; return mainCombinations(curResults, allInputs, indepVar); }
//Method starting the creation of the corresponding "ValidCombination" and, eventually, storing it in the list including all the ones so far private List<ValidCombination> addToAllCombinations(Combination curCombination, List<Input> inputs, Config curConfig, List<ValidCombination> allCombinations, Variable indepVar) { ValidCombination curValid = newCombination(curCombination, indepVar, inputs, allCombinations, curConfig); if (curValid != null) { if (allCombinations.Count >= curConfig.maxNoCombs) { allCombinations = allCombinations.OrderByDescending(x => x.assessment.globalRating).ToList(); if (curValid.assessment.globalRating > allCombinations[allCombinations.Count - 1].assessment.globalRating) { allCombinations.RemoveAt(allCombinations.Count - 1); } else { curValid = null; } } if (curValid != null) { if (curValid.dependentVars.items.Count < 1) { if (curValid.coeffs.B != 0.0 || curValid.coeffs.C != 0.0) curValid = null; } } } if(curValid != null) allCombinations.Add(curValid); return allCombinations; }
//Method performing all the required actions to create the combinations under the most difficult conditions (i.e., more than one variable), that is: perform all the //combinations among variables, exponents and operations; call the methods in charge of creating the corresponding "ValidCombination"; and, eventually, add the new //instance to the list of all the valid combinations so far //NOTA DEL CREADOR: modestia aparte, esta función es una puta obra de arte (en Spanish porque suena mejor :)) private List<ValidCombination> addMulti(List<Input> inputs, List<int> indices, Config curConfig, List<ValidCombination> allCombinations, Variable indepVar) { int[] curExps = new int[indices.Count]; int[] curOpers = new int[indices.Count]; //The code below these lines is fairly complicated as far as it has to deal with many variations (i.e., all the possible combinations among exponents, operations and variables). //In any case, it should be noted that a relevant "combinatorics effort" has already been done before calling this function, that is: setting all the possible combinations of variables. //The combinations are created as shown in the following example (vars: var1, var2, var3; exps: 1, 2; operations: *, +): // var1^1 * var2^1 * var3^1 // var1^1 * var2^1 + var3^1 // var1^1 * var2^1 * var3^2 // var1^1 * var2^1 + var3^2 // var1^1 + var2^1 * var3^1 // var1^1 + var2^1 + var3^1 // var1^1 + var2^1 * var3^2 //etc. ExpRelUpdate obj1 = new ExpRelUpdate(indices.Count - 2, curConfig.exponents.Count - 1, indices.Count, curExps); curExps[obj1.index] = -1; while (!obj1.completed) { obj1 = updateObjs13(obj1, true); if (obj1.completed) break; ExpRelUpdate obj2 = new ExpRelUpdate(indices.Count - 2, curConfig.exponents.Count - 1, indices.Count, curExps); while (!obj2.completed) { for (int i = 0; i < indices.Count; i++) { curOpers[i] = 0; } ExpRelUpdate obj3 = new ExpRelUpdate(indices.Count - 2, curConfig.operations.Count - 1, indices.Count, curOpers); curOpers[obj3.index] = -1; while (!obj3.completed) { obj3 = updateObjs13(obj3, false); if (obj3.completed) break; for (int exp = 0; exp < curConfig.exponents.Count; exp++) { if (cancelSim) break; curExps[indices.Count - 1] = exp; allCombinations = internalLoop(curOpers, curExps, allCombinations, curConfig, indices, inputs, indepVar); } } obj2 = updateObj2(obj2); if (obj2.otherProp) { obj1.completed = true; break; } } } return allCombinations; }
//Method in charge of putting together all the inputs for the given combination (i.e., variables, exponents and operations) and bring together all the remaining factors (e.g., suitability of the fit) //to form the associated ValidCombination and add it to the list of valid combinations created so far private List<ValidCombination> addCombination(List<Input> inputs, List<int> indices, Config curConfig, List<ValidCombination> allCombinations, Variable indepVar) { if (indices.Count == 1) { //Only one variable is accounted for and thus the whole combinatorial process will consist in accounting for the different exponents int genIndex = indices[0]; for (int i = 0; i < curConfig.exponents.Count; i++) { if (cancelSim) break; Combination curCombination = new Combination(); //The cast to decimal type is included in order to avoid problems from the double floating point (e.g., 10.55) int maxDec = inputs[genIndex].vals.Max(x => x <= Int32.MaxValue ? ((decimal)x - Convert.ToInt32((decimal)x)).ToString().Length - 2 : 0); if (maxDec < 0) maxDec = 0; Variable curVariable = new Variable { index = genIndex, input = inputs[genIndex], noDec = maxDec }; CombinationItem curItem = new CombinationItem() { variable = curVariable, exponent = curConfig.exponents[i], operation = curConfig.operations[0] }; curCombination.items.Add(curItem); allCombinations = addToAllCombinations(curCombination, inputs, curConfig, allCombinations, indepVar); } } else { //Various variables are accounted for and thus everything (i.e., variables, exponents & operations) have to be brought into picture allCombinations = addMulti(inputs, indices, curConfig, allCombinations, indepVar); } return allCombinations; }