public SenseInputs GetPredictedFutureInput(SenseInputs actualInput, AssociationsLookup associations) { var predictedFutureInput = new SenseInputs(); foreach (var senseInput in actualInput) { double predictedInput = 0; var associationsRelatedToThisInput = associations[senseInput.Key]; var associationTotal = 0.0; foreach (var association in associationsRelatedToThisInput) { if (association.Value > 0) { predictedInput += actualInput[association.Key] * association.Value; associationTotal += association.Value; } } predictedInput = (predictedInput + senseInput.Value) / (associationTotal + 1); predictedFutureInput.Add(senseInput.Key, predictedInput); } return(predictedFutureInput); }
public void MeanSenseInputs_Should_ReturnExpectedMeanValues() { var service = GetService(); var senseInputs1 = new SenseInputs { { "redLight", 0.1 }, { "greenLight", 0.8 }, { "blueLight", 0.1 } }; var senseInputs2 = new SenseInputs { { "redLight", 0.5 }, { "greenLight", 0.2 }, { "blueLight", 0.3 } }; var actual = service.MeanSenseInputs(senseInputs2, senseInputs1); foreach (var keyValuePair in actual) { var expectedValue = (senseInputs1[keyValuePair.Key] + senseInputs2[keyValuePair.Key]) / 2; keyValuePair.Value.Should().Be(expectedValue); } }
public void ManageSenseInputs_Should_ReturnPredictedAndUpdateExisting() { var service = GetService(); var senseInputs = new SenseInputs(); var id = "972f97"; var requestedFuturePredictions = 2; var newInputsWeightFactor = 0.1; var expectedExistingAssociationsLookup = new AssociationsLookup(); var expectedUpdatedAssociationsLookup = new AssociationsLookup(); var expectedFuturePredictedInputs = new List <SenseInputs>(); var expectedLastInputs = new SenseInputs(); var expectedCombinedInputs = new SenseInputs(); mockBrainRepository.Setup(x => x.GetLastSenseInputs(id)).Returns(expectedLastInputs); mockMathsService.Setup(x => x.MeanSenseInputs(senseInputs, expectedLastInputs)).Returns(expectedCombinedInputs); mockBrainRepository.Setup(x => x.GetCurrentAssociationsLookup(id)).Returns(expectedExistingAssociationsLookup); mockUpperBrainService.Setup(x => x.GetFuturePredictedInputs(expectedExistingAssociationsLookup, expectedCombinedInputs, requestedFuturePredictions)).Returns(expectedFuturePredictedInputs); mockUpperBrainService.Setup(x => x.UpdateAssociationsLookup(expectedExistingAssociationsLookup, expectedCombinedInputs, newInputsWeightFactor)).Returns(expectedUpdatedAssociationsLookup); mockUpperBrainService.Setup(x => x.GetNoOfPredictions()).Returns(requestedFuturePredictions); mockUpperBrainService.Setup(x => x.GetNewInputsWeightFactor()).Returns(newInputsWeightFactor); List <SenseInputs> actual = service.ManageSenseInputs(id, senseInputs); mockBrainRepository.Verify(x => x.SaveAssociationsLookup(id, expectedUpdatedAssociationsLookup), Times.Once); actual.Should().BeSameAs(expectedFuturePredictedInputs); }
public void CreateAssociations_Should_ReturnNormalisedProducts() { var service = GetService(); var senseInputs = new SenseInputs { { "red", 5 }, { "green", 2 }, { "blue", 13 } }; var expectedRedNormalisedAssociations = new Associations(new Dictionary <string, double> { { "green", 10 }, { "blue", 65 } }); var expectedGreenNormalisedAssociations = new Associations(new Dictionary <string, double> { { "red", 10 }, { "blue", 26 } }); var expectedBlueNormalisedAssociations = new Associations(new Dictionary <string, double> { { "green", 26 }, { "red", 65 } }); var actual = service.CreateAssociations(senseInputs); actual["red"].Should().BeEquivalentTo(expectedRedNormalisedAssociations); actual["green"].Should().BeEquivalentTo(expectedGreenNormalisedAssociations); actual["blue"].Should().BeEquivalentTo(expectedBlueNormalisedAssociations); }
public AssociationsLookup UpdateAssociationsLookup(AssociationsLookup existingAssociationsLookup, SenseInputs actualInputs, double weightFactor) { var inputAssociationsLookup = brainService.CreateAssociations(actualInputs); var updatedAssociationsLookup = brainService.AddAssociationsLookups(inputAssociationsLookup, existingAssociationsLookup, weightFactor); return(updatedAssociationsLookup); }
public SenseInputs MeanSenseInputs(SenseInputs dictionary1, SenseInputs dictionary2) { var result = AddDictionaries(dictionary1, dictionary2); var senseInputs = new SenseInputs(); foreach (var entry in result) { senseInputs.Add(entry.Key, entry.Value / 2); } return(senseInputs); }
public AssociationsLookup CreateAssociations(SenseInputs senseInputs) { var associations = new AssociationsLookup(); foreach (var senseInput in senseInputs) { var otherSenseInputs = senseInputs.Where(s => s.Key != senseInput.Key); var associationsDictionary = otherSenseInputs .Select(o => new KeyValuePair <string, double>(o.Key, o.Value * senseInput.Value)) .ToDictionary(x => x.Key, x => x.Value); //var normalisedAssociationsDictionary = mathsService.NormaliseAssociations(new Associations(associationsDictionary)); associations.Add(senseInput.Key, new Associations(associationsDictionary)); } return(associations); }
public List <SenseInputs> GetFuturePredictedInputs(AssociationsLookup existingAssociationsLookup, SenseInputs actualInput, int requestedFuturePredictionLength) { var futurePredictedInputs = new List <SenseInputs>(); SenseInputs predictedFutureInput; SenseInputs nextInput = actualInput; for (var i = 0; i < requestedFuturePredictionLength; i++) { predictedFutureInput = brainService.GetPredictedFutureInput(nextInput, existingAssociationsLookup); futurePredictedInputs.Add(predictedFutureInput); nextInput = predictedFutureInput; } return(futurePredictedInputs); }
public void UpdateAssociationsLookup_Should_AddAsExpectedAndSave() { var service = GetService(); var actualInputs = new SenseInputs(); var existingAssociationsLookup = new AssociationsLookup(); var inputAssociationsLookup = new AssociationsLookup(); var combinedNormalisedLookup = new AssociationsLookup(); var weightFactor = 0.2; mockBrainService.Setup(x => x.CreateAssociations(actualInputs)).Returns(inputAssociationsLookup); mockBrainService.Setup(x => x.AddAssociationsLookups(inputAssociationsLookup, existingAssociationsLookup, weightFactor)).Returns(combinedNormalisedLookup); var actual = service.UpdateAssociationsLookup(existingAssociationsLookup, actualInputs, weightFactor); actual.Should().BeSameAs(combinedNormalisedLookup); }
public void GetPredictedFutureInput_should_ReturnExpectedPrediction() { var service = GetService(); var actualInput = new SenseInputs { { "yellow", 20 }, { "volume", 50 }, { "pain", 0 } }; var associations = new AssociationsLookup { { "yellow", new Associations { { "pain", 0.8 }, { "volume", 0.2 }, } }, { "volume", new Associations { { "pain", 0.8 }, { "yellow", 0.2 }, } }, { "pain", new Associations { { "yellow", 0.8 }, { "volume", 0.2 }, } } }; var actual = service.GetPredictedFutureInput(actualInput, associations); actual["yellow"].Should().Be(15.0); actual["volume"].Should().Be(27.0); actual["pain"].Should().Be(13.0); }
public List <SenseInputs> ManageSenseInputs(string id, SenseInputs senseInputs) { var lastInputs = brainRepository.GetLastSenseInputs(id); foreach (var key in lastInputs.Keys) { if (!senseInputs.ContainsKey(key)) { senseInputs.Add(key, 0); } } var combinedInputs = mathsService.MeanSenseInputs(senseInputs, lastInputs); brainRepository.SaveSenseInputs(id, combinedInputs); var requestedFuturePredictions = upperBrainService.GetNoOfPredictions(); var newInputsWeightFactor = upperBrainService.GetNewInputsWeightFactor(); var existingAssociationsLookup = brainRepository.GetCurrentAssociationsLookup(id); var futurePredictedInputs = upperBrainService.GetFuturePredictedInputs(existingAssociationsLookup, combinedInputs, requestedFuturePredictions); var updatedAssociationsLookup = upperBrainService.UpdateAssociationsLookup(existingAssociationsLookup, combinedInputs, newInputsWeightFactor); brainRepository.SaveAssociationsLookup(id, updatedAssociationsLookup); return(futurePredictedInputs); }
public JsonResult Post(string id, [FromBody] SenseInputs senseInputs) { var predicted = memoryService.ManageSenseInputs(id, senseInputs); return(new JsonResult(predicted)); }
public void GetFuturePredictedInputs_Should_ReturnTheExpectedList() { var service = GetService(); var actualInput = new SenseInputs(); var requestedFuturePredictionLength = 3; var currentAssociationsLookup = new AssociationsLookup { { "key1", new Associations { { "key2", 0.1 }, { "key3", 0.9 } } }, { "key2", new Associations { { "key1", 0.5 }, { "key3", 0.5 } } }, { "key3", new Associations { { "key1", 0.3 }, { "key2", 0.7 } } } }; var expectedFutureInput1 = new SenseInputs { { "1", 0.1 } }; var expectedFutureInput2 = new SenseInputs { { "2", 0.2 } }; var expectedFutureInput3 = new SenseInputs { { "3", 0.3 } }; mockBrainService.Setup(x => x.GetPredictedFutureInput(actualInput, currentAssociationsLookup)) .Returns(expectedFutureInput1); mockBrainService.Setup(x => x.GetPredictedFutureInput(expectedFutureInput1, currentAssociationsLookup)) .Returns(expectedFutureInput2); mockBrainService.Setup(x => x.GetPredictedFutureInput(expectedFutureInput2, currentAssociationsLookup)) .Returns(expectedFutureInput3); List <SenseInputs> actualPredictedFuture = service.GetFuturePredictedInputs(currentAssociationsLookup, actualInput, requestedFuturePredictionLength); actualPredictedFuture[0].Should().BeSameAs(expectedFutureInput1); actualPredictedFuture[1].Should().BeSameAs(expectedFutureInput2); actualPredictedFuture[2].Should().BeSameAs(expectedFutureInput3); }
public void SaveSenseInputs(string id, SenseInputs senseInputs) { _lastSenseInputsById[id] = senseInputs; }