Skip to content

Commit

Permalink
all previous cpp tests are pdated and working!
Browse files Browse the repository at this point in the history
  • Loading branch information
gAldeia committed Nov 14, 2023
1 parent 95efb07 commit eafb245
Show file tree
Hide file tree
Showing 6 changed files with 757 additions and 666 deletions.
File renamed without changes.
245 changes: 120 additions & 125 deletions tests/cpp/test_data.cpp
Original file line number Diff line number Diff line change
@@ -1,126 +1,121 @@
// #include "testsHeader.h"
// #include "../../src/program/program.h"
// #include "../../src/search_space.h"
// #include "../../src/program/dispatch_table.h"

// TEST(Data, ErrorHandling)
// {
// // Creating an empty dataset throws error
// EXPECT_THROW({
// MatrixXf X(0,0);
// ArrayXf y(0);

// try
// {
// Dataset dt(X, y);
// }
// catch( const std::runtime_error& err )
// {
// const string msg = err.what();
// ASSERT_NE(
// msg.find("Error during the initialization of the dataset"),
// std::string::npos);
// throw;
// }
// }, std::runtime_error);
// }

// TEST(Data, MixedVariableTypes)
// {
// // We need to set at least the mutation options (and respective
// // probabilities) in order to call PRG.predict()
// PARAMS["write_mutation_trace"] = true;
// PARAMS["mutation_options"] = {
// {"point",0.167}, {"insert", 0.167}, {"delete", 0.167}, {"subtree", 0.167}, {"toggle_weight_on", 0.167}, {"toggle_weight_off", 0.167}
// };

// MatrixXf X(5,3);
// X << 0 , 1, 0 , // binary with integer values
// 0.0, 1.0, 1.0, // binary with float values
// 2 , 1.0, -3.0, // integer with float and negative values
// 2 , 1 , 3 , // integer with integer values
// 2.1, 3.7, -5.2; // float values

// X.transposeInPlace();

// ArrayXf y(3);

// y << 6.1, 7.7, -4.2; // y = x_0 + x_1 + x_2
#include "testsHeader.h"


TEST(Data, ErrorHandling)
{
// Creating an empty dataset throws error
EXPECT_THROW({
MatrixXf X(0,0);
ArrayXf y(0);

try
{
Dataset dt(X, y);
}
catch( const std::runtime_error& err )
{
const string msg = err.what();
ASSERT_NE(
msg.find("Error during the initialization of the dataset"),
std::string::npos);
throw;
}
}, std::runtime_error);
}

