| 1 | /* -*- mode: C++ -*- */ |
|---|
| 2 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 3 | // FILE : test.tol |
|---|
| 4 | // PURPOSE: Package MatQuery test |
|---|
| 5 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 6 | |
|---|
| 7 | Text email_ = "vdebuen@tol-project.org"; |
|---|
| 8 | Text link_ = ""; |
|---|
| 9 | Text summary_ = "checking matrix row selection"; |
|---|
| 10 | |
|---|
| 11 | //Write here the initial test TOL code if needed |
|---|
| 12 | Real numErr0 = Copy(NError); |
|---|
| 13 | Real numWar0 = Copy(NWarning); |
|---|
| 14 | |
|---|
| 15 | //Random seed setting |
|---|
| 16 | Real PutRandomSeed(0); |
|---|
| 17 | Real rndSeed = GetRandomSeed(0); |
|---|
| 18 | WriteLn("Current Random Seed = "<<rndSeed); |
|---|
| 19 | |
|---|
| 20 | #Embed "../../MatQuery.tol"; |
|---|
| 21 | //#Require MatQuery; |
|---|
| 22 | |
|---|
| 23 | //The order of magnitude required for number of geographic points |
|---|
| 24 | Real point.num.approx = 20000; |
|---|
| 25 | //Number of cities |
|---|
| 26 | Real city.num = 500; |
|---|
| 27 | //Distance around targets |
|---|
| 28 | Real distance = 2000; |
|---|
| 29 | //Factor of Country size respect dtarget distance |
|---|
| 30 | Real coordFactor = 400; |
|---|
| 31 | //Distance of points inside a city |
|---|
| 32 | Real cityInsideDistance = distance / 4; |
|---|
| 33 | |
|---|
| 34 | //Country dimensions |
|---|
| 35 | Real coord.min = -distance*coordFactor; |
|---|
| 36 | Real coord.max = +distance*coordFactor; |
|---|
| 37 | |
|---|
| 38 | //Log-Normal random generation of number of point by city |
|---|
| 39 | Matrix city.points.num = |
|---|
| 40 | { |
|---|
| 41 | Real sigma = 0.9995; |
|---|
| 42 | Real ratio = point.num.approx/city.num; |
|---|
| 43 | Real nu = Log(ratio)-0.5*sigma^2; |
|---|
| 44 | Round(Exp(Gaussian(city.num,1,nu,0.99995))) |
|---|
| 45 | }; |
|---|
| 46 | Matrix city.points.freq = Frequency(city.points.num,100); |
|---|
| 47 | |
|---|
| 48 | //Exact number of geographic points |
|---|
| 49 | Real point.num = MatSum(city.points.num); |
|---|
| 50 | |
|---|
| 51 | //Locations of cities |
|---|
| 52 | Matrix city.center = BinGroup("<<",For(1,city.num, Matrix(Real city) |
|---|
| 53 | { |
|---|
| 54 | Rand(1, 2, coord.min, coord.max) |
|---|
| 55 | })); |
|---|
| 56 | |
|---|
| 57 | //Locations of points inside each city as gaussian around the center |
|---|
| 58 | Set city.points = For(1,city.num, Matrix(Real city) |
|---|
| 59 | { |
|---|
| 60 | Real n = MatDat(city.points.num,city,1); |
|---|
| 61 | Real x = MatDat(city.center,city,1); |
|---|
| 62 | Real y = MatDat(city.center,city,2); |
|---|
| 63 | Matrix X = Rand(n,1,x,x); |
|---|
| 64 | Matrix Y = Rand(n,1,y,y); |
|---|
| 65 | (X|Y)+Gaussian(n, 2, 0, cityInsideDistance) |
|---|
| 66 | }); |
|---|
| 67 | |
|---|
| 68 | //All points |
|---|
| 69 | VMatrix points = Mat2VMat(BinGroup("<<",city.points)); |
|---|
| 70 | |
|---|
| 71 | //Target point |
|---|
| 72 | VMatrix p0 = SubRow(points,[[IntRand(1,point.num)]])+ |
|---|
| 73 | Gaussian(1, 2, 0, cityInsideDistance); |
|---|
| 74 | |
|---|
| 75 | Real t0 = Copy(Time); |
|---|
| 76 | //Selecting neighbors of target in an hipercube |
|---|
| 77 | WriteLn("SelectRowsInHyperCube full ..."); |
|---|
| 78 | Set p0.select.idx.HC = MatQuery::SelectRowsInHyperCube( |
|---|
| 79 | points, p0, distance); |
|---|
| 80 | VMatrix p0.select.HC = SubRow(points,p0.select.idx.HC); |
|---|
| 81 | Real t1 = Copy(Time); |
|---|
| 82 | WriteLn("SelectRowsInHyperCube full "<<VRows(p0.select.HC)+" items " |
|---|
| 83 | "Time:"<<(t1-t0)); |
|---|
| 84 | |
|---|
| 85 | //Selecting neighbors of target in an hipersphere |
|---|
| 86 | WriteLn("SelectRowsInHyperSphere full ..."); |
|---|
| 87 | Set p0.select.idx.HE.1 = MatQuery::SelectRowsInHyperSphere( |
|---|
| 88 | points, p0, distance); |
|---|
| 89 | VMatrix p0.select.HE.1 = SubRow(points,p0.select.idx.HE.1); |
|---|
| 90 | Real t2 = Copy(Time); |
|---|
| 91 | WriteLn("SelectRowsInHyperSphere full "<<VRows(p0.select.HE.1)+" items " |
|---|
| 92 | "Time:"<<(t2-t1)); |
|---|
| 93 | |
|---|
| 94 | //Selecting neighbors of target in an hipersphere from the hipercube |
|---|
| 95 | WriteLn("SelectRowsInHyperSphere over SelectRowsInHyperCube ..."); |
|---|
| 96 | Set p0.select.idx.HE.2 = MatQuery::SelectRowsInHyperSphere( |
|---|
| 97 | p0.select.HC, p0, distance); |
|---|
| 98 | VMatrix p0.select.HE.2 = SubRow(p0.select.HC,p0.select.idx.HE.2); |
|---|
| 99 | Real t3 = Copy(Time); |
|---|
| 100 | WriteLn("SelectRowsInHyperSphere over SelectRowsInHyperCube "<< |
|---|
| 101 | VRows(p0.select.HE.2)+" items Time:"<<(t3-t2)); |
|---|
| 102 | WriteLn("SelectRowsInHyperSphere+SelectRowsInHyperCube Time:"<<(t3-t2+t1-t0)); |
|---|
| 103 | |
|---|
| 104 | Real ok.1 = And( |
|---|
| 105 | Card(p0.select.idx.HE.1) <= Card(p0.select.idx.HC), |
|---|
| 106 | Card(p0.select.idx.HE.1 * p0.select.idx.HC) == |
|---|
| 107 | Card(p0.select.idx.HE.1)); |
|---|
| 108 | Real ok.2 = VMatMax(Abs(p0.select.HE.2-p0.select.HE.1))==0; |
|---|
| 109 | |
|---|
| 110 | Real numErr1 = Copy(NError); |
|---|
| 111 | Real numWar1 = Copy(NWarning); |
|---|
| 112 | |
|---|
| 113 | Set partialResults_ = [[numErr0, numErr1, |
|---|
| 114 | ok.1, ok.2]]; |
|---|
| 115 | |
|---|
| 116 | //This is a messure of the success of the test |
|---|
| 117 | Real quality_ = And(numErr1 == numErr0, |
|---|
| 118 | numWar1 == numWar0, |
|---|
| 119 | ok.1, ok.2); |
|---|
| 120 | |
|---|
| 121 | //Return the results |
|---|
| 122 | Set resultStr_ = @strTestStatus(summary_, link_, quality_, |
|---|
| 123 | "Partial results = "<<partialResults_, |
|---|
| 124 | "NO DBServerType", "NO DBServerHost", "NO DBAlias", |
|---|
| 125 | email_); |
|---|
| 126 | WriteLn(""<<resultStr_); |
|---|
| 127 | resultStr_; |
|---|
| 128 | |
|---|
| 129 | /* */ |
|---|
| 130 | |
|---|