Skip to content

Commit

Permalink
(File) Reformat ParametersParser code and comments.
Browse files Browse the repository at this point in the history
(Releng) Ignore lib folder in windows code coverage.
  • Loading branch information
kdesnos committed Jun 24, 2020
1 parent 851287c commit 642d1da
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 223 deletions.
2 changes: 1 addition & 1 deletion RunCoverageWindows.bat
Original file line number Diff line number Diff line change
@@ -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

Expand Down
71 changes: 47 additions & 24 deletions gegelatilib/include/file/parametersParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <string>

#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 &param, 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
286 changes: 144 additions & 142 deletions gegelatilib/src/file/parametersParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,157 +36,159 @@
#include <iostream>
#include <fstream>
#include <json.h>

#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);
}
Loading

0 comments on commit 642d1da

Please sign in to comment.