示例#1
0
        static public List <IPartialSnapshotCompression> NewInstancesOfUnitCountTreeCompression(Dictionary <ISensoryUnit, int> unitCountDictonary, ISensationSnapshot partialSnapshot, ISensationSnapshot snapShot, FieldOfVisionTypes fieldOfVision, DirectionTypes direction)
        {
            var result = new List <IPartialSnapshotCompression>();

            // Find 2-7 if 2-7 fields around are marked as Filled or Empty (two pattern with counted units) --> fieldOfVision.ThreeByThree
            foreach (KeyValuePair <ISensoryUnit, int> unitCountEntry in unitCountDictonary)
            {
                var patterns = partialSnapshot.SensoryPatterns.Where(p => p.SensoryUnits.Contains(unitCountEntry.Key)).ToList();
                foreach (ISensoryPattern pattern in patterns)
                {
                    ISensationSnapshot partialSnapshot2 = SensationSnapshot.ExtractSnapshot(snapShot, fieldOfVision, PuzzleReferee.Addition(direction, pattern.DirectionType));
                    var unitCountDictonary2             = SensationSnapshot.CountUnits(partialSnapshot2);
                    foreach (KeyValuePair <ISensoryUnit, int> unitCountEntry2 in unitCountDictonary2)
                    {
                        if (unitCountEntry2.Key.Equals(unitCountEntry.Key) && unitCountEntry2.Value <= 1)
                        {
                            // If the same unit found one time in the field of view, it must be the exact same one.
                            continue;
                        }
                        var unitCompression = new PartialSnapshotCompression(CompressionTypes.UnitCountTree, fieldOfVision, DirectionTypes.Undefined);
                        var node            = new PartialSnapshotCompressionNode(unitCountEntry.Key);
                        for (int i = 0; i < unitCountEntry2.Value; i++)
                        {
                            node.ChildNodes.Add(new PartialSnapshotCompressionNode(unitCountEntry2.Key));
                        }
                        unitCompression.ChildNodes.Add(node);
                        if (!result.Contains(unitCompression))
                        {
                            result.Add(unitCompression);
                        }
                    }
                }
            }
            return(result);
        }
示例#2
0
        public double CheckForNegativeFeedback(ISensationSnapshot snapshot)
        {
            double result = 0.0;

            IEnumerable <FieldOfVisionTypes> fieldOfVisions = NegativeDictPartialSnapshotCompressions.Keys.Select(e => e.FieldOfVision).Distinct();
            var dictDirectionToSnapshot = new Dictionary <Tuple <FieldOfVisionTypes, DirectionTypes>, ISensationSnapshot>();
            var baseDirection           = Action.Direction;

            foreach (FieldOfVisionTypes fieldOfVision in fieldOfVisions)
            {
                var basePartialSnapshot = SensationSnapshot.ExtractSnapshot(snapshot, fieldOfVision, baseDirection);
                dictDirectionToSnapshot.Add(new Tuple <FieldOfVisionTypes, DirectionTypes>(fieldOfVision, baseDirection), basePartialSnapshot);
                foreach (var childDirection in basePartialSnapshot.SensoryPatterns.Select(p => p.DirectionType))
                {
                    if (childDirection != baseDirection)
                    {
                        var childPartialSnapshot = SensationSnapshot.ExtractSnapshot(snapshot, fieldOfVision, childDirection);
                        dictDirectionToSnapshot.Add(new Tuple <FieldOfVisionTypes, DirectionTypes>(fieldOfVision, childDirection), childPartialSnapshot);
                    }
                }
            }

            foreach (IPartialSnapshotCompression partialSnapshotCompression in NegativeDictPartialSnapshotCompressions.Keys)
            {
                if (PartialSnapshotCompression.Contains(dictDirectionToSnapshot, partialSnapshotCompression))
                {
                    result = Math.Max(result, GetNegativeFeedbackPercentage(partialSnapshotCompression));
                }
            }
            return(result);
        }
