private double createRandomValueFor(OriginData originData, RandomPopulationSettings populationSettings, string parameterName, out bool success)
        {
            var parameterRange = populationSettings.ParameterRange(parameterName);
            var parameter      = _individualModelTask.MeanOrganismParameter(originData, parameterName);

            return(tryCreateRandomValueFor(parameterRange, parameter, out success));
        }
        private RandomPopulation createPopulationFor(RandomPopulationSettings populationSettings)
        {
            var randomPopulation = _objectBaseFactory.Create <RandomPopulation>();

            randomPopulation.Root     = _objectBaseFactory.Create <IRootContainer>();
            randomPopulation.Settings = populationSettings;
            randomPopulation.SetAdvancedParameters(_objectBaseFactory.Create <IAdvancedParameterCollection>());
            return(randomPopulation);
        }
Beispiel #3
0
        private void perturbate(Individual currentIndividual, RandomPopulationSettings populationSettings, RandomGenerator randomGenerator)
        {
            bool success    = true;
            var  originData = currentIndividual.OriginData;
            var  diseaseStateImplementation = _diseaseStateImplementationFactory.CreateFor(originData.DiseaseState);
            uint numberOfTry = 0;

            do
            {
                numberOfTry++;

                //first create a new age value if necessary
                if (originData.Population.IsAgeDependent)
                {
                    originData.Age = new OriginDataParameter(createRandomValueFor(originData, populationSettings, CoreConstants.Parameters.AGE, randomGenerator, out success));
                    currentIndividual.Organism.Parameter(CoreConstants.Parameters.AGE).Value = originData.Age.Value;
                    if (!success)
                    {
                        continue;
                    }
                }

                if (originData.Population.IsPreterm)
                {
                    originData.GestationalAge = new OriginDataParameter(createDiscreteRandomValueFor(populationSettings, Constants.Parameters.GESTATIONAL_AGE, randomGenerator, out success));
                    currentIndividual.Organism.Parameter(Constants.Parameters.GESTATIONAL_AGE).Value = originData.GestationalAge.Value;
                    if (!success)
                    {
                        continue;
                    }
                }

                //Then define gender depending on selecting proportions
                if (originData.Population.IsHeightDependent)
                {
                    originData.Height = new OriginDataParameter(createRandomValueFor(originData, populationSettings, CoreConstants.Parameters.MEAN_HEIGHT, randomGenerator, out success));
                    currentIndividual.Organism.Parameter(CoreConstants.Parameters.HEIGHT).Value = originData.Height.Value;
                }

                //now assign any random parameters due to disease state
                foreach (var diseaseStateParameter in originData.DiseaseStateParameters)
                {
                    diseaseStateParameter.Value = createDiseaseStateRandomParameterValueFor(originData, populationSettings, diseaseStateParameter, randomGenerator, out success);
                }

                if (!success)
                {
                    continue;
                }
                (success, _) = diseaseStateImplementation.IsValid(originData);
            } while (!success && numberOfTry < _maxIterations);

            if (numberOfTry >= _maxIterations)
            {
                throw new CannotCreatePopulationWithConstraintsException(_reportGenerator.StringReportFor(populationSettings));
            }
        }
Beispiel #4
0
        public RandomPopulationSettings Clone(ICloneManager cloneManager)
        {
            var clone = new RandomPopulationSettings {
                BaseIndividual = cloneManager.Clone(BaseIndividual), NumberOfIndividuals = NumberOfIndividuals
            };

            _genderRatios.Each(gr => clone.AddGenderRatio(gr.Clone()));
            _parameterRanges.Each(pr => clone.AddParameterRange(pr.Clone()));
            return(clone);
        }
