Skip to content

Commit

Permalink
fixe main
Browse files Browse the repository at this point in the history
  • Loading branch information
mathislebel committed Jan 23, 2025
1 parent 33a7dda commit a8f2e33
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 231 deletions.
61 changes: 60 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,65 @@
"unordered_map": "cpp",
"queue": "cpp",
"iostream": "cpp",
"stack": "cpp"
"stack": "cpp",
"__bit_reference": "cpp",
"__hash_table": "cpp",
"__node_handle": "cpp",
"__split_buffer": "cpp",
"__threading_support": "cpp",
"__tree": "cpp",
"__verbose_abort": "cpp",
"any": "cpp",
"array": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"charconv": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"execution": "cpp",
"memory": "cpp",
"forward_list": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"list": "cpp",
"locale": "cpp",
"map": "cpp",
"mutex": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"print": "cpp",
"ratio": "cpp",
"regex": "cpp",
"set": "cpp",
"shared_mutex": "cpp",
"span": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"tuple": "cpp",
"typeinfo": "cpp",
"valarray": "cpp",
"variant": "cpp",
"algorithm": "cpp"
}
}
2 changes: 1 addition & 1 deletion src/api.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include "crow_all.h"



5 changes: 5 additions & 0 deletions src/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include <algorithm>
#include <iostream>

// Constructeur par défaut
Graph::Graph() : adjList() {}

