Beispiel #1
0
        private static async Task GetAndPostOrganismsAsync(StartTrainingSessionResponse startTrainingSessionResponse,
                                                           SslTcpNetworkConnector networkConnector, MessageListener <GetOrganismsResponse> getOrganismsResponseListener,
                                                           MessageListener <PostOrganismsScoreResponse> postOrganismsScoreResponseListener)
        {
            GetOrganismsRequest getOrganismsRequest =
                new GetOrganismsRequest(startTrainingSessionResponse.TrainingSession.Id, 10);
            await networkConnector.SendMessageAsync(getOrganismsRequest, CancellationToken.None);

            GetOrganismsResponse getOrganismsResponse =
                await getOrganismsResponseListener.ReceiveMessageAsync(CancellationToken.None);

            Console.WriteLine(
                $"GetOrganismsResponse: \n\tId: {getOrganismsResponse.Id}\n\tRequestId: {getOrganismsResponse.RequestId}\n\tDateTime: {getOrganismsResponse.DateTime}\n\tMessage: {getOrganismsResponse.Message}\n\tSuccess: {getOrganismsResponse.Success}\n\tOrganismsCount: {getOrganismsResponse.Organisms.Count()}\n\tConnectionGenesCount: {getOrganismsResponse.Organisms.Sum(o => o.ConnectionGenes.Count)}");

            Dictionary <Guid, double> organismsScoreDictionary = new Dictionary <Guid, double>();

            foreach (OrganismDto organism in getOrganismsResponse.Organisms)
            {
                organism.Score += Random.NextDouble() + 0.001;
                organismsScoreDictionary.Add(organism.Id, organism.Score);
            }

            PostOrganismsScoreRequest postOrganismsScoreRequest =
                new PostOrganismsScoreRequest(startTrainingSessionResponse.TrainingSession.Id, organismsScoreDictionary);
            await networkConnector.SendMessageAsync(postOrganismsScoreRequest, CancellationToken.None);

            PostOrganismsScoreResponse postOrganismsScoreResponse =
                await postOrganismsScoreResponseListener.ReceiveMessageAsync(CancellationToken.None);

            Console.WriteLine(
                $"PostOrganismsScoreResponse: \n\tId: {postOrganismsScoreResponse.Id}\n\tRequestId: {postOrganismsScoreResponse.RequestId}\n\tDateTime: {postOrganismsScoreResponse.DateTime}\n\tMessage: {postOrganismsScoreResponse.Message}\n\tSuccess: {postOrganismsScoreResponse.Success}");
        }
Beispiel #2
0
        public async Task Run_For_5_Generations()
        {
            StartTrainingSessionRequest startTrainingSessionRequest = new StartTrainingSessionRequest
            {
                Id             = Guid.NewGuid(),
                DateTime       = DateTime.Now,
                TrainingRoomId = TrainingRoomId,
                UserId         = UserId
            };
            StartTrainingSessionResponse startTrainingSessionResponse = await TrainingSessionService.StartTrainingSessionAsync(startTrainingSessionRequest);

            Guid trainingSessionId = startTrainingSessionResponse.TrainingSession.Id;

            int generations = 5;
            PostOrganismsScoreResponse postOrganismsScoreResponse = default;

            for (int i = 1; i < generations; i++)
            {
                GetOrganismsRequest  getOrganismsRequest  = GetOrganismsRequest(trainingSessionId);
                GetOrganismsResponse getOrganismsResponse = await TrainingSessionService.GetOrganismsAsync(getOrganismsRequest);

                PostOrganismsScoreRequest postOrganismsScoreRequest = PostOrganismsScoreRequest(trainingSessionId, getOrganismsResponse);
                postOrganismsScoreResponse = await TrainingSessionService.PostOrganismsScoreAsync(postOrganismsScoreRequest);
            }

            Assert.IsTrue(postOrganismsScoreResponse.Success);
            Assert.AreEqual("Successfully updated the organisms and advanced a generation!", postOrganismsScoreResponse.Message);
        }
        /// <inheritdoc cref="ITrainingSessionService.PostOrganismsScoreAsync(PostOrganismsScoreRequest)"/>
        public async Task <PostOrganismsScoreResponse> PostOrganismsScoreAsync(PostOrganismsScoreRequest postOrganismsScoreRequest)
        {
            TrainingSession trainingSession = await _trainingSessionRepository.FindSingleOrDefaultAsync(p => p.Id.Equals(postOrganismsScoreRequest.TrainingSessionId));

            if (trainingSession == default)
            {
                return(new PostOrganismsScoreResponse(postOrganismsScoreRequest.Id, "Training session does not exist."));
            }
            int count = 0;

            List <LeasedOrganism> orgs = postOrganismsScoreRequest.OrganismScores
                                         .Select(o =>
            {
                LeasedOrganism oo = trainingSession.LeasedOrganisms.SingleOrDefault(a => a.OrganismId.Equals(o.Key));
                if (oo == default)
                {
                    count++;
                }
                return(oo);
            }).ToList();

            if (count > 0)
            {
                return(new PostOrganismsScoreResponse(postOrganismsScoreRequest.Id, $"{count} of the organisms does not exist in the training session."));
            }

            foreach (LeasedOrganism leasedOrganism in orgs)
            {
                leasedOrganism.Organism.Score       = postOrganismsScoreRequest.OrganismScores.Find(o => o.Key == leasedOrganism.OrganismId).Value;
                leasedOrganism.Organism.IsEvaluated = true;
            }

            await _trainingSessionRepository.SaveChangesAsync();

            _logger.LogInformation("UPDATED ORGANISM SCORES!!!");

            string message = "Successfully updated the organisms scores.";

            if (trainingSession.TrainingRoom.AllOrganismsInCurrentGenerationAreEvaluated())
            {
                trainingSession.LeasedOrganisms.Clear();
                await _trainingSessionRepository.SaveChangesAsync();

                _logger.LogInformation("CLEARED LEASED ORGANISMS AND SAVED CHANGES!!!");

                trainingSession.TrainingRoom.EndGeneration(
                    (organism) => _trainingSessionRepository.MarkOrganismForRemoval(organism),
                    (species) => _trainingSessionRepository.MarkSpeciesForRemoval(species));

                message = "Successfully updated the organisms and advanced a generation!";

                await _trainingSessionRepository.UpdateOrganismsAsync(trainingSession);

                _logger.LogInformation("ENDED THE GENERATION!!!");
            }
            return(new PostOrganismsScoreResponse(postOrganismsScoreRequest.Id, message, true));
        }
