Example #1
0
        public void NewResultIsNotStoredIfOneAlreadyExists()
        {
            // Prepare second result.
            var secondRuntime = ResultStorageActorTest.TestResultRuntime + TimeSpan.FromMilliseconds(1);
            var secondResult  = new TestResult(secondRuntime);

            // Send two result messages containing two different results.
            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(
                        ResultStorageActorTest.Genome,
                        ResultStorageActorTest.TestInstance),
                    ResultStorageActorTest.TestResult));
            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(
                        ResultStorageActorTest.Genome,
                        ResultStorageActorTest.TestInstance),
                    secondResult));

            // Check that the first result's runtime is returned if the genome - instance combination gets requested.
            this._resultStorageActorRef.Tell(new GenomeResultsRequest(ResultStorageActorTest.Genome));
            var isStored = this.ExpectMsg <GenomeResults <TestInstance, TestResult> >().RunResults
                           .TryGetValue(ResultStorageActorTest.TestInstance, out var result);

            isStored.ShouldBeTrue();
            Assert.Equal(
                ResultStorageActorTest.TestResultRuntime,
                result.Runtime);
        }
        /// <summary>
        /// Responds to the provided <paramref name="event"/> passing the <paramref name="event"/> to an Akka.Net actor.
        /// </summary>
        /// <param name="event">The <see cref="HelloWorldRepliedTo"/> to respond to or "handle"</param>
        public void Handle(HelloWorldRepliedTo @event)
        {
            IActorRef item = AggregateResolver.ResolveActor <Actor>();

            // bool result = global::Akka.Actor.Futures.Ask<bool>(item, @event).Result;
            ActorRefImplicitSenderExtensions.Tell(item, @event);
        }
Example #3
0
        public void ResultRequestReturnsCorrectResultForReorderedAgedGenome()
        {
            // Create first genome.
            var genome1 = new Genome(age: 1);

            genome1.SetGene("a", new Allele <int>(1));
            genome1.SetGene("b", new Allele <int>(2));

            // Then create a second one with same gene values, but different age and order.
            var genome2 = new Genome(age: 2);

            genome2.SetGene("b", new Allele <int>(2));
            genome2.SetGene("a", new Allele <int>(1));

            // Store result for first one.
            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(
                        new ImmutableGenome(genome1),
                        ResultStorageActorTest.TestInstance),
                    ResultStorageActorTest.TestResult));

            // But request it for the second one.
            this._resultStorageActorRef.Tell(new GenomeResultsRequest(new ImmutableGenome(genome2)));
            var isStored = this.ExpectMsg <GenomeResults <TestInstance, TestResult> >().RunResults
                           .TryGetValue(ResultStorageActorTest.TestInstance, out var result);

            isStored.ShouldBeTrue();
            result.Runtime.ShouldBe(ResultStorageActorTest.TestResult.Runtime);
        }
Example #4
0
        public void RunResultGetsStoredEvenIfResultForOtherInstanceAlreadyExists()
        {
            // Prepare second instance and result.
            TestInstance secondInstance      = new TestInstance("2");
            var          secondResultRuntime = ResultStorageActorTest.TestResultRuntime + TimeSpan.FromMilliseconds(1);
            var          secondResult        = new TestResult(secondResultRuntime);

            // Send two result messages containing run results.
            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(
                        ResultStorageActorTest.Genome,
                        ResultStorageActorTest.TestInstance),
                    ResultStorageActorTest.TestResult));
            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(
                        ResultStorageActorTest.Genome,
                        secondInstance),
                    secondResult));

            // Check if the runtime of the second result is returned if the genome - instance combination gets
            // requested.
            this._resultStorageActorRef.Tell(new GenomeResultsRequest(ResultStorageActorTest.Genome));
            var genomeResults = this.ExpectMsg <GenomeResults <TestInstance, TestResult> >().RunResults;

            genomeResults.ShouldNotBeNull();
            genomeResults.Count.ShouldBe(2);
            genomeResults.ShouldContainKey(ResultStorageActorTest.TestInstance);
            genomeResults.ShouldContainKey(secondInstance);
        }
Example #5
0
        public void GenomeResultsRequestMayReturnEmptyCollection()
        {
            // Create genome.
            var genome = new ImmutableGenome(new Genome(age: 1));

            // Ask for all results for that genome.
            ActorRefImplicitSenderExtensions.Tell(this._resultStorageActorRef, new GenomeResultsRequest(genome));
            var results = this.ExpectMsg <GenomeResults <TestInstance, TestResult> >();

            // Check no results for that genome have been returned.
            Assert.True(results.RunResults.IsEmpty, "Did not expect any results for the genome.");
        }
