public static void SimulateToFile(MatrixProvider capabilitiesProvider, MatrixProvider contiguityProvider, MatrixProvider majorPowersProvider,
            MatrixProvider conflictProvider, string outputFile, string expectedAllianceFile, string conflictOutputFile, string srgOutputFile, int networkID, bool randomConflict, bool randomContig, bool randomMajorPower, bool outputDyadic, bool expectedAllianceDyadic, bool srgDyadic,
            MatrixProvider srgProvider, MatrixProvider dProvider, MatrixProvider jccProvider, MatrixProvider midProvider, MatrixProvider csProvider, MatrixProvider relProvider, int maxIter, bool overwrite, double br)
        {
            Matrix tmpContig = contiguityProvider.ReadNext(overwrite);
            SymmetricBinaryMatrix contiguity = tmpContig == null ? null : new SymmetricBinaryMatrix(tmpContig);
            Matrix initialCapabilities = capabilitiesProvider.ReadNext(overwrite);
            Vector majorPowers = majorPowersProvider.ReadNext(overwrite) as Vector;
            Matrix conflict = conflictProvider.ReadNext(overwrite);
            Matrix srg = srgProvider.ReadNext(overwrite);
            Matrix D = dProvider.ReadNext(overwrite);
            Matrix JCC = jccProvider.ReadNext(overwrite);
            Matrix MID = midProvider.ReadNext(overwrite);
            Matrix CS = csProvider.ReadNext(overwrite);
            Matrix REL = relProvider.ReadNext(overwrite);

            //int tryIntVar1 = srg.NetworkId;
            Matrix P = null;

            if (randomConflict)
            {
                P = Matrix.Zero(conflict.Rows, conflict.Cols);
                for (int i = 0; i < P.Rows; ++i)
                    for (int j = i + 1; j < P.Rows; ++j)
                        if (contiguity == null ? srg[i, j] > 0.0 : contiguity.GetValue(i, j))
                            P[i, j] = P[j, i] = RNG.RandomFloat(0.01, 0.03);
                        else
                            P[i, j] = P[j, i] = RNG.RandomFloat(0.002, 0.004);

                for (int i = 0; i < conflict.Rows; ++i)
                    for (int j = i + 1; j < conflict.Cols; ++j)
                        conflict[i, j] = conflict[j, i] = RNG.RandomBinary(P[i, j]);
            }

            if (randomContig && contiguity != null)
            {
                if (CS == null)
                {
                    contiguity.Clear();
                    int[] values = { 3, 4, 5 };
                    for (int row = 0; row < contiguity.Rows; ++row)
                    {
                        int count = RNG.Choose<int>(values);

                        count = Math.Min(count, contiguity.Cols);

                        for (int i = 0; i < count; ++i)
                        {
                            int which = RNG.RandomInt(contiguity.Cols);
                            if (contiguity[row, which] == 1)
                                --i;
                            contiguity[row, which] = 1;
                        }
                    }
                }
            }

            if (randomMajorPower)
            {
                int[] values = { 4, 5, 6 };
                int count = RNG.Choose<int>(values);

                count = Math.Min(count, majorPowers.Size);
                majorPowers.Clear();

                for (int i = 0; i < count; ++i)
                {
                    int which = RNG.RandomInt(majorPowers.Size);
                    if (majorPowers[which] == 1)
                        --i;
                    majorPowers[which] = 1;
                }
            }

            Matrix capabilities = initialCapabilities;
            if (initialCapabilities is Vector)
            {
                capabilities = Matrix.Zero(initialCapabilities.Rows, initialCapabilities.Cols);
                capabilities.SetDiagonalFromVector(initialCapabilities as Vector);
            }

            Matrix expectedAlliance = null;

            if (CS != null) // 5
            {
                Matrix SRGOutput = new Matrix(srg);
                Matrix EAOutput = null;
                networkID = srg.NetworkId;
                //expectedAlliance = SimulateNAPTStageOne(capabilities, srg, contiguity, MID, D, JCC, CS, REL, ref SRGOutput, ref EAOutput, expectedAllianceFile, outputFile, overwrite, expectedAllianceDyadic, outputDyadic, networkID);
                SimulateNAPTStageOne(capabilities, srg, contiguity, MID, D, JCC, CS, REL, ref SRGOutput, ref EAOutput, expectedAllianceFile, outputFile, overwrite, expectedAllianceDyadic, outputDyadic, networkID);

                /*
                if (!outputDyadic)
                    MatrixWriter.WriteMatrixToMatrixFile(expectedAlliance, outputFile, overwrite);
                else
                    MatrixWriter.WriteMatrixToDyadicFile(expectedAlliance, outputFile, overwrite);
                */
                if (srgOutputFile != "")
                {
                    if (!srgDyadic)
                        MatrixWriter.WriteMatrixToMatrixFile(SRGOutput, srgOutputFile, overwrite);
                    else
                        MatrixWriter.WriteMatrixToDyadicFile(SRGOutput, srgOutputFile, overwrite);
                }

                return;
            }
            else if (conflict == null && D != null) // 4
                expectedAlliance = SimulateSimplifiedLiberalStageOne(capabilities, srg, MID, D, JCC, outputFile, br);
            else if (MID != null) // 3
                expectedAlliance = SimulateSimplifiedRealistStageOne(capabilities, srg, MID, outputFile, br);
            else if (D == null) // 1
                expectedAlliance = SimulateRealistStageOne(contiguity, capabilities, srg, majorPowers, br);
            else // 2
                expectedAlliance = SimulateLiberalStageOne(contiguity, capabilities, srg, majorPowers, D, JCC, br);

            //if (outputDyadic) MatrixWriter.WriteDyadicHeader(outputFile);
            int editedNetworkId = srg.NetworkId;

            expectedAlliance.NetworkId = int.Parse(editedNetworkId + "01");   // Problem encountered 2/15/11
            expectedAlliance.CopyLabelsFrom(capabilities);
            srg.NetworkId = int.Parse(editedNetworkId + "01");
            srg.CopyLabelsFrom(capabilities);

            if (!outputDyadic)
                MatrixWriter.WriteMatrixToMatrixFile(expectedAlliance, outputFile, overwrite);
            else
                MatrixWriter.WriteMatrixToDyadicFile(expectedAlliance, outputFile, overwrite);
            if (srgOutputFile != "")
            {
                if (!srgDyadic)
                    MatrixWriter.WriteMatrixToMatrixFile(srg, srgOutputFile, overwrite);
                else
                    MatrixWriter.WriteMatrixToDyadicFile(srg, srgOutputFile, overwrite);
            }
            if (conflict != null)
            {
                conflict.NetworkId = expectedAlliance.NetworkId;
                if (!string.IsNullOrEmpty(conflictOutputFile) && !conflictProvider.IsFromFile)
                {

                    MatrixWriter.WriteMatrixToMatrixFile(conflict, conflictOutputFile, overwrite);

                }
            }

            if (CS != null) ; // 5
                //  expectedAlliance = SimulateNAPTStageTwo(...)
            else if (conflict == null && D != null) // 4
                expectedAlliance = SimulateSimplifiedLiberalStageTwo(capabilities, srg, MID, D, JCC, expectedAlliance, outputFile, srgOutputFile, editedNetworkId /*networkID*/, outputDyadic, srgDyadic, maxIter, br);
            else if (MID != null) // 3
                expectedAlliance = SimulateSimplifiedRealistStageTwo(capabilities, srg, MID, expectedAlliance, outputFile, srgOutputFile, networkID, outputDyadic, srgDyadic, maxIter, br);
            else if (D == null) // 1
                expectedAlliance = SimulateRealistStageTwo(conflict, expectedAlliance, capabilities, br);
            else // 2
                expectedAlliance = SimulateLiberalStageTwo(conflict, expectedAlliance, capabilities, D, JCC, br);

            if (MID == null) //if not simplified version
            {
                expectedAlliance.NetworkId = int.Parse(editedNetworkId + "02");
                expectedAlliance.CopyLabelsFrom(capabilities);

                if (!outputDyadic)
                    MatrixWriter.WriteMatrixToMatrixFile(expectedAlliance, outputFile, overwrite);
                else
                    MatrixWriter.WriteMatrixToDyadicFile(expectedAlliance, outputFile, overwrite);
            }

            if (conflict != null) //if conflict file exists
            {
                P = new Matrix(conflict);
                for (int i = 0; i < P.Rows; ++i)
                {
                    for (int j = i + 1; j < P.Cols; ++j)
                    {
                        P[j, i] = P[i, j] = RNG.RandomFloat(0.002, 0.004);
                        if (conflict[i, j] == 1)
                            P[j, i] = P[i, j] = RNG.RandomFloat(0.08, 0.25);
                        if (expectedAlliance[i, j] == 0.5)
                            P[j, i] = P[i, j] = 1 / ((double)P.Rows * (double)(P.Rows - 1));
                        else if (expectedAlliance[i, j] == 1)
                            P[j, i] = P[i, j] = 0.5 / ((double)P.Rows * (double)(P.Rows - 1));
                    }
                }

                conflict = new Matrix(P.Rows);
                conflict.RowLabels.CopyFrom(expectedAlliance.RowLabels);
                conflict.ColLabels.CopyFrom(expectedAlliance.ColLabels);
                for (int i = 0; i < conflict.Rows; ++i)
                    for (int j = i + 1; j < conflict.Cols; ++j)
                        conflict[i, j] = conflict[j, i] = RNG.RandomBinary(P[i, j]);

                if (D == null)
                    expectedAlliance = SimulateRealistStageTwo(conflict, expectedAlliance, capabilities, br);
                else
                    expectedAlliance = SimulateLiberalStageTwo(conflict, expectedAlliance, capabilities, D, JCC, br);

                expectedAlliance.NetworkId = int.Parse(editedNetworkId + "03");
                expectedAlliance.CopyLabelsFrom(capabilities);

                if (!outputDyadic)
                    MatrixWriter.WriteMatrixToMatrixFile(expectedAlliance, outputFile, overwrite);
                else
                    MatrixWriter.WriteMatrixToDyadicFile(expectedAlliance, outputFile, overwrite);

                conflict.NetworkId = expectedAlliance.NetworkId;
                if (!string.IsNullOrEmpty(conflictOutputFile) && !conflictProvider.IsFromFile)
                {

                    if (!outputDyadic)
                        MatrixWriter.WriteMatrixToMatrixFile(expectedAlliance, outputFile, overwrite);
                    else
                        MatrixWriter.WriteMatrixToDyadicFile(expectedAlliance, outputFile, overwrite);
                }
            }//if conflict file exists
        }
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            //try
            //{
                int N = int.Parse(nTextBox.Text);
                //int numberOfIterations = int.Parse(nTextBox.Text);
                int iterations = int.Parse(tMaxTextBox.Text);
                int maxIterations;
                if(maxStageTextBox.Text=="")
                    maxIterations = int.MaxValue;
                else
                    maxIterations = int.Parse(maxStageTextBox.Text);
                bool rangeN = multipleNCheckBox.Checked && multipleNCheckBox.Enabled;
                int rangeNmin = int.Parse(minNbox.Text);
                int rangeNmax = int.Parse(maxNbox.Text);
                int rangeNint = int.Parse(intNbox.Text);

                bool useSrg = CheckFile(srgMatrixFile.Text, srgCheckBox.Checked);
                MatrixProvider capabilProvider = new MatrixProvider(capabilityMatrixFile.Text,
                            capabilitiesCheckBox.Checked ? capabilityMatrixFile.Text : null,
                            capabilitiesCheckBox.Checked ? MatrixProvider.Type.RandomDiagonal : MatrixProvider.Type.MatrixFile,
                            N,
                            N,
                            false);
                /*
                MatrixProvider contigProvider = new MatrixProvider(contiguityMatrixFile.Text,
                            contiguityCheckBox.Checked ? contiguityMatrixFile.Text : null,
                            !useSrg ? MatrixProvider.Type.NullFile :
                            (contiguityCheckBox.Checked ? MatrixProvider.Type.RandomSymmetric : MatrixProvider.Type.MatrixFile),
                            N,
                            N,
                            contiguityDCheckBox.Checked);
                */

                MatrixProvider contigProvider = new MatrixProvider(contiguityMatrixFile.Text,
                            contiguityCheckBox.Checked ? contiguityMatrixFile.Text : null,
                            contiguityMatrixFile.Visible ?
                            (contiguityCheckBox.Checked ? MatrixProvider.Type.RandomSymmetric : MatrixProvider.Type.MatrixFile) : MatrixProvider.Type.NullFile,
                            N,
                            N,
                            contiguityDCheckBox.Checked);

                /*
                MatrixProvider contigProvider = new MatrixProvider(contiguityMatrixFile.Text,
                                contiguityCheckBox.Checked ? contiguityMatrixFile.Text : null,
                                contiguityMatrixFile.Text == "" ? (contiguityCheckBox.Checked ? MatrixProvider.Type.RandomSymmetric : MatrixProvider.Type.NullFile) :
                                MatrixProvider.Type.MatrixFile,
                                N,
                                N,
                                contiguityDCheckBox.Checked);
                */
                MatrixProvider majPowProvider = new MatrixProvider(majorPowersVectorFile.Text,
                            majorPowersCheckBox.Checked ? majorPowersVectorFile.Text : null,
                            useSrg ? MatrixProvider.Type.NullFile :
                            (majorPowersCheckBox.Checked ? MatrixProvider.Type.RandomVector : MatrixProvider.Type.VectorFile),
                            N,
                            N,
                            majorPowersDCheckBox.Checked);
                MatrixProvider conflictProvider = new MatrixProvider(conflictMatrixFile.Text,
                            null, //conflictCheckBox.Checked ? conflictMatrixFile.Text : null,
                            conflictCheckBox.Visible ?
                            (conflictCheckBox.Checked ? MatrixProvider.Type.RandomSymmetric : MatrixProvider.Type.MatrixFile) : MatrixProvider.Type.NullFile,
                            N,
                            N,
                            conflictDCheckBox.Checked);
                MatrixProvider srgProvider = new MatrixProvider(srgMatrixFile.Text,
                            srgCheckBox.Checked ? srgMatrixFile.Text : null,
                            useSrg ?
                            (srgCheckBox.Checked ? MatrixProvider.Type.RandomSymmetric : MatrixProvider.Type.MatrixFile) : MatrixProvider.Type.NullFile,
                            N,
                            N,
                            srgDCheckBox.Checked);
                MatrixProvider demoProvider = new MatrixProvider(demoMatrixFile.Text,
                            demoCheckBox.Checked ? demoMatrixFile.Text : null,
                            demoCheckBox.Visible ?
                            (demoCheckBox.Checked ? MatrixProvider.Type.RandomSymmetric : MatrixProvider.Type.MatrixFile) : MatrixProvider.Type.NullFile,
                            N,
                            N,
                            demoDCheckBox.Checked);
                MatrixProvider jcProvider = new MatrixProvider(jcMatrixFile.Text,
                            jcCheckBox.Checked ? jcMatrixFile.Text : null,
                            jcCheckBox.Visible ?
                            (jcCheckBox.Checked ? MatrixProvider.Type.RandomSymmetric : MatrixProvider.Type.MatrixFile) : MatrixProvider.Type.NullFile,
                            N,
                            N,
                            jcDCheckBox.Checked);
                MatrixProvider midProvider = new MatrixProvider(midMatrixFile.Text,
                            midCheckBox.Checked ? midMatrixFile.Text : null,
                            midCheckBox.Visible ?
                            (midCheckBox.Checked ? MatrixProvider.Type.RandomSymmetric : MatrixProvider.Type.MatrixFile) : MatrixProvider.Type.NullFile,
                            N,
                            N,
                            midDCheckBox.Checked);
                MatrixProvider csProvider = new MatrixProvider(csMatrixFile.Text,
                            csCheckBox.Checked ? csMatrixFile.Text : null,
                            csCheckBox.Visible ?
                            (csCheckBox.Checked ? MatrixProvider.Type.RandomSymmetric : MatrixProvider.Type.MatrixFile) : MatrixProvider.Type.NullFile,
                            N,
                            N,
                            csDCheckbox.Checked);
                MatrixProvider relProvider = new MatrixProvider(reliabilityMatrixFile.Text,
                            reliabilityCheckBox.Checked ? reliabilityMatrixFile.Text : null,
                            reliabilityCheckBox.Visible ?
                            (reliabilityCheckBox.Checked ? MatrixProvider.Type.RandomSymmetric : MatrixProvider.Type.MatrixFile) : MatrixProvider.Type.NullFile,
                            N,
                            N,
                            reliabilityDCheckBox.Checked);

                //if (demoCheckBox.Checked)
                //{
                //    demoProvider.Min = 0.15;
                //    demoProvider.Max = 0.35;
                //}
                //if (jcCheckBox.Checked)
                //{
                //    jcProvider.Min = 0.3;
                //    jcProvider.Max = 0.6;
                //}

                if (demoCheckBox.Checked)
                {
                    if (demominbox.Text == "" && demomaxbox.Text == "")
                        demoProvider.Prange = false;
                    else
                    {
                        demoProvider.Prange = true;
                        demoProvider.Pmin = double.Parse(demominbox.Text);
                        demoProvider.Pmax = double.Parse(demomaxbox.Text);
                    }
                }
                if (srgCheckBox.Checked)
                {
                    if (srgminbox.Text == "" && srgmaxbox.Text == "")
                        srgProvider.Prange = false;
                    else
                    {
                        srgProvider.Prange = true;
                        srgProvider.Pmin = double.Parse(srgminbox.Text);
                        srgProvider.Pmax = double.Parse(srgmaxbox.Text);
                    }
                }
                if (jcCheckBox.Checked)
                {
                    if (jcminbox.Text == "" && jcmaxbox.Text == "")
                        jcProvider.Prange = false;
                    else
                    {
                        jcProvider.Prange = true;
                        jcProvider.Pmin = double.Parse(jcminbox.Text);
                        jcProvider.Pmax = double.Parse(jcmaxbox.Text);
                    }
                }
                if (midCheckBox.Checked)
                {
                    if (midminbox.Text == "" && midmaxbox.Text == "")
                        midProvider.Prange = false;
                    else
                    {
                        midProvider.Prange = true;
                        midProvider.Pmin = double.Parse(midminbox.Text);
                        midProvider.Pmax = double.Parse(midmaxbox.Text);
                    }
                }
                if (conflictCheckBox.Checked)
                {
                    if (confminbox.Text == "" && confmaxbox.Text == "")
                        conflictProvider.Prange = false;
                    else
                    {
                        conflictProvider.Prange = true;
                        conflictProvider.Pmin = double.Parse(confminbox.Text);
                        conflictProvider.Pmax = double.Parse(confmaxbox.Text);
                    }
                }
                if (majorPowersCheckBox.Checked)
                {
                    if (majminbox.Text == "" && majmaxbox.Text == "")
                        majPowProvider.Prange = false;
                    else
                    {
                        majPowProvider.Prange = true;
                        majPowProvider.Pmin = double.Parse(majminbox.Text);
                        majPowProvider.Pmax = double.Parse(majmaxbox.Text);
                    }
                }
                if (contiguityCheckBox.Checked)
                {
                    if (contminbox.Text == "" && contmaxbox.Text == "")
                        contigProvider.Prange = false;
                    else
                    {
                        contigProvider.Prange = true;
                        contigProvider.Pmin = double.Parse(contminbox.Text);
                        contigProvider.Pmax = double.Parse(contmaxbox.Text);
                    }
                }
                if (csCheckBox.Checked)
                {
                    if (csMinBox.Text == "" && csMaxBox.Text == "")
                        csProvider.Prange = false;
                    else
                    {
                        csProvider.Prange = true;
                        csProvider.Pmin = double.Parse(csMinBox.Text);
                        csProvider.Pmax = double.Parse(csMaxBox.Text);
                    }
                }
                if (reliabilityCheckBox.Checked)
                {
                    if (reliabilityMinBox.Text == "" && reliabilityMaxBox.Text == "")
                        relProvider.Prange = false;
                    else
                    {
                        relProvider.Prange = true;
                        relProvider.Pmin = double.Parse(reliabilityMinBox.Text);
                        relProvider.Pmax = double.Parse(reliabilityMaxBox.Text);
                    }
                }

                capabilProvider.ForceVector = capabilitiesVectorCheckBox.Checked;
                capabilProvider.WriteRepeatCount = 3;

                if (File.Exists(conflictMatrixFile.Text) && _overwrite && conflictCheckBox.Checked)
                    File.Delete(conflictMatrixFile.Text);

                if (File.Exists(outputFile.Text) && _overwrite)
                    File.Delete(outputFile.Text);

                if (outputDyadicCheckBox.Checked) MatrixWriter.WriteDyadicHeader(outputFile.Text);

                bool overwrite = parent.GetSaveOverwrite();

                if (!rangeN)
                {
                    for (int i = 1; i <= iterations; ++i) // changed from iterations to N
                    {
                        if (!capabilitiesCheckBox.Checked) N = capabilProvider.GetNextMatrixRows();
                        else if (!useSrg && !contiguityCheckBox.Checked) N = contigProvider.GetNextMatrixRows();
                        else if (!useSrg && !majorPowersCheckBox.Checked) N = majPowProvider.GetNextMatrixRows();
                        else if (!conflictCheckBox.Checked) N = conflictProvider.GetNextMatrixRows();
                        else if (useSrg && !srgCheckBox.Checked) N = srgProvider.GetNextMatrixRows();
                        else if (!demoCheckBox.Checked) N = demoProvider.GetNextMatrixRows();
                        else if (!jcCheckBox.Checked) N = jcProvider.GetNextMatrixRows();
                        else if (!midCheckBox.Checked) N = midProvider.GetNextMatrixRows();
                        else if (!csCheckBox.Checked) N = csProvider.GetNextMatrixRows();
                        else if (!reliabilityCheckBox.Checked) N = relProvider.GetNextMatrixRows();

                        if (N == -1)
                            N = int.Parse(nTextBox.Text);

                        if (capabilitiesCheckBox.Checked) capabilProvider.Rows = capabilProvider.Cols = N;
                        if (!useSrg && contiguityCheckBox.Checked) contigProvider.Rows = contigProvider.Cols = N;
                        if (!useSrg && majorPowersCheckBox.Checked) majPowProvider.Rows = majPowProvider.Cols = N;
                        if (conflictCheckBox.Checked) conflictProvider.Rows = conflictProvider.Cols = N;
                        if (useSrg && srgCheckBox.Checked) srgProvider.Rows = srgProvider.Cols = N;
                        if (demoCheckBox.Checked) demoProvider.Rows = demoProvider.Cols = N;
                        if (jcCheckBox.Checked) jcProvider.Rows = jcProvider.Cols = N;
                        if (midCheckBox.Checked) midProvider.Rows = midProvider.Cols = N;
                        if (csCheckBox.Checked) csProvider.Rows = csProvider.Cols = N;
                        if (reliabilityCheckBox.Checked) relProvider.Rows = relProvider.Cols = N;

                        NetworkFormationSimulation.SimulateToFile(
                            capabilProvider,
                            contigProvider,
                            majPowProvider,
                            conflictProvider,
                            outputFile.Text,
                            expectedAllianceFile.Text,
                            conflictMatrixFile.Text,
                            srgSaveMatrixFile.Text,
                            i,
                            conflictCheckBox.Checked,
                            contiguityCheckBox.Checked,
                            majorPowersCheckBox.Checked,
                            outputDyadicCheckBox.Checked,
                            expectedAllianceDCheckBox.Checked,
                            srgDCheckBox.Checked,
                            srgProvider,
                            demoProvider,
                            jcProvider,
                            midProvider,
                            csProvider,
                            relProvider,
                            maxIterations,
                            overwrite,
                            double.Parse(brTextBox.Text));
                        overwrite = false;
                        (sender as BackgroundWorker).ReportProgress((int)(0.5 + Math.Ceiling(100.0 * i / (double)iterations)));
                        if (e.Cancel)
                            break;
                    }
                }
                else
                {
                    for (int i = 1; i <= iterations; ++i) // changed from iterations to N
                    {
                        N = RNG.RandomInt((rangeNmax - rangeNmin) / rangeNint) * rangeNint + rangeNmin;
                        if (capabilitiesCheckBox.Checked) capabilProvider.Rows = capabilProvider.Cols = N;
                        if (useSrg && contiguityCheckBox.Checked) contigProvider.Rows = contigProvider.Cols = N; // usrSrg is now True as of 8/31/2012
                        if (!useSrg && majorPowersCheckBox.Checked) majPowProvider.Rows = majPowProvider.Cols = N;
                        if (conflictCheckBox.Checked) conflictProvider.Rows = conflictProvider.Cols = N;
                        if (useSrg && srgCheckBox.Checked) srgProvider.Rows = srgProvider.Cols = N;
                        if (demoCheckBox.Checked) demoProvider.Rows = demoProvider.Cols = N;
                        if (jcCheckBox.Checked) jcProvider.Rows = jcProvider.Cols = N;
                        if (midCheckBox.Checked) midProvider.Rows = midProvider.Cols = N;
                        if (csCheckBox.Checked) csProvider.Rows = csProvider.Cols = N;
                        if (reliabilityCheckBox.Checked) relProvider.Rows = relProvider.Cols = N;

                        NetworkFormationSimulation.SimulateToFile(
                            capabilProvider,
                            contigProvider,
                            majPowProvider,
                            conflictProvider,
                            outputFile.Text,
                            expectedAllianceFile.Text,
                            conflictMatrixFile.Text,
                            srgSaveMatrixFile.Text,
                            i,
                            conflictCheckBox.Checked,
                            contiguityCheckBox.Checked,
                            majorPowersCheckBox.Checked,
                            outputDyadicCheckBox.Checked,
                            expectedAllianceDCheckBox.Checked,
                            srgDCheckBox.Checked,
                            srgProvider,
                            demoProvider,
                            jcProvider,
                            midProvider,
                            csProvider,
                            relProvider,
                            maxIterations,
                            overwrite,
                            double.Parse(brTextBox.Text));
                        overwrite = false;
                        (sender as BackgroundWorker).ReportProgress((int)(0.5 + Math.Ceiling(100.0 * i / (double)iterations)));
                        if (e.Cancel)
                            break;
                    }

                }
            //catch (Exception exception)
            {
             //   MessageBox.Show("Error: " + exception.Message + Environment.NewLine + exception.StackTrace);
            }
        }