Beispiel #5
0
        public override void UpdatePropertiesFrom(IUpdatable sourceObject, ICloneManager cloneManager)
        {
            base.UpdatePropertiesFrom(sourceObject, cloneManager);
            var sourcePopulation = sourceObject as RandomPopulation;

            if (sourcePopulation == null)
            {
                return;
            }
            Settings = sourcePopulation.Settings.Clone(cloneManager);
        }
        private RandomPopulation createPopulationFor(RandomPopulationSettings populationSettings, int?seed)
        {
            var randomPopulation = _objectBaseFactory.Create <RandomPopulation>();

            randomPopulation.Root     = _objectBaseFactory.Create <IRootContainer>();
            randomPopulation.Settings = populationSettings;
            randomPopulation.SetAdvancedParameters(_objectBaseFactory.Create <AdvancedParameterCollection>());

            if (seed != null)
            {
                randomPopulation.Seed = seed.Value;
            }

            return(randomPopulation);
        }
        private void updateCurrentIndividualFromSettings(RandomPopulationSettings populationSettings, Individual currentIndividual,
                                                         PathCache <IDistributedParameter> allCurrentDistribuedParameters, PathCache <IDistributedParameter> allBaseDistributedParamters, Gender currentGender)
        {
            //clone of the original origin data
            currentIndividual.OriginData        = populationSettings.BaseIndividual.OriginData.Clone();
            currentIndividual.OriginData.Gender = currentGender;

            //create a new seed for the individual in order to randomize correctly our values
            currentIndividual.GenerateSeed();

            //perform random varation of the origin data according to the settings defined for the population
            perturbate(currentIndividual, populationSettings);

            //Update the distribution according to the new origin data
            _distributedParametersUpdater.UpdateDistributedParameter(allCurrentDistribuedParameters, allBaseDistributedParamters, currentIndividual.OriginData);
        }
        private void perturbate(Individual currentIndividual, RandomPopulationSettings populationSettings)
        {
            bool success     = true;
            var  originData  = currentIndividual.OriginData;
            uint numberOfTry = 0;

            do
            {
                numberOfTry++;

                //first create a new age value if necessary
                if (originData.SpeciesPopulation.IsAgeDependent)
                {
                    originData.Age = createRandomValueFor(originData, populationSettings, CoreConstants.Parameter.AGE, out success);
                    currentIndividual.Organism.Parameter(CoreConstants.Parameter.AGE).Value = originData.Age.Value;
                    if (!success)
                    {
                        continue;
                    }
                }

                if (originData.SpeciesPopulation.IsPreterm)
                {
                    originData.GestationalAge = createDiscreteRandomValueFor(populationSettings, CoreConstants.Parameter.GESTATIONAL_AGE, out success);
                    currentIndividual.Organism.Parameter(CoreConstants.Parameter.GESTATIONAL_AGE).Value = originData.GestationalAge.Value;
                    if (!success)
                    {
                        continue;
                    }
                }

                //Then define gender depending on selecting proportions
                if (originData.SpeciesPopulation.IsHeightDependent)
                {
                    originData.Height = createRandomValueFor(originData, populationSettings, CoreConstants.Parameter.MEAN_HEIGHT, out success);
                    currentIndividual.Organism.Parameter(CoreConstants.Parameter.HEIGHT).Value = originData.Height.Value;
                }
            } while (!success && numberOfTry < _maxIterations);

            if (numberOfTry >= _maxIterations)
            {
                throw new CannotCreatePopulationWithConstraintsException(_reportGenerator.StringReportFor(populationSettings));
            }
        }
        private double createDiscreteRandomValueFor(RandomPopulationSettings populationSettings, string parameterName, out bool success)
        {
            var parameterRange = populationSettings.ParameterRange(parameterName) as DiscreteParameterRange;

            success = true;

            if (parameterRange == null)
            {
                success = false;
                return(0);
            }

            if (parameterRange.IsConstant)
            {
                return(parameterRange.MinValue.Value);
            }

            return(_randomGenerator.NextInteger(parameterRange.MinValue.ConvertedTo <int>(), parameterRange.MaxValue.ConvertedTo <int>()));
        }
        private void fllUpGenderQueueBasedOn(RandomPopulationSettings populationSettings)
        {
            _genderQueue = new Queue <Gender>();
            foreach (var genderRatio in populationSettings.GenderRatios)
            {
                var numberOfGender = genderRatio.Ratio / 100.0 * populationSettings.NumberOfIndividuals;
                for (int i = 0; i < numberOfGender; i++)
                {
                    _genderQueue.Enqueue(genderRatio.Gender);
                }
            }

            //add possible items missing because of rounding artefacts
            //+1 is because we need to add one element more in any case in order to be able to dequeue properly
            for (int i = 0; i < populationSettings.NumberOfIndividuals + 1 - _genderQueue.Count; i++)
            {
                _genderQueue.Enqueue(populationSettings.BaseIndividual.OriginData.Gender);
            }
        }
