Ejemplo n.º 1
0
        private bool CheckForConflicts(ISensationSnapshot snapshot)
        {
            foreach (IActionMemory actionMemory in ActionMemoryDictonary.Values)
            {
                var    percentageForDifferenceByActualSnapshot = actionMemory.CheckForDifferencePattern(snapshot);
                double posibilityForDifference = Math.Min(actionMemory.NegProcentualNoDifference, percentageForDifferenceByActualSnapshot);

                if (posibilityForDifference > 0.0)
                {
                    var positivePartialSnapshotCompression = actionMemory.GetMaxPositivePartialSnapshotCompression(snapshot);
                    var negativePartialSnapshotCompression = actionMemory.GetMaxNegativePartialSnapshotCompression(snapshot);
                    if (positivePartialSnapshotCompression != null && negativePartialSnapshotCompression != null)
                    {
                        var positivPercentage  = actionMemory.GetPositiveFeedbackPercentage(positivePartialSnapshotCompression);
                        var negativePercentage = actionMemory.GetNegativeFeedbackPercentage(negativePartialSnapshotCompression);
                        if (positivPercentage + negativePercentage > 1.5)
                        {
                            RaiseConflictDetected();
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
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.º 4
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.º 5
0
 public SensationResult(ISensationSnapshot before, IPuzzleAction action, ISensationSnapshot after, long feedbackValue, bool saveable = true)
 {
     Id             = -1;
     SnapshotBefore = before;
     Action         = action;
     SnapshotAfter  = after;
     FeedbackValue  = feedbackValue;
 }
Ejemplo n.º 6
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.º 7
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.º 8
0
        public IActionMemoryQuartet FindBestActionMemoryQuartet(Point position)
        {
            Position = position;
            // Look around at special position
            RaiseExperienceWanted();
            ISensationSnapshot sensationSnapshotBeforeAction = _lastSensationSnapshot;

            IActionMemoryQuartet bestActionMemoryQuartet = GetBestActionMemoryQuartet(position, sensationSnapshotBeforeAction);

            return(bestActionMemoryQuartet);
        }
Ejemplo n.º 9
0
        public IPartialSnapshotCompression GetMaxNegativePartialSnapshotCompression(ISensationSnapshot snapshot)
        {
            IPartialSnapshotCompression result = null;
            double maxPercentage = 0.0;

            foreach (KeyValuePair <IPartialSnapshotCompression, IFeedbackCounter> entry in NegativeDictPartialSnapshotCompressions.Where(p => p.Value.PositiveCount > 0))
            {
                // ToDo: Check in given snapshot if partialSnapshotCompression included.
            }

            return(result);
        }
Ejemplo n.º 10
0
        public void TryToLearn()
        {
            ActionFeedback = 0;

            // Zuerst mal die Lage erkunden
            RaiseExperienceWanted();
            ISensationSnapshot sensationSnapshotBeforeAction = _lastSensationSnapshot;

            if (CheckForConflicts(sensationSnapshotBeforeAction))
            {
                return;
            }

            // Nun wird entschieden welche Aktion ausgeführt werden soll
            IPuzzleAction action = GetDecisionByMemory(sensationSnapshotBeforeAction);

            if (action == null)
            {
            }

            // Nun eine Aktion ausführen und Reaktion erkennen
            RaiseActionWanted(action);
            int actionFeedback = ActionFeedback;

            // Wieder die Lage ermitteln und mit dem Zustand zuvor vergleichen
            RaiseExperienceWanted();
            ISensationSnapshot sensationSnapshotAfterAction = _lastSensationSnapshot;

            var           difference   = SensationSnapshot.GetDifferencePatterns(sensationSnapshotBeforeAction, sensationSnapshotAfterAction);
            IActionMemory actionMemory = ActionMemoryDictonary[action];
            bool          isDifferent  = difference.SensoryPatterns.Any();

            actionMemory.RememberDifference(isDifferent, sensationSnapshotBeforeAction);
            if (isDifferent)
            {
                if (actionFeedback != 0)
                {
                    if (actionFeedback < 0)
                    {
                        Console.WriteLine("Error occurred!");
                    }
                    actionMemory.RememberFeedback(actionFeedback, sensationSnapshotBeforeAction);
                    actionMemory.RefreshOverallNegativePscList(ActionMemoryDictonary.Values.ToList());
                }

                _actionFeedbackHistory.Add(actionFeedback);
                while (_actionFeedbackHistory.Count > MAX_ACTION_FEEDBACK_HISTORY_COUNT)
                {
                    _actionFeedbackHistory.RemoveAt(0);
                }
            }
        }
Ejemplo n.º 11
0
        static public ISensationSnapshot GetDifferencePatterns(ISensationSnapshot a, ISensationSnapshot b)
        {
            var result = new SensationSnapshot(a.Direction, a.FieldOfVision, a.SensoryPatterns, false);

            foreach (ISensoryPattern sensoryPattern in b.SensoryPatterns)
            {
                if (a.SensoryPatterns.Contains(sensoryPattern))
                {
                    result.SensoryPatterns.Remove(sensoryPattern);
                }
            }
            result.SensoryPatterns.Sort();
            return(result);
        }
Ejemplo n.º 12
0
        private IActionMemoryQuartet GetBestActionMemoryQuartet(Point position, ISensationSnapshot snapshot)
        {
            // ToDo JK: Find all possible actions for that snapshot
            Dictionary <IPuzzleAction, IActionMemoryQuartet> rangeOfActions = GetRangeOfActions(snapshot, 0.0);

            if (rangeOfActions.Any())
            {
                // ToDo JK: Use FUZZY to choose the best action???
                var orderdByStepSizeActions = rangeOfActions.OrderBy(s => s.Value.StepSize);
                KeyValuePair <IPuzzleAction, IActionMemoryQuartet> bestEntry = orderdByStepSizeActions.LastOrDefault();
                return(bestEntry.Value);
            }

            return(null);
        }
Ejemplo n.º 13
0
        private List <ISensoryPattern> SplitPattern(ISensationSnapshot snapShot, int unitSize)
        {
            var result = new List <ISensoryPattern>();

            foreach (var pattern in snapShot.SensoryPatterns)
            {
                foreach (var splittedPattern in SensoryPattern.Split(pattern))
                {
                    if (splittedPattern.SensoryUnits.Count == unitSize && !result.Contains(splittedPattern))
                    {
                        result.Add(splittedPattern);
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 14
0
        private List <ISensoryUnit> SplitUnits(ISensationSnapshot snapShot)
        {
            var result = new List <ISensoryUnit>();

            foreach (var pattern in snapShot.SensoryPatterns)
            {
                foreach (var unit in SplitUnits(pattern))
                {
                    if (!result.Contains(unit))
                    {
                        result.Add(unit);
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 15
0
        static public Dictionary <ISensoryUnit, int> CountUnits(ISensationSnapshot snapshot)
        {
            var result = new Dictionary <ISensoryUnit, int>();

            foreach (var pattern in snapshot.SensoryPatterns)
            {
                foreach (var unit in pattern.SensoryUnits)
                {
                    if (!result.ContainsKey(unit))
                    {
                        result.Add(unit, 0);
                    }
                    result[unit]++;
                }
            }
            return(result);
        }
Ejemplo n.º 16
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.º 17
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();
 }
Ejemplo n.º 18
0
        public double CheckForDifferencePattern(ISensationSnapshot snapshot)
        {
            List <FieldOfVisionTypes> fieldOfVisions = GetFieldOfVisionsForDifferences();
            double result = 1.0;

            foreach (var fieldOfVision in fieldOfVisions)
            {
                var partialSnapshot = SensationSnapshot.ExtractSnapshot(snapshot, fieldOfVision, Action.Direction);

                foreach (var pattern in SplitPattern(partialSnapshot, 1))
                {
                    if (GetNoDifferencePattern(fieldOfVision).ContainsKey(pattern))
                    {
                        double posibilityForDifference = 1.0 - (double)GetNoDifferencePattern(fieldOfVision)[pattern] / MINIMUM_PATTERN_NO_DIFFERENT_COUNT;
                        result = Math.Min(result, posibilityForDifference);
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 19
0
        private IPuzzleAction GetDecisionByMemory(ISensationSnapshot snapshot)
        {
            // FUZZY-Logic: Entscheiden welcher Modus aktiv sein sollte
            if (_actionFeedbackHistory.Any())
            {
                var percentagePositive = (double)_actionFeedbackHistory.Count(e => e > 0) / _actionFeedbackHistory.Count;
                var percentageNegative = (double)_actionFeedbackHistory.Count(e => e < 0) / _actionFeedbackHistory.Count;
                var percentageNeutral  = (double)_actionFeedbackHistory.Count(e => e == 0) / _actionFeedbackHistory.Count;

                _fillAPixFuzzyLogic.SetValue <FuzzyPositiveHistoryTypes>(percentagePositive);
                _fillAPixFuzzyLogic.SetValue <FuzzyErrorHistoryTypes>(percentageNegative);
                _fillAPixFuzzyLogic.SetValue <FuzzyNeutralHistoryTypes>(percentageNeutral);

                _fillAPixFuzzyLogic.CalculateOutput();
                double learningDegree = _fillAPixFuzzyLogic.GetDegree(FuzzyInteractionModeTypes.Learning);
                double solvingDegree  = _fillAPixFuzzyLogic.GetDegree(FuzzyInteractionModeTypes.Solving);
                double hundredPercent = learningDegree + solvingDegree;
                if (hundredPercent > 0)
                {
                    PercentageSolving = solvingDegree / hundredPercent * 100;
                }
            }

            double positionInRangeByRandom = _random.NextDouble();
            Dictionary <IPuzzleAction, IActionMemoryQuartet> rangeOfActions = GetRangeOfActions(snapshot, RiskFactor);

            foreach (var rangeOfAction in rangeOfActions)
            {
                var stepSize = rangeOfAction.Value.StepSize;
                if (stepSize >= positionInRangeByRandom)
                {
                    return(rangeOfAction.Key);
                }
                positionInRangeByRandom -= stepSize;
            }
            return(null);
        }
Ejemplo n.º 20
0
        private Dictionary <IPuzzleAction, IActionMemoryQuartet> GetRangeOfActions(ISensationSnapshot snapshot, double riskFactor)
        {
            double sumeOfPosibilityForDifference       = 0.0;
            double sumeOfPosibilityForPositiveFeedback = 0.0;
            double sumeOfPosibilityForNegativeFeedback = 0.0;
            Dictionary <IPuzzleAction, double> posibilityForDifferencesByAction      = new Dictionary <IPuzzleAction, double>();
            Dictionary <IPuzzleAction, double> posibilityForPositiveFeedbackByAction = new Dictionary <IPuzzleAction, double>();
            Dictionary <IPuzzleAction, double> posibilityForNegativeFeedbackByAction = new Dictionary <IPuzzleAction, double>();

            foreach (IActionMemory actionMemory in ActionMemoryDictonary.Values)
            {
                var    percentageForDifferenceByActualSnapshot = actionMemory.CheckForDifferencePattern(snapshot);
                double posibilityForDifference = Math.Min(actionMemory.NegProcentualNoDifference, percentageForDifferenceByActualSnapshot);
                sumeOfPosibilityForDifference += posibilityForDifference;
                posibilityForDifferencesByAction.Add(actionMemory.Action, posibilityForDifference);

                if (posibilityForDifference > 0.0)
                {
                    //var positivePartialSnapshotCompression = actionMemory.GetMaxPositivePartialSnapshotCompression(snapshot);
                    //var negativePartialSnapshotCompression = actionMemory.GetMaxNegativePartialSnapshotCompression(snapshot);
                    //if (positivePartialSnapshotCompression != null && negativePartialSnapshotCompression != null)
                    //{
                    //    var positivPercentage = actionMemory.GetPositiveFeedbackPercentage(positivePartialSnapshotCompression);
                    //    var negativePercentage = actionMemory.GetNegativeFeedbackPercentage(negativePartialSnapshotCompression);
                    //    if (positivPercentage + negativePercentage > 1.5)
                    //    {
                    //        RaiseConflictDetected();
                    //    }
                    //}
                    double positiveFeedback = actionMemory.CheckForPositiveFeedback(snapshot);
                    positiveFeedback = Math.Max(actionMemory.NegProcentualNegativeFeedback, positiveFeedback);
                    double negativeFeedback = actionMemory.CheckForNegativeFeedback(snapshot);
                    if (positiveFeedback >= (1.0 - riskFactor) && positiveFeedback > negativeFeedback && negativeFeedback < riskFactor)
                    {
                        negativeFeedback = 0.0;
                    }
                    else if (negativeFeedback > positiveFeedback && negativeFeedback >= riskFactor)
                    {
                        positiveFeedback = 0.0;
                    }

                    if (positiveFeedback < 1.0 - riskFactor)
                    {
                        positiveFeedback = 0.0;
                        if (actionMemory.NegativeFeedbackCount > 0)
                        {
                            negativeFeedback = Math.Max(1.0 - actionMemory.NegProcentualNegativeFeedback, negativeFeedback);
                        }
                    }
                    if (negativeFeedback > riskFactor)
                    {
                        negativeFeedback = 1.0;
                    }

                    sumeOfPosibilityForPositiveFeedback += positiveFeedback;
                    posibilityForPositiveFeedbackByAction.Add(actionMemory.Action, positiveFeedback);

                    sumeOfPosibilityForNegativeFeedback += negativeFeedback;
                    posibilityForNegativeFeedbackByAction.Add(actionMemory.Action, negativeFeedback);
                }
            }

            var    rangeOfActions        = new Dictionary <IPuzzleAction, IActionMemoryQuartet>();
            double rangeSize             = 0.0;
            double positvieMultiplicator = 1.0 + (1.0 - riskFactor) * 100;

            foreach (IActionMemory actionMemory in ActionMemoryDictonary.Values)
            {
                IPuzzleAction        action        = actionMemory.Action;
                IActionMemoryQuartet memoryQuartet = new ActionMemoryQuartet(action);
                if (posibilityForDifferencesByAction.ContainsKey(action))
                {
                    memoryQuartet.Difference = posibilityForDifferencesByAction[action];
                }
                if (posibilityForPositiveFeedbackByAction.ContainsKey(action))
                {
                    memoryQuartet.PositiveFeedback = posibilityForPositiveFeedbackByAction[action];
                }
                if (posibilityForNegativeFeedbackByAction.ContainsKey(action))
                {
                    memoryQuartet.NegativeFeedback = posibilityForNegativeFeedbackByAction[action];
                }
                if (memoryQuartet.PositiveFeedback >= 1.0 && memoryQuartet.NegativeFeedback <= 0.0)
                {
                    memoryQuartet.PositiveFeedback *= positvieMultiplicator;
                }
                var stepSize = memoryQuartet.StepSize;
                if (stepSize > 0.0)
                {
                    rangeSize += stepSize;
                    rangeOfActions.Add(action, memoryQuartet);
                }
            }

            if (!rangeOfActions.Any())
            {
                rangeSize = sumeOfPosibilityForDifference;
                foreach (IActionMemory actionMemory in ActionMemoryDictonary.Values)
                {
                    IActionMemoryQuartet memoryQuartet = new ActionMemoryQuartet(actionMemory.Action);
                    memoryQuartet.Difference = posibilityForDifferencesByAction[actionMemory.Action];
                    rangeOfActions.Add(actionMemory.Action, memoryQuartet);
                }
            }

            foreach (var sizeInRange in rangeOfActions)
            {
                sizeInRange.Value.RangeSize = rangeSize;
            }
            return(rangeOfActions);
        }
Ejemplo n.º 21
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.º 22
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.º 23
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]++;
                        }
                    }
                }
            }
        }