示例#1
0
        /// <inheritdoc/>
        public IndividualAttribute GetAttribute(bool createNewInstance = true)
        {
            if (createNewInstance || lastInstance is null)
            {
                double value         = Value;
                double randStdNormal = 0;

                if (StandardDeviation > 0)
                {
                    double u1 = RandomNumberGenerator.Generator.NextDouble();
                    double u2 = RandomNumberGenerator.Generator.NextDouble();
                    randStdNormal = Math.Sqrt(-2.0 * Math.Log(u1)) *
                                    Math.Sin(2.0 * Math.PI * u2);
                }
                value = Math.Min(MaximumValue, Math.Max(MinimumValue, value + StandardDeviation * randStdNormal));

                Single valuef = Convert.ToSingle(value);

                lastInstance = new IndividualAttribute()
                {
                    InheritanceStyle = InheritanceStyle,
                    StoredValue      = valuef
                };
            }
            return(lastInstance);
        }
示例#2
0
        private new void OnSimulationCommencing(object sender, EventArgs e)
        {
            // locate AE relationship
            adultEquivalentRelationship = this.FindAllChildren <Relationship>().FirstOrDefault(a => a.Name.ToUpper().Contains("AE"));

            Items = new List <LabourType>();
            foreach (LabourType labourChildModel in this.FindAllChildren <LabourType>())
            {
                IndividualAttribute att = new IndividualAttribute()
                {
                    StoredValue = labourChildModel.Name
                };
                if (UseCohorts)
                {
                    LabourType labour = new LabourType()
                    {
                        Sex                = labourChildModel.Sex,
                        Individuals        = labourChildModel.Individuals,
                        Parent             = this,
                        InitialAge         = labourChildModel.InitialAge,
                        AgeInMonths        = labourChildModel.InitialAge * 12,
                        LabourAvailability = labourChildModel.LabourAvailability,
                        Name               = labourChildModel.Name,
                        Hired              = labourChildModel.Hired
                    };
                    labour.Attributes.Add("Group", att);
                    labour.TransactionOccurred += Resource_TransactionOccurred;
                    Items.Add(labour);
                }
                else
                {
                    for (int i = 0; i < labourChildModel.Individuals; i++)
                    {
                        // get the availability from provided list
                        LabourType labour = new LabourType()
                        {
                            Sex                = labourChildModel.Sex,
                            Individuals        = 1,
                            Parent             = this,
                            InitialAge         = labourChildModel.InitialAge,
                            AgeInMonths        = labourChildModel.InitialAge * 12,
                            LabourAvailability = labourChildModel.LabourAvailability,
                            Name               = labourChildModel.Name + ((labourChildModel.Individuals > 1) ? "_" + (i + 1).ToString() : ""),
                            Hired              = labourChildModel.Hired
                        };
                        labour.Attributes.Add("Group", att);
                        labour.TransactionOccurred += Resource_TransactionOccurred;
                        Items.Add(labour);
                    }
                }
            }
            // clone pricelist so model can modify if needed and not affect initial parameterisation
            if (this.FindAllChildren <LabourPricing>().Count() > 0)
            {
                PayList = Apsim.Clone(this.FindAllChildren <LabourPricing>().FirstOrDefault());
            }
        }
