Skip to content

Commit a25a9d1

Browse files
authored
Use random seed by default unless user specifies one (#29)
# updated devel branch to match master and also fixed tests
1 parent 97690fe commit a25a9d1

22 files changed

+105
-46
lines changed

src/Rcpp-adapters/RcppModules.cpp

Lines changed: 81 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
* @author Mahmoud ElKarargy
1111
* @author David Helmy
1212
* @date 2023-01-20
13-
**/
13+
**/
1414

1515
#include <Rcpp.h>
1616

@@ -32,38 +32,100 @@ RCPP_MODULE(ExaGeoStatCPP) {
3232

3333
/** Hardware Class **/
3434
class_<ExaGeoStatHardware>("Hardware")
35-
.constructor<std::string, int, int, int, int>()
36-
.method("finalize_hardware", &ExaGeoStatHardware::FinalizeHardware, "Manually finalize the hardware");
35+
.constructor<std::string, int, int, int, int>()
36+
.method("finalize_hardware", &ExaGeoStatHardware::FinalizeHardware,
37+
"Manually finalize the hardware");
3738

3839
/** Data Class **/
3940
class_<ExaGeoStatData<double>>("Data")
40-
.constructor<int, std::string>();
41+
.constructor<int, std::string>();
4142

4243
function("simulate_data", &exageostat::adapters::R_ExaGeoStatLoadData,
43-
List::create(_["kernel"], _["initial_theta"], _["distance_matrix"] = "euclidean", _["problem_size"],
44-
_["seed"] = 0, _["dts"], _["lts"] = 0, _["dimension"] = "2D", _["log_path"] = "",
45-
_["data_path"] = "", _["observations_file"] = "", _["recovery_file"] = ""));
44+
List::create(
45+
_["kernel"],
46+
_["initial_theta"],
47+
_["distance_matrix"] = "euclidean",
48+
_["problem_size"],
49+
_["seed"] = static_cast<unsigned int>(time(0)),
50+
_["dts"],
51+
_["lts"] = 0,
52+
_["dimension"] = "2D",
53+
_["log_path"] = "",
54+
_["data_path"] = "",
55+
_["observations_file"] = "",
56+
_["recovery_file"] = ""
57+
));
4658

4759
function("model_data", &exageostat::adapters::R_ExaGeoStatModelData,
48-
List::create(_["computation"] = "exact", _["kernel"], _["distance_matrix"] = "euclidean", _["lb"], _["ub"],
49-
_["tol"] = 4, _["mle_itr"], _["dts"], _["lts"] = 0, _["dimension"] = "2D", _["band"] = 0,
50-
_["max_rank"] = 500, _["acc"] = 0, _["data"] = R_NilValue, _["matrix"] = R_NilValue, _["x"] = R_NilValue,
51-
_["y"] = R_NilValue, _["z"] = R_NilValue));
60+
List::create(
61+
_["computation"] = "exact",
62+
_["kernel"],
63+
_["distance_matrix"] = "euclidean",
64+
_["lb"],
65+
_["ub"],
66+
_["tol"] = 4,
67+
_["mle_itr"],
68+
_["dts"],
69+
_["lts"] = 0,
70+
_["dimension"] = "2D",
71+
_["band"] = 0,
72+
_["max_rank"] = 500,
73+
_["acc"] = 0,
74+
_["data"] = R_NilValue,
75+
_["matrix"] = R_NilValue,
76+
_["x"] = R_NilValue,
77+
_["y"] = R_NilValue,
78+
_["z"] = R_NilValue
79+
));
5280

5381
function("predict_data", &exageostat::adapters::R_ExaGeoStatPredictData,
54-
List::create(_["kernel"], _["distance_matrix"] = "euclidean", _["estimated_theta"], _["dts"], _["lts"] = 0,
55-
_["dimension"] = "2D", _["train_data"], _["test_data"]));
82+
List::create(
83+
_["kernel"],
84+
_["distance_matrix"] = "euclidean",
85+
_["estimated_theta"],
86+
_["dts"],
87+
_["lts"] = 0,
88+
_["dimension"] = "2D",
89+
_["train_data"],
90+
_["test_data"]
91+
));
5692

5793
function("mloe_mmom", &exageostat::adapters::R_ExaGeoStatMLOE_MMOM,
58-
List::create(_["kernel"], _["distance_matrix"] = "euclidean", _["estimated_theta"], _["true_theta"],
59-
_["dts"], _["lts"] = 0, _["dimension"] = "2D", _["train_data"], _["test_data"]));
94+
List::create(
95+
_["kernel"],
96+
_["distance_matrix"] = "euclidean",
97+
_["estimated_theta"],
98+
_["true_theta"],
99+
_["dts"],
100+
_["lts"] = 0,
101+
_["dimension"] = "2D",
102+
_["train_data"],
103+
_["test_data"]
104+
));
60105

