public Individual CreateAndOptimizeFor(OriginData originData, int?seed = null) { var individual = CreateStandardFor(originData); if (seed.HasValue) { individual.Seed = seed.Value; } //this creates a healthy individual _createIndividualAlgorithm.Optimize(individual); //Apply disease states if required var diseaseStateImplementation = _diseaseStateImplementationFactory.CreateFor(individual); diseaseStateImplementation.ApplyTo(individual); validate(individual); return(individual); }
public Task <RandomPopulation> CreateFor(RandomPopulationSettings populationSettings, CancellationToken cancellationToken, int?seed = null, bool addMoleculeParametersVariability = true) { return(Task.Run(() => { using (var progressUpdater = _progressManager.Create()) { var randomPopulation = createPopulationFor(populationSettings, seed); fllUpGenderQueueBasedOn(populationSettings); progressUpdater.Initialize(populationSettings.NumberOfIndividuals, PKSimConstants.UI.CreatingPopulation); var diseaseStateImplementation = _diseaseStateImplementationFactory.CreateFor(populationSettings.BaseIndividual); //the base individual is used to retrieve the default values. var baseIndividual = diseaseStateImplementation.CreateBaseIndividualForPopulation(populationSettings.BaseIndividual); //current individual defined as a clone of the based individual. The current individual will be the one varying var currentIndividual = _cloner.Clone(baseIndividual); //cache containing all parameters changed by the create individual from the current individual. This will be used just as reference to the current parameters var allChangedByCreatedIndividualParameters = getAllCreateIndividualParametersFrom(currentIndividual); //all distributed parameters. This will be used to update the distribution for the current individual var allDistributedParameters = getAllDistributedParametersFrom(currentIndividual); var allBaseDistributedParameters = getAllDistributedParametersFrom(baseIndividual); //all individual parameters. Just an optimization to avoid call GetAllChildren for each individual var allCurrentIndividualParameters = currentIndividual.GetAllChildren <IParameter>(); int maxTotalIterations = populationSettings.NumberOfIndividuals * _maxIterations; uint numberOfTry = 0; var currentGender = _genderQueue.Dequeue(); do { cancellationToken.ThrowIfCancellationRequested(); numberOfTry++; //could not create one item in max Iteration try=>exit if (numberOfTry > _maxIterations && randomPopulation.NumberOfItems == 0) { throw new CannotCreatePopulationWithConstraintsException(_reportGenerator.StringReportFor(populationSettings)); } //create a new individual based on population settings defined by the user updateCurrentIndividualFromSettings(populationSettings, baseIndividual, currentIndividual, allDistributedParameters, allBaseDistributedParameters, currentGender, randomPopulation.RandomGenerator); var success = tryRandomize(currentIndividual, populationSettings, allCurrentIndividualParameters, randomPopulation.RandomGenerator); if (!success) { continue; } success = diseaseStateImplementation.ApplyForPopulationTo(currentIndividual); if (!success) { continue; } randomPopulation.AddIndividualValues(_individualValuesMapper.MapFrom(currentIndividual, allChangedByCreatedIndividualParameters)); diseaseStateImplementation.ResetParametersAfterPopulationIteration(currentIndividual); currentGender = _genderQueue.Dequeue(); progressUpdater.IncrementProgress(PKSimConstants.UI.CreatingIndividualInPopulation(randomPopulation.NumberOfItems, populationSettings.NumberOfIndividuals)); } while (randomPopulation.NumberOfItems < populationSettings.NumberOfIndividuals && numberOfTry < maxTotalIterations); if (numberOfTry >= maxTotalIterations) { throw new CannotCreatePopulationWithConstraintsException(_reportGenerator.StringReportFor(populationSettings)); } if (addMoleculeParametersVariability) { _moleculeParameterVariabilityCreator.AddVariabilityTo(randomPopulation); } _moleculeOntogenyVariabilityUpdater.UpdateAllOntogenies(randomPopulation); randomPopulation.IsLoaded = true; return randomPopulation; } }, cancellationToken)); }
private void updateMoleculeParametersForDiseaseState(ISimulationSubject simulationSubject, IndividualMolecule moleculeInIndividual) { var diseaseStateImplementation = _diseaseStateImplementationFactory.CreateFor(simulationSubject.Individual); diseaseStateImplementation.ApplyTo(moleculeInIndividual); }