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); }
public ProductClasses(double[] probalityZero, BooleanFuntionWithInputDistortion truthTable) { //if (_probalityZeroAndOne == null) // throw new Exception("Error! Zero probalities for fucntion are not set."); _probalityZero = new double[2][]; _probalityZero[0] = new double[probalityZero.Length]; _probalityZero[1] = new double[probalityZero.Length]; for (int i = 0; i < probalityZero.Length; i++) { _probalityZero[0][i] = probalityZero[i]; _probalityZero[1][i] = 1 - probalityZero[i]; } CalcDeterminedDistortionProbabilities(truthTable); }
public static DataView GetView(BooleanFuntionWithInputDistortion tTable) { var dataTable = new DataTable(); //_dataTable column names and types dataTable.Columns.Add("X", typeof(string)); dataTable.Columns.Add("F(X)", typeof(string)); // add unsorted data to the DataTable and return. for (int i = 0; i < tTable.GetLinesCount(); i++) { dataTable.Rows.Add(ConvertNumberToBinary(tTable.GetOperandByLineIndex(i)), ConvertNumberToBinary(tTable.GetResultByLineIndex(i))); } return(dataTable.AsDataView()); }
private void LoadDistortionToBoolFunction(BooleanFuntionWithInputDistortion f, int indexBase, int count) { double[] distortionToZeroProbability = new double[f.InputNumberOfDigits]; double[] distortionToOneProbability = new double[f.InputNumberOfDigits]; double[] distortionToInverseProbability = new double[f.InputNumberOfDigits]; for (int i = 0; i < count; i++) { distortionToZeroProbability[i] = _inputDistortionProb.DistortionToZeroProbability[i + indexBase]; distortionToOneProbability[i] = _inputDistortionProb.DistortionToOneProbability[i + indexBase]; distortionToInverseProbability[i] = _inputDistortionProb.DistortionToInverseProbability[i + indexBase]; } f.DistortionToZeroProbability = distortionToZeroProbability; f.DistortionToOneProbability = distortionToOneProbability; f.DistortionToInverseProbability = distortionToInverseProbability; }
// 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; }
public ProbGxyCalcSuperposition(BooleanFuntionWithInputDistortion truthTable1, BooleanFuntionWithInputDistortion truthTable2, InputDistortionProbabilities inputDistortionProb) { _f1 = truthTable1; _f2 = truthTable2; if (inputDistortionProb.ZeroProbability.Length != _f1.InputNumberOfDigits + _f2.InputNumberOfDigits - _f2.OutputNumberOfDigits) { throw new Exception("Size of probalityZero array don't fit the InputNumberOfDigits of truth table composition."); } _inputDistortionProb = inputDistortionProb; // copy distortion for f2 LoadDistortionToBoolFunction(_f2, _f1.InputNumberOfDigits - _f2.OutputNumberOfDigits, _f2.InputNumberOfDigits); LoadDistortionToBoolFunction(_f1, 0, _f1.InputNumberOfDigits - _f2.OutputNumberOfDigits); _probCalcF1 = null; }
private void showTable(BooleanFuntionWithInputDistortion table) { DataView v = TruthTableView.GetView(table); tableGridView.DataSource = v; foreach (DataGridViewRow row in tableGridView.Rows) { if (row.IsNewRow) { continue; } row.HeaderCell.Value = String.Format("{0}", row.Index + 1); } tableGridView.AutoResizeRowHeadersWidth( DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders); }
private ProductClasses _inputBitsDistortionsProbabilities; // contain probabilites g0, gcaij, geaij, p0i, p1i // next data stored in _truthTable //private double[] _distortionToZeroProbability; //g const0 //private double[] _distortionToOneProbability; // g const1 //private double[] _distortionToInverseProbability; // g inv // store for intermediate calculation // first [] correspond to 0 or 1 value of binary digit in tuple //private double[] _correctValueProbability; // gxaij -> the same as _correctValueProbability in truth Table //private double[][] _autoCorrectionValueProbability; // gcaij //private double[][] _distortedValueProbability; // geaij public ProbabilitiesGxyCalc(BooleanFuntionWithInputDistortion truthTable, double[] probalityZero) { _truthTable = truthTable; // TODO: move probalityZero to truthTable class (AbstractBooleanFuntionWithInputDistortion) if (probalityZero.Length != truthTable.InputNumberOfDigits) { throw new Exception("Length of probalityZero array don't fit the InputNumberOfDigits of truth table"); } // const int BinaryDigitStates = 2; // _probalityZeroAndOne = new double[BinaryDigitStates][]; // _probalityZeroAndOne[0] = new double[_truthTable.InputNumberOfDigits]; // _probalityZeroAndOne[1] = new double[_truthTable.InputNumberOfDigits]; // for (int i = 0; i < probalityZero.Length; i++) // { // _probalityZeroAndOne[0][i] = probalityZero[i]; // _probalityZeroAndOne[1][i] = 1 - probalityZero[i]; // } _inputBitsDistortionsProbabilities = new ProductClasses(probalityZero, truthTable); }
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 CalcDeterminedDistortionProbabilities(BooleanFuntionWithInputDistortion truthTable) { const int BinaryDigitStates = 2; AllocateDeterminedDistortionProbalilitiesVectors(ref _autoCorrectionValueProbability, truthTable.CorrectValueProbability.Length); AllocateDeterminedDistortionProbalilitiesVectors(ref _distortedValueProbability, truthTable.CorrectValueProbability.Length); double[][] digitDistortionProbability = new double[BinaryDigitStates][]; // temp variable digitDistortionProbability[0] = truthTable.DistortionToZeroProbability; digitDistortionProbability[1] = truthTable.DistortionToOneProbability; for (int i = 0; i < truthTable.InputNumberOfDigits; i++) { for (int digit = 0; digit < BinaryDigitStates; digit++) { // gcaij = g(const_(aij)) * p_aij _autoCorrectionValueProbability[digit][i] = digitDistortionProbability[digit][i];// *ProbabilityZeroAndOne(digit, i); // geaij = (g(const_(not aij)) + p inv ) * p_aij _distortedValueProbability[digit][i] = (digitDistortionProbability[BinaryDigitStates - digit - 1][i] //digitDistortionProbability[!digit][i] + truthTable.DistortionToInverseProbability[i]); // *ProbabilityZeroAndOne(digit, i); } } }
private void button1_Click(object sender, EventArgs e) { // select function input way if (rbAnaliticFormula.Checked) { ShowInput(new BoolFuncHandInput(SetBoolFunction)); } else if (rbTextFile.Checked) { //ShowInput(new } else if (rbTruthTable.Checked) { ShowInput(new FuncTruthTableInput(SetBoolFunction)); } else if (rbDllImport.Checked) { _bf = new BooleanFunctionDelegate(6, 1, f6); } else if (rbAdder.Checked) { ShowInput(new AdderInputChoose(SetBoolFunction)); } }
public ProbabilitiesGxyCalc(BooleanFuntionWithInputDistortion truthTable, ProductClasses prodClasses) { _truthTable = truthTable; _inputBitsDistortionsProbabilities = prodClasses; }
private void LoadDistortionToBoolFunction(BooleanFuntionWithInputDistortion f, InputDistortionProbabilities inputDistortionProb) { f.SetDistortionProbabilitiesVectors(inputDistortionProb.DistortionToZeroProbability, inputDistortionProb.DistortionToOneProbability, inputDistortionProb.DistortionToInverseProbability); }
public ProductClasses(double[] correctValueProbability, double[][] autoCorrectionValueProbability, double[][] distortedValueProbability, double[] probalityZero, BooleanFuntionWithInputDistortion truthTable) { _correctValueProbability = new double[2][]; _correctValueProbability[1] = _correctValueProbability[0] = new double[truthTable.InputNumberOfDigits]; AllocateDeterminedDistortionProbalilitiesVectors(ref _autoCorrectionValueProbability, truthTable.InputNumberOfDigits); AllocateDeterminedDistortionProbalilitiesVectors(ref _distortedValueProbability, truthTable.InputNumberOfDigits); //_probalityZero = new double[probalityZero.Length]; for (int i = 0; i < correctValueProbability.Length; i++) { _correctValueProbability[0][i + truthTable.InputNumberOfDigits - correctValueProbability.Length] = correctValueProbability[i]; _autoCorrectionValueProbability[0][i + truthTable.InputNumberOfDigits - correctValueProbability.Length] = autoCorrectionValueProbability[0][i]; _autoCorrectionValueProbability[1][i + truthTable.InputNumberOfDigits - correctValueProbability.Length] = autoCorrectionValueProbability[1][i]; _distortedValueProbability[0][i + truthTable.InputNumberOfDigits - correctValueProbability.Length] = distortedValueProbability[0][i]; _distortedValueProbability[1][i + truthTable.InputNumberOfDigits - correctValueProbability.Length] = distortedValueProbability[1][i]; } const int BinaryDigitStates = 2; double[][] digitDistortionProbability = new double[BinaryDigitStates][]; // temp variable digitDistortionProbability[0] = truthTable.DistortionToZeroProbability; digitDistortionProbability[1] = truthTable.DistortionToOneProbability; for (int i = 0; i < truthTable.InputNumberOfDigits - correctValueProbability.Length; i++) { _correctValueProbability[0][i] = truthTable.CorrectValueProbability[i]; for (int digit = 0; digit < BinaryDigitStates; digit++) { // gcaij = g(const_(aij)) * p_aij _autoCorrectionValueProbability[digit][i] = digitDistortionProbability[digit][i];// *ProbabilityZeroAndOne(digit, i); // geaij = (g(const_(not aij)) + p inv ) * p_aij _distortedValueProbability[digit][i] = (digitDistortionProbability[BinaryDigitStates - digit - 1][i] //digitDistortionProbability[!digit][i] + truthTable.DistortionToInverseProbability[i]); // *ProbabilityZeroAndOne(digit, i); } } }
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); }
private void SetBoolFunction(BooleanFuntionWithInputDistortion bf) { _bf = bf; }