TEST(Data, MixedVariableTypes)
{
Parameters params;

MatrixXf X(5,3);
X << 0 , 1, 0 , // binary with integer values
0.0, 1.0, 1.0, // binary with float values
2 , 1.0, -3.0, // integer with float and negative values
2 , 1 , 3 , // integer with integer values
2.1, 3.7, -5.2; // float values

X.transposeInPlace();

ArrayXf y(3);

y << 6.1, 7.7, -4.2; // y = x_0 + x_1 + x_2

// unordered_map<string, float> user_ops = {
// {"Add", 0.5},
// {"Sub", 0.5},
// // a boolean operator
// {"And", 1.0},
// {"Or", 1.0},
// // operator that takes boolean as argument
// {"SplitOn", 1.0}
// };

// Dataset dt(X, y);
// SearchSpace SS;
// SS.init(dt, user_ops);

// dt.print();
// SS.print();

// for (size_t d = 5; d < 10; ++d)
// for (size_t s = 5; s < 20; ++s)
// {
// fmt::print(
// "=================================================\n"
// "depth={}, size={}. ", d, s
// );

// PARAMS["max_size"] = s;
// PARAMS["max_depth"] = d;

// RegressorProgram PRG = SS.make_regressor(s-4, d-4);

// fmt::print(
// "Tree model: {}\n", PRG.get_model("compact", true)
// );

// // visualizing detailed information for the model
// std::for_each(PRG.Tree.begin(), PRG.Tree.end(),
// [](const auto& n) {
// fmt::print("Name {}, node {}, feature {}\n"
// " sig_hash {}\n ret_type {}\n ret_type type {}\n",
// n.name, n.node_type, n.get_feature(),
// n.sig_hash, n.ret_type, typeid(n.ret_type).name());
// });

// std::cout << std::endl;

// fmt::print( "PRG fit\n");
// PRG.fit(dt);
// fmt::print( "PRG predict\n");
// ArrayXf y_pred = PRG.predict(dt);
// fmt::print( "y_pred: {}\n", y_pred);

// // creating and fitting a child
// auto opt = PRG.mutate();

// if (!opt){
// fmt::print("Mutation failed to create a child\n");
// fmt::print("{}\n", PARAMS["mutation_trace"].get<json>().dump());
// }
// else {
// auto Child = opt.value();

// fmt::print("Child model: {}\n", Child.get_model("compact", true));

// fmt::print( "Child fit\n");
// Child.fit(dt);
// fmt::print( "Child predict\n");
// ArrayXf y_pred_child = Child.predict(dt);
// fmt::print( "y_pred: {}\n", y_pred);
// }
// }

// // Brush exports two DispatchTable structs named dtable_fit and dtable_predict.
// // These structures holds the mapping between nodes and its corresponding
// // operations, and are used to resolve the evaluation of an expression.
// // dtable_fit.print();
// // dtable_predict.print();
// }
params.functions = {
{"Add", 0.5},
{"Sub", 0.5},
// a boolean operator
{"And", 1.0},
{"Or", 1.0},
// operator that takes boolean as argument
{"SplitOn", 1.0}
};

Dataset dt(X, y);
SearchSpace SS;
SS.init(dt, params.functions);

dt.print();
SS.print();

for (size_t d = 5; d < 10; ++d)
for (size_t s = 5; s < 20; ++s)
{
fmt::print(
"=================================================\n"
"depth={}, size={}. ", d, s
);

params.max_size = s;
params.max_depth = d;

// TODO: update all calls of make_<program> to use params
RegressorProgram PRG = SS.make_regressor(0, 0, params);

fmt::print(
"Tree model: {}\n", PRG.get_model("compact", true)
);

// visualizing detailed information for the model
std::for_each(PRG.Tree.begin(), PRG.Tree.end(),
[](const auto& n) {
fmt::print("Name {}, node {}, feature {}\n"
" sig_hash {}\n ret_type {}\n ret_type type {}\n",
n.name, n.node_type, n.get_feature(),
n.sig_hash, n.ret_type, typeid(n.ret_type).name());
});
std::cout << std::endl;

fmt::print( "PRG fit\n");
PRG.fit(dt);

fmt::print( "PRG predict\n");
ArrayXf y_pred = PRG.predict(dt);
fmt::print( "y_pred: {}\n", y_pred);

// creating and fitting a child
Variation variator = Variation<ProgramType::Regressor>(params, SS);
std::optional<RegressorProgram> opt = variator.mutate(PRG);

if (!opt){
fmt::print("Mutation failed to create a child\n");
}
else {
auto Child = opt.value();

fmt::print("Child model: {}\n", Child.get_model("compact", true));

fmt::print( "Child fit\n");
Child.fit(dt);

fmt::print( "Child predict\n");
ArrayXf y_pred_child = Child.predict(dt);
fmt::print( "y_pred: {}\n", y_pred);
}
}

// Brush exports two DispatchTable structs named dtable_fit and dtable_predict.
// These structures holds the mapping between nodes and its corresponding
// operations, and are used to resolve the evaluation of an expression.
// dtable_fit.print();
// dtable_predict.print();
}
18 changes: 18 additions & 0 deletions tests/cpp/test_population.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "testsHeader.h"

TEST(Population, PopulationTests)
{

Population<T> pop;

// size,
// island sizes growns and comes back to the same,
// update and prep offspring slots.
// no overlap in island indexes.
// works with even and uneven pop sizes.
// initialize population works?
// migrate?
// print models

}

Loading

0 comments on commit eafb245

Please sign in to comment.