Ejemplo n.º 1
0
        public void CreateChildrenOnItemId(int itemId, AttributeValues attributeValues)
        {
            if (double.IsNaN(Entropy) || Entropy < 0.0001)
            {
                //System.Console.WriteLine("Will not create children.  Entropy of {0} is below threshold.", Entropy);
                return;
            }

            Attribute bestAttribute;
            object    bestAttributeValue;

            SplitAttributeInformationGain = Sources.FindGreatestInformationGain(itemId, attributeValues,
                                                                                out bestAttribute, out bestAttributeValue);
            SplitAttribute      = bestAttribute;
            SplitAttributeValue = bestAttributeValue;

            //if (SplitAttributeInformationGain < Entropy / 100)
            if (SplitAttributeInformationGain < Entropy / 10000000)
            {
                //System.Console.WriteLine("Will not create children.  Information gain of {0} is below threshold.",
                //    SplitAttributeInformationGain);
                return;
            }

            Dictionary <object, DataSourcesCollection> splits;

            Sources.DivideOnAttribute(bestAttribute, bestAttributeValue, attributeValues.ValuesMap[bestAttribute],
                                      out splits);

            Parallel.ForEach(splits, split =>
            {
                split.Value.UseAttributesWithValues = Sources.UseAttributesWithValues;

                DecisionTreeNode child = new DecisionTreeNode
                {
                    Sources = split.Value,
                    ItemId  = itemId,
                    ParentSplitAttribute      = SplitAttribute,
                    ParentSplitAttributeValue = split.Key,
                };

                lock (Children)
                {
                    Children.Add(split.Key, child);
                }

                child.CreateChildrenOnItemId(itemId, attributeValues);
            });
        }
        public double FindGreatestInformationGain(int itemId, AttributeValues attributeValues,
                                                  out Attribute bestAttribute, out object bestAttributeValue)
        {
            double    bestInformationGain     = double.MinValue;
            Attribute bestAttributeLocal      = Attribute.Build;
            object    bestAttributeValueLocal = null;

            Parallel.ForEach(Enum.GetValues(typeof(LootGainLib.Attribute)).Cast <LootGainLib.Attribute>(), attribute =>
            {
                switch (attribute)
                {
                case LootGainLib.Attribute.Quest:
                    if (!UseAttributesWithValues)
                    {
                        break;
                    }

                    Parallel.ForEach(attributeValues.ValuesMap[LootGainLib.Attribute.Quest].Keys, quest =>
                    {
                        var questInformationGain = this.InformationGainOnItemId(itemId, attribute, quest,
                                                                                attributeValues.ValuesMap[attribute]);
                        if (questInformationGain > bestInformationGain)
                        {
                            lock (lockObject)
                            {
                                if (questInformationGain > bestInformationGain)
                                {
                                    bestInformationGain     = questInformationGain;
                                    bestAttributeLocal      = attribute;
                                    bestAttributeValueLocal = quest;

                                    System.Console.WriteLine("New best information gain: Quest {0} at {1}",
                                                             quest, questInformationGain);
                                }
                            }
                        }
                        //System.Console.WriteLine("Information gain on quest {0}: {1}", quest,
                        //    questInformationGain);
                    });
                    break;

                case LootGainLib.Attribute.Loot:
                    break;

                default:
                    var loopInformationGain = this.InformationGainOnItemId(itemId, attribute, null,
                                                                           attributeValues.ValuesMap[attribute]);
                    if (loopInformationGain > bestInformationGain)
                    {
                        lock (lockObject)
                        {
                            if (loopInformationGain > bestInformationGain)
                            {
                                bestInformationGain     = loopInformationGain;
                                bestAttributeLocal      = attribute;
                                bestAttributeValueLocal = null;

                                System.Console.WriteLine("New best information gain: {0} at {1}",
                                                         attribute.ToString(), loopInformationGain);
                            }
                        }
                    }
                    //System.Console.WriteLine("Information gain on {0}: {1}", attribute.ToString(),
                    //    loopInformationGain);
                    break;
                }
            });

            bestAttribute      = bestAttributeLocal;
            bestAttributeValue = bestAttributeValueLocal;

            return(bestInformationGain);
        }