Example #6
0
        public void ResultRequestProducesStorageMissEvenIfGenomeIsKnown()
        {
            // Prepare another instance.
            TestInstance secondInstance = new TestInstance("2");

            // Add result for it.
            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(ResultStorageActorTest.Genome, secondInstance),
                    ResultStorageActorTest.TestResult));

            // Send result request and check that a storage miss is returned.
            this._resultStorageActorRef.Tell(new GenomeResultsRequest(ResultStorageActorTest.Genome));
            this.ExpectMsg <GenomeResults <TestInstance, TestResult> >().RunResults.ShouldNotContainKey(ResultStorageActorTest.TestInstance);
        }
Example #7
0
        public void GenomeResultsRequestReturnsAllStoredResultsForGenome()
        {
            // Create genomes.
            var genome1 = new Genome(age: 1);

            genome1.SetGene("a", new Allele <int>(1));
            var genome2 = new Genome(age: 2);

            genome2.SetGene("a", new Allele <int>(2));

            // Create instances.
            var instance1 = new TestInstance("1");
            var instance2 = new TestInstance("2");

            // Create results.
            var result1 = new TestResult(1);
            var result2 = new TestResult(2);
            var result3 = new TestResult(3);

            // Store some results.
            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(new ImmutableGenome(genome1), instance1),
                    result1));
            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(new ImmutableGenome(genome1), instance2),
                    result2));
            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(new ImmutableGenome(genome2), instance1),
                    result3));

            // Ask for all results of one of the genomes.
            ActorRefImplicitSenderExtensions.Tell(this._resultStorageActorRef, new GenomeResultsRequest(new ImmutableGenome(genome1)));
            var results = this.ExpectMsg <GenomeResults <TestInstance, TestResult> >();

            // Check all results for that genome have been returned.
            var instanceResults = new Dictionary <TestInstance, TestResult>(results.RunResults);

            Assert.Equal(2, instanceResults.Count);
            Assert.Equal(result1.Runtime, instanceResults[instance1].Runtime);
            Assert.Equal(result2.Runtime, instanceResults[instance2].Runtime);
        }
Example #8
0
        public void EvaluationStatisticRequestReturnsCorrectNumbers()
        {
            // Create 3 genomes, two with the same values.
            var genome1 = new Genome(age: 1);

            genome1.SetGene("a", new Allele <int>(1));
            var genome2 = new Genome(age: 2);

            genome2.SetGene("a", new Allele <int>(2));
            var genome3 = new Genome(age: 3);

            genome3.SetGene("a", new Allele <int>(2));

            // Create instances.
            var instance1 = new TestInstance("1");
            var instance2 = new TestInstance("2");

            // Store some results.
            var result = new TestResult(TimeSpan.FromMilliseconds(1));

            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(new ImmutableGenome(genome1), instance1),
                    result));
            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(new ImmutableGenome(genome2), instance1),
                    result));
            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(new ImmutableGenome(genome3), instance2),
                    result));

            // Ask for statistic.
            ActorRefImplicitSenderExtensions.Tell(this._resultStorageActorRef, new EvaluationStatisticRequest());
            var results = this.ExpectMsg <EvaluationStatistic>();

            // Check the two identical genome values have been recognized.
            Assert.Equal(
                2,
                results.ConfigurationCount);
            Assert.Equal(3, results.TotalEvaluationCount);
        }
Example #9
0
        public void ResultRequestReturnsCorrectResultForStoredRun()
        {
            // Store the result.
            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(
                        ResultStorageActorTest.Genome,
                        ResultStorageActorTest.TestInstance),
                    ResultStorageActorTest.TestResult));

            // Request it again and compare the runtime to the stored one.
            this._resultStorageActorRef.Tell(new GenomeResultsRequest(ResultStorageActorTest.Genome));
            var isStored = this.ExpectMsg <GenomeResults <TestInstance, TestResult> >().RunResults
                           .TryGetValue(ResultStorageActorTest.TestInstance, out var result);

            isStored.ShouldBeTrue();
            Assert.Equal(
                ResultStorageActorTest.TestResultRuntime,
                result.Runtime);
        }
