/// <summary> /// Applies 'Vi.Types.Percentage.TryParse' /// </summary> /// <param name="value">The value to convert.</param> /// <param name="default">The result if 'tryParse' fails.</param> /// <returns>The percentage associated with the 'value', default otherwise.</returns> public static Vi.Types.Percentage ToPercentage(this string value, Vi.Types.Percentage @default) { Vi.Types.Percentage result = 0; var parseOk = Vi.Types.Percentage.TryParse(value, out result); return(parseOk ? result : @default); }
/// <summary> /// Main CTor Create an instance of INI set with the provided values /// </summary> /// <param name="radius"></param> /// <param name="people"></param> /// <param name="steps"></param> /// <param name="ticks"></param> /// <param name="isolation"></param> public INI(decimal radius, int people, byte steps, int ticks, decimal isolation) { this.Radius = radius; this.People = people; this.Steps = steps; this.Ticks = ticks; this.Isolation = new Vi.Types.Percentage(isolation); }
/// <summary> /// /// </summary> /// <param name="locations"></param> /// <param name="virusGrowth"></param> /// <param name="antibodyGrowth"></param> /// <param name="antibodyDecay"></param> /// <param name="deadThreshold"></param> /// <param name="mobility"></param> public Person( CoViD.CL.Locations locations, Vi.Types.Percentage virusGrowth, Vi.Types.Percentage antibodyGrowth, Vi.Types.Percentage antibodyDecay, Vi.Types.Percentage deadThreshold, byte mobility ) { this.Configure(locations, virusGrowth, antibodyGrowth, antibodyDecay, mobility); }
// <remarks> // Why 'system.decimal // The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 // to negative 79,228,162,514,264,337,593,543,950,335. The default value of a Decimal is 0. The Decimal value type // is appropriate for financial calculations that require large numbers of significant integral and fractional digits // and no round-off errors. The Decimal type does not eliminate the need for rounding. Rather, it minimizes errors // due to rounding. For example, the following code produces a result of 0.9999999999999999999999999999 instead of 1. // // When the result of the division and multiplication is passed to the Round method, the result suffers no loss of // precision, as the following code shows. // https://docs.microsoft.com/en-us/dotnet/api/system.decimal?view=net-5.0 // </remarks> /// <summary> /// Converts the string representation of a number to its Decimal equivalent. A return value indicates whether the conversion succeeded or failed. /// </summary> /// <param name="value">The string representation of the number to convert.</param> /// <param name="result">When this method returns, contains the Percentage number that is equivalent /// to the numeric value contained in the parameter 'value', if the conversion succeeded, or zero if the conversion failed. /// The conversion fails if the 'value' parameter is null or Empty, is not a number in a format compliant /// with style, or represents a number less than MinValue or greater than MaxValue. This parameter is /// passed uininitialized; any value originally supplied in result is overwritten.</param> /// <returns>true if 'value' was converted successfully; otherwise, false.</returns> public static bool TryParse(string value, out Vi.Types.Percentage result) { decimal _result = 0; try { value = value.ToLower().Remove("%", "pct", "pc").Trim(); var parseOk = System.Decimal.TryParse(value, out _result); result = _result; return(parseOk); } catch (System.Exception) { result = 0; return(false); } }
/// <summary> /// /// </summary> /// <param name="locations"></param> /// <param name="virusGrowth"></param> /// <param name="antibodyGrowth"></param> /// <param name="antibodyDecay"></param> /// <param name="mobility"></param> private void Configure(CoViD.CL.Locations locations, Vi.Types.Percentage virusGrowth, Vi.Types.Percentage antibodyGrowth, Vi.Types.Percentage antibodyDecay, byte mobility) { this.Locations = locations; this.SIR = SIRStates.Susceptible; this.State = States.Susceptible; this.AntibodyGrowth = antibodyGrowth; this.AntibodyDecay = antibodyDecay; this.VirusGrowth = virusGrowth; this.DeadThreshold = ((float)Person.Rnd.Next(72, 100, 3)) / (float)100; //this.DeadThreshold = (float)((float)Person.Rnd.Next(50000, 100000) / 100000F); this.Age = Person.Rnd.Next(0, 100, 4); this.AntibodyGrowthRatio = (decimal)1 + antibodyGrowth.Value; this.AntibodyDecayRatio = (decimal)1 - antibodyDecay; this.VirusGrowthRatio = (decimal)1 + virusGrowth.Value; this._VirusGrowthRatio = this.VirusGrowthRatio; this.Mobility = mobility; }
/// <summary> /// Create a list of points on the grid to simulate a person's movements. /// The "grid is splitted in 4 different sub grids: cities. Each people moves /// in its own city. /// Travellers can move across cities. /// If Traveller is 1 (100%) there is only one big city /// Points are taken randomly. /// (This method calls the other method in overload with 'travellers = 1') /// </summary> /// <param name="radius">"Grid" is a square which side is twice 'radius'.</param> /// <param name="distribution">Change the distribution from 1: Omogeneous, 2:triangular, 3: parabolic ... Gaussian (should be 'The Central limit theorem' if I'm right.)</param> /// <param name="traveller"></param> public Locations(int radius, byte distribution, Vi.Types.Percentage traveller) { var subRadius = radius / 20; var centerX = Locations.Rnd.Next(-radius, radius, distribution); var centerY = Locations.Rnd.Next(-radius, radius, distribution); int x = Locations.Rnd.Next(centerX - subRadius, centerX + subRadius, iterations: distribution); int y = Locations.Rnd.Next(centerY - subRadius, centerY + subRadius, iterations: distribution); // these are the places reached during the journey var targets = Locations.Rnd.Next(3, 10, 2); for (int i = 0; i < targets; i++) { int x1 = Locations.Rnd.Next(centerX - subRadius, centerX + subRadius, iterations: distribution); int y1 = Locations.Rnd.Next(centerY - subRadius, centerY + subRadius, iterations: distribution); float length = Locations.Rnd.Next(10, 30, 1); float y0x = x; float mx = ((float)x1 - (float)x) / length; float y0y = y; float my = ((float)y1 - (float)y) / length; // this is the segment between two 'targets' for (int j = 0; j < length; j++) { var point = new CoViD.CL.Point( (int)(y0x + mx * j), (int)(y0y + my * j) ); this.Add(point); } x = x1; y = y1; } }
/// <summary> /// /// </summary> /// <param name="rnd">The current instance of the 'System.Random object.</param> /// <param name="probability">A value between 0 and 100. The expected probability an event is successfull.</param> /// <returns>A random value (rnd.next > probability)</returns> public static bool Bet(this System.Random rnd, Vi.Types.Percentage probability) { return(rnd.Bet(1, probability)); }
/// <summary> /// Compare a random value between 0 and 100 against the value of 'probability' /// </summary> /// <param name="rnd"></param> /// <param name="grade">The grade of the polinomial distribution.</param> /// <param name="probability">a number between 0 and 100. Grater becom 100 smallest becom 0</param> /// <returns>A true if 'brobability is grater than a random value (rnd.next > probability)(</returns> public static bool Bet(this System.Random rnd, byte grade, Vi.Types.Percentage probability) { return(rnd.Next(0, 1, grade) > probability.Value); }