Skip to content
Snippets Groups Projects
Commit 4b536cb2 authored by Jaime Arias's avatar Jaime Arias
Browse files

refactor: remove map of labels in the graph class

parent 1964e1f9
No related branches found
No related tags found
No related merge requests found
Pipeline #8743 failed with stages
in 8 minutes and 42 seconds
......@@ -111,7 +111,7 @@ int main(int argc, char *argv[]) {
/* --------------------- Integer Linear Programming --------------------- */
std::vector<std::string> optimal_paths =
PathGenerator::ExtractShortPaths(g, generate_paths);
PathGenerator::ExtractShortPaths(g->labels(), generate_paths);
int nb = 0;
size_t avg_opt_size = 0;
......
......@@ -6,6 +6,7 @@
#include <fstream>
#include <iostream>
#include <regex>
#include <set>
#include <sstream>
#include <utility>
#include <vector>
......@@ -69,7 +70,7 @@ T GetRandomElement(const std::set<T> &elements) {
return *std::next(elements.begin(), random(elements.size()));
}
Graph::Graph() {};
Graph::Graph(){};
Graph::Graph(Node *n) : initial_node_(n) {}
......@@ -98,22 +99,12 @@ void Graph::add_state(int id, const std::string &name) {
states_.push_back(n);
}
int Graph::get_label_id(const std::string &label) {
return labels_[label];
}
void Graph::add_label(const std::string &label) {
labels_.insert({label, labels_.size()});
labels_.push_back(label);
}
std::vector<std::string> Graph::labels() {
std::vector<std::string> labels;
for (auto it = labels_.begin(); it != labels_.end(); it++) {
labels.push_back(it->first);
}
return labels;
return labels_;
}
void Graph::add_successor(int source, int target, const std::string &label) {
......
#ifndef IMPLEM_GRAPH_H
#define IMPLEM_GRAPH_H
#include <map>
#include <set>
#include <string>
#include <vector>
......@@ -17,7 +15,7 @@ class Graph {
std::vector<Node *> states_;
/** list of labels of the graph */
std::map<std::string, int> labels_;
std::vector<std::string> labels_;
public:
/**
......@@ -114,13 +112,6 @@ class Graph {
* @return string representation of the graph
*/
std::string toString();
/**
* Get the identfier of a label
* @param label - label
* @return identifier of the label
*/
int get_label_id(const std::string &label);
};
#endif // IMPLEM_GRAPH_H
......@@ -2,6 +2,7 @@
#include <algorithm>
#include <iostream>
#include <unordered_map>
#include "ortools/linear_solver/linear_solver.h"
#include "utils.h"
......@@ -127,12 +128,19 @@ vector<string> PathGenerator::ExtractPaths(const string &regex) {
}
std::vector<std::string> PathGenerator::ExtractShortPaths(
Graph *g, const std::vector<std::string> &generated_paths) {
size_t lignes = g->labels().size();
const std::vector<std::string> &labels,
const std::vector<std::string> &generated_paths) {
size_t lignes = labels.size();
size_t colonnes = generated_paths.size();
cout << "Taille de la Matrice des contraintes : \n"
<< "(" << lignes << " , " << colonnes << ")\n";
// Precompute the index of each label in the vector
std::unordered_map<std::string, int> labels_index;
for (int i = 0; i < lignes; ++i) {
labels_index[labels[i]] = i;
}
float start_time = GetTime();
// Allocation dynamique de la matrice
......@@ -160,7 +168,7 @@ std::vector<std::string> PathGenerator::ExtractShortPaths(
label.clear();
label = generated_paths[i].substr(pos + 1, j - pos);
constraints[g->get_label_id(label)][i] = 1;
constraints[labels_index[label]][i] = 1;
pos = j;
}
......@@ -185,12 +193,12 @@ std::vector<std::string> PathGenerator::ExtractShortPaths(
// Créez les contraintes.
int i = 0;
for (const auto &label : g->labels()) {
for (const auto &label : labels) {
operations_research::MPConstraint *constraint = solver->MakeRowConstraint(
1, operations_research::MPSolver::infinity(), "");
int transition_count = 0;
for (int j = 0; j < colonnes; ++j) {
if (constraints[g->get_label_id(label)][j] == 1) {
if (constraints[labels_index[label]][j] == 1) {
constraint->SetCoefficient(x[j], 1);
transition_count++;
}
......
......@@ -27,8 +27,13 @@ class PathGenerator {
static std::vector<std::string> ExtractPaths(const std::string &regex);
/**
* Extract the shortest paths from the generated paths using integer linear
*programming
**/
static std::vector<std::string> ExtractShortPaths(
Graph *g, const std::vector<std::string> &generated_paths);
const std::vector<std::string> &labels,
const std::vector<std::string> &generated_paths);
};
#endif // IMPLEM_PATHGENERATOR_H
#include "regex_generator.h"
#include <algorithm>
#include <set>
using namespace std;
......
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