Skip to content

Commit 6591ae8

Browse files
authored
Implement biomass sampling experiments for metabolic networks in C++ (#197)
* implement biomass sampling experiments * modify circleci/config.yml * fix bug in log-concave tests * change test function name
1 parent f9d3414 commit 6591ae8

File tree

4 files changed

+263
-32
lines changed

4 files changed

+263
-32
lines changed

test/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ else ()
172172
include_directories (BEFORE ../include/lp_oracles)
173173
include_directories (BEFORE ../include/nlp_oracles)
174174
include_directories (BEFORE ../include/misc)
175+
include_directories (BEFORE ../test)
175176

176177
#for Eigen
177178
if (${CMAKE_VERSION} VERSION_LESS "3.12.0")
@@ -295,6 +296,8 @@ else ()
295296
COMMAND logconcave_sampling_test -tc=hmc)
296297
add_test(NAME logconcave_sampling_test_uld
297298
COMMAND logconcave_sampling_test -tc=uld)
299+
add_test(NAME logconcave_sampling_test_exponential_biomass_sampling
300+
COMMAND logconcave_sampling_test -tc=exponential_biomass_sampling)
298301

299302
add_executable (simple_mc_integration simple_mc_integration.cpp $<TARGET_OBJECTS:test_main>)
300303
add_test(NAME simple_mc_integration_over_limits

test/logconcave_sampling_test.cpp

Lines changed: 79 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ void benchmark_polytope_linear_program_optimization(
703703
bool rounding=false,
704704
bool centered=true,
705705
unsigned int max_draws=80000,
706-
unsigned int num_burns=20000) {
706+
unsigned int num_burns=10000) {
707707
typedef Cartesian<NT> Kernel;
708708
typedef std::vector<Point> pts;
709709
typedef boost::mt19937 RNGType;
@@ -756,6 +756,8 @@ void benchmark_polytope_linear_program_optimization(
756756
GaussianRDHRWalk::Walk<Polytope, RandomNumberGenerator> gaussian_walk(P, x0, lp_params.L, rng);
757757
int n_warmstart_samples = 100;
758758

759+
std::cout << "Warm start" << std::endl;
760+
759761
for (int i = 0; i < n_warmstart_samples; i++) {
760762
gaussian_walk.apply(P, x0, lp_params.L, walk_length, rng);
761763
}
@@ -788,14 +790,15 @@ void benchmark_polytope_linear_program_optimization(
788790
hmc.apply(rng, walk_length);
789791
}
790792

793+
hmc.solver->eta = hmc.solver->eta*NT(10);
794+
hmc.disable_adaptive();
795+
791796
std::cout << std::endl;
792-
std::cout << "Optimizing" << std::endl;
793-
unsigned int num_phases = 0;
797+
std::cout << "Sampling" << std::endl;
798+
unsigned int num_phases = 1;
794799

795800
auto start = std::chrono::high_resolution_clock::now();
796-
for (unsigned int j = 0; j < max_phases; j++) {
797-
num_phases++;
798-
std::cout << "Temperature " << opt_params.T << std::endl;
801+
for (unsigned int j = 0; j < 1; j++) {
799802
for (unsigned int i = 0; i < max_actual_draws; i++) {
800803
hmc.apply(rng, walk_length);
801804
if (f_lp(minimum) >= f_lp(hmc.x)) {
@@ -813,12 +816,12 @@ void benchmark_polytope_linear_program_optimization(
813816

814817
NT ETA = (NT) std::chrono::duration_cast<std::chrono::microseconds>(stop - start).count();
815818

816-
std::cerr << "Min LP Value: " << f_lp(minimum) << std::endl;
819+
std::cerr << "Min biomass Value: " << f_lp(minimum) << std::endl;
817820
std::cerr << "Argmin: " << minimum.getCoefficients().transpose() << std::endl;
818-
std::cerr << "Average ETA: " << ETA / (NT) num_phases << std::endl;
819-
std::cerr << "Average Time per Independent sample: " << ETA / total_min_ess << std::endl;
820-
std::cerr << "Average Max PSRF: " << total_max_psrf / (NT) num_phases << std::endl;
821-
std::cerr << "Average Min ESS: " << total_min_ess / (NT) num_phases << std::endl;
821+
std::cerr << "ETA: " << ETA / (NT) num_phases << std::endl;
822+
std::cerr << "Time per Independent sample: " << total_min_ess / ETA << std::endl;
823+
std::cerr << "Max PSRF: " << total_max_psrf / (NT) num_phases << std::endl;
824+
std::cerr << "Min ESS: " << total_min_ess / (NT) num_phases << std::endl;
822825
std::cerr << "Average number of reflections: " <<
823826
(1.0 * hmc.solver->num_reflections) / hmc.solver->num_steps << std::endl;
824827
std::cerr << "Step size (final): " << hmc.solver->eta << std::endl;
@@ -893,10 +896,10 @@ void call_test_benchmark_polytopes_grid_search() {
893896
std::make_tuple(generate_cube<Hpolytope>(100, false), "100_cube", false),
894897
std::make_tuple(generate_prod_simplex<Hpolytope>(50, false), "50_prod_simplex", false),
895898
std::make_tuple(generate_birkhoff<Hpolytope>(10), "10_birkhoff", false),
896-
std::make_tuple(read_polytope<Hpolytope, NT>("./metabolic_full_dim/polytope_iAB_RBC_283.ine"), "iAB_RBC_283", true),
897-
std::make_tuple(read_polytope<Hpolytope, NT>("./metabolic_full_dim/polytope_iAT_PLT_636.ine"), "iAT_PLT_636", true),
898-
std::make_tuple(read_polytope<Hpolytope, NT>("./metabolic_full_dim/polytope_e_coli.ine"), "e_coli", true),
899-
std::make_tuple(read_polytope<Hpolytope, NT>("./metabolic_full_dim/polytope_recon2.ine"), "recon2", true)
899+
std::make_tuple(read_polytope<Hpolytope, NT>("metabolic_full_dim/polytope_iAB_RBC_283.ine"), "iAB_RBC_283", true),
900+
std::make_tuple(read_polytope<Hpolytope, NT>("metabolic_full_dim/polytope_iAT_PLT_636.ine"), "iAT_PLT_636", true),
901+
std::make_tuple(read_polytope<Hpolytope, NT>("metabolic_full_dim/polytope_e_coli.ine"), "e_coli", true),
902+
std::make_tuple(read_polytope<Hpolytope, NT>("metabolic_full_dim/polytope_recon2.ine"), "recon2", true)
900903
};
901904

902905
Hpolytope P;
@@ -998,30 +1001,74 @@ void call_test_benchmark_spectrahedra_grid_search() {
9981001

9991002
}
10001003

1004+
template <typename Point, typename NT>
1005+
Point load_biomass_function(std::string name)
1006+
{
1007+
std::vector<double> v1;
1008+
std::string line;
1009+
NT element;
1010+
1011+
std::ifstream myFile(name);
1012+
while(std::getline(myFile, line))
1013+
{
1014+
std::istringstream lineStream(line);
1015+
1016+
while(lineStream >> element) {
1017+
v1.push_back(element);
1018+
}
1019+
}
1020+
Point biomass_function = Point(v1.size(), v1);
1021+
NT length = biomass_function.length();
1022+
biomass_function *= (NT(1) / length);
1023+
return biomass_function;
1024+
}
1025+
1026+
inline bool exists_check (const std::string& name) {
1027+
std::ifstream f(name.c_str());
1028+
return f.good();
1029+
}
1030+
10011031
template <typename NT>
1002-
void call_test_optimization() {
1032+
void call_test_exp_sampling() {
10031033
typedef Cartesian<NT> Kernel;
10041034
typedef typename Kernel::Point Point;
10051035
typedef HPolytope<Point> Hpolytope;
1036+
std::string name;
1037+
std::vector<std::tuple<Hpolytope, Point, std::string, bool>> polytopes;
1038+
1039+
1040+
if (exists_check("metabolic_full_dim/e_coli_biomass_function.txt") && exists_check("metabolic_full_dim/polytope_e_coli.ine")){
1041+
Point biomass_function_e_coli = load_biomass_function<Point, NT>("metabolic_full_dim/e_coli_biomass_function.txt");
1042+
polytopes.push_back(std::make_tuple(read_polytope<Hpolytope, NT>("metabolic_full_dim/polytope_e_coli.ine"), biomass_function_e_coli, "e_coli", true));
1043+
}
10061044

1007-
std::vector<std::tuple<Hpolytope, std::string, bool>> polytopes{
1008-
std::make_tuple(generate_cube<Hpolytope>(100, false), "100_cube", false),
1009-
std::make_tuple(read_polytope<Hpolytope, NT>("./metabolic_full_dim/polytope_e_coli.ine"), "e_coli", true),
1010-
};
1045+
if (exists_check("metabolic_full_dim/iAT_PTL_636_biomass_function.txt") && exists_check("metabolic_full_dim/polytope_iAT_PTL_636.ine")){
1046+
Point biomass_function_iAT = load_biomass_function<Point, NT>("metabolic_full_dim/iAT_PTL_636_biomass_function.txt");
1047+
polytopes.push_back(std::make_tuple(read_polytope<Hpolytope, NT>("metabolic_full_dim/polytope_iAT_PTL_636.ine"), biomass_function_iAT, "iAT_PTL_636", true));
1048+
}
1049+
1050+
if (exists_check("metabolic_full_dim/recon1_function.txt") && exists_check("metabolic_full_dim/polytope_recon1.ine")){
1051+
Point biomass_function_recon1 = load_biomass_function<Point, NT>("metabolic_full_dim/recon1_biomass_function.txt");
1052+
polytopes.push_back(std::make_tuple(read_polytope<Hpolytope, NT>("metabolic_full_dim/polytope_recon1.ine"), biomass_function_recon1, "recon1", true));
1053+
}
10111054

10121055
Hpolytope P;
1013-
std::string name;
10141056
std::ofstream outfile;
1057+
typedef BoostRandomNumberGenerator<boost::mt19937, NT> RNGType;
10151058

1016-
for (std::tuple<Hpolytope, std::string, bool> polytope_tuple : polytopes) {
1017-
P = std::get<0>(polytope_tuple);
1018-
name = std::get<1>(polytope_tuple);
1019-
std::cerr << name << std::endl;
1020-
P.normalize();
1021-
Point coeffs = Point::all_ones(P.dimension());
1022-
for (unsigned int walk_length = 500; walk_length <= P.dimension(); walk_length += P.dimension() / 10) {
1023-
benchmark_polytope_linear_program_optimization<NT, Hpolytope>(coeffs, P, 0, NT(-1), walk_length, false);
1024-
}
1059+
for (std::tuple<Hpolytope, Point, std::string, bool> polytope_tuple : polytopes) {
1060+
P = std::get<0>(polytope_tuple);
1061+
name = std::get<2>(polytope_tuple);
1062+
1063+
std::cerr << "Model: " + name << std::endl;
1064+
std::cout<< "Dimension of " + name + ": " <<P.dimension()<<std::endl;
1065+
1066+
P.normalize();
1067+
RNGType rng(P.dimension());
1068+
Point coeffs = std::get<1>(polytope_tuple);
1069+
for (unsigned int walk_length = P.dimension(); walk_length <= 2*P.dimension(); walk_length += P.dimension()) {
1070+
benchmark_polytope_linear_program_optimization<NT, Hpolytope>(coeffs, P, 0, NT(-1), walk_length, false);
1071+
}
10251072
}
10261073

10271074
}
@@ -1145,8 +1192,8 @@ TEST_CASE("uld") {
11451192
call_test_uld<double>();
11461193
}
11471194

1148-
TEST_CASE("optimization") {
1149-
call_test_optimization<double>();
1195+
TEST_CASE("exponential_biomass_sampling") {
1196+
call_test_exp_sampling<double>();
11501197
}
11511198

11521199
TEST_CASE("benchmark_hmc") {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.057595362706991726e-01 6.268231060406626726e-04 5.417130007052790575e-01 -4.161378433635810614e-01 -2.634451558388141157e-01 -1.377859647362443540e-01 -1.239169700599200043e-02 -1.771478818636848418e+00 -1.474689492379328559e+00 3.575169148021269949e-01 -1.913772250344042547e+01 1.261758376970891285e-02 -1.425712605922182963e+00 2.234425519598324783e+01 2.942928129761246510e+02 8.469069143284598056e+01 1.059563561026396172e+01 -7.351985582038952316e+01 1.377496801177623098e+01 1.319707473523769181e+02 -3.196840752498849625e+01 -7.523019962729542875e+00 -1.761179600696947745e+01 -6.982659339018495892e+00

0 commit comments

Comments
 (0)