Пример #1
0
        public static IList <Constituency> GetConstituencies(Election electionToPredict, bool includeEnglandWales, bool includeScotland)
        {
            dynamic loadedJson;

            using (StreamReader r = new StreamReader(constuencyDataPath))
            {
                string json = r.ReadToEnd();
                //List<Item> items = JsonConvert.DeserializeObject<List<Item>>(json);
                loadedJson = JsonConvert.DeserializeObject(json);
            }

            var constituencies = loadedJson.constituencies;

            List <Constituency> constituenciesList = new List <Constituency>();

            foreach (var constituency in constituencies)
            {
                var c = new Constituency();

                var regionString = (string)constituency.region;
                if (regionString == "Northern Ireland" || (!includeScotland && regionString == "Scotland") || (!includeEnglandWales && regionString != "Scotland"))
                {
                    continue; // ignore NI
                }
                c.Region = GetRegionFromString(regionString);

                c.Name = (string)constituency.name;

                c.ONSReference = (string)constituency.onsRef;

                var euRef = constituency.referendums.eu;
                c.ReferendumResults = new Dictionary <ReferendumResult, double>
                {
                    { ReferendumResult.Leave, (double)euRef.leave },
                    { ReferendumResult.Remain, (double)euRef.remain }
                };

                var electionResult = electionToPredict == Election.e2017 ? constituency.elections["2015"] : constituency.elections["2017"];
                c.PreviousVote = new Dictionary <Party, double>();
                foreach (var result in electionResult)
                {
                    Party  party = GetPartyFromString((string)result.Name);
                    double value = (double)result.Value;

                    if (party == Party.Other && c.PreviousVote.ContainsKey(Party.Other))
                    {
                        c.PreviousVote[party] = c.PreviousVote[party] + value;
                    }
                    else
                    {
                        c.PreviousVote.Add(party, value);
                    }
                }

                if (electionToPredict == Election.e2017)
                {
                    var actualResults = new Dictionary <Party, double>();
                    foreach (var result in constituency.elections["2017"])
                    {
                        Party  party = GetPartyFromString((string)result.Name);
                        double value = (double)result.Value;

                        if (party == Party.Other && actualResults.ContainsKey(Party.Other))
                        {
                            actualResults[party] = actualResults[party] + value;
                        }
                        else
                        {
                            actualResults.Add(party, value);
                        }
                    }

                    c.ActualWinner = actualResults.OrderByDescending(r => r.Value).First().Key;
                }

                c.Ages = new Dictionary <AgeGroup, double>();
                foreach (var result in constituency.age)
                {
                    AgeGroup age = (AgeGroup)Enum.Parse(typeof(AgeGroup), (string)result.Name);
                    c.Ages.Add(age, (double)result.Value);
                }

                c.SocialGrades = new Dictionary <SocialGrade, double>();
                foreach (var result in constituency.socialGrades)
                {
                    SocialGrade socialGrade = (SocialGrade)Enum.Parse(typeof(SocialGrade), (string)result.Name);
                    c.SocialGrades.Add(socialGrade, (double)result.Value);
                }

                constituenciesList.Add(c);
            }

            return(constituenciesList);
        }
        public Party GetLikeliestParty(Party previousVote, AgeGroup age, Gender gender, ReferendumResult referendum, Region region, SocialGrade socialGrade)
        {
            var likeliestParty = Party.None;
            var highestScore   = 0d;

            foreach (var party in Enum.GetValues(typeof(Party)).Cast <Party>())
            {
                //if (party == Party.SNP && region != Region.Scotland)
                //{
                //    continue;
                //}

                //var isRegionalParty = (region == Region.Scotland && party == Party.SNP) || (region == Region.MidlandsWales && party == Party.PlaidCymru);
                //var regionalMultiplier = isRegionalParty ? 2 : 1;
                //var bonusPreviousParty = previousVote == party ? 2 : 1;

                var score = AgeProbabilities[age].GetVotingLikelihood(party) +
                            GenderProbabilities[gender].GetVotingLikelihood(party) +
                            ReferendumProbabilities[referendum].GetVotingLikelihood(party) +
                            (RegionProbabilities[region].GetVotingLikelihood(party)) +
                            SocialGradeProbabilities[socialGrade].GetVotingLikelihood(party);

                if (previousVote == Party.UKIP && party == Party.UKIP && referendum == ReferendumResult.Leave)
                {
                    var x = true;
                }

                if (PreviousElectionProbabilities.ContainsKey(previousVote))
                {
                    score += PreviousElectionProbabilities[previousVote].GetVotingLikelihood(party);
                    score /= 6;
                }
                else
                {
                    score /= 5;
                }

                if (previousVote == party || region == Region.Scotland || true)
                {
                    score *= 5;

                    Random random = new Random();
                    var    rand   = random.Next(100);
                    if (region == Region.Scotland && rand > 90 && previousVote.IsUnionist() && party.IsUnionist())
                    {
                        score *= 100;
                    }
                    else if (rand > 80 && previousVote == party)// || ())
                    {
                        score *= 100;
                    }
                    else if (rand > 40)
                    {
                        //if (rand > 60 && region == Region.Scotland)
                        //{
                        //    score += RegionProbabilities[region].GetVotingLikelihood(party) * 80;
                        //}
                        //else if (region != Region.Scotland)
                        {
                            if (rand > 68)
                            {
                                score += AgeProbabilities[age].GetVotingLikelihood(party) * 80;
                            }
                            else if (rand > 62)
                            {
                                score += GenderProbabilities[gender].GetVotingLikelihood(party) * 80;
                            }
                            else if (rand > 52)
                            {
                                score += SocialGradeProbabilities[socialGrade].GetVotingLikelihood(party) * 80;
                            }
                            else if (region != Region.Scotland)
                            {
                                score += RegionProbabilities[region].GetVotingLikelihood(party) * 80;
                            }
                        }
                    }
                }

                if (score > highestScore)
                {
                    likeliestParty = party;
                    highestScore   = score;
                }
            }

            if (likeliestParty == Party.None)
            {
                throw new Exception("Couldnt find likeliest party");
            }

            return(likeliestParty);
        }