Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. structure Tests =
  2. struct
  3.  
  4. type point = int * int
  5. type box = (int * int) * (int * int)
  6.  
  7. fun rng s = ((s * 991) + 1) mod 5711
  8.  
  9. structure Seq = ArraySequence
  10.  
  11. (*Tweak these numbers*)
  12. val seed = 15210 (*Seed for RNG*)
  13. val points=10 (*Number of points*)
  14. val pointRange = 20 (*x and y coordinates will come from range*)
  15. val pointBias=10 (*Range is [0-pointBias,pointRange-pointBias)*)
  16. val weightRange = 100
  17. val bias = 50 (*Generated weight is from [0-bias,weightRange-bias) *)
  18. val tests = 100000 (*Number of tests run*)
  19.  
  20. fun nums n = Seq.tabulate(fn i=>i-pointBias)(pointRange)
  21.  
  22. fun shuffle S 0 s = (S,s)
  23. | shuffle S i s =
  24. let
  25. val s' = rng s
  26. val ind = s' mod i
  27. val v = Seq.nth(S)(i-1)
  28. val S' = Seq.update(S,(i-1,Seq.nth(S)(ind)))
  29. val S'' = Seq.update(S',(ind,v))
  30. in
  31. shuffle S'' (i-1) s'
  32. end
  33.  
  34. fun randW(s) =
  35. let
  36. val s' = rng s
  37. in
  38. ((s' mod weightRange) - bias,s')
  39. end
  40.  
  41. fun genPoints xl yl 0 s = ([],s)
  42. | genPoints [] _ _ _ = raise Fail "List empty"
  43. | genPoints _ [] _ _ = raise Fail "List empty"
  44. | genPoints (x::xl) (y::yl) i s =
  45. let
  46. val (w,s') = randW(s)
  47. val (L,s'') = genPoints xl yl (i-1) s'
  48. in
  49. (((x,y),w)::L,s'')
  50. end
  51.  
  52. fun genBox s =
  53. let
  54. val s1 = rng s
  55. val x1 = (s1 mod (pointRange+3))-pointBias-1
  56. val s2 = rng s1
  57. val y1 = (s2 mod (pointRange+3))-pointBias-1
  58. val s3 = rng s2
  59. val x2 = (s3 mod (pointRange+3))-pointBias-1
  60. val s4 = rng s3
  61. val y2 = (s4 mod (pointRange+3))-pointBias-1
  62. val s5 = rng s4
  63. in
  64. if x1=x2 orelse y1=y2 then
  65. genBox s5
  66. else
  67. (((Int.min(x1,x2),Int.max(y1,y2)),(Int.max(x1,x2),Int.min(y1,y2))),s5)
  68. end
  69.  
  70. fun gen 0 s = ([],s)
  71. | gen n s =
  72. let
  73. val s1 = rng s
  74. val (L,s2) = gen (n-1) s1
  75. val (xs,s3) = shuffle(nums points)(points)(s2)
  76. val (ys,s4) = shuffle(nums points)(points)(s3)
  77. val xl = Seq.toList(xs)
  78. val yl = Seq.toList(ys)
  79. val (points,s5) = genPoints xl yl points s4
  80. val (box,s6) = genBox s5
  81. in
  82. ((points,box)::L,s6)
  83. end
  84.  
  85. (* Each value is ((x, y), w), with x, y coordinates in a 2D space and w the
  86. * weight given to this point *)
  87. val points1 = [((0,0),2),((1,2),5),((3,3),7),((4,4),3),((5,1),9)]
  88.  
  89. (* Each test is (points, box), where correctness is verified by calling
  90. * (rangeSum (Seq.fromList points) box) and comparing it with the result of
  91. * our reference solution. *)
  92. val (L,_) = (gen tests seed)
  93.  
  94. val tests : ((point * int) list * box) list = [
  95. (points1, ((1,3),(5,1)))
  96. ] @ L
  97.  
  98. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement