Пример #1
0
        private void InitProbabilisticCombinations(Dictionary <int, Combination> combinations)
        {
            foreach (var comb in combinations)
            {
                float totalWeight        = 0;
                int   probabilisticCount = 0;
                int   determinedCount    = 0;
                comb.Value.WeightedPackages.ForEach(
                    x =>
                {
                    totalWeight += x.Weight;
                    if (x.Weight == 0)
                    {
                        determinedCount++;
                    }
                    else
                    {
                        probabilisticCount++;
                    }
                });

                var probabilisticComb = new ProbabilisticCombination();
                probabilisticComb.Id = comb.Key;
                probabilisticComb.DeterminedPackages    = new ProbabilisticPackage[determinedCount];
                probabilisticComb.ProbabilisticPackages = new ProbabilisticPackage[probabilisticCount];

                int currentWeight = 0;
                int d = 0, p = 0;
                foreach (var pac in comb.Value.WeightedPackages)
                {
                    var probPac = new ProbabilisticPackage();
                    probPac.Id = pac.Id;
                    probPac.DeterminedResourceItems    = _packages[pac.Id].DeterminedResourceItems;
                    probPac.ProbabilisticResourceItems = _packages[pac.Id].ProbabilisticResourceItems;

                    if (pac.Weight == 0)
                    {
                        probPac.Probability = 1;
                        probabilisticComb.DeterminedPackages[d] = probPac;

                        d++;
                    }
                    else
                    {
                        currentWeight      += pac.Weight;
                        probPac.Probability = currentWeight / totalWeight;
                        probabilisticComb.ProbabilisticPackages[p] = probPac;

                        p++;
                    }
                }

                _combinations.Add(comb.Key, probabilisticComb);
            }
        }
Пример #2
0
        private void CombinationGeneration(ProbabilisticCombination combination, Dictionary <int, DeterminedResourceItem> result)
        {
            foreach (var pac in combination.DeterminedPackages)
            {
                PackageGeneration(pac, result);
            }

            double r = _randomizer.NextDouble();

            foreach (var pac in combination.ProbabilisticPackages)
            {
                if (r < pac.Probability)
                {
                    PackageGeneration(pac, result);
                    break;
                }
            }
        }