Skip to content

Commit

Permalink
chore: updating comments for GenOrder, removed unnecessary loop in solve
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper Bollen authored and Casper Bollen committed Nov 1, 2023
1 parent cb659d7 commit 6fb9d7c
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 42 deletions.
4 changes: 3 additions & 1 deletion src/Informedica.GenOrder.Lib/Exceptions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Exceptions =
/// Equation exception
exception OrderException of Exceptions.Message


/// Raise an `EquationException` with `Message` `m`.
let raiseExc log m o =
match log with
Expand All @@ -30,4 +31,5 @@ module Exceptions =
match m.Data0 with
| Exceptions.OrderCouldNotBeSolved(m, o) ->
$"{o} could not be resolved because: {m}"
| _ -> $"cannot turn {exn} to string"
| _ ->
exn.ToString()
17 changes: 17 additions & 0 deletions src/Informedica.GenOrder.Lib/Logging.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,34 @@ module Logging =
|> logger.Log


/// <summary>
/// Log an informative message
/// </summary>
/// <param name="logger">The logger</param>
/// <param name="msg">The message</param>
let logInfo logger msg =
msg
|> Logging.OrderEvent
|> log LoggingType.Informative logger


/// <summary>
/// Log a warning message
/// </summary>
/// <param name="logger">The logger</param>
/// <param name="msg">The message</param>
let logWarning logger msg =
msg
|> Logging.OrderEvent
|> log LoggingType.Warning logger



/// <summary>
/// Log an error message
/// </summary>
/// <param name="logger">The logger</param>
/// <param name="msg">The message</param>
let logError (logger : LoggingType.Logger) msg =
msg
|> Logging.OrderException
Expand Down
15 changes: 8 additions & 7 deletions src/Informedica.GenOrder.Lib/Order.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1461,13 +1461,14 @@ module Order =
ovar
|> OrderVariable.minIncrMaxToValues n
)

ord
|> fromOrdVars ovars
|> solveOrder false logger
|> function
| Ok ord -> loop flag ord
| Error _ -> ord
if not flag then ord
else
ord
|> fromOrdVars ovars
|> solveOrder false logger
|> function
| Ok ord -> loop flag ord
| Error _ -> ord

loop true ord
// |> fun ord ->
Expand Down
80 changes: 65 additions & 15 deletions src/Informedica.GenOrder.Lib/OrderVariable.fs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace Informedica.GenOrder.Lib




/// Functions that deal with the `VariableUnit` type
module OrderVariable =


open MathNet.Numerics

open Informedica.Utils.Lib
Expand All @@ -14,6 +13,7 @@ module OrderVariable =
open Informedica.GenSolver.Lib
open Informedica.GenUnits.Lib


module ValueRange = Variable.ValueRange
module Minimum = ValueRange.Minimum
module Maximum = ValueRange.Maximum
Expand All @@ -28,6 +28,13 @@ module OrderVariable =
module Constraints =


/// <summary>
/// Create a `Constraints` record
/// </summary>
/// <param name="min">An optional Minimum</param>
/// <param name="incr">An optional Increment</param>
/// <param name="max">An optional Maximum</param>
/// <param name="vs">An optional ValueSet</param>
let create min incr max vs =
{
Min = min
Expand All @@ -36,6 +43,21 @@ module OrderVariable =
Values = vs
}


/// <summary>
/// Get the string representation of a `Constraints` record (in Dutch)
/// </summary>
/// <param name="cs">The Constraints record</param>
/// <remarks>
/// <code>
/// let min = 1N |> ValueUnit.singleWithUnit Units.Mass.milliGram |> Minimum.create true
/// let max = 10N |> ValueUnit.singleWithUnit Units.Mass.milliGram |> Maximum.create false
/// let incr = 1N |> ValueUnit.singleWithUnit Units.Mass.milliGram |> Increment.create
/// let cs = Constraints.create (Some min) (Some incr) (Some max) None
/// cs |> toString
/// // vanaf 1 mg[Mass] per 1 mg[Mass] tot 10 mg[Mass]
/// </code>
/// </remarks>
let toString (cs : Constraints) =
let toStr = ValueUnit.toStringDutchShort

Expand All @@ -55,17 +77,32 @@ module OrderVariable =
|> ValueUnit.toStringDutchShort


/// <summary>
/// Map the functions, fMin, fMax, fIncr and fVals over the `Constraints` record
/// </summary>
/// <param name="fMin">The function to map the Min</param>
/// <param name="fMax">The function to map the Max</param>
/// <param name="fIncr">The function to map the Incr</param>
/// <param name="fVals">The function to map the Values</param>
/// <param name="cs">The Constraints record</param>
let map fMin fMax fIncr fVals (cs : Constraints) =
{ cs with
Min = cs.Min |> Option.map fMin
Max = cs.Max |> Option.map fMax
Incr = cs.Incr |> Option.map fIncr
Values = cs.Values |> Option.map fVals

}


/// Create a `OrderVariable` with preset values
/// <summary>
/// Create an OrderVariable, an OrderVariable is a Variable with Constraints
/// </summary>
/// <param name="n">The Name of the Variable</param>
/// <param name="min">An optional Minimum of the Variable</param>
/// <param name="incr">An optional Increment of the Variable</param>
/// <param name="max">An optional Maximum of the Variable</param>
/// <param name="vs">An optional ValueSet of the Variable</param>
/// <param name="cs">The Constraints for the OrderVariable</param>
let create n min incr max vs cs =
ValueRange.create min incr max vs
|> fun vlr ->
Expand All @@ -76,19 +113,26 @@ module OrderVariable =
}


/// Create a new `VariableUnit` with
/// `Name` **nm** and `Unit` **un**
/// <summary>
/// Create a new OrderVariable. The OrderVariable will have an exclusive
/// Minimum of zero and a Constraints with an exclusive Minimum of zero
/// </summary>
/// <param name="n">The Name of the Variable</param>
/// <param name="un">The Unit of the Values in the Variable and Constraints</param>
let createNew n un =
let vu = 0N |> ValueUnit.createSingle un
let min = Minimum.create false vu |> Some

Constraints.create min None None None
|> create n min None None None

/// Apply **f** to `VariableUnit` **vru**
let apply f (ovar: OrderVariable) = ovar |> f


/// <summary>
/// Map a function f to the Values (i.e. ValueUnit) of the Variable
/// of the OrderVariable
/// </summary>
/// <param name="f">The function to map</param>
/// <param name="ovar">The OrderVariable</param>
let map f (ovar: OrderVariable) =
{ ovar with
Variable =
Expand All @@ -101,21 +145,27 @@ module OrderVariable =
}


/// Utility function to facilitate type inference
let get = apply id


/// Get the `Variable` from a `VariableUnit`
/// Get the `Variable` from an OrderVariable
let getVar { Variable = var } = var


/// Get the `Variable.Name` from a `VariableUnit` **vru**
/// Get the `Variable.Name` from an OrderVariable
let getName ovar = (ovar |> getVar).Name


/// Check whether two OrderVariables have the same name
let eqsName ovar1 ovar2 = (ovar1 |> getName) = (ovar2 |> getName)


