Skip to content

Commit

Permalink
chore: sync commit working on GenSolver comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper Bollen authored and Casper Bollen committed Sep 9, 2023
1 parent 927cce5 commit 38c98d6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 31 deletions.
5 changes: 3 additions & 2 deletions src/Informedica.GenSolver.Lib/Exceptions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ namespace Informedica.GenSolver.Lib

module Exceptions =

/// Equation exception
/// Solver exception
exception SolverException of Exceptions.Message list


/// Raise an `EquationException` with `Message` `m`.
/// Raise an `EquationException` with `Message` `m` and adds it to
/// the list of `errs`.
let raiseExc log errs m =

match log with
Expand Down
71 changes: 45 additions & 26 deletions src/Informedica.GenSolver.Lib/Types.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@ module rec Types =
type Name = Name of string


/// <summary>
/// The minimal value in
/// a `ValueRange`. Can be inclusive
/// or exclusive.
/// </summary>
type Minimum =
| MinIncl of ValueUnit
| MinExcl of ValueUnit


/// <summary>
/// The maximum value in
/// a `ValueRange`. Can be inclusive
/// or exclusive.
/// </summary>
type Maximum =
| MaxIncl of ValueUnit
| MaxExcl of ValueUnit
Expand All @@ -44,70 +48,85 @@ module rec Types =
type Increment = Increment of ValueUnit


/// <summary>
/// Represents a domain of rational numbers.
/// </summary>
/// <remarks>
/// A `ValueRange` can be one of the following:
/// <list type="bullet">
/// <item><description>`Unrestricted`: any rational number</description></item>
/// <item><description>`Increment`: any number that is a multiple of an increment</description></item>
/// <item><description>`Min`: has a minimum</description></item>
/// <item><description>`MinIncrement`: a minimum with the domain consisting of multiples of one increment</description></item>
/// <item><description>`Max`: has a maximum</description></item>
/// <item><description>`IncrementMax`: a domain of multiples of an increment with a maximum</description></item>
/// <item><description>`MinMax`: has both a minimum and maximum</description></item>
/// </list>
/// </remarks>
type ValueRange =
| Unrestricted
| NonZeroNoneNegative
| Min of Minimum
| Max of Maximum
| MinMax of Minimum * Maximum
| Incr of Increment
| MinIncr of min: Minimum * incr: Increment
| IncrMax of incr: Increment * max: Maximum
| MinIncrMax of min: Minimum * incr: Increment * max: Maximum
| ValSet of ValueSet // Set<BigRational>
/// <summary>
/// Represents a domain of rational numbers.
/// </summary>
/// <remarks>
/// A `ValueRange` can be one of the following:
/// <list type="bullet">
/// <item><description>`Unrestricted`: any rational number</description></item>
/// <item><description>`NonZeroNoneNegative`: any positive rational number greater than zero</description></item>
/// <item><description>`Min`: has a minimum</description></item>
/// <item><description>`Max`: has a maximum</description></item>
/// <item><description>`MinMax`: has both a minimum and maximum</description></item>
/// <item><description>`Incr`: any number that is a multiple of an increment</description></item>
/// <item><description>`MinIncr`: a minimum with the domain consisting of multiples of one increment</description></item>
/// <item><description>`IncrMax`: a domain of multiples of an increment with a maximum</description></item>
/// <item><description>`MinIncrMax`: a minimum with a domain of multiples of an increment with a maximum</description></item>
/// <item><description>`ValSet`: a set of discrete values</description></item>
/// </list>
/// </remarks>
type ValueRange =
| Unrestricted
| NonZeroNoneNegative
| Min of Minimum
| Max of Maximum
| MinMax of Minimum * Maximum
| Incr of Increment
| MinIncr of min: Minimum * incr: Increment
| IncrMax of incr: Increment * max: Maximum
| MinIncrMax of min: Minimum * incr: Increment * max: Maximum
| ValSet of ValueSet // Set<BigRational>


/// <summary>
/// Represents a variable in an
/// `Equation`. The variable is
/// identified by `Name` and has
/// a `Values` described by the
/// `ValueRange`.
/// </summary>
type Variable = { Name: Name; Values: ValueRange }


/// <summary>
/// Represents a property of a `Variable`.
/// </summary>
type Property =
| MinProp of Minimum
| MaxProp of Maximum
| IncrProp of Increment
| ValsProp of ValueSet


/// <summary>
/// An equation is either a `ProductEquation`
/// or a `Sumequation`, the first variable is the
/// dependent variable, i.e. the result of the
/// equation, the second part are the independent
/// variables in the equation
/// </summary>
type Equation =
| ProductEquation of Variable * Variable list
| SumEquation of Variable * Variable list


/// <summary>
/// The `Result` of solving an `Equation`
/// is that either the `Equation` is the
/// same or has `Changed`.
/// </summary>
type SolveResult =
| Unchanged
| Changed of List<Variable * Property Set>
| Errored of Exceptions.Message list


/// <summary>
/// Represents a constraint on a `Variable`.
/// I.e. either a set of values, or an increment
/// or a minimum of maximum.
/// </summary>
type Constraint =
{
Name: Name
Expand Down
21 changes: 18 additions & 3 deletions src/Informedica.GenSolver.Lib/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ module Utils =
/// <param name="exact">Whether the exact values should be printed</param>
/// <example>
/// <code>
/// ValueUnit.create [| 1N; 2N |] Units.Mass.milliGram |> toStr false
///
/// [| 1N/3N |] |> ValueUnit.create Units.Mass.milliGram |> toStr false
/// // returns "0,333 mg"
/// [| 1N/3N |] |> ValueUnit.create Units.Mass.milliGram |> toStr true
/// // returns "1/3 mg"
/// </code>
/// </example>
let toStr exact =
Expand All @@ -56,6 +58,19 @@ module Utils =
toStringDecimalDutchShortWithPrec 3


/// <summary>
/// Print a ValueUnit to a string with a given precision delimited by "#" for Value and "|" for Unit
/// </summary>
/// <param name="prec">The precision with which value should be printed</param>
/// <param name="vu">The ValueUnit to print</param>
/// <example>
/// <code>
/// [| 1N/3N |] |> ValueUnit.create Units.Mass.milliGram |> toDelimitedString 2
/// // returns "#0,33# |mg|"
/// [| 1N/3N; 1N/5N |] |> ValueUnit.create Units.Mass.milliGram |> toDelimitedString 2
/// // returns "#0,33#, #0,2# |mg|"
/// </code>
/// </example>
let toDelimitedString prec vu =
let u =
vu
Expand Down Expand Up @@ -122,7 +137,7 @@ module Utils =

/// Match an operator `op` to either
/// multiplication, division, addition
/// or subtraction, returns `NoOp` when
/// or subtraction, fails when
/// the operation is neither.
let (|Mult|Div|Add|Subtr|) op =
match op with
Expand Down
2 changes: 2 additions & 0 deletions src/Informedica.GenSolver.Lib/Variable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Variable =

open Informedica.Utils.Lib.BCL


/// Create with continuation with **succ** function
/// when success and **fail** function when failure.
/// Creates a `Name` from a`string`.
Expand All @@ -34,6 +35,7 @@ module Variable =
/// an `NameException` when it fails.
let createExc = create id (raiseExc [])


/// Return the `string` value of a `Name`.
let toString (Name s) = s

Expand Down

0 comments on commit 38c98d6

Please sign in to comment.