Skip to content

Commit

Permalink
feat: add time per city measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
Akhil-CM committed Apr 24, 2024
1 parent 6845082 commit da54313
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 80 deletions.
36 changes: 18 additions & 18 deletions DiscreteENN_TSP_table.csv
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
name,distance,points,error,time(ms)
berlin52,7973.632812,52,5.723055,2.000000
st70,702.206238,70,4.030554,3.000000
eil76,579.543701,76,7.721878,5.000000
pr76,112965.257812,76,4.443696,5.000000
kroA100,22022.208984,100,3.478099,11.000000
kroC100,21645.824219,100,4.322253,9.000000
kroD100,22554.314453,100,5.918636,15.000000
rd100,8143.917969,100,2.957244,9.000000
eil101,663.061829,101,5.415235,14.000000
lin105,15119.822266,105,5.152112,10.000000
ch130,6537.805176,130,7.001721,23.000000
ch150,7053.360352,150,8.047800,40.000000
tsp225,4098.513672,225,4.660717,117.000000
a280,2758.375244,280,6.955225,161.000000
pcb442,54979.710938,442,8.274668,747.000000
pr1002,279898.437500,1002,8.050121,10270.000000
pr2392,419447.375000,2392,10.955521,281731.000000
name,points,error,time(us),time per city(us),time per city min(us),time per city max(us),distance,optimal_distance
berlin52,52,8.519310,288.000000,5.538462,340282346638528859811704183484516925440.000000,-1.000000,8184.526367,7542.000000
st70,70,6.103633,620.000000,8.857142,340282346638528859811704183484516925440.000000,-1.000000,716.199524,675.000000
eil76,76,4.718426,1096.000000,14.421053,340282346638528859811704183484516925440.000000,-1.000000,563.385132,538.000000
pr76,76,3.699711,1183.000000,15.565789,340282346638528859811704183484516925440.000000,-1.000000,112160.570312,108159.000000
kroA100,100,8.135396,1597.000000,15.970000,340282346638528859811704183484516925440.000000,-1.000000,23013.375000,21282.000000
kroC100,100,0.799013,1571.000000,15.710000,340282346638528859811704183484516925440.000000,-1.000000,20914.787109,20749.000000
kroD100,100,5.920948,1818.000000,18.180000,340282346638528859811704183484516925440.000000,-1.000000,22554.806641,21294.000000
rd100,100,5.625124,2576.000000,25.760000,340282346638528859811704183484516925440.000000,-1.000000,8354.947266,7910.000000
eil101,101,7.306752,2005.000000,19.851484,340282346638528859811704183484516925440.000000,-1.000000,674.959473,629.000000
lin105,105,5.906815,1573.000000,14.980952,340282346638528859811704183484516925440.000000,-1.000000,15228.340820,14379.000000
ch130,130,4.879879,3963.000000,30.484615,340282346638528859811704183484516925440.000000,-1.000000,6408.160645,6110.000000
ch150,150,5.822298,5902.000000,39.346668,340282346638528859811704183484516925440.000000,-1.000000,6908.079590,6528.000000
tsp225,225,4.255092,16507.000000,73.364441,340282346638528859811704183484516925440.000000,-1.000000,4082.629395,3916.000000
a280,280,8.966777,31469.000000,112.389282,340282346638528859811704183484516925440.000000,-1.000000,2810.253174,2579.000000
pcb442,442,8.710396,112518.000000,254.565613,340282346638528859811704183484516925440.000000,-1.000000,55200.964844,50778.000000
pr1002,1002,7.701775,1799999.000000,1796.406128,340282346638528859811704183484516925440.000000,-1.000000,278996.062500,259045.000000
pr2392,2392,10.202493,97116608.000000,40600.589844,340282346638528859811704183484516925440.000000,-1.000000,416600.687500,378032.000000
36 changes: 18 additions & 18 deletions DiscreteENN_TSP_table.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
name distance points error time(ms)
berlin52 7973.632812 52 5.723055 2.000000
st70 702.206238 70 4.030554 3.000000
eil76 579.543701 76 7.721878 5.000000
pr76 112965.257812 76 4.443696 5.000000
kroA100 22022.208984 100 3.478099 11.000000
kroC100 21645.824219 100 4.322253 9.000000
kroD100 22554.314453 100 5.918636 15.000000
rd100 8143.917969 100 2.957244 9.000000
eil101 663.061829 101 5.415235 14.000000
lin105 15119.822266 105 5.152112 10.000000
ch130 6537.805176 130 7.001721 23.000000
ch150 7053.360352 150 8.047800 40.000000
tsp225 4098.513672 225 4.660717 117.000000
a280 2758.375244 280 6.955225 161.000000
pcb442 54979.710938 442 8.274668 747.000000
pr1002 279898.437500 1002 8.050121 10270.000000
pr2392 419447.375000 2392 10.955521 281731.000000
name points error time(us) time per city(us) time per city min(us) time per city max(us) distance optimal_distance
berlin52 52 8.519310 288.000000 5.538462 340282346638528859811704183484516925440.000000 -1.000000 8184.526367 7542.000000
st70 70 6.103633 620.000000 8.857142 340282346638528859811704183484516925440.000000 -1.000000 716.199524 675.000000
eil76 76 4.718426 1096.000000 14.421053 340282346638528859811704183484516925440.000000 -1.000000 563.385132 538.000000
pr76 76 3.699711 1183.000000 15.565789 340282346638528859811704183484516925440.000000 -1.000000 112160.570312 108159.000000
kroA100 100 8.135396 1597.000000 15.970000 340282346638528859811704183484516925440.000000 -1.000000 23013.375000 21282.000000
kroC100 100 0.799013 1571.000000 15.710000 340282346638528859811704183484516925440.000000 -1.000000 20914.787109 20749.000000
kroD100 100 5.920948 1818.000000 18.180000 340282346638528859811704183484516925440.000000 -1.000000 22554.806641 21294.000000
rd100 100 5.625124 2576.000000 25.760000 340282346638528859811704183484516925440.000000 -1.000000 8354.947266 7910.000000
eil101 101 7.306752 2005.000000 19.851484 340282346638528859811704183484516925440.000000 -1.000000 674.959473 629.000000
lin105 105 5.906815 1573.000000 14.980952 340282346638528859811704183484516925440.000000 -1.000000 15228.340820 14379.000000
ch130 130 4.879879 3963.000000 30.484615 340282346638528859811704183484516925440.000000 -1.000000 6408.160645 6110.000000
ch150 150 5.822298 5902.000000 39.346668 340282346638528859811704183484516925440.000000 -1.000000 6908.079590 6528.000000
tsp225 225 4.255092 16507.000000 73.364441 340282346638528859811704183484516925440.000000 -1.000000 4082.629395 3916.000000
a280 280 8.966777 31469.000000 112.389282 340282346638528859811704183484516925440.000000 -1.000000 2810.253174 2579.000000
pcb442 442 8.710396 112518.000000 254.565613 340282346638528859811704183484516925440.000000 -1.000000 55200.964844 50778.000000
pr1002 1002 7.701775 1799999.000000 1796.406128 340282346638528859811704183484516925440.000000 -1.000000 278996.062500 259045.000000
pr2392 2392 10.202493 97116608.000000 40600.589844 340282346638528859811704183484516925440.000000 -1.000000 416600.687500 378032.000000
Binary file modified a.out
Binary file not shown.
15 changes: 9 additions & 6 deletions compile_cpp.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
# COMPILER="clang++"
COMPILER="g++"

