Skip to content

Commit

Permalink
tests: implemented new min max tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper Bollen authored and Casper Bollen committed Oct 7, 2023
1 parent a2231dd commit 51757a7
Show file tree
Hide file tree
Showing 5 changed files with 647 additions and 276 deletions.
12 changes: 12 additions & 0 deletions src/Informedica.GenOrder.Lib/Scripts/Script1.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ open Informedica.GenSolver.Lib
open Informedica.GenOrder.Lib


let vu1 =
[|1N..1N..100N|]
|> ValueUnit.withUnit Units.Count.times
let vu2 =
[|1N..1N..10N|]
|> ValueUnit.withUnit Units.Count.times

(vu1 * vu2)
|> ValueUnit.getValue
|> Array.length



module List =

Expand Down
7 changes: 4 additions & 3 deletions src/Informedica.GenSolver.Lib/Scripts/Tests.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -1346,18 +1346,19 @@ module Tests =
|> Expect.isTrue "should all be Unrestricted"
}

test "when mult/div is applied to x1 = Zero and x2 = Unrestricted" {
test "when mult, div is applied to x1 = Zero and x2 = Unrestricted" {
let x1 = 0N |> ValueUnit.singleWithUnit Units.Count.times |> ValueRange.createValSet
let x2 = ValueRange.Unrestricted
let exp = ValSet (ValueSet (ValueUnit ([|0N|], ZeroUnit)))
[
calc (*) (x1, x2)
// calc (/) (x1, x2) TODO: need to fix this
]
|> List.forall (fun y -> y = x1)
|> List.forall (fun y -> y = exp)
|> Expect.isTrue "should all be Zero"
}

test "when add/sub is applied to x1 = Zero and x2 = Unrestricted" {
test "when add/, sub is applied to x1 = Zero and x2 = Unrestricted" {
let x1 = 0N |> ValueUnit.singleWithUnit Units.Count.times |> ValueRange.createValSet
let x2 = ValueRange.Unrestricted
[
Expand Down
4 changes: 2 additions & 2 deletions src/Informedica.GenSolver.Lib/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ module Utils =
/// multiplication, division, addition
/// or subtraction, fails when
/// the operation is neither.
let (|Mult|Div|Add|Subtr|) op =
let (|Mult|Div|Add|Sub|) op =
match op with
| _ when op |> opIsMult -> Mult
| _ when op |> opIsDiv -> Div
| _ when op |> opIsAdd -> Add
| _ when op |> opIsSubtr -> Subtr
| _ when op |> opIsSubtr -> Sub
| _ -> failwith "Operator is not supported"

42 changes: 23 additions & 19 deletions src/Informedica.GenSolver.Lib/Variable.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1776,7 +1776,8 @@ module Variable =
MinIncrMax(min, incr, max)

else
// TODO: ugly hack to prevent expensive calc
// TODO: ugly hack to prevent expensive calc, throws exception when too many values
// TODO: probably not correct, because this could lead to a ValueSet that has too many values
try
ValueSet.minIncrMaxToValueSet min incr max
|> ValSet
Expand Down Expand Up @@ -2515,10 +2516,10 @@ module Variable =

// SUBTRACTION
// subtracting a None to another value always results in a None
| None, _, Some _, _, _, _, Subtr
| Some _, _, None, _, _, _, Subtr -> None
| None, _, Some _, _, _, _, Sub
| Some _, _, None, _, _, _, Sub -> None
// in any other case we can calculate the result
| Some v1, _, Some v2, _, _, _, Subtr -> v1 |> op <| v2 |> c isIncl |> Some
| Some v1, _, Some v2, _, _, _, Sub -> v1 |> op <| v2 |> c isIncl |> Some


/// Calculate an optional `Minimum`
Expand Down Expand Up @@ -2734,16 +2735,19 @@ module Variable =
| Mult -> multiplication
| Div -> division
| Add -> addition
| Subtr -> subtraction
| Sub -> subtraction


/// Applies an infix operator **op**
/// to `ValueRange` **x1** and **x2**.
/// Calculates `Minimum`, increment or `Maximum`
/// if either **x1** or **x2** is not a `ValueSet`.
/// Doesn't perform any calculation when both
/// **x1** and **x2** are `Unrestricted`.
let calc onlyMinIncrMax op (x1, x2) =
/// <summary>
/// Applies an infix operator `op` (either *, /, + or -)
/// to `ValueRange` vr1 and vr2. If onlyMinIncrMax then
/// only the minimum, increment and maximum are calculated.
/// </summary>
/// <param name="onlyMinIncrMax">Whether only the minimum, increment and maximum should be calculated</param>
/// <param name="op">The infix operator</param>
/// <param name="vr1">The first `ValueRange`</param>
/// <param name="vr2">The second `ValueRange`</param>
let calc onlyMinIncrMax op (vr1, vr2) =

let calcMinMax min1 max1 min2 max2 =
let getMin m =
Expand All @@ -2764,15 +2768,15 @@ module Variable =

MinMaxCalculator.calcMinMax op (min1 |> getMin) (max1 |> getMax) (min2 |> getMin) (max2 |> getMax)

match x1, x2 with
match vr1, vr2 with
| Unrestricted, Unrestricted -> unrestricted
| ValSet s1, ValSet s2 ->
if not onlyMinIncrMax ||
(s1 |> ValueSet.count = 1 && (s2 |> ValueSet.count = 1)) then
ValueSet.calc op s1 s2 |> ValSet
else
let min1, max1 = x1 |> getMin, x1 |> getMax
let min2, max2 = x2 |> getMin, x2 |> getMax
let min1, max1 = vr1 |> getMin, vr1 |> getMax
let min2, max2 = vr2 |> getMin, vr2 |> getMax

let min, max =
calcMinMax min1 max1 min2 max2
Expand All @@ -2795,8 +2799,8 @@ module Variable =
| ValSet s, MinIncrMax (_, i, _)
| MinIncrMax (_, i, _), ValSet s ->

let min1, max1 = x1 |> getMin, x1 |> getMax
let min2, max2 = x2 |> getMin, x2 |> getMax
let min1, max1 = vr1 |> getMin, vr1 |> getMax
let min2, max2 = vr2 |> getMin, vr2 |> getMax

let min, max =
calcMinMax min1 max1 min2 max2
Expand All @@ -2817,10 +2821,10 @@ module Variable =
// In any other case calculate min, incr and max
| _ ->
let min1, incr1, max1 =
x1 |> getMin, x1 |> getIncr, x1 |> getMax
vr1 |> getMin, vr1 |> getIncr, vr1 |> getMax

let min2, incr2, max2 =
x2 |> getMin, x2 |> getIncr, x2 |> getMax
vr2 |> getMin, vr2 |> getIncr, vr2 |> getMax

let min, max =
calcMinMax min1 max1 min2 max2
Expand Down
Loading

0 comments on commit 51757a7

Please sign in to comment.