Пример #1
0
        public static IEnumerable <Multiset <T> > GetAllMultisets(List <T> elements, int maxWeightPerElement)
        {
            int[] indicies = new int[elements.Count];

            while (true)
            {
                for (int i = 0; i < elements.Count; i++)
                {
                    indicies[i]++;

                    if (indicies[i] > maxWeightPerElement)
                    {
                        indicies[i] = 0;

                        if (i == elements.Count - 1)
                        {
                            yield break;
                        }
                    }
                    else
                    {
                        break;
                    }
                }

                var resultMset = new HashMultiset <T>();

                for (int i = 0; i < elements.Count; i++)
                {
                    resultMset[elements[i]] = indicies[i];
                }

                yield return(resultMset);
            }
        }
Пример #2
0
        public static Multiset <T> SampleMultisetUniformNaive(List <T> elements, int count)
        {
            var result = new HashMultiset <T>();

            for (int i = 0; i < count; i++)
            {
                result[elements[Rand.NextInt(0, elements.Count)]]++;
            }

            return(result);
        }
Пример #3
0
        public static IEnumerable <Multiset <T> > GetMultisets <T>(List <T> elements, int weight)
        {
            Multiset <T> mset = new HashMultiset <T>();

            while (true)
            {
                bool moreResults = mset.NextMultiset(elements, weight);

                yield return(new HashMultiset <T>(mset));

                if (!moreResults)
                {
                    break;
                }
            }
        }
Пример #4
0
        public static Multiset <Int64> SampleMultisetUrn(Int64 elementCount, int setWeight, double alpha)
        {
            var result = new HashMultiset <Int64>();
            var urn    = new CumulativeProbabilityDistribution(elementCount);

            for (int i = 0; i < setWeight; i++)
            {
                var index = urn.Sample();

                urn.AddWeightByIndex(index, alpha);

                result[index]++;
            }

            return(result);
        }
        public override Multiset <long> SampleElectorate(int voteCount)
        {
            var result = new HashMultiset <long>();

            var electorateOffset = new double[Dimensions.Length];

            for (int j = 0; j < Dimensions.Length; j++)
            {
                electorateOffset[j] = Rand.SampleNormal(0.0, Dimensions[j] / (2 * Math.Sqrt((double)this.DistrictMagnitude)));
            }

            for (int i = 0; i < voteCount; i++)
            {
                var voterCoords = new double[Dimensions.Length];

                for (int j = 0; j < Dimensions.Length; j++)
                {
                    voterCoords[j] = Rand.SampleNormal(electorateOffset[j], Dimensions[j]);
                }

                List <Tuple <double, int> > distancesByPartyIndex = new List <Tuple <double, int> >();

                for (int p = 0; p < PartyCount; p++)
                {
                    double x = 0.0;

                    for (int j = 0; j < Dimensions.Length; j++)
                    {
                        x += Math.Pow(parties[p].Coords[j] - voterCoords[j], 2.0);
                    }

                    distancesByPartyIndex.Add(new Tuple <double, int>(Math.Sqrt(x), p));
                }

                var prefOrder = (from x in distancesByPartyIndex orderby x.Item1 ascending select x.Item2).ToList();

                var index = DiscreteMath.GetIndexByPermutation(prefOrder);

                result[index]++;
            }

            return(result);
        }
Пример #6
0
            public HashMultiset <Int64> ToMultiset(int totalVotes)
            {
                var result = new HashMultiset <Int64>();

                for (int i = 0; i < PartyCount; i++)
                {
                    for (int j = 0; j < PartyCount; j++)
                    {
                        for (int k = 0; k < PartyCount; k++)
                        {
                            if (i != j && j != k && i != k)
                            {
                                result[DiscreteMath.GetIndexByPermutation(GetRandPermutation(i, j, k))] += (int)(VotesByParty[i, j, k] * (double)totalVotes);
                            }
                        }
                    }
                }

                return(result);
            }