61106
function("fisher", &exageostat::adapters::R_ExaGeoStatFisher,
62-
List::create(_["kernel"], _["distance_matrix"] = "euclidean", _["estimated_theta"], _["dts"], _["lts"] = 0,
63-
_["dimension"] = "2D", _["train_data"], _["test_data"]));
107+
List::create(
108+
_["kernel"],
109+
_["distance_matrix"] = "euclidean",
110+
_["estimated_theta"],
111+
_["dts"],
112+
_["lts"] = 0,
113+
_["dimension"] = "2D",
114+
_["train_data"],
115+
_["test_data"]
116+
));
64117

65118
function("idw", &exageostat::adapters::R_ExaGeoStatIDW,
66-
List::create(_["kernel"], _["distance_matrix"] = "euclidean", _["estimated_theta"], _["dts"], _["lts"] = 0,
67-
_["dimension"] = "2D", _["train_data"], _["test_data"], _["test_measurements"]));
119+
List::create(
120+
_["kernel"],
121+
_["distance_matrix"] = "euclidean",
122+
_["estimated_theta"],
123+
_["dts"],
124+
_["lts"] = 0,
125+
_["dimension"] = "2D",
126+
_["train_data"],
127+
_["test_data"],
128+
_["test_measurements"]
129+
));
68130

69131
}

