public override Organism Clone(TrainingRoomSettings trainingRoomSettings) { // Prepares a new id for the organisms to clone with. Guid newGuid = Guid.NewGuid(); // Create a new organism with the given node id and training room settings. return(new EvaluatableOrganism(newGuid, trainingRoomSettings, Generation, ConnectionGenes.Select(gene => gene.Clone(newGuid)).ToList())); //TODO: When inputs can mutate they should also be cloned }
public void Initialize() { _user = new User() { Id = Guid.NewGuid(), Username = "******" }; _trainingRoomSettings = new TrainingRoomSettings(0, 2, 1, 1, 1, 0.4, 3, 0.05, 0.03, 0.75, 0.001, 1, 0.8, 0.1, 0.5, 0.25, 0); _trainingRoom = new TrainingRoom(_user, "CoolRoom", _trainingRoomSettings); }
public EvaluatableOrganism(Guid id, TrainingRoomSettings trainingRoomSettings, uint generation, List <ConnectionGene> connectionGenes) { Id = id; Generation = generation; ConnectionGenes = new List <ConnectionGene>(connectionGenes); if (connectionGenes.Any()) { _maxInnovation = ConnectionGenes.Max(p => p.InnovationNumber); } GenerateInputAndOutputNodes(trainingRoomSettings); Name = GenerateName(trainingRoomSettings.Random.Next); }
protected override void GenerateInputAndOutputNodes(TrainingRoomSettings trainingRoomSettings) { Inputs = new List <OrganismInputNode>((int)trainingRoomSettings.InputCount); for (uint nodeIdentifier = 0; nodeIdentifier < trainingRoomSettings.InputCount; nodeIdentifier++) { Inputs.Add(new OrganismInputNode(this, new EvaluatableInputNode(nodeIdentifier))); } Outputs = new List <OrganismOutputNode>((int)trainingRoomSettings.OutputCount); for (uint nodeIdentifier = 0; nodeIdentifier < trainingRoomSettings.OutputCount; nodeIdentifier++) { Outputs.Add(new OrganismOutputNode(this, new EvaluatableOutputNode(nodeIdentifier + trainingRoomSettings.InputCount))); } }
public void Initialize() { _user = new User() { Id = Guid.NewGuid(), Username = "******" }; Guid trainingRoomId = Guid.NewGuid(); _organismFactory = new OrganismFactory(); _trainingRoomSettings = new TrainingRoomSettings(trainingRoomId, 0, 2, 1, 1, 1, 0.4, 3, 0.05, 0.03, 0.75, 0.001, 1, 0.8, 0.1, 0.5, 0.25, 0, 15, 5); _trainingRoom = new TrainingRoom(trainingRoomId, _user, "CoolRoom", _trainingRoomSettings, _organismFactory); }
public EvaluatableOrganism(uint generation, TrainingRoomSettings trainingRoomSettings) { Id = Guid.NewGuid(); // Sets the current generation. Generation = generation; // Initializes the list of connection genes. ConnectionGenes = new List <ConnectionGene>(); // Initializes the Input & Output lists for the many to many relations. GenerateInputAndOutputNodes(trainingRoomSettings); // Generates a random name for the organism. Name = GenerateName(trainingRoomSettings.Random.Next); }
public void Initialize() { _fakeUser = new User(); Guid trainingRoomId = Guid.NewGuid(); _roomName = "CoolRoom"; //Create a training room with really high mutation settings TrainingRoomSettings trainingRoomSettings = new TrainingRoomSettings(trainingRoomId: trainingRoomId, organismCount: 25, inputCount: 3, outputCount: 1, c1: 1, c2: 1, c3: 0.4, threshold: 3, addConnectionChance: 1, addNodeChance: 1, crossOverChance: 0.75, interSpeciesChance: 0.001, mutationChance: 1, mutateWeightChance: 0.8, weightReassignChance: 0.1, topAmountToSurvive: 0.5, enableConnectionChance: 0.25, seed: 1, maxStagnantTime: 15, championCloneMinSpeciesSize: 5); _organismFactory = new EvaluatableOrganismFactory(); _trainingRoom = new TrainingRoom(trainingRoomId, _fakeUser, _roomName, trainingRoomSettings, _organismFactory); for (int i = 0; i < trainingRoomSettings.OrganismCount; i++) { EvaluatableOrganism organism = new EvaluatableOrganism(trainingRoomSettings, _trainingRoom.GetInnovationNumber) { IsLeased = true }; _trainingRoom.AddOrganism(organism); } _trainingRoom.IncreaseNodeIdTo(trainingRoomSettings.InputCount + trainingRoomSettings.OutputCount); }
/// <inheritdoc cref="ITrainingSessionService.GetOrganismsAsync(GetOrganismsRequest)"/> public async Task <GetOrganismsResponse> GetOrganismsAsync(GetOrganismsRequest getOrganismsRequest) { string message = "Successfully fetched all requested organisms."; TrainingSession trainingSession; if (getOrganismsRequest.Amount < 1) { return(new GetOrganismsResponse(getOrganismsRequest.Id, new List <OrganismDto>(), "Amount cannot be smaller than 1.")); } if ((trainingSession = await EntityRepository.FindSingleOrDefaultAsync(ts => ts.Id.Equals(getOrganismsRequest.TrainingSessionId))) == default) { return(new GetOrganismsResponse(getOrganismsRequest.Id, new List <OrganismDto>(), "Training session does not exist.")); } if (!trainingSession.TrainingRoom.IsUserAuthorized(getOrganismsRequest.UserId) || trainingSession.UserId != getOrganismsRequest.UserId) { return(new GetOrganismsResponse(getOrganismsRequest.Id, new List <OrganismDto>(), "User is not authorized.")); } if (trainingSession.EndedTimestamp != default) { return(new GetOrganismsResponse(getOrganismsRequest.Id, new List <OrganismDto>(), "Training session has ended and can not be used any more.")); } TrainingRoomSettings trainingRoomSettings = trainingSession.TrainingRoom.TrainingRoomSettings; // if the list is empty then get new ones from the training room if (trainingSession.LeasedOrganisms.Count(o => !o.Organism.IsEvaluated) == 0) { if (trainingSession.TrainingRoom.Generation == 0) { for (int i = 0; i < trainingRoomSettings.OrganismCount; i++) { Organism organism = new Organism(trainingRoomSettings, trainingSession.TrainingRoom.GetInnovationNumber) { IsLeased = true }; trainingSession.TrainingRoom.AddOrganism(organism); trainingSession.LeasedOrganisms.Add(new LeasedOrganism(organism, trainingSession.Id)); } trainingSession.TrainingRoom.IncreaseNodeIdTo(trainingRoomSettings.InputCount + trainingRoomSettings.OutputCount); message = $"First generation; generated {trainingSession.TrainingRoom.TrainingRoomSettings.OrganismCount} organisms."; await _trainingSessionRepository.InsertFirstGenerationAsync(trainingSession); } else { trainingSession.LeasedOrganisms.AddRange(GetNewLeasedOrganisms(getOrganismsRequest.Amount)); message = "Start of new generation."; await _trainingSessionRepository.InsertLeasedOrganismsAsync(trainingSession); } } else if (trainingSession.LeasedOrganisms.Count(o => !o.Organism.IsEvaluated) < getOrganismsRequest.Amount) { int take = getOrganismsRequest.Amount - trainingSession.LeasedOrganisms.Count(o => !o.Organism.IsEvaluated); List <LeasedOrganism> newLeasedOrganisms = GetNewLeasedOrganisms(take); trainingSession.LeasedOrganisms.AddRange(newLeasedOrganisms); await _trainingSessionRepository.SaveChangesAsync(); } if (trainingSession.LeasedOrganisms.Count(o => !o.Organism.IsEvaluated) < getOrganismsRequest.Amount && getOrganismsRequest.Amount < trainingRoomSettings.OrganismCount) { message = "The requested amount of organisms are not all available. The training room is close to a new generation."; } List <OrganismDto> organismDtos = trainingSession.LeasedOrganisms .Where(lo => !lo.Organism.IsEvaluated) .Take(getOrganismsRequest.Amount) .Select(lo => { OrganismDto organismDto = Mapper.Map <OrganismDto>(lo.Organism); // Because the input and output nodes are set using a Many To Many relation the nodes are converted separately. organismDto.InputNodes = lo.Organism.Inputs.Select(input => Mapper.Map <NodeDto>(input.InputNode)).ToList(); organismDto.OutputNodes = lo.Organism.Outputs.Select(input => Mapper.Map <NodeDto>(input.OutputNode)).ToList(); return(organismDto); }).ToList(); return(new GetOrganismsResponse(getOrganismsRequest.Id, organismDtos, message, organismDtos.Any())); List <LeasedOrganism> GetNewLeasedOrganisms(int take) { return(trainingSession.TrainingRoom.Species .SelectMany(sp => sp.Organisms) .Where(lo => !lo.IsLeased) .Take(take).Select(o => { o.IsLeased = true; return new LeasedOrganism(o, trainingSession.Id); }).ToList()); } }
static async Task Main(string[] args) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); List <Task> tasks = new List <Task>(); for (int j = 0; j < ClientCount; j++) { Task clientTask = Task.Run(async() => { IMessageProcessor messageProcessor = new ClientMessageProcessor(); IMessageSerializer messageSerializer = new JsonMessageSerializer(); SslTcpNetworkConnector networkConnector = new SslTcpNetworkConnector(messageSerializer, messageProcessor, Host, Port); await networkConnector.ConnectAsync(CancellationToken.None); await networkConnector.AuthenticateAsClient(CancellationToken.None); networkConnector.Start(); MessageListener <AuthenticateResponse> loginResponseListener = new MessageListener <AuthenticateResponse>(); MessageListener <RegisterResponse> registerMessageListener = new MessageListener <RegisterResponse>(); MessageListener <CreateTrainingRoomResponse> createTrainingRoomResponseListener = new MessageListener <CreateTrainingRoomResponse>(); MessageListener <GetEnabledTrainingRoomsResponse> getEnabledTrainingRoomsResponseListener = new MessageListener <GetEnabledTrainingRoomsResponse>(); MessageListener <StartTrainingSessionResponse> startTrainingSessionResponseListener = new MessageListener <StartTrainingSessionResponse>(); MessageListener <GetOrganismsResponse> getOrganismsResponseListener = new MessageListener <GetOrganismsResponse>(); MessageListener <PostOrganismsScoreResponse> postOrganismsScoreResponseListener = new MessageListener <PostOrganismsScoreResponse>(); loginResponseListener.Subscribe(messageProcessor); createTrainingRoomResponseListener.Subscribe(messageProcessor); registerMessageListener.Subscribe(messageProcessor); getEnabledTrainingRoomsResponseListener.Subscribe(messageProcessor); startTrainingSessionResponseListener.Subscribe(messageProcessor); getOrganismsResponseListener.Subscribe(messageProcessor); postOrganismsScoreResponseListener.Subscribe(messageProcessor); for (int i = 0; i < MessageCount; i++) { Guid name = Guid.NewGuid(); RegisterRequest registerRequest = new RegisterRequest(name.ToString(), "password2", "Name2"); await networkConnector.SendMessageAsync(registerRequest, CancellationToken.None); RegisterResponse registerResponse = await registerMessageListener.ReceiveMessageAsync(CancellationToken.None); Console.WriteLine($"RegisterResponse: \n\tSuccess: {registerResponse.Success}, \n\tRequestId: {registerResponse.RequestId}, \n\tResponseId: {registerResponse.Id}, \n\tMessage:{registerResponse.Message}"); AuthenticateRequest loginRequest = new AuthenticateRequest(name.ToString(), "password2", "Name2"); await networkConnector.SendMessageAsync(loginRequest, CancellationToken.None); AuthenticateResponse loginResponse = await loginResponseListener.ReceiveMessageAsync(CancellationToken.None); Console.WriteLine($"AuthenticateResponse: \n\tSuccess: {loginResponse.Success}, \n\tAccessToken: {loginResponse.AccessToken}, \n\tRequestId: {loginResponse.RequestId}, \n\tResponseId: {loginResponse.Id}, \n\tMessage:{loginResponse.Message}"); TrainingRoomSettings trainingRoomSettings = new TrainingRoomSettings(50, 2, 1, 1, 1, 0.4, 3, 0.05, 0.03, 0.75, 0.001, 1, 0.8, 0.1, 0.5, 0.25, 0); CreateTrainingRoomRequest createTrainingRoomRequest = new CreateTrainingRoomRequest(loginResponse.UserId, Guid.NewGuid().ToString(), trainingRoomSettings); await networkConnector.SendMessageAsync(createTrainingRoomRequest, CancellationToken.None); CreateTrainingRoomResponse createTrainingRoomResponse = await createTrainingRoomResponseListener.ReceiveMessageAsync(CancellationToken.None); Console.WriteLine($"CreateTrainingRoomResponse: \n\tId: {createTrainingRoomResponse.Id}\n\tRequestId: {createTrainingRoomResponse.RequestId}\n\tDateTime: {createTrainingRoomResponse.DateTime}\n\tMessage: {createTrainingRoomResponse.Message}\n\tSuccess: {createTrainingRoomResponse.Success}\n\tTrainingRoomId: {createTrainingRoomResponse.TrainingRoomId}"); StartTrainingSessionRequest startTrainingSessionRequest = new StartTrainingSessionRequest(loginResponse.UserId, createTrainingRoomResponse.TrainingRoomId); await networkConnector.SendMessageAsync(startTrainingSessionRequest, CancellationToken.None); StartTrainingSessionResponse startTrainingSessionResponse = await startTrainingSessionResponseListener.ReceiveMessageAsync(CancellationToken.None); Console.WriteLine($"StartTrainingSessionResponse: \n\tId: {startTrainingSessionResponse.Id}\n\tRequestId: {startTrainingSessionResponse.RequestId}\n\tDateTime: {startTrainingSessionResponse.DateTime}\n\tMessage: {startTrainingSessionResponse.Message}\n\tSuccess: {startTrainingSessionResponse.Success}\n\tTrainingSessionId: {startTrainingSessionResponse.TrainingSession.Id}"); await GetAndPostOrganismsAsync(startTrainingSessionResponse, networkConnector, getOrganismsResponseListener, postOrganismsScoreResponseListener); await GetAndPostOrganismsAsync(startTrainingSessionResponse, networkConnector, getOrganismsResponseListener, postOrganismsScoreResponseListener); await GetAndPostOrganismsAsync(startTrainingSessionResponse, networkConnector, getOrganismsResponseListener, postOrganismsScoreResponseListener); await GetAndPostOrganismsAsync(startTrainingSessionResponse, networkConnector, getOrganismsResponseListener, postOrganismsScoreResponseListener); await GetAndPostOrganismsAsync(startTrainingSessionResponse, networkConnector, getOrganismsResponseListener, postOrganismsScoreResponseListener); // await GetAndPostOrganismsAsync(startTrainingSessionResponse, networkConnector, getOrganismsResponseListener, postOrganismsScoreResponseListener); //GetEnabledTrainingRoomsRequest getEnabledTrainingRoomsRequest = new GetEnabledTrainingRoomsRequest(); //await networkConnector.SendMessageAsync(getEnabledTrainingRoomsRequest, CancellationToken.None); //GetEnabledTrainingRoomsResponse getEnabledTrainingRoomsResponse = await getEnabledTrainingRoomsResponseListener.ReceiveMessageAsync(CancellationToken.None); //Console.WriteLine($"GetEnabledTrainingRoomsResponse: \n\tId: {getEnabledTrainingRoomsResponse.Id}\n\tRequestId: {getEnabledTrainingRoomsResponse.RequestId}\n\tDateTime: {getEnabledTrainingRoomsResponse.DateTime}\n\tMessage: {getEnabledTrainingRoomsResponse.Message}\n\tSuccess: {getEnabledTrainingRoomsResponse.Success}"); //foreach (TrainingRoomDto trainingRoomDto in getEnabledTrainingRoomsResponse.TrainingRooms) // Console.WriteLine($"TrainingRoom:\n\tId: {trainingRoomDto.Id}\n\tName: {trainingRoomDto.Name}\n\tOwner: {trainingRoomDto.Owner.Username}"); } loginResponseListener.Unsubscribe(); }); tasks.Add(clientTask); } await Task.WhenAll(tasks).ContinueWith(action => { stopwatch.Stop(); Console.WriteLine($"Messages per second: {TotalMessages / stopwatch.Elapsed.TotalSeconds:N0}"); Console.ReadKey(); }); }
/// <inheritdoc cref="ITrainingRoomService.GetOrganismsAsync(GetOrganismsRequest)"/> public async Task <GetOrganismsResponse> GetOrganismsAsync(GetOrganismsRequest getOrganismsRequest) { string message = "Successfully fetched all requested organisms."; TrainingSession trainingSession; if (getOrganismsRequest.TrainingSessionId.Equals(Guid.Empty)) { return(new GetOrganismsResponse(getOrganismsRequest.Id, new List <OrganismDto>(), "Training room id cannot be an empty guid.")); } if (getOrganismsRequest.Amount < 1) { return(new GetOrganismsResponse(getOrganismsRequest.Id, new List <OrganismDto>(), "Amount cannot be smaller than 1.")); } if ((trainingSession = await _trainingSessionRepository.FindSingleOrDefaultAsync(ts => ts.Id.Equals(getOrganismsRequest.TrainingSessionId))) == default) { return(new GetOrganismsResponse(getOrganismsRequest.Id, new List <OrganismDto>(), "Training session does not exist.")); } // if the list is empty then get new ones from the training room if (trainingSession.LeasedOrganisms.Count(o => !o.Organism.Evaluated) == 0) { if (trainingSession.TrainingRoom.Generation == 0) { TrainingRoomSettings trainingRoomSettings = trainingSession.TrainingRoom.TrainingRoomSettings; for (int i = 0; i < trainingRoomSettings.OrganismCount; i++) { Organism organism = new Organism(trainingSession.TrainingRoom.Generation, trainingRoomSettings) { Leased = true }; trainingSession.TrainingRoom.AddOrganism(organism); trainingSession.LeasedOrganisms.Add(new LeasedOrganism(organism)); } trainingSession.TrainingRoom.IncreaseNodeIdTo(trainingRoomSettings.InputCount + trainingRoomSettings.OutputCount); message = $"First generation; generated {trainingSession.TrainingRoom.TrainingRoomSettings.OrganismCount} organisms."; } else { trainingSession.LeasedOrganisms.AddRange(GetNewLeasedOrganisms(getOrganismsRequest.Amount)); message = "Start of new generation."; } } else if (trainingSession.LeasedOrganisms.Count(o => !o.Organism.Evaluated) < getOrganismsRequest.Amount) { int take = getOrganismsRequest.Amount - trainingSession.LeasedOrganisms.Count(o => !o.Organism.Evaluated); List <LeasedOrganism> newLeasedOrganisms = GetNewLeasedOrganisms(take); trainingSession.LeasedOrganisms.AddRange(newLeasedOrganisms); } if (trainingSession.LeasedOrganisms.Count(o => !o.Organism.Evaluated) < getOrganismsRequest.Amount) { message = "The requested amount of organisms are not all available. The training room is probably close to a new generation or is waiting on other training sessions to complete."; } await _trainingSessionRepository.UpdateAsync(trainingSession); List <OrganismDto> organismDtos = trainingSession.LeasedOrganisms .Where(lo => !lo.Organism.Evaluated) .Take(getOrganismsRequest.Amount) .Select(lo => { OrganismDto organismDto = EntityToDtoConverter.Convert <OrganismDto, Organism>(lo.Organism); // Because the input and output nodes are set using a Many To Many relation the nodes are converted separately. organismDto.InputNodes = lo.Organism.Inputs.Select(input => EntityToDtoConverter.Convert <NodeDto, InputNode>(input.InputNode)).ToList(); organismDto.OutputNodes = lo.Organism.Outputs.Select(input => EntityToDtoConverter.Convert <NodeDto, OutputNode>(input.OutputNode)).ToList(); return(organismDto); }).ToList(); return(new GetOrganismsResponse(getOrganismsRequest.Id, organismDtos, message, organismDtos.Any())); List <LeasedOrganism> GetNewLeasedOrganisms(int take) { return(trainingSession.TrainingRoom.Species.SelectMany(sp => sp.Organisms).Where(lo => !lo.Leased) .Take(take).Select(o => { o.Leased = true; return new LeasedOrganism(o); }).ToList()); } }
public EvaluatableOrganism(TrainingRoomSettings trainingRoomSettings, Func <uint, uint, uint> innovationFunction) : this(0, trainingRoomSettings) { AddConnectionMutation(trainingRoomSettings, innovationFunction); }
/// <summary> /// Initializes an instance of the <see cref="CreateTrainingRoomRequest"/> class. /// </summary> /// <param name="ownerId">The owner id.</param> /// <param name="trainingRoomName">The training room name.</param> /// <param name="trainingRoomSettings">The training room settings.</param> public CreateTrainingRoomRequest(Guid ownerId, string trainingRoomName, TrainingRoomSettings trainingRoomSettings) { OwnerId = ownerId; TrainingRoomName = trainingRoomName; TrainingRoomSettings = trainingRoomSettings; }