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 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 AssociationsLookup UpdateAssociationsLookup(AssociationsLookup existingAssociationsLookup, SenseInputs actualInputs, double weightFactor) { var inputAssociationsLookup = brainService.CreateAssociations(actualInputs); var updatedAssociationsLookup = brainService.AddAssociationsLookups(inputAssociationsLookup, existingAssociationsLookup, weightFactor); return(updatedAssociationsLookup); }
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 void AddAndNormaliseAssociationsLookups_Should_ReturnExpected() { var service = GetService(); var associationsLookup1 = new AssociationsLookup(); var associationsLookup2 = new AssociationsLookup(); var weightFactor = 0.5; var expectedAssociationsLookup = new AssociationsLookup(); mockMathsService.Setup(x => x.AddAssociationLookups(associationsLookup1, associationsLookup2, weightFactor)) .Returns(expectedAssociationsLookup); var actual = service.AddAssociationsLookups(associationsLookup1, associationsLookup2, weightFactor); actual.Should().BeSameAs(expectedAssociationsLookup); }
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); }
/// <summary> /// Associations1 will be multiplied by the weightFactor then added to associations2. The result will be normalised. /// </summary> /// <param name="associations1"></param> /// <param name="associations2"></param> /// <param name="weightFactor"></param> /// <returns></returns> public AssociationsLookup AddAssociationLookups(AssociationsLookup associationsLookup1, AssociationsLookup associationsLookup2, double weightFactor) { var otherWeightFactor = 1 - weightFactor; var combinedAssociationsLookup = new AssociationsLookup(); foreach (var associationEntry in associationsLookup1) { var key = associationEntry.Key; var associations = new Associations(); var weightedAssociations1 = associationEntry.Value.Select(x => new KeyValuePair <string, double>(x.Key, x.Value * weightFactor)); var associations2 = associationsLookup2[key]; var combinedAssociations = weightedAssociations1.ToDictionary(x => x.Key, x => x.Value + associations2[x.Key] * otherWeightFactor); combinedAssociationsLookup.Add(key, new Associations(combinedAssociations)); } return(combinedAssociationsLookup); }
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 AssociationsLookup AddAssociationsLookups(AssociationsLookup lookup1, AssociationsLookup lookup2, double weightFactor) { var combinedLookups = mathsService.AddAssociationLookups(lookup1, lookup2, weightFactor); return(combinedLookups); }
public void AddAssociations_Should_AddUsingWeight() { var service = GetService(); var associations1 = new AssociationsLookup { { "red", new Associations { { "blue", 0.1 }, { "green", 0.9 } } }, { "blue", new Associations { { "red", 0.1 }, { "green", 0.9 } } }, { "green", new Associations { { "red", 0.9 }, { "blue", 0.1 } } } }; var associations2 = new AssociationsLookup { { "red", new Associations { { "blue", 0.5 }, { "green", 0.5 } } }, { "blue", new Associations { { "red", 0.5 }, { "green", 0.5 } } }, { "green", new Associations { { "red", 0.5 }, { "blue", 0.5 } } } }; var weightFactor = 0.1; AssociationsLookup actual = service.AddAssociationLookups(associations1, associations2, weightFactor); actual["red"]["blue"].Should().Be(0.46); actual["red"]["green"].Should().Be(0.54); actual["blue"]["red"].Should().Be(0.46); actual["blue"]["green"].Should().Be(0.54); actual["green"]["red"].Should().Be(0.54); actual["green"]["blue"].Should().Be(0.46); }
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 SaveAssociationsLookup(string id, AssociationsLookup associationsLookup) { _associationsLookupById[id] = associationsLookup; }