FLAGS_CXX="-march=native -fstrict-aliasing -Wall -Wextra -pedantic -fsanitize=address,undefined"
CXX_STANDARD="-std=c++17"

FLAGS_EXTRA="-lsfml-graphics -lsfml-window -lsfml-system"
# FLAGS_EXTRA=""
FLAGS_CXX="-fstrict-aliasing -Wall -Wextra -pedantic"
FLAGS_CXX_EXTRA="-fsanitize=address,undefined"

FLAGS_RELEASE=""

FLAGS_EXTRA="-lsfml-graphics -lsfml-window -lsfml-system"
# FLAGS_EXTRA=""

if [[ "$1" == "run" ]]; then
FLAGS_RELEASE="-O3 -DNDEBUG"
FLAGS_RELEASE="-march=native -O3 -DNDEBUG"
FLAGS_CXX_EXTRA=""
shift
fi

echo "Compiling the following:"
echo "$COMPILER $CXX_STANDARD $FLAGS_CXX $FLAGS_EXTRA $FLAGS_RELEASE $@"
"$COMPILER" $CXX_STANDARD $FLAGS_CXX $FLAGS_EXTRA $FLAGS_RELEASE "$@"
echo "$COMPILER $CXX_STANDARD $FLAGS_CXX $FLAGS_CXX_EXTRA $FLAGS_EXTRA $FLAGS_RELEASE $@"
"$COMPILER" $CXX_STANDARD $FLAGS_CXX $FLAGS_CXX_EXTRA $FLAGS_EXTRA $FLAGS_RELEASE "$@"
# sleep 2 &&
# time ./a.out
4 changes: 2 additions & 2 deletions draw_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# List of pairs of coordinates, where each pair is a line segment
# Each element is ((x1, y1), (x2, y2))
line_segments = [
((80.000000, 41.000000), (80.000000, 25.000000)),
((80.000000, 25.000000), (72.000000, 25.000000)),
((6850.000000, 11700.000000), (6665.000000, 11710.000000)),
((6665.000000, 11860.000000), (6775.000000, 11650.000000)),
]