Example #10
0
        public void RunResultGetsStored()
        {
            // Send result message containing a target algorithm run result.
            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(
                        ResultStorageActorTest.Genome,
                        ResultStorageActorTest.TestInstance),
                    ResultStorageActorTest.TestResult));

            // Check if that one is returned if the genome - instance combination gets requested.
            this._resultStorageActorRef.Tell(new GenomeResultsRequest(ResultStorageActorTest.Genome));
            var isStored = this.ExpectMsg <GenomeResults <TestInstance, TestResult> >().RunResults
                           .TryGetValue(ResultStorageActorTest.TestInstance, out var storedResult);

            isStored.ShouldBeTrue();
            Assert.Equal(
                ResultStorageActorTest.TestResultRuntime,
                storedResult.Runtime);
        }
Example #11
0
 public void PollsAreAcceptedInReadyState()
 {
     ActorRefImplicitSenderExtensions.Tell(this._evaluationActorRef, new Poll());
     this.ExpectMsg <Accept>();
 }
Example #12
0
        public void AllResultsRequestReturnsAllStoredResults()
        {
            // Create genomes.
            var genome1 = new Genome(age: 1);

            genome1.SetGene("a", new Allele <int>(1));
            var genome2 = new Genome(age: 2);

            genome2.SetGene("a", new Allele <int>(2));

            // Create instances.
            var instance1 = new TestInstance("1");
            var instance2 = new TestInstance("2");

            // Create results.
            var result1 = new TestResult(TimeSpan.FromMilliseconds(1));
            var result2 = new TestResult(TimeSpan.FromMilliseconds(2));
            var result3 = new TestResult(TimeSpan.FromMilliseconds(3));

            // Store some results.
            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(new ImmutableGenome(genome1), instance1),
                    result1));
            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(new ImmutableGenome(genome1), instance2),
                    result2));
            ActorRefImplicitSenderExtensions.Tell(
                this._resultStorageActorRef,
                new EvaluationResult <TestInstance, TestResult>(
                    new GenomeInstancePair <TestInstance>(new ImmutableGenome(genome2), instance1),
                    result3));

            // Ask for all results.
            ActorRefImplicitSenderExtensions.Tell(this._resultStorageActorRef, new AllResultsRequest());
            var results = this.ExpectMsg <AllResults <TestInstance, TestResult> >();

            // Check returned results are associated with correct genomes.
            Assert.Equal(2, results.RunResults.Count);
            Assert.True(
                results.RunResults.Keys.Select(g => g.CreateMutableGenome()).Any(g => Tuner.Genomes.Genome.GenomeComparer.Equals(g, genome1)),
                "Expected different genome.");
            Assert.True(
                results.RunResults.Keys.Select(g => g.CreateMutableGenome()).Any(g => Tuner.Genomes.Genome.GenomeComparer.Equals(g, genome2)),
                "Expected different genome.");

            // Check all results have been returned.
            var resultsFirstGenome = new Dictionary <TestInstance, TestResult>(
                results.RunResults.Single(
                    kvp => Tuner.Genomes.Genome.GenomeComparer.Equals(kvp.Key.CreateMutableGenome(), genome1)).Value);

            Assert.Equal(2, resultsFirstGenome.Count);
            Assert.Equal(result1.Runtime, resultsFirstGenome[instance1].Runtime);
            Assert.Equal(result2.Runtime, resultsFirstGenome[instance2].Runtime);
            var resultsSecondGenome = new Dictionary <TestInstance, TestResult>(
                results.RunResults.Single(
                    kvp => Tuner.Genomes.Genome.GenomeComparer.Equals(kvp.Key.CreateMutableGenome(), genome2)).Value);

            Assert.Single(resultsSecondGenome);
            Assert.Equal(result3.Runtime, resultsSecondGenome[instance1].Runtime);
        }
        /// <summary>
        /// Asynchronously tells a message to an <see cref="IActorRef"/>.
        /// </summary>
        /// <param name="receiver">The actor who will receive the message.</param>
        /// <param name="message">The message.</param>
        /// <remarks>Will automatically resolve the current sender using the current <see cref="ActorCell"/>, if any.</remarks>
        public static void TellObservable <TMessage>(this IActorRef receiver, TMessage message) where TMessage : notnull
        {
            var activity = Activity.Current;

            ActorRefImplicitSenderExtensions.Tell(receiver, new ObservableEnvelope <TMessage>(message, activity.Context));
        }