src/api/ExaGeoStat.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ using namespace exageostat::configurations;
2727
template<typename T>
2828
void ExaGeoStat<T>::ExaGeoStatLoadData(Configurations &aConfigurations, std::unique_ptr<ExaGeoStatData<T>> &aData) {
2929

30-
int seed = 0;
30+
int seed = aConfigurations.GetSeed();
3131
std::srand(seed);
3232
aConfigurations.PrintSummary();
3333
LOGGER("** ExaGeoStat data generation/loading **")
@@ -78,6 +78,7 @@ T ExaGeoStat<T>::ExaGeoStatDataModeling(Configurations &aConfigurations, std::un
7878
else {
7979
LOGGER("\t--> Modeling Using Tile Low Rank - HiCMA")
8080
}
81+
8182
// Optimize mle using nlopt.
8283
optimizing_function.optimize(aConfigurations.GetStartingTheta(), opt_f);
8384
aConfigurations.SetEstimatedTheta(aConfigurations.GetStartingTheta());
@@ -130,4 +131,4 @@ void ExaGeoStat<T>::ExaGeoStatPrediction(Configurations &aConfigurations, std::u
130131
prediction::Prediction<T>::PredictMissingData(aData, aConfigurations, apMeasurementsMatrix, *pKernel,
131132
apTrainLocations, apTestLocations);
132133
delete pKernel;
133-
}
134+
}

src/configurations/Configurations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Configurations::Configurations() {
5252
SetLowerBounds(theta);
5353
SetUpperBounds(theta);
5454
SetEstimatedTheta(theta);
55-
SetSeed(0);
55+
SetSeed(static_cast<unsigned int>(time(0)));
5656
SetLogger(false);
5757
SetUnknownObservationsNb(0);
5858
SetApproximationMode(1);

tests/cpp-tests/api/TestExaGeoStatApi.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ using namespace exageostat::configurations;
2424
void TEST_GENERATE_DATA() {
2525
SECTION("Data generation - Observations")
2626
{
27-
int seed = 0;
28-
srand(seed);
29-
3027
// Create a new synthetic_data_configurations object with the provided command line arguments
3128
Configurations synthetic_data_configurations;
3229
int N = 16;
30+
synthetic_data_configurations.SetSeed(0);
3331
synthetic_data_configurations.SetProblemSize(N);
3432
synthetic_data_configurations.SetKernelName("UnivariateMaternStationary");
3533
synthetic_data_configurations.SetDenseTileSize(9);
@@ -67,12 +65,11 @@ void TEST_GENERATE_DATA() {
6765
}
6866

6967
void TEST_MODEL_DATA(Computation aComputation) {
70-
int seed = 0;
71-
srand(seed);
7268

7369
// Create a new configurations object with the provided command line arguments
7470
Configurations configurations;
7571
int N = 16;
72+
configurations.SetSeed(0);
7673
configurations.SetProblemSize(N);
7774
configurations.SetKernelName("UnivariateMaternStationary");
7875
int dts = 8;

tests/cpp-tests/kernels/concrete/TestBivariateMaternFlexible.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void TEST_KERNEL_GENERATION_BivariateMaternFlexible() {
3131
Configurations synthetic_data_configurations;
3232

3333
int N = 27;
34+
synthetic_data_configurations.SetSeed(0);
3435
synthetic_data_configurations.SetProblemSize(N);
3536
synthetic_data_configurations.SetKernelName("BivariateMaternFlexible");
3637
synthetic_data_configurations.SetDimension(Dimension2D);
@@ -46,8 +47,6 @@ void TEST_KERNEL_GENERATION_BivariateMaternFlexible() {
4647
// initialize ExaGeoStat Hardware.
4748
auto hardware = ExaGeoStatHardware(EXACT_DENSE, 3, 0);
4849

49-
int seed = 0;
50-
srand(seed);
5150
std::unique_ptr<ExaGeoStatData<double>> data;
5251
exageostat::api::ExaGeoStat<double>::ExaGeoStatLoadData(synthetic_data_configurations, data);
5352
auto *CHAM_descriptorZ = data->GetDescriptorData()->GetDescriptor(exageostat::common::CHAMELEON_DESCRIPTOR,

tests/cpp-tests/kernels/concrete/TestBivariateMaternParsimonious.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void TEST_KERNEL_GENERATION_BivariateMaternParsimonious() {
3232
Configurations synthetic_data_configurations;
3333

3434
int N = 16;
35-
35+
synthetic_data_configurations.SetSeed(0);
3636
synthetic_data_configurations.SetProblemSize(N);
3737
synthetic_data_configurations.SetKernelName("BivariateMaternParsimonious");
3838

@@ -47,8 +47,6 @@ void TEST_KERNEL_GENERATION_BivariateMaternParsimonious() {
4747
// initialize ExaGeoStat Hardware.
4848
auto hardware = ExaGeoStatHardware(EXACT_DENSE, 3, 0);
4949

50-
int seed = 0;
51-
srand(seed);
5250
std::unique_ptr<ExaGeoStatData<double>> data;
5351
exageostat::api::ExaGeoStat<double>::ExaGeoStatLoadData(synthetic_data_configurations, data);
5452
auto *CHAM_descriptorZ = data->GetDescriptorData()->GetDescriptor(exageostat::common::CHAMELEON_DESCRIPTOR,

tests/cpp-tests/kernels/concrete/TestBivariateSpacetimeMaternStationary.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ void TEST_KERNEL_GENERATION_BivariateSpacetimeMaternStationary() {
3333
Configurations synthetic_data_configurations;
3434

3535
int N = 4;
36+
synthetic_data_configurations.SetSeed(0);
3637
synthetic_data_configurations.SetProblemSize(N);
3738
synthetic_data_configurations.SetKernelName("BivariateSpacetimeMaternStationary");
3839

@@ -48,8 +49,6 @@ void TEST_KERNEL_GENERATION_BivariateSpacetimeMaternStationary() {
4849
// initialize ExaGeoStat Hardware.
4950
auto hardware = ExaGeoStatHardware(EXACT_DENSE, 3, 0);
5051

51-
int seed = 0;
52-
srand(seed);
5352
std::unique_ptr<ExaGeoStatData<double>> data;
5453
exageostat::api::ExaGeoStat<double>::ExaGeoStatLoadData(synthetic_data_configurations, data);
5554
auto *CHAM_descriptorZ = data->GetDescriptorData()->GetDescriptor(exageostat::common::CHAMELEON_DESCRIPTOR,

tests/cpp-tests/kernels/concrete/TestTrivariateMaternParsimonious.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ void TEST_KERNEL_GENERATION_TrivariateMaternParsimonious() {
3333
Configurations synthetic_data_configurations;
3434

3535
int N = 16;
36+
synthetic_data_configurations.SetSeed(0);
3637
synthetic_data_configurations.SetProblemSize(N);
3738
synthetic_data_configurations.SetKernelName("TrivariateMaternParsimonious");
3839

@@ -45,8 +46,6 @@ void TEST_KERNEL_GENERATION_TrivariateMaternParsimonious() {
4546
// initialize ExaGeoStat Hardware.
4647
auto hardware = ExaGeoStatHardware(EXACT_DENSE, 3, 0);
4748

48-
int seed = 0;
49-
srand(seed);
5049
std::unique_ptr<ExaGeoStatData<double>> data;
5150
exageostat::api::ExaGeoStat<double>::ExaGeoStatLoadData(synthetic_data_configurations, data);
5251
auto *CHAM_descriptorZ = data->GetDescriptorData()->GetDescriptor(exageostat::common::CHAMELEON_DESCRIPTOR,

tests/cpp-tests/kernels/concrete/TestUnivariateMaternDbeta.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void TEST_KERNEL_GENERATION_UnivariateMaternDbeta() {
2929
// Create a new synthetic_data_configurations object with the provided command line arguments
3030
Configurations synthetic_data_configurations;
3131
int N = 9;
32+
synthetic_data_configurations.SetSeed(0);
3233
synthetic_data_configurations.SetProblemSize(N);
3334
synthetic_data_configurations.SetKernelName("UnivariateMaternDbeta");
3435
int dts = 5;

tests/cpp-tests/kernels/concrete/TestUnivariateMaternDdbetaBeta.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void TEST_KERNEL_GENERATION_UnivariateMaternDdbetaBeta() {
3131
// Create a new synthetic_data_configurations object with the provided command line arguments
3232
Configurations synthetic_data_configurations;
3333
int N = 16;
34+
synthetic_data_configurations.SetSeed(0);
3435
synthetic_data_configurations.SetProblemSize(N);
3536
synthetic_data_configurations.SetKernelName("UnivariateMaternDdbetaBeta");
3637

0 commit comments

Comments
 (0)