Beispiel #4
0
        public async Task <IActionResult> OrganismsAsync(PostOrganismsScoreRequest postOrganismsScoreRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState.GetErrorMessages()));
            }
            PostOrganismsScoreResponse response = await _trainingSessionService.PostOrganismsScoreAsync(postOrganismsScoreRequest);

            return(new OkObjectResult(response));
        }
        /// <inheritdoc cref="ITrainingRoomService.PostOrganismsScoreAsync(PostOrganismsScoreRequest)"/>
        public async Task <PostOrganismsScoreResponse> PostOrganismsScoreAsync(PostOrganismsScoreRequest postOrganismsScoreRequest)
        {
            TrainingSession trainingSession = await _trainingSessionRepository.FindSingleOrDefaultAsync(p => p.Id.Equals(postOrganismsScoreRequest.TrainingSessionId));

            if (trainingSession == default)
            {
                return(new PostOrganismsScoreResponse(postOrganismsScoreRequest.Id, "Training session does not exist."));
            }
            int count = 0;

            List <LeasedOrganism> orgs = postOrganismsScoreRequest.OrganismScores
                                         .Select(o =>
            {
                LeasedOrganism oo = trainingSession.LeasedOrganisms.SingleOrDefault(a => a.OrganismId.Equals(o.Key));
                if (oo == default)
                {
                    count++;
                }
                return(oo);
            }).ToList();

            if (count > 0)
            {
                return(new PostOrganismsScoreResponse(postOrganismsScoreRequest.Id, $"{count} of the organisms does not exist in the training session."));
            }

            foreach (LeasedOrganism leasedOrganism in orgs)
            {
                trainingSession.TrainingRoom.PostScore(leasedOrganism.Organism, postOrganismsScoreRequest.OrganismScores[leasedOrganism.OrganismId]);
            }

            string message = "Successfully updated the organisms scores.";

            if (trainingSession.TrainingRoom.Species.SelectMany(p => p.Organisms).All(lo => lo.Evaluated))
            {
                message = trainingSession.TrainingRoom.EndGeneration()
                    ? "Successfully updated the organisms and advanced a generation!"
                    : "Successfully updated the organisms but failed to advance a generation!";
                trainingSession.LeasedOrganisms.Clear();
            }
            await _trainingSessionRepository.UpdateAsync(trainingSession);

            return(new PostOrganismsScoreResponse(postOrganismsScoreRequest.Id, message, true));
        }
Beispiel #6
0
        public async Task PostOrganismsAsync_Should_Successfully_Start_A_New_Generation()
        {
            StartTrainingSessionRequest startTrainingSessionRequest = new StartTrainingSessionRequest
            {
                Id             = Guid.NewGuid(),
                DateTime       = DateTime.Now,
                TrainingRoomId = TrainingRoomId,
                UserId         = UserId
            };
            StartTrainingSessionResponse startTrainingSessionResponse = await TrainingSessionService.StartTrainingSessionAsync(startTrainingSessionRequest);

            Guid trainingSessionId = startTrainingSessionResponse.TrainingSession.Id;

            GetOrganismsRequest  getOrganismsRequest  = GetOrganismsRequest(trainingSessionId);
            GetOrganismsResponse getOrganismsResponse = await TrainingSessionService.GetOrganismsAsync(getOrganismsRequest);

            PostOrganismsScoreRequest  postOrganismsScoreRequest  = PostOrganismsScoreRequest(trainingSessionId, getOrganismsResponse);
            PostOrganismsScoreResponse postOrganismsScoreResponse = await TrainingSessionService.PostOrganismsScoreAsync(postOrganismsScoreRequest);

            Assert.IsTrue(postOrganismsScoreResponse.Success);
            Assert.AreEqual("Successfully updated the organisms and advanced a generation!", postOrganismsScoreResponse.Message);
        }