示例#3
0
        static public IPartialSnapshotCompression Parse(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(null);
            }

            string parseText = text.Trim();

            if (!parseText.StartsWith("{") || !parseText.EndsWith("}"))
            {
                return(null);
            }
            parseText = parseText.Substring(1, parseText.Length - 2);

            string[] splits = parseText.Split(new[] { ',' });
            if (splits.Length <= 2)
            {
                return(null);
            }

            CompressionTypes   compressionType = (CompressionTypes)Enum.Parse(typeof(CompressionTypes), splits[0].Trim(), true);
            FieldOfVisionTypes fieldOfVision   = (FieldOfVisionTypes)Enum.Parse(typeof(FieldOfVisionTypes), splits[1].Trim(), true);
            DirectionTypes     direction       = DirectionTypes.Undefined;
            string             childNodesText  = splits[2].Trim();

            if (!childNodesText.Contains("{"))
            {
                direction      = (DirectionTypes)Enum.Parse(typeof(DirectionTypes), childNodesText, true);
                childNodesText = splits[3].Trim();
            }

            var result = new PartialSnapshotCompression(compressionType, fieldOfVision, direction);

            if (!childNodesText.Contains("<Empty>"))
            {
                if (!childNodesText.StartsWith(IdentifierChildNodes))
                {
                    result.ChildNodes.Add(PartialSnapshotCompressionNode.Parse(childNodesText));
                }
                else
                {
                    // =======================================================================================================================
                    // ToDo: Check ChildNodeTree also!!! Up to now it's only one single child handled! (Think about regular expressions maybe)
                    // =======================================================================================================================
                    childNodesText = childNodesText.Substring(IdentifierChildNodes.Length);
                    childNodesText = IdentifierChildNodes.Substring(0, childNodesText.Length - 1);
                    var splitedChildNodesText = childNodesText.Split(new[] { ';' });
                    foreach (var childNodeText in splitedChildNodesText)
                    {
                        result.ChildNodes.Add(PartialSnapshotCompressionNode.Parse(childNodeText));
                    }
                }
            }
            return(result);
        }
示例#4
0
        static public List <IPartialSnapshotCompression> NewInstancesOfUnitCompression(Dictionary <ISensoryUnit, int> unitCountDictonary, FieldOfVisionTypes fieldOfVision)
        {
            // Single units for fieldOfVision.Single and fieldOfVision.ThreeByThree allows to find 0 and 9
            var result = new List <IPartialSnapshotCompression>();

            foreach (var unitCountEntry in unitCountDictonary)
            {
                var unitCompression = new PartialSnapshotCompression(CompressionTypes.Unit, fieldOfVision, DirectionTypes.Undefined);
                var node            = new PartialSnapshotCompressionNode(unitCountEntry.Key);
                unitCompression.ChildNodes.Add(node);
                result.Add(unitCompression);
            }
            return(result);
        }
示例#5
0
        static public List <IPartialSnapshotCompression> NewInstancesOfMultiUnitCountTreeCompression(Dictionary <ISensoryUnit, int> unitCountDictonary, ISensationSnapshot partialSnapshot, ISensationSnapshot snapShot, FieldOfVisionTypes fieldOfVision, DirectionTypes direction)
        {
            var result = new List <IPartialSnapshotCompression>();

            // Find 2-7 if 2-7 fields around are marked as Filled or Empty (two pattern with counted units) --> fieldOfVision.ThreeByThree
            foreach (KeyValuePair <ISensoryUnit, int> unitCountEntry in unitCountDictonary)
            {
                var patterns = partialSnapshot.SensoryPatterns.Where(p => p.SensoryUnits.Contains(unitCountEntry.Key)).ToList();
                foreach (ISensoryPattern pattern in patterns)
                {
                    ISensationSnapshot partialSnapshot2 = SensationSnapshot.ExtractSnapshot(snapShot, fieldOfVision, PuzzleReferee.Addition(direction, pattern.DirectionType));
                    var unitCountDictonary2             = SensationSnapshot.CountUnits(partialSnapshot2);
                    List <ISensoryUnit> sortedUnits     = new List <ISensoryUnit>();
                    sortedUnits.AddRange(unitCountDictonary2.Keys.ToList());
                    sortedUnits.Sort();
                    for (int i = 0; i < sortedUnits.Count - 1; i++)
                    {
                        var unitKey1   = sortedUnits[i];
                        int unitValue1 = unitCountDictonary2[unitKey1];
                        if (unitKey1.Equals(unitCountEntry.Key))
                        {
                            unitValue1--;
                            if (unitValue1 < 1)
                            {
                                // If the same unit found one time in the field of view, it must be the exact same one.
                                continue;
                            }
                        }
                        for (int j = i + 1; j < sortedUnits.Count; j++)
                        {
                            var unitKey2   = sortedUnits[j];
                            var unitValue2 = unitCountDictonary2[unitKey2];
                            if (unitKey2.Equals(unitCountEntry.Key))
                            {
                                unitValue2--;
                                if (unitValue2 < 1)
                                {
                                    // If the same unit found one time in the field of view, it must be the exact same one.
                                    continue;
                                }
                            }
                            var unitCompression = new PartialSnapshotCompression(CompressionTypes.MultiUnitCountTree, fieldOfVision, DirectionTypes.Undefined);
                            var node            = new PartialSnapshotCompressionNode(unitCountEntry.Key);

                            for (int q = 0; q < unitValue1; q++)
                            {
                                node.ChildNodes.Add(new PartialSnapshotCompressionNode(unitKey1));
                            }
                            for (int q = 0; q < unitValue2; q++)
                            {
                                node.ChildNodes.Add(new PartialSnapshotCompressionNode(unitKey2));
                            }

                            unitCompression.ChildNodes.Add(node);
                            if (!result.Contains(unitCompression))
                            {
                                result.Add(unitCompression);
                            }
                        }
                    }
                }
            }
            return(result);
        }
