private double GetTupleProbabilityKjcClass(BitArray tuple) { double Kjc = 0; BitArray distortionVect = new BitArray(_truthTable.InputNumberOfDigits, false); // 00...0 distortionVect[0] = true; // 00..01 do { double tempProbability = 1.0; // find the probability of the operandIt tuple with distortionVect distortion for (int i = 0; i < _truthTable.InputNumberOfDigits; i++) { int bitValue = Convert.ToInt32(tuple[i]); // value of i-th: zero or one if (distortionVect[i]) { tempProbability *= _inputBitsDistortionsProbabilities.AutoCorrectionValueProbability[bitValue][i] * _inputBitsDistortionsProbabilities.ProbabilityZeroAndOne(bitValue, i); } else { tempProbability *= _truthTable.CorrectValueProbability[i] * _inputBitsDistortionsProbabilities.ProbabilityZeroAndOne(bitValue, i); } } Kjc += tempProbability; } while (BooleanFuntionWithInputDistortion.IncrementOperand(distortionVect)); return(Kjc); }
private void CalcF2ProbabilitiesGxyNew() { if (null != _probCalcF1) { return; } int resultsCount = 1 << _f2.OutputNumberOfDigits; Gprobabilites[] boolFunc2Gp = new Gprobabilites[resultsCount]; double[] f2ProbalityZero = new double[_f2.InputNumberOfDigits]; for (int i = 0; i < f2ProbalityZero.Length; i++) { f2ProbalityZero[i] = _inputDistortionProb.ZeroProbability[i + _f1.InputNumberOfDigits - _f2.OutputNumberOfDigits]; } var f2Calc = new ProbabilitiesGxyCalc(_f2, f2ProbalityZero); double[] probabilityZeroF1 = new double[_f1.InputNumberOfDigits]; // copy input zero probability for function f1 in range [1,..,t] for (int i = 0; i < _f1.InputNumberOfDigits - _f2.OutputNumberOfDigits; i++) { probabilityZeroF1[i] = _inputDistortionProb.ZeroProbability[i]; } for (int i = 0; i < _f2.OutputNumberOfDigits; i++) { probabilityZeroF1[i + _f1.InputNumberOfDigits - _f2.OutputNumberOfDigits] = 0.0; } // create Gprobabilities for a separate bit of f2 function result const int BinaryDigitState = 2; Gprobabilites[][] digitSeparateProb = new Gprobabilites[BinaryDigitState][]; for (int i = 0; i < BinaryDigitState; i++) { digitSeparateProb[i] = new Gprobabilites[f2Calc.OutputNumberOfDigits()]; } // calc results probabilities int indexBoolFunc2Gp = 0; BitArray resultVect = new BitArray(_f2.OutputNumberOfDigits, false); // 00...0 do { boolFunc2Gp[indexBoolFunc2Gp] = f2Calc.GetGprobabilitesResult(resultVect); // calc G prob of separate bits of result for (int i = 0; i < digitSeparateProb[0].Length; i++) { int digit = resultVect[i] ? 1 : 0; digitSeparateProb[digit][i].G0 = boolFunc2Gp[indexBoolFunc2Gp].G0; digitSeparateProb[digit][i].Gc += boolFunc2Gp[indexBoolFunc2Gp].Gc; digitSeparateProb[digit][i].Gce += boolFunc2Gp[indexBoolFunc2Gp].Gce; digitSeparateProb[digit][i].Gee += boolFunc2Gp[indexBoolFunc2Gp].Gee; } ++indexBoolFunc2Gp; } while (BooleanFuntionWithInputDistortion.IncrementOperand(resultVect)); var prodClasses = new ProductClasses(_inputDistortionProb, digitSeparateProb, _f1.InputNumberOfDigits); _probCalcF1 = new ProbabilitiesGxyCalc(_f1, prodClasses); }
// Get probability of correct (0||1) result == Result with distortion public double GetProbabilityGcResult(BitArray result) { double pGc = 0.0; BitArray operandIt = new BitArray(_truthTable.InputNumberOfDigits, false); // the first operand in tTable 00...0 do { if (_truthTable.GetResult(operandIt).Eq(result)) { pGc += GetTupleProbabilityKjcClass(operandIt); } } while (BooleanFuntionWithInputDistortion.IncrementOperand(operandIt)); return(pGc); }
// Calculationg of E1 and E2: // probability of autocorrection and error after distortion in the corresponding tuples // intermediate calculation // TODO: rename method private void CalcE1E2(BitArray result, ref double Gce, ref double Gee) { double E1 = 0.0, E2 = 0.0; BitArray operandIt = new BitArray(_truthTable.InputNumberOfDigits, false); // the first operand in tTable 00...0 do { if (_truthTable.GetResult(operandIt).Eq(result)) { GetTupleProbabilityKjeClass(result, ref E1, ref E2, operandIt); } } while (BooleanFuntionWithInputDistortion.IncrementOperand(operandIt)); Gce = E1; Gee = E2; }
private void button1_Click(object sender, EventArgs e) { int resultsCount = _pCalc.OutputNumberOfDigits(); uint count = 1u << resultsCount; double G0 = 0, Gc = 0, Ge = 0; BitArray result = new BitArray(resultsCount, false); for (int i = 0; i < result.Count; i++) { result[i] = false; } double timeLeft = .0; do { TimeSpan begin = Process.GetCurrentProcess().TotalProcessorTime; var prob = _pCalc.GetGprobabilitesResult(result); TimeSpan end = Process.GetCurrentProcess().TotalProcessorTime; timeLeft += (end - begin).TotalMilliseconds; dataGridView1.Rows.Add(ConvertNumberToBinary(result), prob.G0, prob.Gc + prob.Gce, prob.Gee); Ge += prob.Gee; Gc += prob.Gc + prob.Gce; G0 = prob.G0; } while(BooleanFuntionWithInputDistortion.IncrementOperand(result)); labelTime.Text = timeLeft + " ms."; textBoxG0.Text = G0.ToString(); textBoxGc.Text = Gc.ToString(); textBoxGe.Text = Ge.ToString(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.IsNewRow) { continue; } row.HeaderCell.Value = String.Format("{0}", row.Index + 1); } }
private void CalcF2ProbabilitiesGxy() { if (null != _probCalcF1) { return; } int resultsCount = 1 << _f2.OutputNumberOfDigits; Gprobabilites[] boolFunc2Gp = new Gprobabilites[resultsCount]; double[] f2ProbalityZero = new double[_f2.InputNumberOfDigits]; for (int i = 0; i < f2ProbalityZero.Length; i++) { f2ProbalityZero[i] = _inputDistortionProb.ZeroProbability[i + _f1.InputNumberOfDigits - _f2.OutputNumberOfDigits]; } var f2Calc = new ProbabilitiesGxyCalc(_f2, f2ProbalityZero); double[] probabilityZeroF1 = new double[_f1.InputNumberOfDigits]; // copy input zero probability for function f1 in range [1,..,t] for (int i = 0; i < _f1.InputNumberOfDigits - _f2.OutputNumberOfDigits; i++) { probabilityZeroF1[i] = _inputDistortionProb.ZeroProbability[i]; } for (int i = 0; i < _f2.OutputNumberOfDigits; i++) { probabilityZeroF1[i + _f1.InputNumberOfDigits - _f2.OutputNumberOfDigits] = 0.0; } // get zero probability int indexBoolFunc2Gp = 0; BitArray resultVect = new BitArray(_f2.OutputNumberOfDigits, false); // 00...0 do { boolFunc2Gp[indexBoolFunc2Gp] = f2Calc.GetGprobabilitesResult(resultVect); // calc input zero probability for function f1 in range [t+1,..., t+_f2.OutputNumberOfDigits] for (int i = 0; i < _f2.OutputNumberOfDigits; i++) { if (!resultVect[i]) // if res[i] == 0 { probabilityZeroF1[i + _f1.InputNumberOfDigits - _f2.OutputNumberOfDigits] += boolFunc2Gp[indexBoolFunc2Gp].SumCorrectionAndError(); // sum without G0 } } ++indexBoolFunc2Gp; } while (BooleanFuntionWithInputDistortion.IncrementOperand(resultVect)); // add G0 for (int i = 0; i < _f2.OutputNumberOfDigits; i++) { probabilityZeroF1[i + _f1.InputNumberOfDigits - _f2.OutputNumberOfDigits] += boolFunc2Gp[0].G0 / _f2.Length; } // get distortion prob const int DistortionTypes = 3; double[][] distProb = new double[DistortionTypes][]; for (int r = 0; r < distProb.Length; r++) { distProb[r] = new double[_f1.InputNumberOfDigits]; for (int i = 0; i < _f1.InputNumberOfDigits - _f2.OutputNumberOfDigits; i++) { distProb[r][i] = _f1.ProbabilityVectors[r + 1][i]; } int indexBase = _f1.InputNumberOfDigits - _f2.OutputNumberOfDigits; for (int i = 0; i < _f2.OutputNumberOfDigits; i++) { distProb[r][indexBase + i] = 0; } } indexBoolFunc2Gp = 0; resultVect = new BitArray(_f2.OutputNumberOfDigits, false); // 00...0 do { int indexBase = _f1.InputNumberOfDigits - _f2.OutputNumberOfDigits; for (int i = 0; i < resultVect.Length; i++) { double bitProb = 0.0; if (resultVect[i]) // distortionTo1 { bitProb = 1 - probabilityZeroF1[indexBase + i]; distProb[1][i + indexBase] += (boolFunc2Gp[indexBoolFunc2Gp].Gc + boolFunc2Gp[indexBoolFunc2Gp].Gce);// / bitProb; } else // distortionTo0 { bitProb = probabilityZeroF1[indexBase + i]; distProb[0][i + indexBase] += (boolFunc2Gp[indexBoolFunc2Gp].Gc + boolFunc2Gp[indexBoolFunc2Gp].Gce); // / bitProb; } distProb[DistortionTypes - 1][i + indexBase] += boolFunc2Gp[indexBoolFunc2Gp].Gee; // / (1 - bitProb); } ++indexBoolFunc2Gp; } while (BooleanFuntionWithInputDistortion.IncrementOperand(resultVect)); // Set zero prob and dist prob to _f1 _f1.DistortionToZeroProbability = distProb[0]; _f1.DistortionToOneProbability = distProb[1]; _f1.DistortionToInverseProbability = distProb[2]; _f1.CorrectValueProbability = null; double[] correctValueProbability = new double[_f1.InputNumberOfDigits]; double[][] autoCorrectionValueProbability = null; double[][] distortedValueProbability = null; AllocateDeterminedDistortionProbalilitiesVectors(ref autoCorrectionValueProbability); AllocateDeterminedDistortionProbalilitiesVectors(ref distortedValueProbability); _probCalcF1 = new ProbabilitiesGxyCalc(_f1, probabilityZeroF1); }