示例#3
0
        /// <inheritdoc/>
        public IndividualAttribute GetAttribute(bool createNewInstance = true)
        {
            if (createNewInstance || lastInstance is null)
            {
                // get all individual Attributes
                var attributeInds = ruminantActivity.CurrentHerd().Where(a => a.Attributes.Exists(AttributeName));
                if (filterGroups.Any())
                {
                    attributeInds = filterGroups.FirstOrDefault().Filter(attributeInds);
                }
                var inds = attributeInds.Select(a => (float)a.Attributes.GetValue(AttributeName).StoredValue);

                Single valuef = 0;

                switch (CalculationStyle)
                {
                case SetAttributeFromHerdType.Mean:
                    valuef = Convert.ToSingle(inds.Sum() / inds.Count());
                    break;

                case SetAttributeFromHerdType.Median:
                    int cnt = inds.Count();
                    if (cnt % 2 == 0)
                    {
                        valuef = Convert.ToSingle(inds.Skip(((cnt + 1) / 2) - 1).Take(1));
                    }
                    else
                    {
                        valuef = Convert.ToSingle((inds.Skip((cnt / 2) - 1).First() + inds.Skip(cnt / 2).First()) / 2);
                    }
                    break;

                case SetAttributeFromHerdType.Minimum:
                    valuef = Convert.ToSingle(inds.Min());
                    break;

                case SetAttributeFromHerdType.Maximum:
                    valuef = Convert.ToSingle(inds.Max());
                    break;

                case SetAttributeFromHerdType.Random:
                    valuef = Convert.ToSingle(inds.Take(RandomNumberGenerator.Generator.Next(0, inds.Count() - 1)));
                    break;

                default:
                    break;
                }

                lastInstance = new IndividualAttribute()
                {
                    InheritanceStyle = InheritanceStyle,
                    StoredValue      = valuef * Multiplier
                };
            }
            return(lastInstance);
        }
示例#4
0
        /// <summary>
        /// Get the attribute inherited by an offspring given both parent attribute values stored for a breeder
        /// </summary>
        /// <returns>A ruminant attribute to supply the offspring</returns>
        public object GetInheritedAttribute()
        {
            IndividualAttribute newAttribute = new IndividualAttribute()
            {
                InheritanceStyle = this.InheritanceStyle
            };

            switch (InheritanceStyle)
            {
            case AttributeInheritanceStyle.None:
                return(null);

            case AttributeInheritanceStyle.Maternal:
                newAttribute.StoredValue = StoredValue;
                break;

            case AttributeInheritanceStyle.Paternal:
                newAttribute.StoredValue = StoredMateValue;
                break;

            case AttributeInheritanceStyle.LeastParentValue:
                if (this.Value <= this.MateValue)
                {
                    newAttribute.StoredValue = StoredValue;
                }
                else
                {
                    newAttribute.StoredValue = StoredMateValue;
                }
                break;

            case AttributeInheritanceStyle.GreatestParentValue:
                if (this.Value >= this.MateValue)
                {
                    newAttribute.StoredValue = StoredValue;
                }
                else
                {
                    newAttribute.StoredValue = StoredMateValue;
                }
                break;

            case AttributeInheritanceStyle.LeastBothParents:
                if (StoredValue != null & StoredMateValue != null)
                {
                    if (this.Value <= this.MateValue)
                    {
                        newAttribute.StoredValue = StoredValue;
                    }
                    else
                    {
                        newAttribute.StoredValue = StoredMateValue;
                    }
                }
                else
                {
                    return(null);
                }
                break;

            case AttributeInheritanceStyle.GreatestBothParents:
                if (StoredValue != null & StoredValue != null)
                {
                    if (Value >= MateValue)
                    {
                        newAttribute.StoredValue = StoredValue;
                    }
                    else
                    {
                        newAttribute.StoredValue = StoredMateValue;
                    }
                }
                else
                {
                    return(null);
                }
                break;

            case AttributeInheritanceStyle.MeanValueZeroAbsent:
                float offSpringValue = 0;
                if (StoredValue != null)
                {
                    offSpringValue += Value;
                }

                if (StoredMateValue != null)
                {
                    offSpringValue += MateValue;
                }

                newAttribute.StoredValue = (offSpringValue / 2.0f);
                break;

            case AttributeInheritanceStyle.MeanValueIgnoreAbsent:
                offSpringValue = 0;
                int cnt = 0;
                if (StoredValue != null)
                {
                    offSpringValue += Value;
                    cnt++;
                }
                if (StoredMateValue != null)
                {
                    offSpringValue += MateValue;
                    cnt++;
                }
                newAttribute.StoredValue = StoredValue = (offSpringValue / (float)cnt);
                break;

            case AttributeInheritanceStyle.AsGeneticTrait:
                throw new NotImplementedException();

            default:
                return(null);
            }
            return(newAttribute);
        }