Ejemplo n.º 1
0
 public SensationSnapshot(DirectionTypes directionType, FieldOfVisionTypes fieldOfVisionType, List <ISensoryPattern> sensoryPatterns, bool saveable = true)
 {
     Id            = -1;
     Direction     = directionType;
     FieldOfVision = fieldOfVisionType;
     SensoryPatterns.AddRange(sensoryPatterns);
 }
Ejemplo n.º 2
0
 public PartialSnapshotCompression(CompressionTypes compressionType, FieldOfVisionTypes fieldOfVision, DirectionTypes direction)
 {
     Id = -1;
     CompressionType = compressionType;
     FieldOfVision   = fieldOfVision;
     Direction       = direction;
 }
Ejemplo n.º 3
0
        public void Experience(FieldOfVisionTypes fieldOfVisionType, PuzzleBoard partialBoard)
        {
            Point centerPos = new Point();

            switch (fieldOfVisionType)
            {
            case FieldOfVisionTypes.Single:
                centerPos = new Point(0, 0);
                break;

            case FieldOfVisionTypes.ThreeByThree:
                centerPos = new Point(1, 1);
                break;

            case FieldOfVisionTypes.FiveByFive:
                centerPos = new Point(2, 2);
                break;
            }

            List <ISensoryPattern> sensoryPatterns = new List <ISensoryPattern>();

            for (int y = 0; y < partialBoard.Rows; y++)
            {
                for (int x = 0; x < partialBoard.Columns; x++)
                {
                    Point                pos           = new Point(x, y);
                    DirectionTypes       directionType = PuzzleReferee.ConvertToDirectionType(new Point(pos.X - centerPos.X, pos.Y - centerPos.Y));
                    PuzzleCellStateTypes state         = partialBoard.GetState(pos);
                    int    value       = partialBoard.GetValue(pos);
                    string valueString = value >= 0 ? value.ToString() : " ";

                    if (state != PuzzleCellStateTypes.Undefined)
                    {
                        ISensoryUnit sensoryUnitState = GetOrCreateSensoryUnit(SensoryTypes.FieldState, state.ToString());
                        ISensoryUnit sensoryUnitValue = GetOrCreateSensoryUnit(SensoryTypes.FieldValue, valueString);

                        List <ISensoryUnit> sensoryUnits = new List <ISensoryUnit>();
                        sensoryUnits.Add(sensoryUnitState);
                        sensoryUnits.Add(sensoryUnitValue);

                        ISensoryPattern sensoryPattern = new SensoryPattern(directionType, sensoryUnits);

                        if (_kownSensoryPatterns.Contains(sensoryPattern))
                        {
                            sensoryPattern = _kownSensoryPatterns[_kownSensoryPatterns.IndexOf(sensoryPattern)];
                        }
                        else
                        {
                            _kownSensoryPatterns.Add(sensoryPattern);
                            _kownSensoryPatterns.Sort();
                        }

                        sensoryPatterns.Add(sensoryPattern);
                    }
                }
            }
            _lastSensationSnapshot = new SensationSnapshot(DirectionTypes.Center, fieldOfVisionType, sensoryPatterns, IS_SAVEABLE_SNAPSHOT);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
0
        static public bool Contains(Dictionary <Tuple <FieldOfVisionTypes, DirectionTypes>, ISensationSnapshot> dictDirectionToSnapshot, IPartialSnapshotCompression partialSnapshotCompression)
        {
            ISensoryUnit       baseUnit            = partialSnapshotCompression.ChildNodes.FirstOrDefault().Unit;
            var                baseDirection       = DirectionTypes.Center;
            FieldOfVisionTypes fieldOfVision       = partialSnapshotCompression.FieldOfVision;
            ISensationSnapshot basePartialSnapshot = dictDirectionToSnapshot[new Tuple <FieldOfVisionTypes, DirectionTypes>(fieldOfVision, baseDirection)];

            switch (partialSnapshotCompression.CompressionType)
            {
            case CompressionTypes.Unit:
                return(basePartialSnapshot.SensoryPatterns.Any(p => p.SensoryUnits.Contains(baseUnit)));

            case CompressionTypes.UnitSimpleTree:
            case CompressionTypes.UnitCountTree:
            case CompressionTypes.MultiUnitCountTree:
                IEnumerable <ISensoryPattern> patterns   = basePartialSnapshot.SensoryPatterns.Where(p => p.SensoryUnits.Contains(baseUnit));
                IEnumerable <ISensoryUnit>    childUnits = partialSnapshotCompression.ChildNodes.FirstOrDefault().ChildNodes.Select(p => p.Unit);
                foreach (var direction in patterns.Select(p => p.DirectionType))
                {
                    var  childPartialSnapshot  = dictDirectionToSnapshot[new Tuple <FieldOfVisionTypes, DirectionTypes>(fieldOfVision, direction)];
                    bool areChildNodesIncluded = false;
                    foreach (var childUnit in childUnits.Distinct())
                    {
                        var minCount = childUnits.Count(u => u.Equals(childUnit));
                        if (baseUnit.Equals(childUnit))
                        {
                            minCount++;
                        }
                        areChildNodesIncluded = childPartialSnapshot.SensoryPatterns.Count(p => p.SensoryUnits.Contains(childUnit)) >= minCount;
                        if (!areChildNodesIncluded)
                        {
                            break;
                        }
                    }
                    if (areChildNodesIncluded)
                    {
                        return(true);
                    }
                }
                break;

            default:
                throw new NotImplementedException();
            }
            return(false);
        }
Ejemplo n.º 6
0
        private void FillPartialBoard(PuzzleBoard patialBoard, FieldOfVisionTypes fieldOfVisionType, Point position)
        {
            switch (fieldOfVisionType)
            {
            case FieldOfVisionTypes.Single:
                SetValueAndState(position, new Point(0, 0), patialBoard);
                break;

            case FieldOfVisionTypes.ThreeByThree:
                FillPartialBoard(patialBoard, FieldOfVisionTypes.Single, position);
                SetValueAndState(position, new Point(-1, 0), patialBoard);
                SetValueAndState(position, new Point(1, 0), patialBoard);
                SetValueAndState(position, new Point(0, -1), patialBoard);
                SetValueAndState(position, new Point(0, 1), patialBoard);
                SetValueAndState(position, new Point(-1, -1), patialBoard);
                SetValueAndState(position, new Point(-1, 1), patialBoard);
                SetValueAndState(position, new Point(1, 1), patialBoard);
                SetValueAndState(position, new Point(1, -1), patialBoard);
                break;

            case FieldOfVisionTypes.FiveByFive:
                FillPartialBoard(patialBoard, FieldOfVisionTypes.ThreeByThree, position);
                SetValueAndState(position, new Point(-2, 0), patialBoard);
                SetValueAndState(position, new Point(2, 0), patialBoard);
                SetValueAndState(position, new Point(0, 2), patialBoard);
                SetValueAndState(position, new Point(0, -2), patialBoard);
                SetValueAndState(position, new Point(-2, -1), patialBoard);
                SetValueAndState(position, new Point(-2, 1), patialBoard);
                SetValueAndState(position, new Point(2, -1), patialBoard);
                SetValueAndState(position, new Point(2, 1), patialBoard);

                SetValueAndState(position, new Point(-1, 2), patialBoard);
                SetValueAndState(position, new Point(1, 2), patialBoard);
                SetValueAndState(position, new Point(-1, -2), patialBoard);
                SetValueAndState(position, new Point(1, -2), patialBoard);
                SetValueAndState(position, new Point(-2, -2), patialBoard);
                SetValueAndState(position, new Point(-2, 2), patialBoard);
                SetValueAndState(position, new Point(2, -2), patialBoard);
                SetValueAndState(position, new Point(2, 2), patialBoard);
                break;
            }
        }
Ejemplo n.º 7
0
        private PuzzleBoard CreatePartialBoard(FieldOfVisionTypes fieldOfVisionType, Point position)
        {
            PuzzleBoard partialBoard;

            switch (fieldOfVisionType)
            {
            case FieldOfVisionTypes.Single:
                partialBoard = new PuzzleBoard(1, 1);
                break;

            case FieldOfVisionTypes.ThreeByThree:
                partialBoard = new PuzzleBoard(3, 3);
                break;

            case FieldOfVisionTypes.FiveByFive:
                partialBoard = new PuzzleBoard(5, 5);
                break;

            default:
                return(null);
            }
            FillPartialBoard(partialBoard, fieldOfVisionType, position);
            return(partialBoard);
        }
Ejemplo n.º 8
0
        private void DrawFieldOfVision(FieldOfVisionTypes fieldOfVisionType, Point position)
        {
            PuzzleBoard partialBoard = CreatePartialBoard(fieldOfVisionType, position);

            if (partialBoard == null)
            {
                return;
            }

            int width             = partialBoard.Columns;
            int height            = partialBoard.Rows;
            int drawCellSize      = Math.Min(_pbxLookResult.Width, _pbxLookResult.Height);
            int maxFieldDimension = Math.Max(width, height);

            drawCellSize = drawCellSize / maxFieldDimension;

            int    drawWidth            = width * drawCellSize;
            int    drawHeight           = height * drawCellSize;
            Bitmap playGround           = new Bitmap(drawWidth + 1, drawHeight + 1);
            Bitmap playGroundBackground = new Bitmap(drawWidth + 1, drawHeight + 1);
            Font   font = new Font(Font.FontFamily, (int)Math.Round(drawCellSize * 0.55));

            using (Graphics graphics = Graphics.FromImage(playGround))
            {
                using (Graphics graphicsBackground = Graphics.FromImage(playGroundBackground))
                {
                    graphics.Clear(Color.Transparent);
                    graphicsBackground.Clear(Color.White);

                    for (int y = 0; y <= partialBoard.Rows; y++)
                    {
                        graphics.DrawLine(new Pen(Color.Gray), 0, y * drawCellSize, drawWidth, y * drawCellSize);
                    }
                    for (int x = 0; x <= partialBoard.Columns; x++)
                    {
                        graphics.DrawLine(new Pen(Color.Gray), x * drawCellSize, 0, x * drawCellSize, drawHeight);
                    }
                    for (int y = 0; y < partialBoard.Rows; y++)
                    {
                        for (int x = 0; x < partialBoard.Columns; x++)
                        {
                            Point cellPos = new Point(x, y);
                            int   drawX   = x * drawCellSize;
                            int   drawY   = y * drawCellSize;
                            int   number  = partialBoard.GetValue(cellPos);
                            if (number >= 0 && number <= 9)
                            {
                                Brush fontBrush = partialBoard.GetState(cellPos) == PuzzleCellStateTypes.Filled ? Brushes.White : Brushes.Black;
                                graphics.DrawString(number.ToString(), font, fontBrush, new Rectangle(drawX + (30 / maxFieldDimension), drawY + (10 / maxFieldDimension), drawCellSize, drawCellSize));
                            }
                            switch (partialBoard.GetState(cellPos))
                            {
                            case PuzzleCellStateTypes.Undefined:
                                graphicsBackground.FillRectangle(Brushes.Yellow, drawX, drawY, drawCellSize, drawCellSize);
                                break;

                            case PuzzleCellStateTypes.Filled:
                                graphicsBackground.FillRectangle(Brushes.Black, drawX, drawY, drawCellSize, drawCellSize);
                                break;

                            case PuzzleCellStateTypes.Empty:
                                graphicsBackground.FillRectangle(Brushes.Gainsboro, drawX, drawY, drawCellSize, drawCellSize);
                                graphicsBackground.DrawLine(new Pen(Color.Gray), drawX, drawY, drawX + drawCellSize, drawY + drawCellSize);
                                graphicsBackground.DrawLine(new Pen(Color.Gray), drawX, drawY + drawCellSize, drawX + drawCellSize, drawY);
                                break;

                            case PuzzleCellStateTypes.Outside:
                                graphicsBackground.FillRectangle(Brushes.DarkBlue, drawX, drawY, drawCellSize, drawCellSize);
                                graphicsBackground.DrawLine(new Pen(Color.White), drawX, drawY, drawX + drawCellSize, drawY + drawCellSize);
                                graphicsBackground.DrawLine(new Pen(Color.White), drawX, drawY + drawCellSize, drawX + drawCellSize, drawY);
                                break;
                            }
                        }
                    }
                }
            }
            _pbxLookResult.Image           = playGround;
            _pbxLookResult.BackgroundImage = playGroundBackground;
        }
Ejemplo n.º 9
0
 public Dictionary <ISensoryPattern, int> GetNoDifferencePattern(FieldOfVisionTypes fieldOfVision)
 {
     return(_noDifferencePatternDictonary[fieldOfVision]);
 }
Ejemplo n.º 10
0
        static public List <IPartialSnapshotCompression> NewInstances(ISensationSnapshot snapshot, FieldOfVisionTypes fieldOfVision, DirectionTypes direction, CompressionTypes maximumCompression)
        {
            var result = new List <IPartialSnapshotCompression>();

            ISensationSnapshot partialSnapshot = SensationSnapshot.ExtractSnapshot(snapshot, fieldOfVision, direction);

            // Single units for fieldOfVision.Single and fieldOfVision.ThreeByThree allows to find 0 and 9
            var unitCountDictonary = SensationSnapshot.CountUnits(partialSnapshot);


            result.AddRange(NewInstancesOfUnitCompression(unitCountDictonary, fieldOfVision));

            if (maximumCompression >= CompressionTypes.UnitSimpleTree)
            {
                // Find 1 and 8 if a field around is marked as Filled or Empty (two pattern with single unit) --> fieldOfVision.ThreeByThree
                result.AddRange(NewInstancesOfUnitSimpleTreeCompression(unitCountDictonary, partialSnapshot, snapshot, fieldOfVision, direction));
            }

            if (maximumCompression >= CompressionTypes.UnitCountTree)
            {
                // ToDo: Find 2-7 if 2-7 fields around are marked as Filled or Empty (two pattern with counted units) --> fieldOfVision.ThreeByThree
                result.AddRange(NewInstancesOfUnitCountTreeCompression(unitCountDictonary, partialSnapshot, snapshot, fieldOfVision, direction));
            }

            if (maximumCompression >= CompressionTypes.MultiUnitCountTree)
            {
                // ToDo: Find 1-5 fields at the boarder with combination of Empty and Outside  --> fieldOfVision.ThreeByThree
                result.AddRange(NewInstancesOfMultiUnitCountTreeCompression(unitCountDictonary, partialSnapshot, snapshot, fieldOfVision, direction));
            }

            return(result);
        }
Ejemplo n.º 11
0
        static public List <IPartialSnapshotCompression> NewInstancesOfMultiUnitCountTreeCompression(ISensationSnapshot snapshot, FieldOfVisionTypes fieldOfVision, DirectionTypes direction)
        {
            var result = new List <IPartialSnapshotCompression>();

            ISensationSnapshot partialSnapshot = SensationSnapshot.ExtractSnapshot(snapshot, fieldOfVision, direction);
            var unitCountDictonary             = SensationSnapshot.CountUnits(partialSnapshot);

            result.AddRange(NewInstancesOfMultiUnitCountTreeCompression(unitCountDictonary, partialSnapshot, snapshot, fieldOfVision, direction));

            return(result);
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
0
        public void RememberDifference(bool isDifferent, ISensationSnapshot snapshot)
        {
            FieldOfVisionTypes fieldOfVision = GetFieldOfVisionsForDifferences().Last();
            var partialSnapshot = SensationSnapshot.ExtractSnapshot(snapshot, fieldOfVision, Action.Direction);

            // Handles counter and single units
            if (isDifferent)
            {
                DifferenceCount++;
                var singleUnits = SplitUnits(partialSnapshot);
                foreach (var unit in singleUnits)
                {
                    if (!DifferentUnits.ContainsKey(unit) && NoDifferentUnits.ContainsKey(unit))
                    {
                        DifferentUnits.Add(unit, 0);
                    }
                    if (DifferentUnits.ContainsKey(unit))
                    {
                        DifferentUnits[unit]++;
                    }
                }

                List <ISensoryUnit> unitsToRemove = new List <ISensoryUnit>();
                foreach (var entry in DifferentUnits)
                {
                    if (!NoDifferentUnits.ContainsKey(entry.Key))
                    {
                        unitsToRemove.Add(entry.Key);
                    }
                }
                foreach (var unit in unitsToRemove)
                {
                    DifferentUnits.Remove(unit);
                }
            }
            else
            {
                NoDifferenceCount++;
                var singleUnits = SplitUnits(partialSnapshot);
                foreach (var unit in singleUnits)
                {
                    if (!NoDifferentUnits.ContainsKey(unit))
                    {
                        NoDifferentUnits.Add(unit, 0);
                    }
                    NoDifferentUnits[unit]++;
                }
            }

            if (NoDifferenceCount > MINIMUM_CALL_COUNT_FOR_DIFFERENT_PATTERN && DifferenceCount > 0)
            {
                if (isDifferent)
                {
                    // Look for pattern in No-Difference-Dictionary, which have brought about a change
                    foreach (var pattern in SplitPattern(partialSnapshot, 1))
                    {
                        if (GetNoDifferencePattern(fieldOfVision).ContainsKey(pattern))
                        {
                            GetNoDifferencePattern(fieldOfVision).Remove(pattern);
                        }
                    }
                }
                else
                {
                    // Looking for pattern that probably show, that there is no effect by handling this action
                    foreach (var pattern in SplitPattern(partialSnapshot, 1))
                    {
                        bool patternFound = true;
                        foreach (var unit in pattern.SensoryUnits)
                        {
                            if (!NoDifferentUnits.ContainsKey(unit))
                            {
                                patternFound = false;
                                break;
                            }
                        }
                        if (patternFound)
                        {
                            if (!GetNoDifferencePattern(fieldOfVision).ContainsKey(pattern))
                            {
                                GetNoDifferencePattern(fieldOfVision).Add(pattern, 0);
                            }
                            GetNoDifferencePattern(fieldOfVision)[pattern]++;
                        }
                    }
                }
            }
        }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
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);
                }
            }
        }
