public void CreateInstanceUpdateMessagesWorksForManyInstances()
        {
            int numberInstances = (4 * UpdateInstances.MaximumInstanceChunkSize) + 7;
            var messages        = UpdateInstances.CreateInstanceUpdateMessages(UpdateInstancesTest.CreateInstances(numberInstances)).ToList();

            Assert.True(
                7 == messages.Count,
                "There should be seven messages: One to clear old instances, five to add new ones, and one to complete the instance update.");
            Assert.True(messages[0] is ClearInstances, "The first message should clear old instances.");

            for (int i = 1; i <= 4; i++)
            {
                Assert.True(messages[i] is AddInstances <TestInstance>, $"The {i}th message should add new instances.");
                UpdateInstancesTest.CheckAddMessageContainsCorrectInstances(
                    (AddInstances <TestInstance>)messages[i],
                    firstInstanceIdentifier: (i - 1) * UpdateInstances.MaximumInstanceChunkSize,
                    numberInstances: UpdateInstances.MaximumInstanceChunkSize);
            }

            Assert.True(
                messages[5] is AddInstances <TestInstance>,
                "The fifth message should add the remaining instances.");
            UpdateInstancesTest.CheckAddMessageContainsCorrectInstances(
                (AddInstances <TestInstance>)messages[5],
                firstInstanceIdentifier: 4 * UpdateInstances.MaximumInstanceChunkSize,
                numberInstances: 7);

            Assert.True(messages[6] is InstanceUpdateFinished, "The seventh message should complete the update process.");
        }
        /// <summary>
        /// Additional preparations after <see cref="GenomeEvaluationDelegatorBase{TInstance, TResult}.OpenGenomeEvaluations" /> are set, but no evaluators have been polled
        /// yet.
        /// </summary>
        protected override void PrepareWork()
        {
            foreach (var message in UpdateInstances.CreateInstanceUpdateMessages(this._sortCommand.Instances))
            {
                this.EvaluationActorRouter.Tell(message);
            }

            base.PrepareWork();
        }
        public void CreateInstanceUpdateMessagesWorksForFewInstances()
        {
            var messages = UpdateInstances.CreateInstanceUpdateMessages(UpdateInstancesTest.CreateInstances(number: 2)).ToList();

            Assert.True(
                3 == messages.Count,
                "There should be two messages: One to clear old instances, one to add new ones, and one to complete the instance update.");
            Assert.True(messages[0] is ClearInstances, "The first message should clear old instances.");
            Assert.True(messages[1] is AddInstances <TestInstance>, "The second message should add new instances.");
            Assert.True(messages[2] is InstanceUpdateFinished, "The third message should complete the update process.");

            UpdateInstancesTest.CheckAddMessageContainsCorrectInstances(
                (AddInstances <TestInstance>)messages[1],
                firstInstanceIdentifier: 0,
                numberInstances: 2);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Actor is ready to process mini tournaments.
        /// </summary>
        protected override void Ready()
        {
            // Polls are accepted.
            this.Receive <Poll>(poll => this.Sender.Tell(new Accept()));

            // Switch to working state if a mini tournament was sent.
            this.Receive <MiniTournament>(
                tournament =>
            {
                this._miniTournament = tournament;
                this.CommandIssuer   = this.Sender;
                this.BecomeWorking();
            });

            // Switch to waiting for configuration state if instances are reset.
            this.Receive <ClearInstances>(
                update =>
            {
                this._instancesForEvaluation.Clear();
                this.EvaluationActorRouter.Tell(new Broadcast(update));
                this.Become(this.WaitForInstances);
            });

            // Instance updates without earlier reset indicate missing messages.
            this.Receive <AddInstances <TInstance> >(
                update =>
            {
                this.Sender.Tell(new InstancesRequest());
                this.Become(this.WaitForInstances);
            });
            this.Receive <InstanceUpdateFinished>(
                update =>
            {
                this.Sender.Tell(new InstancesRequest());
                this.Become(this.WaitForInstances);
            });

            // Evaluation actors might ask for instances.
            this.Receive <InstancesRequest>(
                request =>
            {
                foreach (var message in UpdateInstances.CreateInstanceUpdateMessages(this.InstancesForEvaluation))
                {
                    this.Sender.Tell(message);
                }
            });
        }
        public void CreateInstanceUpdateMessagesWorksForEdgeCases()
        {
            int numberInstances = 2 * UpdateInstances.MaximumInstanceChunkSize;
            var messages        = UpdateInstances.CreateInstanceUpdateMessages(UpdateInstancesTest.CreateInstances(numberInstances)).ToList();

            Assert.True(
                4 == messages.Count,
                "There should be four messages: One to clear old instances, two to add new ones, and one to complete the instance update.");
            Assert.True(messages[0] is ClearInstances, "The first message should clear old instances.");

            for (int i = 1; i <= 2; i++)
            {
                Assert.True(messages[i] is AddInstances <TestInstance>, $"The {i}th message should add new instances.");
                UpdateInstancesTest.CheckAddMessageContainsCorrectInstances(
                    (AddInstances <TestInstance>)messages[i],
                    firstInstanceIdentifier: (i - 1) * UpdateInstances.MaximumInstanceChunkSize,
                    numberInstances: UpdateInstances.MaximumInstanceChunkSize);
            }

            Assert.True(messages[3] is InstanceUpdateFinished, "The fourth message should complete the update process.");
        }
Exemplo n.º 6
0
 void MainWin_UpdateInstanceList(object sender, UpdateInstancesEventArgs e)
 {
     if (this.InvokeRequired)
     {
         var invokedMethod = new UpdateInstances(MainWin_UpdateInstanceList);
         this.Invoke(invokedMethod, sender, e);
         return;
     }
     this.IdentitySelectComBox.Items.Clear();
     if (e.Instances.Count == 0)
     {
         this.IdentitySelectComBox.Items.Add("(new)");
         this.IdentitySelectComBox.SelectedIndex = 0;
     }
     else
     {
         foreach (var item in e.Instances)
         {
             this.IdentitySelectComBox.Items.Add(item.Name);
         }
         this.IdentitySelectComBox.SelectedIndex = e.SelectedIndex;
     }
 }
 /// <summary>
 /// Creates all required messages to update evaluation instances.
 /// </summary>
 /// <returns>All required messages to update evaluation instances.</returns>
 public IEnumerable <object> CreateInstanceUpdateMessages()
 {
     return(UpdateInstances.CreateInstanceUpdateMessages(this._selectCommand.Instances));
 }
Exemplo n.º 8
0
        /// <summary>
        /// Actor is working on a specific command.
        /// </summary>
        protected virtual void Working()
        {
            // If an actor is interested in the current instances, tell him and check whether he is available for
            // working now.
            this.Receive <InstancesRequest>(
                request =>
            {
                foreach (var message in UpdateInstances.CreateInstanceUpdateMessages(this.InstancesForEvaluation))
                {
                    this.Sender.Tell(message);
                }

                this.Sender.Tell(new Poll());
            });

            // If poll was accepted and open list is not empty, assign a genome evaluation.
            this.Receive <Accept>(
                accepted =>
            {
                var nextGenomeEvaluation = this.OpenGenomeEvaluations[0];
                this.OpenGenomeEvaluations.RemoveAt(0);

                this.PrepareForEvaluation(this.Sender, nextGenomeEvaluation);
                this.Sender.Tell(nextGenomeEvaluation);

                this.AssignedEvaluations.Add(this.Sender, nextGenomeEvaluation);
            },
                accepted => this.OpenGenomeEvaluations.Any() &&
                !this.AssignedEvaluations.ContainsKey(this.Sender));

            // New results are added to dictionary.
            this.Receive <PartialGenomeEvaluationResults <TResult> >(
                results =>
            {
                if (!this.CurrentRunResults.TryGetValue(results.EvaluationId, out var resultsSoFar))
                {
                    resultsSoFar = new List <TResult>();
                    this.CurrentRunResults.Add(results.EvaluationId, resultsSoFar);
                }

                resultsSoFar.AddRange(results.RunResults);
            });

            // Handle a finished evaluation by modifying fields accordingly
            // and either polling for evaluators again or leaving the working state.
            this.Receive <GenomeEvaluationFinished>(
                finishedMessage =>
            {
                var receivedResultNumber = this.CurrentRunResults.ContainsKey(finishedMessage.EvaluationId)
                                                       ? this.CurrentRunResults[finishedMessage.EvaluationId].Count
                                                       : 0;
                if (receivedResultNumber != finishedMessage.ExpectedResultCount)
                {
                    this.CurrentRunResults.Remove(finishedMessage.EvaluationId);
                    this.OpenGenomeEvaluations.Add(this.AssignedEvaluations[this.Sender]);
                    LoggingHelper.WriteLine(
                        VerbosityLevel.Warn,
                        $"Redo evaluation because we received {receivedResultNumber} instead of {finishedMessage.ExpectedResultCount} evaluation results from {this.Sender}.");
                }

                this.AssignedEvaluations.Remove(this.Sender);
                this.ReceivedGenomeEvaluationFinishedMessage(this.Sender, finishedMessage);

                var done = !this.OpenGenomeEvaluations.Any() && !this.AssignedEvaluations.Any();
                if (done)
                {
                    this.BecomeReady();
                }
                else
                {
                    this.AskForEvaluators();
                }
            });
        }