# Create a plot
Expand Down
52 changes: 31 additions & 21 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@
#include "tsp_discrete_enn.hpp"
#include "utils.hpp"

#include <chrono>

namespace stdfs = std::filesystem;

using TimeMilliS_t = std::chrono::milliseconds;
using TimeMicroS_t = std::chrono::microseconds;
using TimeUnit_t = TimeMilliS_t;
using TimePoint_t = std::chrono::steady_clock::time_point;
const std::string& time_unit{ "ms" };

const std::string& Data_Optimal_Filename{ "./tsp_optimal_distances.csv" };
const stdfs::path Data_Optimal_Path{ utils::getCleanPath(
stdfs::current_path() / stdfs::path(Data_Optimal_Filename)) };
Expand Down Expand Up @@ -127,7 +119,7 @@ int main(int argc, char** argv)
optimal_info.m_distance;
info.m_error = error*100;
}
utils::printInfo("name\tdistance\tpoints\terror\ttime(ms)");
utils::printInfo("name\tpoints\terror\ttime(" + time_unit + ")\ttime per city(" + time_unit + ")\ttime per city min(" + time_unit + ")\ttime per city max(" + time_unit + ")\tdistance\toptimal_distance\n");
for (auto it{ optimal_infos.begin() }; it != optimal_infos.end(); ++it) {
TSPInfo& opt_info{ *it };
int info_pos{ utils::vectFind(opt_info, infos) };
Expand All @@ -138,13 +130,18 @@ int main(int argc, char** argv)
continue;
}
TSPInfo& info{ infos[static_cast<std::size_t>(info_pos)] };
utils::printInfo(info.m_name + "\t" + std::to_string(info.m_distance) +
utils::printInfo(info.m_name +
"\t" + std::to_string(info.m_points) + "\t" +
std::to_string(info.m_error) + "\t" +
std::to_string(info.m_time));
std::to_string(info.m_time) + "\t" +
std::to_string(info.m_timePerCity) + "\t" +
std::to_string(info.m_timePerCityMin) + "\t" +
std::to_string(info.m_timePerCityMax) + "\t" +
std::to_string(info.m_distance) + "\t" +
std::to_string(opt_info.m_distance));
}
std::ofstream table_file{ "DiscreteENN_TSP_table.txt" };
table_file << "name\tdistance\tpoints\terror\ttime(ms)\n";
table_file << "name\tpoints\terror\ttime(" + time_unit + ")\ttime per city(" + time_unit + ")\ttime per city min(" + time_unit + ")\ttime per city max(" + time_unit + ")\tdistance\toptimal_distance\n";
for (auto it{ optimal_infos.begin() }; it != optimal_infos.end(); ++it) {
TSPInfo& opt_info{ *it };
int info_pos{ utils::vectFind(opt_info, infos) };
Expand All @@ -155,15 +152,20 @@ int main(int argc, char** argv)
continue;
}
TSPInfo& info{ infos[static_cast<std::size_t>(info_pos)] };
table_file << (info.m_name + "\t" + std::to_string(info.m_distance) +
"\t" + std::to_string(info.m_points) + "\t" +
std::to_string(info.m_error) + "\t" +
std::to_string(info.m_time))
table_file << (info.m_name +
"\t" + std::to_string(info.m_points) + "\t" +
std::to_string(info.m_error) + "\t" +
std::to_string(info.m_time) + "\t" +
std::to_string(info.m_timePerCity) + "\t" +
std::to_string(info.m_timePerCityMin) + "\t" +
std::to_string(info.m_timePerCityMax) + "\t" +
std::to_string(info.m_distance) + "\t" +
std::to_string(opt_info.m_distance))
<< std::endl;
}
table_file.close();
std::ofstream csv_file{ "DiscreteENN_TSP_table.csv" };
csv_file << "name,distance,points,error,time(ms)\n";
csv_file << "name,points,error,time(" + time_unit + "),time per city(" + time_unit + "),time per city min(" + time_unit + "),time per city max(" + time_unit + "),distance,optimal_distance\n";
for (auto it{ optimal_infos.begin() }; it != optimal_infos.end(); ++it) {
TSPInfo& opt_info{ *it };
int info_pos{ utils::vectFind(opt_info, infos) };
Expand All @@ -174,10 +176,15 @@ int main(int argc, char** argv)
continue;
}
TSPInfo& info{ infos[static_cast<std::size_t>(info_pos)] };
csv_file << (info.m_name + "," + std::to_string(info.m_distance) +
"," + std::to_string(info.m_points) + "," +
std::to_string(info.m_error) + "," +
std::to_string(info.m_time))
csv_file << (info.m_name +
"," + std::to_string(info.m_points) + "," +
std::to_string(info.m_error) + "," +
std::to_string(info.m_time) + "," +
std::to_string(info.m_timePerCity) + "," +
std::to_string(info.m_timePerCityMin) + "," +
std::to_string(info.m_timePerCityMax) + "," +
std::to_string(info.m_distance) + "," +
std::to_string(opt_info.m_distance))
<< std::endl;
}
csv_file.close();
Expand Down Expand Up @@ -379,6 +386,9 @@ int runPipelineSingle(TSPInfo& info, const stdfs::path& data_path,
info.m_distance = dist;
info.m_points = num_cities;
info.m_time = duration;
info.m_timePerCity = static_cast<Value_t>(duration)/num_cities;
info.m_timePerIter = enn_tsp.timePerCity();
std::tie(info.m_timePerCityMin, info.m_timePerCityMax) = enn_tsp.timePerCityMinMax();
std::cout << "\n" + utils::Line_Str + "\n";
std::cout << "[Info]: Total distance is : " << dist << '\n';
std::cout << utils::Line_Str + "\n";
Expand Down
61 changes: 46 additions & 15 deletions tsp_discrete_enn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <tuple>
#include <utility>
#include <array>
#include <chrono>

#include "utils.hpp"

Expand All @@ -27,6 +28,13 @@
#define TSP_DEBUG_PRINT 0
#define TSP_DEBUG_CHECK 0

using TimeMilliS_t = std::chrono::milliseconds;
using TimeMicroS_t = std::chrono::microseconds;
// using TimeUnit_t = TimeMilliS_t;
using TimeUnit_t = TimeMicroS_t;
using TimePoint_t = std::chrono::steady_clock::time_point;
const std::string& time_unit{ "us" };

using namespace std::string_literals;
using utils::ErrorBool, utils::ErrorMsg;

Expand Down Expand Up @@ -457,6 +465,14 @@ class DiscreteENN_TSP
{
return m_repeatLen;
}
Value_t timePerCity()
{
return m_timePerIter/m_iterNum;
}
auto timePerCityMinMax()
{
return std::make_pair(m_timePerIterMin, m_timePerIterMax);
}

int findNode(Node_t node)
{
Expand Down Expand Up @@ -1036,6 +1052,7 @@ class DiscreteENN_TSP
}
continue;
}
// TimePoint_t start_time = std::chrono::steady_clock::now();
const int idx_added = findBestInsertion(it);
if (idx_added == -1) {
utils::printErr("findBestInsertion failed at index " +
Expand Down Expand Up @@ -1080,21 +1097,22 @@ class DiscreteENN_TSP
continue;
}

// if (it_erased1.has_value() or it_erased2.has_value()) {
// it = it_begin;
// }
if (it_erased1.has_value()) {
it = *it_erased1;
}
if (it_erased2.has_value()) {
if (it_erased1.has_value()) {
it = (*it_erased1)->id < (it_erased2.value())->id ?
*it_erased1 :
it_erased2.value();
} else {
it = it_erased2.value();
}
if (it_erased1.has_value() or it_erased2.has_value()) {
const int idx_rand{ distrib(gen) };
it = it_begin + idx_rand;
}
// if (it_erased1.has_value()) {
// it = *it_erased1;
// }
// if (it_erased2.has_value()) {
// if (it_erased1.has_value()) {
// it = (*it_erased1)->id < (it_erased2.value())->id ?
// *it_erased1 :
// it_erased2.value();
// } else {
// it = it_erased2.value();
// }
// }

if (not pattern_hashes.insert(utils::getHash(m_pattern)).second) {
utils::printInfo(
Expand Down Expand Up @@ -1126,6 +1144,13 @@ class DiscreteENN_TSP
std::cout << ("[Debug] (run): drawPath ended\n");
#endif
}
// TimePoint_t end_time = std::chrono::steady_clock::now();
// auto delta = std::chrono::duration_cast<TimeUnit_t>(end_time - start_time);
// const Value_t duration = delta.count();
// m_timePerIter += duration;
// m_timePerIterMin = std::min(duration, m_timePerIterMin);
// m_timePerIterMax = std::max(duration, m_timePerIterMax);
// ++m_iterNum;
}

// #if (TSP_DEBUG_PRINT > 0)
Expand Down Expand Up @@ -1157,8 +1182,11 @@ class DiscreteENN_TSP
int m_initialSize{ Num_Nodes_Initial };
int m_iterRandomize{ Iter_Randomize };
int m_repeatLen{ Repeat_Check_Length };
int m_iterNum{0};
Value_t m_timePerIter{VALUE_ZERO};
Value_t m_timePerIterMin{ std::numeric_limits<Value_t>::max() };
Value_t m_timePerIterMax{ VALUE_ONE_NEG };
std::string m_pattern;
std::vector<std::size_t> m_patternSizes;
Cities_t m_stack;
Path_t m_path;
};
Expand Down Expand Up @@ -1363,7 +1391,10 @@ struct TSPInfo
Value_t m_distance{ VALUE_ONE_NEG };
Value_t m_error{ VALUE_ONE_NEG };
Value_t m_time{ VALUE_ONE_NEG };
Value_t m_timePerIter{ VALUE_ONE_NEG };
Value_t m_timePerCity{ VALUE_ONE_NEG };
Value_t m_timePerCityMin{ VALUE_ONE_NEG };
Value_t m_timePerCityMax{ VALUE_ONE_NEG };
std::string m_name{ "" };
};
template <>
Expand Down

0 comments on commit da54313

Please sign in to comment.