/// <summary>
/// Apply the constraints of an OrderVariable to the Variable
/// of the OrderVariable.
/// </summary>
/// <param name="ovar">The OrderVariable</param>
/// <remarks>
/// If the Constraints have an Increment and a ValueSet, then the
/// only the Increment is applied to the Variable.
/// </remarks>
let applyConstraints (ovar : OrderVariable) =
{ ovar with
Variable =
Expand Down
2 changes: 2 additions & 0 deletions src/Informedica.GenOrder.Lib/Scripts/Api2.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ open Informedica.GenOrder.Lib


let path = Some $"{__SOURCE_DIRECTORY__}/log.txt"


let startLogger () =
// Start the logger at an informative level
OrderLogger.logger.Start path Logging.Level.Informative
Expand Down
1 change: 1 addition & 0 deletions src/Informedica.GenOrder.Lib/Scripts/load.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ open Informedica.Utils.Lib
let zindexPath = __SOURCE_DIRECTORY__ |> Path.combineWith "../../../"
Environment.CurrentDirectory <- zindexPath


10 changes: 10 additions & 0 deletions src/Informedica.GenOrder.Lib/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,19 @@ module Utils =



/// <summary>
/// Get data from a google sheet containing constraints
/// </summary>
/// <param name="sheet">The sheet to get</param>
/// <returns>A array table of string arrays</returns>
let getDataFromConstraints sheet = GoogleSheets.getDataFromSheet constraints sheet


/// <summary>
/// Get data from a google sheet containing data for GenPres
/// </summary>
/// <param name="sheet">The sheet to get</param>
/// <returns>A array table of string arrays</returns>
let getDataFromGenPres sheet = GoogleSheets.getDataFromSheet genpres sheet


30 changes: 25 additions & 5 deletions src/Informedica.GenOrder.Lib/ValueUnit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,44 @@ namespace Informedica.GenOrder.Lib
/// `Informedica.GenUnits.Lib`
module ValueUnit =

open MathNet.Numerics

open Informedica.Utils.Lib.BCL
open Informedica.GenUnits.Lib

open ValueUnit



/// <summary>
/// Return a Unit as a short Dutch string
/// </summary>
let unitToString =
Units.toString Units.Dutch Units.Short


/// <summary>
/// Check if a Unit is an adjust unit, i.e.
/// kg or m2.
/// </summary>
/// <param name="u">The Unit</param>
let isAdjust (u : Unit) =
u |> Group.eqsGroup (Units.Weight.kiloGram) ||
u |> Group.eqsGroup (Units.BSA.m2)


/// <summary>
/// Put an adjust unit in the middle of a combined unit.
/// </summary>
/// <param name="vu">The ValueUnit</param>
/// <example>
/// <code>
/// let u1 = Units.Mass.milliGram
/// let u2 = Units.Weight.kiloGram
/// let u3 = Units.Time.day
/// // mg/day/kg
/// let vu1 = withValue [|1N|] (u1 |> per u3 |> per u2)
/// // = mg/kg/day
/// let vu2 = vu1 |> correctAdjustOrder
/// // check
/// vu1 |> eqsGroup vu2
/// </code>
/// </example>
let correctAdjustOrder vu =
let v, u = vu |> get
match u |> getUnits with
Expand Down
32 changes: 23 additions & 9 deletions src/Informedica.GenOrder.Lib/Variable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,41 @@ module Variable =
open Informedica.GenSolver.Lib.Variable.ValueRange


let inline setOpt m set vr =
let inline private setOpt aOption set vr =
try
match m with
match aOption with
| Some m -> vr |> set m
| None -> vr
with | _ -> vr // TODO: ugly fix need to refactor


/// <summary>
/// Sets an optional minimum value for the value range
/// </summary>
/// <param name="min">The optional Minimum</param>
/// <param name="vr">The ValueRange</param>
let setOptMin min vr = vr |> setOpt min setMin


/// <summary>
/// Sets an optional maximum value for the value range
/// </summary>
/// <param name="max">The optional Maximum</param>
/// <param name="vr">The ValueRange</param>
let setOptMax max vr = vr |> setOpt max setMax


/// <summary>
/// Sets an optional increment value for the value range
/// </summary>
/// <param name="incr">The optional Increment</param>
/// <param name="vr">The ValueRange</param>
let setOptIncr incr vr = vr |> setOpt incr setIncr


let setOptVs vs vr =
try
match vs with
| Some vs -> vr |> setValueSet vs
| None -> vr

with | _ -> vr // TODO: ugly fix need to refactor
/// <summary>
/// Sets an optional value set for the value range
/// </summary>
/// <param name="vs">The optional ValueSet</param>
/// <param name="vr">The ValueRange</param>
let setOptVs vs vr = vr |> setOpt vs setValueSet
Loading

0 comments on commit 6fb9d7c

Please sign in to comment.