Esempio n. 1
0
        private static MirPartyCalcInfo[,] GiveInitialAdditionalMandatesByMirs(int mirsCount, int[,] votesTable2, int partiesCountTable2, List <PartyCalcInfo> partiesWithCalcInfo, List <MirCalcInfo> mirsWithCalcInfo)
        {
            Logger.logger.Info("\r\n== Разпределяне на мандати по райони== ");
            //calculate per mir mandates
            MirPartyCalcInfo[,] mirPartyTable = new MirPartyCalcInfo[mirsCount, partiesCountTable2];
            for (int i = 0; i < mirsCount; i++)
            {
                for (int j = 0; j < partiesCountTable2; j++)
                {
                    int     votes            = votesTable2[i, j];
                    decimal mandateCoefHare  = mirsWithCalcInfo[i].MandateHareQuote != 0 ? decimal.Divide(votes, mirsWithCalcInfo[i].MandateHareQuote) : 0;
                    int     mandatesInit     = (int)mandateCoefHare;
                    decimal mandateCoefHareR = mandateCoefHare - mandatesInit;

                    var mirparty = new MirPartyCalcInfo()
                    {
                        MandateCoefHareR       = mandateCoefHareR,
                        MandatesInit           = mandatesInit,
                        IsMandateCoefHareRUsed = false,
                    };

                    mirsWithCalcInfo[i].MandatesGivenInit          += mandatesInit;
                    partiesWithCalcInfo[j].MandatesGivenByMirsInit += mandatesInit;
                    mirPartyTable[i, j] = mirparty;//for indexed table access
                }

                //additional mandates
                while (mirsWithCalcInfo[i].MandatesGivenAll < mirsWithCalcInfo[i].MandatesLimit)
                {
                    int     maxInd = 0;
                    decimal maxR   = 0;
                    for (int j = 0; j < partiesCountTable2; j++)
                    {
                        if (!mirPartyTable[i, j].IsMandateCoefHareRUsed && maxR < mirPartyTable[i, j].MandateCoefHareR)
                        {
                            maxR   = mirPartyTable[i, j].MandateCoefHareR;
                            maxInd = j;
                        }
                    }
                    mirPartyTable[i, maxInd].MandatesAdditional++;
                    mirPartyTable[i, maxInd].IsMandateCoefHareRUsed = true;

                    partiesWithCalcInfo[maxInd].MandatesGivenByMirsAdditional++;
                    mirsWithCalcInfo[i].MandatesGivenAdditional++;
                }
            }

            Logger.logger.Info("Разпределени мандати по райони:");
            for (int i = 0; i < mirsCount; i++)
            {
                StringBuilder sbLine = new StringBuilder();
                int           sum    = 0;
                for (int j = 0; j < partiesCountTable2; j++)
                {
                    sbLine.AppendFormat("{0, 6}", mirPartyTable[i, j].MandatesInit);
                    sum += mirPartyTable[i, j].MandatesInit;
                }
                sbLine.AppendFormat("[{0,2}]", sum);
                sbLine.AppendFormat(" от максимални [{0,2}]", mirsWithCalcInfo[i].MandatesLimit);

                Logger.logger.Info(sbLine.ToString());
            }

            Logger.logger.Info("Остатъци по райони - R:");
            Logger.logger.Info("* - получават допълнителен мандат");
            for (int i = 0; i < mirsCount; i++)
            {
                StringBuilder sbLine = new StringBuilder();
                for (int j = 0; j < partiesCountTable2; j++)
                {
                    sbLine.AppendFormat("{0, 6}", (mirPartyTable[i, j].IsMandateCoefHareRUsed ? "*" : "") + mirPartyTable[i, j].MandateCoefHareR.ToString("0.00"));
                }
                Logger.logger.Info(sbLine.ToString());
            }

            Logger.logger.Info("Допълнителни мандати по райони:");
            for (int i = 0; i < mirsCount; i++)
            {
                StringBuilder sbLine = new StringBuilder();
                int           sum    = 0;
                for (int j = 0; j < partiesCountTable2; j++)
                {
                    sbLine.AppendFormat("{0, 6}", mirPartyTable[i, j].MandatesAdditional);
                    sum += mirPartyTable[i, j].MandatesAdditional;
                }

                sbLine.AppendFormat("[{0,2}]", sum);
                sbLine.AppendFormat(" от максимални [{0,2}]", mirsWithCalcInfo[i].MandatesLimit);;

                Logger.logger.Info(sbLine.ToString());
            }

            Logger.logger.Info("Мандати по райони:");
            for (int i = 0; i < mirsCount; i++)
            {
                StringBuilder sbLine = new StringBuilder();
                int           sum    = 0;
                for (int j = 0; j < partiesCountTable2; j++)
                {
                    sbLine.AppendFormat("{0, 6}", mirPartyTable[i, j].MandatesGiven);
                    sum += mirPartyTable[i, j].MandatesGiven;
                }
                sbLine.AppendFormat("[{0,2}]", sum);
                sbLine.AppendFormat(" от [{0,2}]", mirsWithCalcInfo[i].MandatesLimit);
                Logger.logger.Info(sbLine.ToString());
            }
            return(mirPartyTable);
        }
        private static void SatisfyAllParties(List<PartyCalcInfo> partiesWithCalcInfo, MirPartyCalcInfo[,] mirPartyTable)
        {
            Logger.logger.Info("");
            Logger.logger.Info("== Преразпределяне на мандати за неудовлетворените партии== ");
            Logger.logger.Info("Текущо състояние:");
            Logger.logger.InfoFormat("{0,5} : [{2,15}] {1,10} {3,11}", "No.", "Разпр. от райони", "Нац. ниво", "Необходими");
            foreach (var party in partiesWithCalcInfo)
            {
                Logger.logger.InfoFormat("{0,5} : [{2,15}] {1,10} {3,11}", party.PartyId, party.MandatesByMirsAll, party.MandatesAll, party.MandatesByMirsUnncessary);
            }

            while (partiesWithCalcInfo.Count(p => p.MandatesByMirsUnncessary < 0) > 0)
            {
                int minUsedCoefI = 0;
                int minUsedCoefJ = 0;
                decimal minUsedCoef = 1.0M;
                bool hasMinUsedCoef = false;

                for (int i = 0; i < mirPartyTable.GetLength(0); i++)
                {
                    for (int j = 0; j < mirPartyTable.GetLength(1); j++)
                    {
                        //min used coef
                        if (partiesWithCalcInfo[j].MandatesByMirsUnncessary > 0)
                        {
                            if (mirPartyTable[i, j].IsMandateCoefHareRUsed
                                && !mirPartyTable[i, j].IsMandateCoefHareRUsed2)
                            {
                                if (mirPartyTable[i, j].MandateCoefHareR < minUsedCoef)
                                {
                                    minUsedCoef = mirPartyTable[i, j].MandateCoefHareR;
                                    minUsedCoefI = i;
                                    minUsedCoefJ = j;
                                    hasMinUsedCoef = true;
                                }
                            }
                        }

                    }
                }

                int maxUnsedCoefI = 0;
                int maxUnsedCoefJ = 0;
                decimal maxUnsedCoef = 0.0M;
                bool hasMaxUnusedCoef = false;

                //find max unused coef
                if (hasMinUsedCoef)
                {
                    int i = minUsedCoefI;
                    for (int j = 0; j < mirPartyTable.GetLength(1); j++)
                    {
                        //max unused coef
                        if (!mirPartyTable[i, j].IsMandateCoefHareRUsed
                            && !mirPartyTable[i, j].IsMandateCoefHareRUsed2)
                        {
                            if (mirPartyTable[i, j].MandateCoefHareR > maxUnsedCoef)//> only because parties are sorted by ID
                            {
                                maxUnsedCoef = mirPartyTable[i, j].MandateCoefHareR;
                                maxUnsedCoefI = i;
                                maxUnsedCoefJ = j;
                                hasMaxUnusedCoef = true;
                            }
                        }
                    }

                    //update for mandates
                    if (hasMaxUnusedCoef)
                    {
                        mirPartyTable[minUsedCoefI, minUsedCoefJ].MandatesAdditional--;
                        partiesWithCalcInfo[minUsedCoefJ].MandatesGivenByMirsAdditional--;
                        mirPartyTable[minUsedCoefI, minUsedCoefJ].IsMandateCoefHareRUsed2 = true;

                        mirPartyTable[maxUnsedCoefI, maxUnsedCoefJ].MandatesAdditional++;
                        partiesWithCalcInfo[maxUnsedCoefJ].MandatesGivenByMirsAdditional++;
                        mirPartyTable[maxUnsedCoefI, maxUnsedCoefJ].IsMandateCoefHareRUsed = true;
                    }
                }

                Logger.logger.Info("Текущо състояние:");
                Logger.logger.InfoFormat("{0,5} : [{2,15}] {1,10} {3,11}", "No.", "Разпр. от райони", "Нац. ниво", "Необходими");
                foreach (var party in partiesWithCalcInfo)
                {
                    Logger.logger.InfoFormat("{0,5} : [{2,15}] {1,10} {3,11}", party.PartyId, party.MandatesByMirsAll, party.MandatesAll, party.MandatesByMirsUnncessary);
                }
            }
        }
        private void GenerateResultsByPartyAndMir(List<PartyCalcInfo> partiesWithCalcInfo, List<MirCalcInfo> mirsWithCalcInfo, MirPartyCalcInfo[,] mirPartyTable)
        {
            var results = new List<Result>();
            //final mandates
            for (int i = 0; i < mirPartyTable.GetLength(0); i++)
            {
                for (int j = 0; j < mirPartyTable.GetLength(1); j++)
                {
                    if (mirPartyTable[i, j].MandatesGiven > 0)
                    {
                        var finalMandateInfo = new Result()
                        {
                            MirId = mirsWithCalcInfo[i].MirId,
                            PartyId = partiesWithCalcInfo[j].PartyId,
                            MandatesCount = mirPartyTable[i, j].MandatesGiven,
                        };
                        _results.Add(finalMandateInfo);
                    }
                }
            }

            Logger.Info("Краен резултат");
            Logger.Info("МИР, Партия, Получени мандати");
            foreach (var mif in _results)
            {
                Logger.logger.InfoFormat("{0},{1},{2}", mif.MirId, mif.PartyId, mif.MandatesCount);
            }
        }
        private static MirPartyCalcInfo[,] GiveInitialAdditionalMandatesByMirs(int mirsCount, int[,] votesTable2, int partiesCountTable2, List<PartyCalcInfo> partiesWithCalcInfo, List<MirCalcInfo> mirsWithCalcInfo)
        {
            Logger.logger.Info("\r\n== Разпределяне на мандати по райони== ");
            //calculate per mir mandates
            MirPartyCalcInfo[,] mirPartyTable = new MirPartyCalcInfo[mirsCount, partiesCountTable2];
            for (int i = 0; i < mirsCount; i++)
            {
                for (int j = 0; j < partiesCountTable2; j++)
                {
                    int votes = votesTable2[i, j];
                    decimal mandateCoefHare = mirsWithCalcInfo[i].MandateHareQuote != 0 ? decimal.Divide(votes, mirsWithCalcInfo[i].MandateHareQuote) : 0;
                    int mandatesInit = (int)mandateCoefHare;
                    decimal mandateCoefHareR = mandateCoefHare - mandatesInit;

                    var mirparty = new MirPartyCalcInfo()
                    {
                        MandateCoefHareR = mandateCoefHareR,
                        MandatesInit = mandatesInit,
                        IsMandateCoefHareRUsed = false,
                    };

                    mirsWithCalcInfo[i].MandatesGivenInit += mandatesInit;
                    partiesWithCalcInfo[j].MandatesGivenByMirsInit += mandatesInit;
                    mirPartyTable[i, j] = mirparty;//for indexed table access
                }

                //additional mandates
                while (mirsWithCalcInfo[i].MandatesGivenAll < mirsWithCalcInfo[i].MandatesLimit)
                {
                    int maxInd = 0;
                    decimal maxR = 0;
                    for (int j = 0; j < partiesCountTable2; j++)
                    {
                        if (!mirPartyTable[i, j].IsMandateCoefHareRUsed && maxR < mirPartyTable[i, j].MandateCoefHareR)
                        {
                            maxR = mirPartyTable[i, j].MandateCoefHareR;
                            maxInd = j;
                        }
                    }
                    mirPartyTable[i, maxInd].MandatesAdditional++;
                    mirPartyTable[i, maxInd].IsMandateCoefHareRUsed = true;

                    partiesWithCalcInfo[maxInd].MandatesGivenByMirsAdditional++;
                    mirsWithCalcInfo[i].MandatesGivenAdditional++;
                }

            }

            Logger.logger.Info("Разпределени мандати по райони:");
            for (int i = 0; i < mirsCount; i++)
            {
                StringBuilder sbLine = new StringBuilder();
                int sum = 0;
                for (int j = 0; j < partiesCountTable2; j++)
                {
                    sbLine.AppendFormat("{0, 6}", mirPartyTable[i, j].MandatesInit);
                    sum += mirPartyTable[i, j].MandatesInit;
                }
                sbLine.AppendFormat("[{0,2}]", sum);
                sbLine.AppendFormat(" от максимални [{0,2}]", mirsWithCalcInfo[i].MandatesLimit);

                Logger.logger.Info(sbLine.ToString());
            }

            Logger.logger.Info("Остатъци по райони - R:");
            Logger.logger.Info("* - получават допълнителен мандат");
            for (int i = 0; i < mirsCount; i++)
            {
                StringBuilder sbLine = new StringBuilder();
                for (int j = 0; j < partiesCountTable2; j++)
                {
                    sbLine.AppendFormat("{0, 6}", (mirPartyTable[i, j].IsMandateCoefHareRUsed ? "*" : "") + mirPartyTable[i, j].MandateCoefHareR.ToString("0.00"));
                }
                Logger.logger.Info(sbLine.ToString());
            }

            Logger.logger.Info("Допълнителни мандати по райони:");
            for (int i = 0; i < mirsCount; i++)
            {
                StringBuilder sbLine = new StringBuilder();
                int sum = 0;
                for (int j = 0; j < partiesCountTable2; j++)
                {
                    sbLine.AppendFormat("{0, 6}", mirPartyTable[i, j].MandatesAdditional);
                    sum += mirPartyTable[i, j].MandatesAdditional;
                }

                sbLine.AppendFormat("[{0,2}]", sum);
                sbLine.AppendFormat(" от максимални [{0,2}]", mirsWithCalcInfo[i].MandatesLimit); ;

                Logger.logger.Info(sbLine.ToString());
            }

            Logger.logger.Info("Мандати по райони:");
            for (int i = 0; i < mirsCount; i++)
            {
                StringBuilder sbLine = new StringBuilder();
                int sum = 0;
                for (int j = 0; j < partiesCountTable2; j++)
                {
                    sbLine.AppendFormat("{0, 6}", mirPartyTable[i, j].MandatesGiven);
                    sum += mirPartyTable[i, j].MandatesGiven;
                }
                sbLine.AppendFormat("[{0,2}]", sum);
                sbLine.AppendFormat(" от [{0,2}]", mirsWithCalcInfo[i].MandatesLimit);
                Logger.logger.Info(sbLine.ToString());
            }
            return mirPartyTable;
        }