Ejemplo n.º 1
0
 private void addUserDefinedVariabilityAndOntogenyForMolecules(RandomPopulation randomPopulation)
 {
     _moleculeParameterVariabilityCreator.AddVariabilityTo(randomPopulation);
     _moleculeOntogenyVariabilityUpdater.UpdateAllOntogenies(randomPopulation);
 }
Ejemplo n.º 2
0
        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);

                    //the base individual is used to retrieve the default values.
                    var baseIndividual = populationSettings.BaseIndividual;

                    //current individual defined as a clone of the based individual. The current individual will be the one varying
                    var currentIndividual = _cloner.Clone(populationSettings.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 allIndividualParameters = currentIndividual.GetAllChildren <IParameter>().ToList();

                    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, currentIndividual, allDistributedParameters, allBaseDistributedParameters, currentGender, randomPopulation.RandomGenerator);

                        bool success = tryRandomize(currentIndividual, populationSettings, allIndividualParameters, randomPopulation.RandomGenerator);
                        if (!success)
                        {
                            continue;
                        }

                        randomPopulation.AddIndividualValues(_individualValuesMapper.MapFrom(currentIndividual, allChangedByCreatedIndividualParameters));

                        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));
        }