From 642d1da39ef29264e76cc8d0069857bc2882d4fb Mon Sep 17 00:00:00 2001 From: kdesnos Date: Wed, 24 Jun 2020 08:35:44 +0200 Subject: [PATCH] (File) Reformat ParametersParser code and comments. (Releng) Ignore lib folder in windows code coverage. --- RunCoverageWindows.bat | 2 +- gegelatilib/include/file/parametersParser.h | 71 +++-- gegelatilib/src/file/parametersParser.cpp | 286 ++++++++++---------- test/parametersParserTest.cpp | 113 ++++---- 4 files changed, 249 insertions(+), 223 deletions(-) diff --git a/RunCoverageWindows.bat b/RunCoverageWindows.bat index 9ff6aaf4..4c0cc025 100644 --- a/RunCoverageWindows.bat +++ b/RunCoverageWindows.bat @@ -1,4 +1,4 @@ -OpenCppCoverage.exe --source %cd% --excluded_source %cd%\bin --export_type html:bin\coverage --excluded_line_regex "\s*else.*" --excluded_line_regex "\s*\{.*" --excluded_line_regex "\s*\}.*" --excluded_sources "*\deterministicRandom.h" .\bin\bin\Debug\runTests.exe +OpenCppCoverage.exe --source %cd% --excluded_source %cd%\bin --excluded_source %cd%\lib --export_type html:bin\coverage --excluded_line_regex "\s*else.*" --excluded_line_regex "\s*\{.*" --excluded_line_regex "\s*\}.*" --excluded_sources "*\deterministicRandom.h" .\bin\bin\Debug\runTests.exe .\bin\coverage\index.html diff --git a/gegelatilib/include/file/parametersParser.h b/gegelatilib/include/file/parametersParser.h index c92ca47f..88895100 100644 --- a/gegelatilib/include/file/parametersParser.h +++ b/gegelatilib/include/file/parametersParser.h @@ -33,39 +33,62 @@ * knowledge of the CeCILL-C license and that you accept its terms. */ -#ifndef GEGELATI_PARAMETERSPARSER_H -#define GEGELATI_PARAMETERSPARSER_H +#ifndef PARAMETERS_PARSER_H +#define PARAMETERS_PARSER_H #include + #include "learn/learningParameters.h" -/// Used to avoid importing the JsonCpp lib now, so it tells the compiler Json::Value exists + /// Used to avoid importing the JsonCpp lib now, so it tells the compiler Json::Value exists namespace Json { - class Value; + class Value; } -namespace ParametersParser { - /// \brief Loads a given json file and puts the parameters it contains in params - /// \param[in] path path of the file we want to read - /// \param[out] params the learning parameters we are going to set - void loadParametersFromJson(const char *path, Learn::LearningParameters *params); +namespace File { + /** + * \brief Namespace containing the functions for filling an instance of the + * Learn::LearningParameters class from a Json file. + */ + namespace ParametersParser { + /** + * \brief Loads a given json file and fills the parameters it contains in + * given LearningParameters. + * + * \param[in] path path of the JSON file from which the parameters are + * read. + * \param[out] params the LearningParameters being updated. + */ + void loadParametersFromJson(const char* path, Learn::LearningParameters& params); - /// \brief Given a parameter name, sets its value in params - /// \param[out] params the learning parameters we are going to set - /// \param[in] param the name of the parameter we want to set - /// \param[in] value the value we want to set the parameter to - void setParameterFromString(Learn::LearningParameters *params, std::string ¶m, double value); + /** + * \brief Given a parameter name, sets its value in given + * LearningParameters. + * + * \param[out] params the learning parameters we are going to set. + * \param[in] param the name of the LearningParameters being updated. + * \param[in] value the value we want to set the parameter to. + */ + void setParameterFromString(Learn::LearningParameters& params, std::string& param, double value); - /// \brief Puts the parameters described in the derivative tree root into params - /// \param[in] root JSON tree we will use to set parameters - /// \param[out] params the learning parameters we are going to set - void setAllParamsFrom(const Json::Value &root, Learn::LearningParameters *params); + /** + * \brief Puts the parameters described in the derivative tree root in + * given LearningParameters. + * + * \param[in] root JSON tree we will use to set parameters. + * \param[out] params the LearningParameters being updated. + */ + void setAllParamsFrom(const Json::Value& root, Learn::LearningParameters& params); - /// \brief Reads a given json file and puts the derivative tree in root - /// \param[in] path path of the file we want to read - /// \param[out] root JSON tree we are going to build with the file - void readConfigFile(const char *path, Json::Value &root); + /** + * \brief Reads a given json file and puts the derivative tree in root. + * + * \param[in] path path of the JSON file from which the parameters are + * read. + * \param[out] root JSON tree we are going to build with the file. + */ + void readConfigFile(const char* path, Json::Value& root); + } } - -#endif //GEGELATI_PARAMETERSPARSER_H +#endif \ No newline at end of file diff --git a/gegelatilib/src/file/parametersParser.cpp b/gegelatilib/src/file/parametersParser.cpp index 6a9087f8..11f74760 100644 --- a/gegelatilib/src/file/parametersParser.cpp +++ b/gegelatilib/src/file/parametersParser.cpp @@ -36,157 +36,159 @@ #include #include #include + #include "file/parametersParser.h" -void ParametersParser::readConfigFile(const char* path, Json::Value &root) { - std::ifstream ifs; - ifs.open(path); +void File::ParametersParser::readConfigFile(const char* path, Json::Value& root) { + std::ifstream ifs; + ifs.open(path); - if (!ifs.is_open()) { - std::cerr << "Error : specified param file doesn't exist : " << path << std::endl; - throw Json::Exception("aborting"); - } + if (!ifs.is_open()) { + std::cerr << "Error : specified param file doesn't exist : " << path << std::endl; + throw Json::Exception("aborting"); + } - Json::CharReaderBuilder builder; - builder["collectComments"] = true; - JSONCPP_STRING errs; - if (!parseFromStream(builder, ifs, &root, &errs)) { - std::cout << errs << std::endl; - std::cerr << "Ignoring ill-formed config file " << path << std::endl; - } + Json::CharReaderBuilder builder; + builder["collectComments"] = true; + JSONCPP_STRING errs; + if (!parseFromStream(builder, ifs, &root, &errs)) { + std::cout << errs << std::endl; + std::cerr << "Ignoring ill-formed config file " << path << std::endl; + } } -void ParametersParser::setAllParamsFrom(const Json::Value& root, Learn::LearningParameters *params) { - for (std::string &key : root.getMemberNames()) { - if (key == "mutation") { - // we have a subtree of mutation : parameters like mutation.xxx.xxx - for (std::string const &key2 : root[key].getMemberNames()) { - if (key2 == "tpg") { - // we're on a mutation.tpg.xxx parameter - for (std::string &key3 : root[key][key2].getMemberNames()) { - double value = root[key][key2][key3].asDouble(); - setParameterFromString(params,key3,value); - } - } else { - if (key2 == "prog") { - // we're on a mutation.prog.xxx parameter - for (std::string &key3 : root[key][key2].getMemberNames()) { - double value = root[key][key2][key3].asDouble(); - setParameterFromString(params,key3,value); - } - } - } - } - continue; - } - if (root[key].size() == 0) { - // we have a parameter without subtree (as a leaf) - double value = root[key].asDouble(); - setParameterFromString(params,key,value); - } - } +void File::ParametersParser::setAllParamsFrom(const Json::Value& root, Learn::LearningParameters& params) { + for (std::string& key : root.getMemberNames()) { + if (key == "mutation") { + // we have a subtree of mutation : parameters like mutation.xxx.xxx + for (std::string const& key2 : root[key].getMemberNames()) { + if (key2 == "tpg") { + // we're on a mutation.tpg.xxx parameter + for (std::string& key3 : root[key][key2].getMemberNames()) { + double value = root[key][key2][key3].asDouble(); + setParameterFromString(params, key3, value); + } + } + else { + if (key2 == "prog") { + // we're on a mutation.prog.xxx parameter + for (std::string& key3 : root[key][key2].getMemberNames()) { + double value = root[key][key2][key3].asDouble(); + setParameterFromString(params, key3, value); + } + } + } + } + continue; + } + if (root[key].size() == 0) { + // we have a parameter without subtree (as a leaf) + double value = root[key].asDouble(); + setParameterFromString(params, key, value); + } + } } -void ParametersParser::setParameterFromString(Learn::LearningParameters * params, std::string& key, double value){ - if (key == "nbActions") { - params->mutation.tpg.nbActions = (size_t)value; - return; - } - if (key == "nbRoots") { - params->mutation.tpg.nbRoots = (size_t)value; - return; - } - if (key == "maxInitOutgoingEdges") { - params->mutation.tpg.maxInitOutgoingEdges = (size_t)value; - return; - } - if (key == "maxOutgoingEdges") { - params->mutation.tpg.maxOutgoingEdges = (size_t)value; - return; - } - if (key == "pEdgeDeletion") { - params->mutation.tpg.pEdgeDeletion = value; - return; - } - if (key == "pEdgeAddition") { - params->mutation.tpg.pEdgeAddition = value; - return; - } - if (key == "pProgramMutation") { - params->mutation.tpg.pProgramMutation = value; - return; - } - if (key == "pEdgeDestinationChange") { - params->mutation.tpg.pEdgeDestinationChange = value; - return; - } - if (key == "pEdgeDestinationIsAction") { - params->mutation.tpg.pEdgeDestinationIsAction = value; - return; - } - if (key == "maxProgramSize") { - params->mutation.prog.maxProgramSize = (size_t)value; - return; - } - if (key == "pDelete") { - params->mutation.prog.pDelete = value; - return; - } - if (key == "pAdd") { - params->mutation.prog.pAdd = value; - return; - } - if (key == "pMutate") { - params->mutation.prog.pMutate = value; - return; - } - if (key == "pSwap") { - params->mutation.prog.pSwap = value; - return; - } - if (key == "archiveSize") { - params->archiveSize = (size_t)value; - return; - } - if (key == "archivingProbability") { - params->archivingProbability = value; - return; - } - if (key == "nbIterationsPerPolicyEvaluation") { - params->nbIterationsPerPolicyEvaluation = (uint64_t)value; - return; - } - if (key == "maxNbActionsPerEval") { - params->maxNbActionsPerEval = (uint64_t)value; - return; - } - if (key == "ratioDeletedRoots") { - params->ratioDeletedRoots = value; - return; - } - if (key == "nbGenerations") { - params->nbGenerations = (uint64_t)value; - return; - } - if (key == "maxNbEvaluationPerPolicy") { - params->maxNbEvaluationPerPolicy = (size_t)value; - return; - } - if (key == "nbRegisters") { - params->nbRegisters = (size_t)value; - return; - } - if (key == "nbThreads") { - params->nbThreads = (size_t)value; - return; - } - // we didn't recognize the symbol - std::cerr << "Ignoring unknown parameter " << key << std::endl; +void File::ParametersParser::setParameterFromString(Learn::LearningParameters& params, std::string& key, double value) { + if (key == "nbActions") { + params.mutation.tpg.nbActions = (size_t)value; + return; + } + if (key == "nbRoots") { + params.mutation.tpg.nbRoots = (size_t)value; + return; + } + if (key == "maxInitOutgoingEdges") { + params.mutation.tpg.maxInitOutgoingEdges = (size_t)value; + return; + } + if (key == "maxOutgoingEdges") { + params.mutation.tpg.maxOutgoingEdges = (size_t)value; + return; + } + if (key == "pEdgeDeletion") { + params.mutation.tpg.pEdgeDeletion = value; + return; + } + if (key == "pEdgeAddition") { + params.mutation.tpg.pEdgeAddition = value; + return; + } + if (key == "pProgramMutation") { + params.mutation.tpg.pProgramMutation = value; + return; + } + if (key == "pEdgeDestinationChange") { + params.mutation.tpg.pEdgeDestinationChange = value; + return; + } + if (key == "pEdgeDestinationIsAction") { + params.mutation.tpg.pEdgeDestinationIsAction = value; + return; + } + if (key == "maxProgramSize") { + params.mutation.prog.maxProgramSize = (size_t)value; + return; + } + if (key == "pDelete") { + params.mutation.prog.pDelete = value; + return; + } + if (key == "pAdd") { + params.mutation.prog.pAdd = value; + return; + } + if (key == "pMutate") { + params.mutation.prog.pMutate = value; + return; + } + if (key == "pSwap") { + params.mutation.prog.pSwap = value; + return; + } + if (key == "archiveSize") { + params.archiveSize = (size_t)value; + return; + } + if (key == "archivingProbability") { + params.archivingProbability = value; + return; + } + if (key == "nbIterationsPerPolicyEvaluation") { + params.nbIterationsPerPolicyEvaluation = (uint64_t)value; + return; + } + if (key == "maxNbActionsPerEval") { + params.maxNbActionsPerEval = (uint64_t)value; + return; + } + if (key == "ratioDeletedRoots") { + params.ratioDeletedRoots = value; + return; + } + if (key == "nbGenerations") { + params.nbGenerations = (uint64_t)value; + return; + } + if (key == "maxNbEvaluationPerPolicy") { + params.maxNbEvaluationPerPolicy = (size_t)value; + return; + } + if (key == "nbRegisters") { + params.nbRegisters = (size_t)value; + return; + } + if (key == "nbThreads") { + params.nbThreads = (size_t)value; + return; + } + // we didn't recognize the symbol + std::cerr << "Ignoring unknown parameter " << key << std::endl; } -void ParametersParser::loadParametersFromJson(const char* path, Learn::LearningParameters *params) { - Json::Value root; - readConfigFile(path, root); +void File::ParametersParser::loadParametersFromJson(const char* path, Learn::LearningParameters& params) { + Json::Value root; + readConfigFile(path, root); - setAllParamsFrom(root, params); + setAllParamsFrom(root, params); } \ No newline at end of file diff --git a/test/parametersParserTest.cpp b/test/parametersParserTest.cpp index 73ef6f03..a66d510d 100644 --- a/test/parametersParserTest.cpp +++ b/test/parametersParserTest.cpp @@ -35,81 +35,82 @@ */ #include -#include "file/parametersParser.h" + #include "../lib/JsonCpp/json.h" +#include "file/parametersParser.h" TEST(LearningParametersTest, readConfigFile) { - Json::Value root; + Json::Value root; - // name validity - ASSERT_THROW(ParametersParser::readConfigFile(TESTS_DAT_PATH "non_existing_file.json", root), Json::Exception) - << "An exception should be raised if file doesn't exist"; - ASSERT_NO_THROW(ParametersParser::readConfigFile(TESTS_DAT_PATH "params.json", root)) - << "An exception is raised in spite of existing file"; + // name validity + ASSERT_THROW(File::ParametersParser::readConfigFile(TESTS_DAT_PATH "non_existing_file.json", root), Json::Exception) + << "An exception should be raised if file doesn't exist"; + ASSERT_NO_THROW(File::ParametersParser::readConfigFile(TESTS_DAT_PATH "params.json", root)) + << "An exception is raised in spite of existing file"; - // content validity - ParametersParser::readConfigFile(TESTS_DAT_PATH "paramsNotConform.json", root); - ASSERT_EQ(0, root.size()) << "Ill-formed parameters file should result in no root filling"; + // content validity + File::ParametersParser::readConfigFile(TESTS_DAT_PATH "paramsNotConform.json", root); + ASSERT_EQ(0, root.size()) << "Ill-formed parameters file should result in no root filling"; - ParametersParser::readConfigFile(TESTS_DAT_PATH "params.json", root); - ASSERT_EQ(10, root.size()) << "Wrong number of elements in parsed json file"; - ASSERT_EQ(9, root["mutation"]["tpg"].size()) << "Wrong number of elements in parsed json file"; - ASSERT_EQ(5, root["mutation"]["prog"].size()) << "Wrong number of elements in parsed json file"; + File::ParametersParser::readConfigFile(TESTS_DAT_PATH "params.json", root); + ASSERT_EQ(10, root.size()) << "Wrong number of elements in parsed json file"; + ASSERT_EQ(9, root["mutation"]["tpg"].size()) << "Wrong number of elements in parsed json file"; + ASSERT_EQ(5, root["mutation"]["prog"].size()) << "Wrong number of elements in parsed json file"; } TEST(LearningParametersTest, setParameterFromString) { - Learn::LearningParameters params; - ASSERT_EQ(params.nbRegisters, 8); - std::string key = "nbRegisters"; - ParametersParser::setParameterFromString(¶ms,key,5); - ASSERT_EQ(params.nbRegisters, 5); + Learn::LearningParameters params; + ASSERT_EQ(params.nbRegisters, 8); + std::string key = "nbRegisters"; + File::ParametersParser::setParameterFromString(params, key, 5); + ASSERT_EQ(params.nbRegisters, 5); } TEST(LearningParametersTest, setAllParamsFrom) { - Learn::LearningParameters params; - Json::Value root; + Learn::LearningParameters params; + Json::Value root; - ParametersParser::readConfigFile(TESTS_DAT_PATH "params.json", root); - ASSERT_NO_THROW(ParametersParser::setAllParamsFrom(root, ¶ms)); + File::ParametersParser::readConfigFile(TESTS_DAT_PATH "params.json", root); + ASSERT_NO_THROW(File::ParametersParser::setAllParamsFrom(root, params)); - ASSERT_EQ(50, params.archiveSize); - ASSERT_EQ(0.5, params.archivingProbability); - ASSERT_EQ(50, params.nbIterationsPerPolicyEvaluation); - ASSERT_EQ(5, params.maxNbActionsPerEval); - ASSERT_EQ(0.85, params.ratioDeletedRoots); - ASSERT_EQ(100, params.maxNbEvaluationPerPolicy); - ASSERT_EQ(3.0, params.nbRegisters); - ASSERT_EQ(2.0, params.nbThreads); - ASSERT_EQ(200, params.nbGenerations); - ASSERT_EQ(100, params.mutation.tpg.nbRoots); - ASSERT_EQ(5, params.mutation.tpg.nbActions); - ASSERT_EQ(3, params.mutation.tpg.maxInitOutgoingEdges); - ASSERT_EQ(60, params.mutation.tpg.maxOutgoingEdges); - ASSERT_EQ(0.8, params.mutation.tpg.pEdgeDeletion); - ASSERT_EQ(0.8, params.mutation.tpg.pEdgeAddition); - ASSERT_EQ(0.8, params.mutation.tpg.pProgramMutation); - ASSERT_EQ(0.3, params.mutation.tpg.pEdgeDestinationChange); - ASSERT_EQ(0.6, params.mutation.tpg.pEdgeDestinationIsAction); - ASSERT_EQ(40, params.mutation.prog.maxProgramSize); - ASSERT_EQ(0.7, params.mutation.prog.pDelete); - ASSERT_EQ(0.7, params.mutation.prog.pAdd); - ASSERT_EQ(1.0, params.mutation.prog.pMutate); - ASSERT_EQ(1.0, params.mutation.prog.pSwap); + ASSERT_EQ(50, params.archiveSize); + ASSERT_EQ(0.5, params.archivingProbability); + ASSERT_EQ(50, params.nbIterationsPerPolicyEvaluation); + ASSERT_EQ(5, params.maxNbActionsPerEval); + ASSERT_EQ(0.85, params.ratioDeletedRoots); + ASSERT_EQ(100, params.maxNbEvaluationPerPolicy); + ASSERT_EQ(3.0, params.nbRegisters); + ASSERT_EQ(2.0, params.nbThreads); + ASSERT_EQ(200, params.nbGenerations); + ASSERT_EQ(100, params.mutation.tpg.nbRoots); + ASSERT_EQ(5, params.mutation.tpg.nbActions); + ASSERT_EQ(3, params.mutation.tpg.maxInitOutgoingEdges); + ASSERT_EQ(60, params.mutation.tpg.maxOutgoingEdges); + ASSERT_EQ(0.8, params.mutation.tpg.pEdgeDeletion); + ASSERT_EQ(0.8, params.mutation.tpg.pEdgeAddition); + ASSERT_EQ(0.8, params.mutation.tpg.pProgramMutation); + ASSERT_EQ(0.3, params.mutation.tpg.pEdgeDestinationChange); + ASSERT_EQ(0.6, params.mutation.tpg.pEdgeDestinationIsAction); + ASSERT_EQ(40, params.mutation.prog.maxProgramSize); + ASSERT_EQ(0.7, params.mutation.prog.pDelete); + ASSERT_EQ(0.7, params.mutation.prog.pAdd); + ASSERT_EQ(1.0, params.mutation.prog.pMutate); + ASSERT_EQ(1.0, params.mutation.prog.pSwap); - // check default parameters - Learn::LearningParameters params2; + // check default parameters + Learn::LearningParameters params2; - ParametersParser::readConfigFile(TESTS_DAT_PATH "paramsWithWrongOne.json", root); - ParametersParser::setAllParamsFrom(root, ¶ms2); + File::ParametersParser::readConfigFile(TESTS_DAT_PATH "paramsWithWrongOne.json", root); + File::ParametersParser::setAllParamsFrom(root, params2); - ASSERT_TRUE(params2.nbThreads > 0) << "A default nbThreads value should be set when no one is specified"; - ASSERT_EQ(params2.nbRegisters, 8) << "Bad parameter should be ignored"; + ASSERT_TRUE(params2.nbThreads > 0) << "A default nbThreads value should be set when no one is specified"; + ASSERT_EQ(params2.nbRegisters, 8) << "Bad parameter should be ignored"; } TEST(LearningParametersTest, loadParametersFromJson) { - Learn::LearningParameters params; - ASSERT_NO_THROW(ParametersParser::loadParametersFromJson(TESTS_DAT_PATH "params.json", ¶ms)); - // only testing 1 parameter as readConfigFile was already tested - ASSERT_EQ(params.nbRegisters, 3.0) << "There should be 3 registers according to the params file"; + Learn::LearningParameters params; + ASSERT_NO_THROW(File::ParametersParser::loadParametersFromJson(TESTS_DAT_PATH "params.json", params)); + // only testing 1 parameter as readConfigFile was already tested + ASSERT_EQ(params.nbRegisters, 3.0) << "There should be 3 registers according to the params file"; }