Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
Ejemplo n.º 5
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);
        }
Ejemplo n.º 6
0
 public void PollsAreAcceptedInReadyState()
 {
     ActorRefImplicitSenderExtensions.Tell(this._evaluationActorRef, new Poll());
     this.ExpectMsg <Accept>();
 }
Ejemplo n.º 7
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));
        }