示例#6
0
        public void RememberFeedback(int feedbackValue, ISensationSnapshot snapshot)
        {
            FieldOfVisionTypes fieldOfVision = GetFieldOfVisionsForFeedback().Last();
            List <IPartialSnapshotCompression> partialSnapshotCompressions = PartialSnapshotCompression.NewInstances(snapshot, fieldOfVision, Action.Direction, GetMaximumCompression());

            if (feedbackValue < 0)
            {
                NegativeFeedbackCount++;
                // ########### PartialSnapshotCompression #############
                foreach (IPartialSnapshotCompression pscEntry in partialSnapshotCompressions)
                {
                    if (PositveDictPartialSnapshotCompressions.ContainsKey(pscEntry))
                    {
                        PositveDictPartialSnapshotCompressions.Remove(pscEntry);
                    }

                    bool containingEntryExists = false;
                    foreach (IPartialSnapshotCompression negativePsc in NegativeDictPartialSnapshotCompressions.Keys)
                    {
                        if (GetNegativeFeedbackPercentage(negativePsc) >= 0.99 && pscEntry.Contains(negativePsc))
                        {
                            containingEntryExists = true;
                            break;
                        }
                    }
                    if (containingEntryExists)
                    {
                        continue;
                    }

                    if (!NegativeDictPartialSnapshotCompressions.ContainsKey(pscEntry))
                    {
                        NegativeDictPartialSnapshotCompressions.Add(pscEntry, new FeedbackCounter(PositiveFeedbackCount, NegativeFeedbackCount));
                    }
                    else if (GetNegativeFeedbackPercentage(pscEntry) >= 0.99)
                    {
                        var entriesToRemove = new List <IPartialSnapshotCompression>();
                        foreach (IPartialSnapshotCompression existingPsc in NegativeDictPartialSnapshotCompressions.Keys.Where(p => p.CompressionType != pscEntry.CompressionType))
                        {
                            if (existingPsc.Contains(pscEntry))
                            {
                                entriesToRemove.Add(existingPsc);
                            }
                        }
                        if (entriesToRemove.Any())
                        {
                            foreach (IPartialSnapshotCompression existingPsc in entriesToRemove)
                            {
                                NegativeDictPartialSnapshotCompressions.Remove(existingPsc);
                            }
                        }
                    }
                    else
                    {
                        NegativeDictPartialSnapshotCompressions[pscEntry].NegativeLifeCycleStamp = NegativeFeedbackCount;
                        NegativeDictPartialSnapshotCompressions[pscEntry].NegativeCount++;
                    }
                }
            }
            else if (feedbackValue > 0)
            {
                PositiveFeedbackCount++;
                // ###########  PartialSnapshotCompression #############
                foreach (IPartialSnapshotCompression pscEntry in partialSnapshotCompressions)
                {
                    if (NegativeDictPartialSnapshotCompressions.ContainsKey(pscEntry))
                    {
                        NegativeDictPartialSnapshotCompressions.Remove(pscEntry);
                    }

                    if (OverallNegativePartialSnapshotCompressions.Contains(pscEntry))
                    {
                        if (!PositveDictPartialSnapshotCompressions.ContainsKey(pscEntry))
                        {
                            PositveDictPartialSnapshotCompressions.Add(pscEntry, new FeedbackCounter(PositiveFeedbackCount, NegativeFeedbackCount));
                        }
                        else
                        {
                            PositveDictPartialSnapshotCompressions[pscEntry].PositiveLifeCycleStamp = PositiveFeedbackCount;
                            PositveDictPartialSnapshotCompressions[pscEntry].PositiveCount++;
                        }
                    }
                }
            }

            if (feedbackValue != 0)
            {
                List <IPartialSnapshotCompression> sortedKeys = NegativeDictPartialSnapshotCompressions.Keys.ToList();
                sortedKeys.Sort();
                var keysToRemove = new List <IPartialSnapshotCompression>();
                for (int i = 0; i < sortedKeys.Count - 1; i++)
                {
                    for (int j = i + 1; j < sortedKeys.Count; j++)
                    {
                        var a = sortedKeys[i];
                        var b = sortedKeys[j];
                        if (a.Contains(b) && NegativeDictPartialSnapshotCompressions[a].NegativeCount < NegativeDictPartialSnapshotCompressions[b].NegativeCount)
                        {
                            keysToRemove.Add(a);
                        }
                        else if (b.Contains(a) && NegativeDictPartialSnapshotCompressions[b].NegativeCount < NegativeDictPartialSnapshotCompressions[a].NegativeCount)
                        {
                            keysToRemove.Add(b);
                        }
                    }
                }
                foreach (var key in keysToRemove)
                {
                    NegativeDictPartialSnapshotCompressions.Remove(key);
                }
            }
        }