Skip to content

Commit

Permalink
adding a Json parser to read parameters from a given file
Browse files Browse the repository at this point in the history
  • Loading branch information
PYLRR committed Jun 18, 2020
1 parent 61fe981 commit 0ec9151
Show file tree
Hide file tree
Showing 12 changed files with 8,526 additions and 33 deletions.
2 changes: 1 addition & 1 deletion RunCoverageLinux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ cmake .. -DCMAKE_BUILD_TYPE=Debug
cmake --build . --target runTests
./bin/runTests
mkdir coverage
gcovr -r .. -e '.*deterministicRandom.h' -e '.*test/.*' --html --html-details -s -o ./coverage/coverage.html
gcovr -r .. -e '.*deterministicRandom.h' -e '.*test/.*' -e '.*lib/.*' --html --html-details -s -o ./coverage/coverage.html

64 changes: 33 additions & 31 deletions gegelatilib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@
# option, see https://cmake.org/cmake/help/latest/command/set.html.
set(LIBRARY_TARGET_NAME ${PROJECT_NAME})

include_directories("../lib/JsonCpp")
# List of CPP (source) library files.
file(
GLOB_RECURSE
${LIBRARY_TARGET_NAME}_SRC
./src/*.cpp
GLOB_RECURSE
${LIBRARY_TARGET_NAME}_SRC
./src/*.cpp
../lib/*.cpp
)

# List of H (header) library files.
file(
GLOB_RECURSE
${LIBRARY_TARGET_NAME}_HDR
./include/*.h
GLOB_RECURSE
${LIBRARY_TARGET_NAME}_HDR
./include/*.h
../lib/*.h
)

# You can add an external dependency using the find_package() function call
Expand Down Expand Up @@ -67,22 +70,22 @@ add_library(${PROJECT_NAME}::${LIBRARY_TARGET_NAME} ALIAS ${LIBRARY_TARGET_NAME}

# Set two minimum target properties for the library.
# See https://cmake.org/cmake/help/latest/command/set_target_properties.html
# Properties are: 1) Library version number
# Properties are: 1) Library version number
set_target_properties(${LIBRARY_TARGET_NAME} PROPERTIES VERSION ${${PROJECT_NAME}_VERSION})
# 2) list of corresponding public headers
# EDIT: Was removed, because hierarchy is not preserved.
# Is replaced with install(DIRECTORY...) below
#PUBLIC_HEADER "${${LIBRARY_TARGET_NAME}_HDR}")
# 2) list of corresponding public headers
# EDIT: Was removed, because hierarchy is not preserved.
# Is replaced with install(DIRECTORY...) below
#PUBLIC_HEADER "${${LIBRARY_TARGET_NAME}_HDR}")

# Specify include directories for both compilation and installation process.
# The $<INSTALL_PREFIX> generator expression is useful to ensure to create
# relocatable configuration files, see https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html#creating-relocatable-packages
if(WIN32)
target_include_directories(${LIBRARY_TARGET_NAME} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
target_include_directories(${LIBRARY_TARGET_NAME} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}>")
else()
target_include_directories(${LIBRARY_TARGET_NAME} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}/${LIBRARY_TARGET_NAME}/include/>")
target_include_directories(${LIBRARY_TARGET_NAME} PUBLIC "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/${CMAKE_INSTALL_INCLUDEDIR}/${LIBRARY_TARGET_NAME}/include/>")
endif()

# If you used find_package() you need to use target_include_directories() and/or
Expand All @@ -100,30 +103,29 @@ install(TARGETS ${LIBRARY_TARGET_NAME}
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT shlib
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" COMPONENT lib
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" COMPONENT bin)
# Removed line for preserving header folder hierarchy.
# PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${LIBRARY_TARGET_NAME}" COMPONENT dev)
# Removed line for preserving header folder hierarchy.
# PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/${LIBRARY_TARGET_NAME}" COMPONENT dev)

# PDB fow windows visual
if(MSVC)
install(FILES $<TARGET_PDB_FILE:${PROJECT_NAME}> CONFIGURATIONS "Debug" "RelWithDebInfo" DESTINATION "${CMAKE_INSTALL_BINDIR}")
install(FILES $<TARGET_PDB_FILE:${PROJECT_NAME}> CONFIGURATIONS "Debug" "RelWithDebInfo" DESTINATION "${CMAKE_INSTALL_BINDIR}")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 --coverage -fprofile-arcs -ftest-coverage")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 --coverage -fprofile-arcs -ftest-coverage")
endif()

# Doc installation
if(WIN32)
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include" # source directory
DESTINATION "." # Destination is CMAKE_BINARY_DIR
FILES_MATCHING # install only matched files
PATTERN "*.h" # select header files
)
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include" # source directory
DESTINATION "." # Destination is CMAKE_BINARY_DIR
FILES_MATCHING # install only matched files
PATTERN "*.h" # select header files
)
else()
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include" # source directory
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${LIBRARY_TARGET_NAME}
FILES_MATCHING # install only matched files
PATTERN "*.h" # select header files
)
install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include" # source directory
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${LIBRARY_TARGET_NAME}
FILES_MATCHING # install only matched files
PATTERN "*.h" # select header files
)
endif()

message(STATUS "Created target ${LIBRARY_TARGET_NAME} for export ${PROJECT_NAME}.")

message(STATUS "Created target ${LIBRARY_TARGET_NAME} for export ${PROJECT_NAME}.")
13 changes: 13 additions & 0 deletions gegelatilib/include/learn/learningParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@

#include "mutator/mutationParameters.h"

namespace Json {
class Value;
}

namespace Learn {
/**
* \brief Structure for simplifying the transmission of LearningParameters
Expand Down Expand Up @@ -80,6 +84,15 @@ namespace Learn {
*/
size_t nbThreads = std::thread::hardware_concurrency();
} LearningParameters;

