Example #1
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (stopped)
            {
                dgvStats.Rows.Clear();
                chPlots.Series.Clear();
                blockedSoFar = 0;
                StaticRandom.Init(seed);
                Simulator.Reset();

                #region UI Initialization
                btnStart.Text  = "Stop";
                cbLogY.Enabled = false;
                stopped        = stopRequest = exitRequest = logIncConsidered = false;

                // Set up plots
                chPlots.Series.Add(new Series(cbThirsty.Text));
                chPlots.Series.Add(new Series(cbm.Text));
                chPlots.Series.Add(new Series(cbb.Text));
                chPlots.Series.Add(new Series(cbN.Text));
                chPlots.Series.Add(new Series(cbDistMessageCount.Text));
                chPlots.Series.Add(new Series(cbEmailCount.Text));
                chPlots.Series.Add(new Series(cbTime.Text));
                chPlots.Series.Add(new Series(cbmm.Text));
                chPlots.ChartAreas[0].AxisX.MajorGrid.LineColor = Color.LightGray;
                chPlots.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.LightGray;
                chPlots.ChartAreas[0].AxisX.LabelStyle.Font     = plotScreenFont;
                chPlots.ChartAreas[0].AxisY.LabelStyle.Font     = plotScreenFont;
                chPlots.ChartAreas[0].AxisX.TitleFont           = plotScreenFont;
                chPlots.ChartAreas[0].AxisY.TitleFont           = plotScreenFont;
                chPlots.Legends[0].Font = plotScreenFont;
                chPlots.Legends[0].IsDockedInsideChartArea = true;
                chPlots.ApplyPaletteColors();

                foreach (var series in chPlots.Series)
                {
                    series.BorderWidth = plotThickness;
                    series.ChartType   = SeriesChartType.Line;
                    series.MarkerSize  = plotMarkerSize;
                }
                chPlots.Series[0].MarkerStyle = MarkerStyle.Square;
                chPlots.Series[1].MarkerStyle = MarkerStyle.Triangle;
                chPlots.Series[2].MarkerStyle = MarkerStyle.Diamond;
                chPlots.Series[3].MarkerStyle = MarkerStyle.Circle;

                chPlots.Series[5].Color       = chPlots.Series[2].Color;
                chPlots.Series[5].MarkerStyle = MarkerStyle.Circle;

                chPlots.Series[6].Color       = chPlots.Series[2].Color;
                chPlots.Series[6].MarkerStyle = MarkerStyle.Circle;
                chPlots.Series[7].Color       = chPlots.Series[3].Color;
                chPlots.Series[7].MarkerStyle = MarkerStyle.Square;
                UpdatePlots();
                Application.DoEvents();
                #endregion

                int n     = (int)Math.Pow(2, tbUserCount.Value);
                int t_max = rbMultipleRuns.Checked ? t_max = tbBadCountMax.Value : tbCorruptCount.Value + 1;

                for (int t = tbCorruptCount.Value; t < t_max && !stopRequest; t++)
                {
                    // Create the distributors
                    var dists = new List <Distributor>();

                    for (int i = 1; i < tbDistCount.Value; i++)
                    {
                        dists.Add(new Distributor());
                    }

                    var leader = new LeaderDistributor(dists.Select(d => d.Id).ToList(),
                                                       rbBnb.Checked ? DistributeAlgorithm.BallsAndBins : DistributeAlgorithm.Matrix, seed);
                    dists.Insert(0, leader);

                    var distIds = dists.Select(d => d.Pseudonym).ToList();

                    // Add honest users
                    for (int i = 0; i < n - t; i++)
                    {
                        new User(distIds);
                    }

                    // Add corrupt users
                    var attackModel = AttackModel.Aggressive;
                    if (rbPrudent.Checked)
                    {
                        attackModel = AttackModel.Prudent;
                    }
                    else if (rbStochastic.Checked)
                    {
                        attackModel = AttackModel.Stochastic;
                    }

                    var censor = new Censor(dists, attackModel, tbStochastic.Value / 40.0, seed);
                    censor.AddCorruptUsers(t);

                    // Create a sufficient number of bridges
                    repeatCount = tbC.Value == 0 ? 1 : tbC.Value * (int)Math.Ceiling(Math.Log(n, 2));
                    var bridges = new List <Bridge>();
                    for (int i = 0; i < (8 * t - 2) * repeatCount; i++)
                    {
                        bridges.Add(new Bridge(distIds));
                    }

                    leader.BridgePseudonyms = bridges.Select(b => b.Pseudonym).ToList();
                    runLog             = new RunLog();
                    leader.OnRoundEnd += OnRoundEnd;

                    leader.Run(repeatCount);

                    logIncConsidered = cbLogY.Checked;
                    cbLogY.Enabled   = true;
                    if (rbMultipleRuns.Checked)
                    {
                        // Add one point to the plot for this run for each plot
                        chPlots.Series[cbTime.Text].Points.AddXY(t, runLog.RoundsCount);

                        var N = runLog.Sum(r => r.BlockedCount) + runLog.Last().BridgeCount;
                        chPlots.Series[cbmm.Text].Points.AddXY(t, N);
                    }
                }

                //for (int x = 8; x < 12 && !stopRequest; x++)
                //{
                //    blockedSoFar = 0;
                //    Simulator.Reset();

                //    int n = (int)Math.Pow(2, x);
                //    int t = n / 2;
                //    // Create the distributors
                //    var dists = new List<Distributor>();

                //    for (int i = 1; i < tbDistCount.Value; i++)
                //        dists.Add(new Distributor());

                //    var leader = new LeaderDistributor(dists.Select(d => d.Id).ToList(),
                //        rbBnb.Checked ? DistributeAlgorithm.BallsAndBins : DistributeAlgorithm.Matrix, seed);
                //    dists.Insert(0, leader);

                //    var distIds = dists.Select(d => d.Pseudonym).ToList();

                //    // Add honest users
                //    for (int i = 0; i < n - t; i++)
                //        new User(distIds);

                //    // Add corrupt users
                //    var attackModel = AttackModel.Aggressive;
                //    if (rbPrudent.Checked)
                //        attackModel = AttackModel.Prudent;
                //    else if (rbStochastic.Checked)
                //        attackModel = AttackModel.Stochastic;

                //    var censor = new Censor(dists, attackModel, tbStochastic.Value / 40.0, seed);
                //    censor.AddCorruptUsers(t);

                //    // Create a sufficient number of bridges
                //    repeatCount = tbC.Value == 0 ? 1 : tbC.Value * (int)Math.Ceiling(Math.Log(n, 2));
                //    var bridges = new List<Bridge>();
                //    for (int i = 0; i < (8 * t - 2) * repeatCount; i++)
                //        bridges.Add(new Bridge(distIds));

                //    leader.BridgePseudonyms = bridges.Select(b => b.Pseudonym).ToList();
                //    runLog = new RunLog();
                //    leader.OnRoundEnd += OnRoundEnd;

                //    leader.Run(repeatCount);

                //    logIncConsidered = cbLogY.Checked;
                //    cbLogY.Enabled = true;

                //    chPlots.Series[cbEmailCount.Text].Points.AddXY(n, (double)runLog.Sum(r => r.EmailCount) / n);

                //}

                //chPlots.Series[cbEmailCount.Text].Points.AddXY(32, 5);
                //chPlots.Series[cbEmailCount.Text].Points.AddXY(64, 10);
                //chPlots.Series[cbEmailCount.Text].Points.AddXY(128, 30);
                //chPlots.Series[cbEmailCount.Text].Points.AddXY(256, 60);
                //chPlots.Series[cbEmailCount.Text].Points.AddXY(512, 90);
                //chPlots.Series[cbEmailCount.Text].Points.AddXY(1024, 126);
                //chPlots.Series[cbEmailCount.Text].Points.AddXY(2048, 168);
                //chPlots.Series[cbEmailCount.Text].Points.AddXY(4096, 210);
            }
            else
            {
                stopRequest = true;
            }

            btnStart.Text = "Start";
            stopped       = true;

            if (exitRequest)
            {
                Close();
            }
        }