Ejemplo n.º 17
0
        static public IList <IActionMemory> Parse(IList <string> lines)
        {
            if (lines == null)
            {
                return(null);
            }
            IList <IActionMemory> result = new List <IActionMemory>();

            IPuzzleAction    action           = null;
            FileActionMemory fileActionMemory = null;
            int parseType = 0;
            FieldOfVisionTypes fieldOfVision = FieldOfVisionTypes.Single;

            foreach (var line in lines)
            {
                if (line.StartsWith(IdentifierIPuzzleAction))
                {
                    action           = PuzzleAction.Parse(line.Substring(IdentifierIPuzzleAction.Length));
                    fileActionMemory = _fileActionMemories.Where(e => e.Action.Equals(action))?.FirstOrDefault();
                    if (fileActionMemory == null)
                    {
                        fileActionMemory = new FileActionMemory(action);
                        _fileActionMemories.Add(fileActionMemory);
                    }
                    result.Add(fileActionMemory);
                    continue;
                }
                if (fileActionMemory == null)
                {
                    continue;
                }

                if (line.StartsWith(IdentifierDifferenceCount))
                {
                    fileActionMemory.DifferenceCount += int.Parse(line.Substring(IdentifierDifferenceCount.Length));
                }
                else if (line.StartsWith(IdentifierNoDifferenceCount))
                {
                    fileActionMemory.NoDifferenceCount += int.Parse(line.Substring(IdentifierNoDifferenceCount.Length));
                }
                else if (line.StartsWith(IdentifierPositiveFeedbackCount))
                {
                    fileActionMemory.PositiveFeedbackCount += int.Parse(line.Substring(IdentifierPositiveFeedbackCount.Length));
                }
                else if (line.StartsWith(IdentifierNegativeFeedbackCount))
                {
                    fileActionMemory.NegativeFeedbackCount += int.Parse(line.Substring(IdentifierNegativeFeedbackCount.Length));
                }
                else if (line.StartsWith(IdentifierDifferentUnits))
                {
                    parseType = 1;
                }
                else if (line.StartsWith(IdentifierNoDifferentUnits))
                {
                    parseType = 2;
                }
                else if (line.StartsWith(IdentifierNGetNoDifferencePattern))
                {
                    fieldOfVision = (FieldOfVisionTypes)Enum.Parse(typeof(FieldOfVisionTypes), line.Substring(IdentifierNGetNoDifferencePattern.Length));
                    parseType     = 3;
                }
                else if (line.StartsWith(IdentifierPositveDictPartialSnapshotCompressions))
                {
                    parseType = 4;
                }
                else if (line.StartsWith(IdentifierNegativeDictPartialSnapshotCompressions))
                {
                    parseType = 5;
                }
                else
                {
                    var splitedLine = line.Split(new[] { '\t' });
                    if (splitedLine.Length < 4)
                    {
                        continue;
                    }

                    switch (parseType)
                    {
                    case 1:     // DifferentUnits
                        ISensoryUnit differentUnit      = SensoryUnit.Parse(splitedLine[2]);
                        int          differentUnitCount = int.Parse(splitedLine[3]);
                        if (!fileActionMemory.DifferentUnits.ContainsKey(differentUnit))
                        {
                            fileActionMemory.DifferentUnits.Add(differentUnit, 0);
                        }
                        fileActionMemory.DifferentUnits[differentUnit] += differentUnitCount;
                        break;

                    case 2:     // NoDifferentUnits
                        ISensoryUnit noDifferentUnit      = SensoryUnit.Parse(splitedLine[2]);
                        int          noDifferentUnitCount = int.Parse(splitedLine[3]);
                        if (!fileActionMemory.NoDifferentUnits.ContainsKey(noDifferentUnit))
                        {
                            fileActionMemory.NoDifferentUnits.Add(noDifferentUnit, 0);
                        }
                        fileActionMemory.NoDifferentUnits[noDifferentUnit] += noDifferentUnitCount;
                        break;

                    case 3:     // DifferentUnits
                        Dictionary <ISensoryPattern, int> dictionarySensoryPatternCount = fileActionMemory.GetNoDifferencePattern(fieldOfVision);
                        ISensoryPattern noDifferentPattern      = SensoryPattern.Parse(splitedLine[2]);
                        int             noDifferentPatternCount = int.Parse(splitedLine[3]);
                        if (!dictionarySensoryPatternCount.ContainsKey(noDifferentPattern))
                        {
                            dictionarySensoryPatternCount.Add(noDifferentPattern, 0);
                        }
                        dictionarySensoryPatternCount[noDifferentPattern] += noDifferentPatternCount;
                        break;

                    case 4:     // PositveDictPartialSnapshotCompressions
                        IPartialSnapshotCompression positveDictPartialSnapshotCompression = PartialSnapshotCompression.Parse(splitedLine[2]);
                        FeedbackCounter             positiveFeedback = FeedbackCounter.Parse(splitedLine[3]) as FeedbackCounter;
                        if (!fileActionMemory.PositveDictPartialSnapshotCompressions.ContainsKey(positveDictPartialSnapshotCompression))
                        {
                            fileActionMemory.PositveDictPartialSnapshotCompressions.Add(positveDictPartialSnapshotCompression, new FeedbackCounter(0, 0));
                        }
                        positiveFeedback += fileActionMemory.PositveDictPartialSnapshotCompressions[positveDictPartialSnapshotCompression] as FeedbackCounter;
                        fileActionMemory.PositveDictPartialSnapshotCompressions[positveDictPartialSnapshotCompression] = positiveFeedback;
                        break;

                    case 5:     // NegativeDictPartialSnapshotCompressions
                        IPartialSnapshotCompression negativeDictPartialSnapshotCompression = PartialSnapshotCompression.Parse(splitedLine[2]);
                        FeedbackCounter             negativeFeedback = FeedbackCounter.Parse(splitedLine[3]) as FeedbackCounter;
                        if (!fileActionMemory.NegativeDictPartialSnapshotCompressions.ContainsKey(negativeDictPartialSnapshotCompression))
                        {
                            fileActionMemory.NegativeDictPartialSnapshotCompressions.Add(negativeDictPartialSnapshotCompression, new FeedbackCounter(0, 0));
                        }
                        negativeFeedback += fileActionMemory.NegativeDictPartialSnapshotCompressions[negativeDictPartialSnapshotCompression] as FeedbackCounter;
                        fileActionMemory.NegativeDictPartialSnapshotCompressions[negativeDictPartialSnapshotCompression] = negativeFeedback;
                        break;
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 18
0
 static public ISensationSnapshot ExtractSnapshot(ISensationSnapshot sensationSnapshot, FieldOfVisionTypes fieldOfVision, DirectionTypes direction)
 {
     if (fieldOfVision == FieldOfVisionTypes.Single)
     {
         Point centerPos      = PuzzleReferee.ConvertToPoint(direction);
         var   resultPatterns = new List <ISensoryPattern>();
         foreach (ISensoryPattern pattern in sensationSnapshot.SensoryPatterns)
         {
             if (direction.Equals(pattern.DirectionType))
             {
                 var   newPattern    = new SensoryPattern(pattern);
                 Point oldPatternPos = PuzzleReferee.ConvertToPoint(newPattern.DirectionType);
                 var   newPatternPos = new Point(oldPatternPos.X - centerPos.X, oldPatternPos.Y - centerPos.Y);
                 newPattern.DirectionType = PuzzleReferee.ConvertToDirectionType(newPatternPos);
                 resultPatterns.Add(newPattern);
             }
         }
         return(new SensationSnapshot(PuzzleReferee.ConvertToDirectionType(centerPos), FieldOfVisionTypes.Single, resultPatterns, false));
     }
     else if (fieldOfVision == FieldOfVisionTypes.ThreeByThree)
     {
         Point centerPos = PuzzleReferee.ConvertToPoint(direction);
         List <DirectionTypes> fieldOfVisionDirections = new List <DirectionTypes>();
         for (int sy = -1; sy < 2; sy++)
         {
             for (int sx = -1; sx < 2; sx++)
             {
                 fieldOfVisionDirections.Add(PuzzleReferee.ConvertToDirectionType(new Point(sx + centerPos.X, sy + centerPos.Y)));
             }
         }
         var resultPatterns = new List <ISensoryPattern>();
         foreach (ISensoryPattern pattern in sensationSnapshot.SensoryPatterns)
         {
             if (fieldOfVisionDirections.Contains(pattern.DirectionType))
             {
                 var   newPattern    = new SensoryPattern(pattern);
                 Point oldPatternPos = PuzzleReferee.ConvertToPoint(newPattern.DirectionType);
                 var   newPatternPos = new Point(oldPatternPos.X - centerPos.X, oldPatternPos.Y - centerPos.Y);
                 newPattern.DirectionType = PuzzleReferee.ConvertToDirectionType(newPatternPos);
                 resultPatterns.Add(newPattern);
             }
         }
         return(new SensationSnapshot(PuzzleReferee.ConvertToDirectionType(centerPos), FieldOfVisionTypes.ThreeByThree, resultPatterns, false));
     }
     throw new NotImplementedException();
 }