private void PrintConfiguration(Label label, SimulationConfiguration config)
 {
     DoOnGuiThread(label, () =>
     {
         label.Text = config.ToString();
     });
 }
        private void GenerateLineConfig(SimulationConfiguration config, int line)
        {
            int lineBudget = 0;

            while (lineBudget < currentLineBudgets[line])
            {
                int busType = GenerateBusType();
                lineBudget += busesCosts[busType];

                if (lineBudget > currentLineBudgets[line])
                {
                    break;
                }

                config.LinesVehicles[line].Add(busType);

                if (config.LineBusesStartTimes[line].Count == 0)
                {
                    config.LineBusesStartTimes[line].Add(startOfPassengersArrival[line] + GenerateStartTime());
                }
                else
                {
                    config.LineBusesStartTimes[line].Add(GenerateInterTime());
                }
            }
        }
        private SimulationConfiguration GenerateConfiguration()
        {
            if (configsWithCurrentBudgetCount >= configsWithCurrentBudgetMaxCount)
            {
                configsWithCurrentBudgetCount = 0;
                for (int i = 0; i < currentLineBudgets.Length; i++)
                {
                    currentLineBudgets[i] += budgetIncrement;
                }
            }

            configsWithCurrentBudgetCount++;

            SimulationConfiguration config = new SimulationConfiguration();

            GenerateLineConfig(config, 0);
            GenerateLineConfig(config, 1);
            GenerateLineConfig(config, 2);

            return(config);
        }
        private void buttStart_Click(object sender, EventArgs e)
        {
            processStopped            = false;
            bestConfiguration         = null;
            testedConfigurationsCount = 0;
            validConfigurationsCount  = 0;
            minArrivedAfterStartRatio = 1;
            minAverageWaitingTime     = double.MaxValue;
            validConfigFound          = false;
            minCost = int.MaxValue;
            minLineBudgets.CopyTo(currentLineBudgets, 0);
            configsWithCurrentBudgetCount = 0;

            Thread thread = new Thread(() =>
            {
                simulation.SetMaxSimSpeed();
                for (int configurationIdx = 0; configurationIdx < configurationsToTestCount; configurationIdx++)
                {
                    // generate configuration
                    SimulationConfiguration config = GenerateConfiguration();

                    // test configuration
                    SetConfiguration(simulation, config);

                    simulation.Simulate((int)numReplicationsCount.Value, endTime - startTime);

                    if (processStopped)
                    {
                        return;
                    }

                    testedConfigurationsCount++;
                    DoOnGuiThread(labelTestedConfigCount, () =>
                    {
                        labelTestedConfigCount.Text = testedConfigurationsCount.ToString();
                    });

                    // check if better than best
                    bool isSimulationResultValid = IsSimulationResultValid(simulation);
                    bool hasBetterStats          =
                        hasBetterStatsThan(simulation, minAverageWaitingTime, minArrivedAfterStartRatio);
                    int currentCost = config.Cost();

                    bool isBetter = false;
                    if (bestConfiguration == null)
                    {
                        isBetter = true;
                    }
                    else if (!validConfigFound)
                    {
                        if (isSimulationResultValid || hasBetterStats)
                        {
                            isBetter = true;
                        }
                    }
                    else if (isSimulationResultValid)
                    {
                        if (currentCost < minCost ||
                            (currentCost == minCost && hasBetterStats))
                        {
                            isBetter = true;
                        }
                    }

                    if (isSimulationResultValid)
                    {
                        validConfigFound = true;
                        validConfigurationsCount++;
                        DoOnGuiThread(labelValidConfigCount, () =>
                        {
                            labelValidConfigCount.Text = validConfigurationsCount.ToString();
                        });
                    }

                    if (isBetter)
                    {
                        bestConfiguration = config;
                        PrintConfiguration(labelBestConfiguration, bestConfiguration);
                        minArrivedAfterStartRatio = simulation.ArrivedAfterStartRatioSim.Mean();
                        minAverageWaitingTime     = simulation.AveragePassengerWaitingTimeSim.Mean();
                        minCost = currentCost;
                        DoOnGuiThread(labelMinCost, () =>
                        {
                            labelMinCost.Text = minCost.ToString();
                            labelMinAverageWaitingTime.Text = StatFormatter.FormatStatistic(
                                simulation.AveragePassengerWaitingTimeSim.Mean(),
                                simulation.AveragePassengerWaitingTimeSim.ConfidenceInterval90);
                            labelMinArrivedAfterStartRatio.Text = StatFormatter.FormatStatistic(
                                simulation.ArrivedAfterStartRatioSim.Mean(),
                                simulation.ArrivedAfterStartRatioSim.ConfidenceInterval90);
                        });
                    }
                }
            });

            thread.Start();
        }
 private void SetConfiguration(MySimulation simulation, SimulationConfiguration config)
 {
     SetConfiguration(simulation.LineVehicles, config.LinesVehicles);
     SetConfiguration(simulation.LineBusesStartTimes, config.LineBusesStartTimes);
 }