public SensationResult(ISensationSnapshot before, IPuzzleAction action, ISensationSnapshot after, long feedbackValue, bool saveable = true) { Id = -1; SnapshotBefore = before; Action = action; SnapshotAfter = after; FeedbackValue = feedbackValue; }
public ActionMemory(IPuzzleAction action) { Action = action; foreach (FieldOfVisionTypes fieldOfVision in Enum.GetValues(typeof(FieldOfVisionTypes))) { _noDifferencePatternDictonary.Add(fieldOfVision, new Dictionary <ISensoryPattern, int>()); } }
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); } } }
public FileActionMemory(IPuzzleAction action) : base(action) { }
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); }
public PuzzleAction(IPuzzleAction action) { Id = action.Id; Type = action.Type; Direction = action.Direction; }
public ActionMemoryQuartet(IPuzzleAction action) { Action = action; }
private void RaiseActionWanted(IPuzzleAction action) { ActionWanted?.Invoke(this, new ActionWantedEventArgs(action)); }
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); }
public ActionWantedEventArgs(IPuzzleAction action) { Action = action; }