/// Reads a given json file and puts the derivative tree in root
void readConfigFile(std::string path, Json::Value &root);

/// Puts the parameters described in the derivative tree root into params
void setAllParamsFrom(Json::Value root, LearningParameters *params);

/// Loads a given json file and puts the parameters it contains in params
void loadParametersFromJson(std::string path, LearningParameters *params);
};

#endif
186 changes: 186 additions & 0 deletions gegelatilib/src/learn/learningParameters.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/**
* Copyright or © or Copr. IETR/INSA - Rennes (2019) :
*
* Karol Desnos <[email protected]> (2019)
*
* GEGELATI is an open-source reinforcement learning framework for training
* artificial intelligence based on Tangled Program Graphs (TPGs).
*
* This software is governed by the CeCILL-C license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/ or redistribute the software under the terms of the CeCILL-C
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*/

#include <iostream>
#include <fstream>
#include <json.h>
#include "learn/learningParameters.h"

void Learn::readConfigFile(std::string 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");
}

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 Learn::setAllParamsFrom(Json::Value root, LearningParameters *params) {
for (std::string const &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 const &key3 : root[key][key2].getMemberNames()) {
double param = root[key][key2][key3].asDouble();
if (key3 == "nbActions") {
params->mutation.tpg.nbActions = param;
continue;
}
if (key3 == "nbRoots") {
params->mutation.tpg.nbRoots = param;
continue;
}
if (key3 == "maxInitOutgoingEdges") {
params->mutation.tpg.maxInitOutgoingEdges = param;
continue;
}
if (key3 == "maxOutgoingEdges") {
params->mutation.tpg.maxOutgoingEdges = param;
continue;
}
if (key3 == "pEdgeDeletion") {
params->mutation.tpg.pEdgeDeletion = param;
continue;
}
if (key3 == "pEdgeAddition") {
params->mutation.tpg.pEdgeAddition = param;
continue;
}
if (key3 == "pProgramMutation") {
params->mutation.tpg.pProgramMutation = param;
continue;
}
if (key3 == "pEdgeDestinationChange") {
params->mutation.tpg.pEdgeDestinationChange = param;
continue;
}
if (key3 == "pEdgeDestinationIsAction") {
params->mutation.tpg.pEdgeDestinationIsAction = param;
continue;
}
}
} else {
if (key2 == "prog") {
// we're on a mutation.prog.xxx parameter
for (std::string const &key3 : root[key][key2].getMemberNames()) {
double param = root[key][key2][key3].asDouble();
if (key3 == "maxProgramSize") {
params->mutation.prog.maxProgramSize = param;
continue;
}
if (key3 == "pDelete") {
params->mutation.prog.pDelete = param;
continue;
}
if (key3 == "pAdd") {
params->mutation.prog.pAdd = param;
continue;
}
if (key3 == "pMutate") {
params->mutation.prog.pMutate = param;
continue;
}
if (key3 == "pSwap") {
params->mutation.prog.pSwap = param;
continue;
}
}
}
}
}
continue;
}
if (root[key].size() == 0) {
// we have a parameter without subtree (as a leaf)
double param = root[key].asDouble();
if (key == "archiveSize") {
params->archiveSize = param;
continue;
}
if (key == "archivingProbability") {
params->archivingProbability = param;
continue;
}
if (key == "nbIterationsPerPolicyEvaluation") {
params->nbIterationsPerPolicyEvaluation = param;
continue;
}
if (key == "maxNbActionsPerEval") {
params->maxNbActionsPerEval = param;
continue;
}
if (key == "ratioDeletedRoots") {
params->ratioDeletedRoots = param;
continue;
}
if (key == "nbGenerations") {
params->nbGenerations = param;
continue;
}
if (key == "maxNbEvaluationPerPolicy") {
params->maxNbEvaluationPerPolicy = param;
continue;
}
if (key == "nbRegisters") {
params->nbRegisters = param;
continue;
}
if (key == "nbThreads") {
params->nbThreads = param;
continue;
}
}
// we didn't recognize the symbol
std::cerr << "Ignoring unknown parameter " << key << std::endl;
}
}

void Learn::loadParametersFromJson(std::string path, LearningParameters *params) {
Json::Value root;
readConfigFile(path, root);

setAllParamsFrom(root, params);
}
Loading

0 comments on commit 0ec9151

Please sign in to comment.