// Ajouter une arête au graphe
void Graph::add_edge(int source, int target, int weight) {
if (source >= static_cast<int>(adjList.size()) || target >= static_cast<int>(adjList.size())) {
adjList.resize(std::max(source, target) + 1);
Expand All @@ -13,6 +17,7 @@ void Graph::add_edge(int source, int target, int weight) {
adjList[target].emplace_back(source, weight);
}

// Trouver le chemin le plus court entre deux nœuds
std::vector<int> Graph::shortest_path(int source, int target, int& totalTime) {
constexpr int INF = std::numeric_limits<int>::max();

Expand Down
11 changes: 8 additions & 3 deletions src/includes/graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@

struct Edge {
int target;
int weight;
int weight;

Edge(int target, int weight) : target(target), weight(weight) {}
};

class Graph {
public:
Graph();

void add_edge(int source, int target, int weight);
std::vector<int> shortest_path(int source, int target, int& totalTime);



private:
std::vector<std::vector<Edge>> adjList;
std::vector<std::vector<Edge>> adjList;
};

#endif
#endif // GRAPH_HPP

57 changes: 16 additions & 41 deletions src/includes/preprocessing.hpp
Original file line number Diff line number Diff line change
@@ -1,44 +1,19 @@
#pragma once
#ifndef PREPROCESSING_HPP
#define PREPROCESSING_HPP

#include "graph.hpp"
#include <string>
#include <vector>
#include <set>
#include <unordered_map>
#include <atomic>
#include <unordered_set>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iostream>
#include <stdexcept>
#include "graph.hpp"

typedef struct s_Connection {
int landmarkA;
int landmarkB;
int time;
} s_Connection;

typedef struct s_Result {
int maxId;
std::vector<std::string> errorReport;
} s_Result;

typedef std::unordered_map<int, std::vector<int>> TPDF_GRAPH;
typedef std::unordered_set<std::string> TPDF_CONNECTIONS;
typedef std::vector<s_Connection> TPDF_CONNECTIONS_WITH_TIME; //

s_Result analyze_file(const std::string& filePath, int maxLines);

bool preprocess_data(const std::string& filePath, int maxLines);

//json file
void write_json_to_file(const TPDF_CONNECTIONS_WITH_TIME& connectionsWithTime, const std::string& outputJsonFile);

bool preprocess_data(const std::string& filePath, int maxLines, const std::string& outputJsonFile);

void process_chunk(
const std::vector<std::string>& lines, // Remarque : Utilisez std::string et std::vector
Graph& graph,
TPDF_CONNECTIONS& seenConnections,
std::atomic<int>& maxId, // Remarque : Utilisez std::atomic
TPDF_CONNECTIONS_WITH_TIME& connectionsWithTime
);
/**
* @brief Prétraite les données d'un fichier CSV et les stocke dans un graphe.
*
* @param filePath Chemin du fichier CSV.
* @param maxLines Nombre maximum de lignes à traiter (0 = toutes les lignes).
* @param graph Référence au graphe dans lequel les données sont stockées.
* @return true si le prétraitement est réussi, false sinon.
*/
bool preprocess_data(const std::string& filePath, int maxLines, Graph& graph);

#endif // PREPROCESSING_HPP
152 changes: 35 additions & 117 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,138 +1,56 @@
#include "includes/graph.hpp"
#include "includes/preprocessing.hpp"
#include <iostream>
#include <vector>
#include <string>
#include <chrono>
#include <fstream>
#include <stdexcept>
#include <chrono> // Pour mesurer le temps d'exécution

using namespace std;
using namespace chrono;

// Fonction pour afficher le logo
void display_logo(const string& file_path) {
ifstream logo_file(file_path);
if (logo_file.is_open()) {
string line;
while (getline(logo_file, line)) {
cout << line << endl;
}
logo_file.close();
} else {
cerr << "Error: Unable to open logo file." << endl;
}
}

// Fonction pour afficher une barre de progression
void display_progress_bar(size_t current, size_t total) {
const int barWidth = 50;
float progress = static_cast<float>(current) / total;
int position = static_cast<int>(progress * barWidth);

cout << "\r[";
for (int i = 0; i < barWidth; ++i) {
if (i < position) cout << "=";
else if (i == position) cout << ">";
else cout << " ";
}
cout << "] " << int(progress * 100.0) << "% (" << current << "/" << total << ")";
cout.flush();
}

// Prétraitement des données
bool preprocess_data_with_progress(const string& filePath, int maxLines, Graph& graph, size_t& totalLinesProcessed) {
ifstream inputFile(filePath);
if (!inputFile.is_open()) {
throw runtime_error("Error: Unable to find the CSV file!" + filePath);
}

// Compter les lignes dans le fichier
size_t totalLines = 0;
string line;
while (getline(inputFile, line)) {
++totalLines;
if (maxLines > 0 && totalLines >= static_cast<size_t>(maxLines)) break;
}
inputFile.clear();
inputFile.seekg(0, ios::beg);

// Charger les données dans le graphe
size_t currentLine = 0;
while (getline(inputFile, line) && (maxLines <= 0 || currentLine < static_cast<size_t>(maxLines))) {
++currentLine;

int source, target, weight;
if (sscanf(line.c_str(), "%d,%d,%d", &source, &target, &weight) == 3) {
graph.add_edge(source, target, weight);
}

display_progress_bar(currentLine, totalLines);
int main(int argc, char* argv[]) {
if (argc != 4) {
std::cerr << "Usage: " << argv[0] << " <input_file> <source> <target>\n";
return 1;
}

inputFile.close();
cout << endl;

totalLinesProcessed = currentLine;
return true;
}

int main() {
string logoFilePath = "logo/Logo QPS.txt"; // Chemin du fichier contenant le logo
display_logo(logoFilePath);

string inputFilePath = "data/usa_roads.csv"; // Remplacez par le chemin de votre fichier CSV
int maxLines = 28854314; // Nombre maximal de lignes à traiter (ou -1 pour tout charger)
std::string inputFilePath = argv[1];
int source = std::stoi(argv[2]);
int target = std::stoi(argv[3]);

Graph graph;

size_t totalLinesProcessed = 0;
try {
cout << "Prétraitement des données en cours..." << endl;
auto startTime = high_resolution_clock::now();

preprocess_data_with_progress(inputFilePath, maxLines, graph, totalLinesProcessed);

auto endTime = high_resolution_clock::now();
duration<double> elapsed = endTime - startTime;

cout << "Prétraitement terminé avec succès !" << endl;
cout << "Nombre total de lignes traitées : " << totalLinesProcessed << endl;
cout << "Temps écoulé : " << elapsed.count() << " secondes." << endl;
} catch (const exception& e) {
cerr << "Erreur: " << e.what() << endl;
return 1;
}

// Boucle principale pour entrer les requêtes de chemin le plus court
while (true) {
int source, target;
cout << "Entrez le point de départ (ou -1 pour quitter) : ";
cin >> source;
if (source == -1) break;

cout << "Entrez le point d'arrivée : ";
cin >> target;

cout << "Recherche du chemin le plus court..." << endl;
auto startTime = high_resolution_clock::now();

int totalTime = 0;
auto path = graph.shortest_path(source, target, totalTime);

auto endTime = high_resolution_clock::now();
duration<double> elapsed = endTime - startTime;
// Mesurer le temps de prétraitement
auto preprocessingStart = std::chrono::high_resolution_clock::now();
preprocess_data(inputFilePath, 0, graph);
auto preprocessingEnd = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> preprocessingTime = preprocessingEnd - preprocessingStart;

std::cout << "Prétraitement terminé. Données chargées en mémoire.\n";
std::cout << "Temps de prétraitement : " << preprocessingTime.count() << " secondes.\n";

// Mesurer le temps de calcul du chemin le plus court
auto pathCalculationStart = std::chrono::high_resolution_clock::now();
int totalTime;
std::vector<int> path = graph.shortest_path(source, target, totalTime);
auto pathCalculationEnd = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> pathCalculationTime = pathCalculationEnd - pathCalculationStart;

if (path.empty()) {
cout << "Aucun chemin trouvé entre " << source << " et " << target << "." << endl;
std::cout << "Aucun chemin trouvé entre " << source << " et " << target << ".\n";
} else {
cout << "Chemin le plus court de " << source << " à " << target << " : ";
std::cout << "Chemin le plus court : ";
for (int node : path) {
cout << node << " ";
std::cout << node << " ";
}
cout << "\nTemps total : " << totalTime << " unités." << endl;
cout << "Recherche terminée en " << elapsed.count() << " secondes." << endl;
std::cout << "\nTemps total : " << totalTime << "\n";
}

std::cout << "Temps de calcul du chemin : " << pathCalculationTime.count() << " secondes.\n";

} catch (const std::exception& e) {
std::cerr << "Erreur : " << e.what() << "\n";
return 1;
}

cout << "Programme terminé. Merci d'avoir utilisé notre service !" << endl;
return 0;
}
Loading

0 comments on commit a8f2e33

Please sign in to comment.