Beispiel #11
0
        private void updateCurrentIndividualFromSettings(
            RandomPopulationSettings populationSettings,
            Individual baseIndividual,
            Individual currentIndividual,
            PathCache <IDistributedParameter> allCurrentDistributedParameters,
            PathCache <IDistributedParameter> allBaseDistributedParameters,
            Gender currentGender,
            RandomGenerator randomGenerator)
        {
            //clone of the original origin data
            currentIndividual.OriginData        = baseIndividual.OriginData.Clone();
            currentIndividual.OriginData.Gender = currentGender;

            //perform random variation of the origin data according to the settings defined for the population
            perturbate(currentIndividual, populationSettings, randomGenerator);

            //Update the distribution according to the new origin data
            _distributedParametersUpdater.UpdateDistributedParameter(allCurrentDistributedParameters, allBaseDistributedParameters, currentIndividual.OriginData);
        }
        private bool tryRandomize(Individual individual, RandomPopulationSettings populationSettings, IEnumerable <IParameter> allIndividualParameters)
        {
            try
            {
                var bodyWeightRange     = populationSettings.ParameterRange(CoreConstants.Parameter.MEAN_WEIGHT);
                var bodyWeightParameter = _individualModelTask.MeanOrganismParameter(individual.OriginData, CoreConstants.Parameter.MEAN_WEIGHT);
                _createIndividualAlgorithm.Randomize(individual, bodyWeightParameter, bodyWeightRange.MinValue, bodyWeightRange.MaxValue, allIndividualParameters);

                if (!individual.OriginData.SpeciesPopulation.IsHeightDependent)
                {
                    return(true);
                }

                //last: Check if the value for the bmi is in the interval
                var bmi      = individual.Organism.Parameter(CoreConstants.Parameter.BMI).Value;
                var bmiRange = populationSettings.ParameterRange(CoreConstants.Parameter.BMI);
                return(bmiRange.IsValueInRange(bmi));
            }
            catch (CannotCreateIndividualWithConstraintsException)
            {
                return(false);
            }
        }
        public Task <RandomPopulation> CreateFor(RandomPopulationSettings populationSettings, CancellationToken cancellationToken)
        {
            Task <RandomPopulation> task = Task.Run(() =>
            {
                using (var progressUpdater = _progressManager.Create())
                {
                    var randomPopulation = createPopulationFor(populationSettings);

                    fllUpGenderQueueBasedOn(populationSettings);
                    progressUpdater.Initialize(populationSettings.NumberOfIndividuals, PKSimConstants.UI.CreatingPopulation);

                    _randomGenerator = randomPopulation.RandomGenerator;

                    //the base indiviudal 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 udpate the distribution for the current individual
                    var allDistributedParameters    = getAllDistributedParametersFrom(currentIndividual);
                    var allBaseDistributedParamters = getAllDistributedParametersFrom(baseIndividual);

                    //all individual parameters. Just an optimiztion 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, allBaseDistributedParamters, currentGender);

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

                        randomPopulation.AddIndividualProperties(_individualPropertiesMapper.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));
                    }

                    addUserDefinedVariabilityAndOntogenyForMolecules(randomPopulation);
                    randomPopulation.IsLoaded = true;
                    return(randomPopulation);
                }
            }, cancellationToken);

            return(task);
        }
Beispiel #14
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);

                    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));
        }
Beispiel #15
0
        private double createDiseaseStateRandomParameterValueFor(OriginData originData, RandomPopulationSettings populationSettings, OriginDataParameter diseaseStateParameter, RandomGenerator randomGenerator, out bool success)
        {
            var parameterRange = populationSettings.ParameterRange(diseaseStateParameter.Name);
            var parameter      = originData.DiseaseState.Parameter(diseaseStateParameter.Name);

            return(tryCreateRandomValueFor(parameterRange, parameter, randomGenerator, out success));
        }