Skip to content
Snippets Groups Projects
Commit 450f6c80 authored by Maxence Naud's avatar Maxence Naud
Browse files

Update Testing.hpp and Testing.cpp according to Aidge coding rules

parent 9d66287d
No related branches found
No related tags found
1 merge request!53GraphView inputs/outputs ordering
......@@ -12,11 +12,11 @@
#ifndef AIDGE_CORE_GRAPH_TESTING_H_
#define AIDGE_CORE_GRAPH_TESTING_H_
#include <cstddef>
#include <vector>
#include <set>
#include <random>
#include <algorithm>
#include <utility>
#include <random> // std::mt19937::result_type
#include <utility> // std::pair
#include "aidge/graph/Node.hpp"
#include "aidge/utils/Types.h"
......@@ -31,11 +31,11 @@ struct RandomGraph {
/// @brief Connection density (between 0 and 1)
float density = 0.5;
/// @brief Max number of inputs per node (regardless if they are connected or not)
size_t maxIn = 5;
std::size_t maxIn = 5;
/// @brief Average number of inputs per node (regardless if they are connected or not)
float avgIn = 1.5;
/// @brief Max number of outputs per node (regardless if they are connected or not)
size_t maxOut = 2;
std::size_t maxOut = 2;
/// @brief Average number of outputs per node (regardless if they are connected or not)
float avgOut = 1.1;
/// @brief List of node types that should be generated in the graph (as GenericOperator)
......@@ -47,11 +47,11 @@ struct RandomGraph {
/**
* Generate a DAG according to the parameters of the class.
* @param seed Random seed. For an identical seed, an identical topology is
* @param seed Random seed. For an identical seed, an identical topology is
* generated, but with a random node ordering in the return set of nodes.
* @param nbNodes Number of nodes to generate.
*/
std::pair<NodePtr, std::set<NodePtr>> gen(std::mt19937::result_type seed, size_t nbNodes) const;
std::pair<NodePtr, std::set<NodePtr>> gen(std::mt19937::result_type seed, std::size_t nbNodes) const;
};
std::string nodePtrToType(NodePtr node);
......@@ -61,6 +61,7 @@ std::set<std::string> nodePtrTo(const std::set<NodePtr>& nodes,
std::vector<std::pair<std::string, IOIndex_t>> nodePtrTo(
const std::vector<std::pair<NodePtr, IOIndex_t>>& nodes,
std::string(*nodeTo)(NodePtr) = nodePtrToType);
}
} // namespace Aidge
#endif /* AIDGE_CORE_GRAPH_TESTING_H_ */
......@@ -9,46 +9,56 @@
*
********************************************************************************/
#include <algorithm> // std::shuffle, std::transform
#include <cstddef>
#include <memory>
#include <numeric> // std::iota
#include <random> // std::binomial_distribution, std::mt19937, std::discrete_distribution
#include <string>
#include <utility> // std::pair
#include <vector>
#include "aidge/graph/Testing.hpp"
#include "aidge/operator/GenericOperator.hpp"
#include "aidge/utils/Types.h"
std::pair<Aidge::NodePtr, std::set<Aidge::NodePtr>> Aidge::RandomGraph::gen(std::mt19937::result_type seed, size_t nbNodes) const {
std::pair<Aidge::NodePtr, std::set<Aidge::NodePtr>> Aidge::RandomGraph::gen(std::mt19937::result_type seed, std::size_t nbNodes) const {
std::mt19937 gen(seed);
std::binomial_distribution<> dIn(maxIn - 1, avgIn/maxIn);
std::binomial_distribution<> dOut(maxOut - 1, avgOut/maxOut);
std::binomial_distribution<> dLink(1, density);
std::discrete_distribution<> dType(typesWeights.begin(), typesWeights.end());
std::vector<std::pair<int, int>> nbIOs;
std::vector<std::pair<IOIndex_t, IOIndex_t>> nbIOs;
std::vector<std::string> nodesType;
for (size_t i = 0; i < nbNodes; ++i) {
for (std::size_t i = 0; i < nbNodes; ++i) {
const auto nbIn = 1 + dIn(gen);
nbIOs.push_back(std::make_pair(nbIn, 1 + dOut(gen)));
nodesType.push_back(types[dType(gen)]);
}
std::vector<int> nodesSeq(nbNodes);
std::iota(nodesSeq.begin(), nodesSeq.end(), 0);
std::vector<std::size_t> nodesSeq(nbNodes);
std::iota(nodesSeq.begin(), nodesSeq.end(), static_cast<std::size_t>(0));
// Don't use gen or seed here, must be different each time!
std::shuffle(nodesSeq.begin(), nodesSeq.end(), std::default_random_engine(std::random_device{}()));
std::vector<NodePtr> nodes(nbNodes, nullptr);
for (auto idx : nodesSeq) {
const std::string name = nodesType[idx] + std::to_string(idx);
nodes[idx] = GenericOperator(nodesType[idx].c_str(), nbIOs[idx].first, 0, nbIOs[idx].second, name.c_str());
nodes[idx] = GenericOperator(nodesType[idx], nbIOs[idx].first, 0, nbIOs[idx].second, name);
}
for (size_t i = 0; i < nbNodes; ++i) {
for (size_t j = (acyclic) ? i + 1 : 0; j < nbNodes; ++j) {
for (std::size_t i = 0; i < nbNodes; ++i) {
for (std::size_t j = (acyclic) ? i + 1 : 0; j < nbNodes; ++j) {
if (i == j) {
// Do not connected node to itself in case of cyclic graph!
continue;
}
for (size_t outId = 0; outId < nodes[i]->nbOutputs(); ++outId) {
for (size_t inId = 0; inId < nodes[j]->nbInputs(); ++inId) {
for (std::size_t outId = 0; outId < nodes[i]->nbOutputs(); ++outId) {
for (std::size_t inId = 0; inId < nodes[j]->nbInputs(); ++inId) {
if (dLink(gen)) {
// Warning: connections can be set multiple time for the
// Warning: connections can be set multiple time for the
// same node input! In this case, the previous connection
// is overwritten. This is the expected behavior.
nodes[i]->addChild(nodes[j], outId, inId);
......@@ -82,7 +92,7 @@ std::pair<Aidge::NodePtr, std::set<Aidge::NodePtr>> Aidge::RandomGraph::gen(std:
NodePtr rootNode = nullptr;
std::set<NodePtr> nodesSet;
for (size_t i = 0; i < nbNodes; ++i) {
for (std::size_t i = 0; i < nbNodes; ++i) {
if (nodes[i]->type() != omitType) {
if (rootNode == nullptr) {
rootNode = nodes[i];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment