24 constexpr uint32_t
uint32_limit = std::numeric_limits<uint32_t>::max();
35 std::vector<std::pair<int32_t, int32_t>>
nodes;
57 uint32_t
dist(
const std::pair<int32_t, int32_t> &p1,
58 const std::pair<int32_t, int32_t> &p2)
const {
59 return static_cast<uint32_t
>(std::round(
60 std::sqrt((p1.first - p2.first) * (p1.first - p2.first) +
61 (p1.second - p2.second) * (p1.second - p2.second))));
68 uint32_t
dist(uint32_t i, uint32_t j)
const {
77 uint32_t
min_dist(uint32_t x, uint32_t y)
const {
79 for (
const auto &[px, py] :
nodes) {
96 std::pair<int32_t, int32_t> &
operator[](uint32_t index) {
113 std::tuple<qbpp::Model, bool, qbpp::Vector<qbpp::Vector<qbpp::Var>>>
121 x(std::get<2>(tuple)) {}
149 const std::vector<uint32_t>
tour;
179 for (
const auto &i :
tour)
181 std::cout <<
" " << i;
184 std::cout << std::endl;
190 if (i == 0 && j == 0)
192 else if (i == 0 || j == 0)
198 std::cout << std::endl;
209 std::vector<std::tuple<int, int, std::string>>
nodes;
211 std::vector<std::tuple<int, int>>
edges;
221 void add_node(
int x,
int y,
const std::string &label =
"") {
222 nodes.push_back(std::make_tuple(x, y, label));
227 void add_node(std::pair<int, int> node,
const std::string &label =
"") {
228 add_node(node.first, node.second, label);
235 void add_edge(
unsigned int node1,
unsigned int node2) {
236 edges.push_back(std::make_pair(node1, node2));
248 void draw(std::string filename) {
249 std::ostringstream dot_stream;
250 dot_stream <<
"graph G {\n"
251 <<
"node [shape=circle, fixedsize=true, width=3, fontsize=100, "
253 <<
"edge [penwidth=5];\n";
255 for (
auto [x, y, s] :
nodes) {
256 dot_stream << index <<
" [label = \"" << index <<
"\", pos = \"" << x
257 <<
"," << y <<
"!\"";
258 if (s !=
"") dot_stream <<
" " << s;
259 dot_stream <<
"];\n";
262 for (
auto [node1, node2] :
edges) {
263 dot_stream << node1 <<
" -- " << node2 <<
"\n";
266 std::string command =
"neato -T" +
267 filename.substr(filename.rfind(
'.') + 1) +
" -o " +
270 std::unique_ptr<FILE, qbpp::misc::PcloseDeleter> pipe(
271 popen(command.c_str(),
"w"));
276 fprintf(pipe.get(),
"%s", dot_stream.str().c_str());
287 for (uint32_t i = 0; i < n; i++) {
288 uint32_t counter = 0;
289 uint32_t max_dist = 1;
291 while (counter < 10) {
295 if (
dist >= max_dist) {
308 inline std::tuple<qbpp::Model, bool, qbpp::Vector<qbpp::Vector<qbpp::Var>>>
312 std::cout <<
"Generating QUBO expression for permutation." << std::endl;
315 std::cout <<
"Generating QUBO expressions for tour distances." << std::endl;
320 auto &
expr = exprs[i];
324 if (j == k)
continue;
325 expr += tsp_map.
dist(j, k) *
x[i][j] *
x[next_i][k];
333 std::cout <<
"Fixing the first visiting node." << std::endl;
337 fix0.push_back({
x[i][0], 0});
339 tsp_expr.replace(fix0);
342 std::cout <<
"Simplifying the QUBO expression." << std::endl;
344 tsp_expr.simplify_as_binary();
355 std::vector<uint32_t>
tour;
371 tour.push_back(node);
energy_t get_energy() const
var_val_t get(vindex_t index) const
void push_back(const T &t)
Class to draw a simple undirected graph.
void add_node(std::pair< int, int > node, const std::string &label="")
Add a node to the graph.
DrawSimpleGraph()=default
Default constructor to create an empty graph.
void add_edge(unsigned int node1, unsigned int node2)
Add an edge to the graph.
std::vector< std::tuple< int, int > > edges
List of edges (node1, node2)
uint32_t node_count() const
Get the number of nodes in the graph.
std::vector< std::tuple< int, int, std::string > > nodes
List of nodes with coordinate (x, y)
void draw(std::string filename)
Draw the graph in a file.
void add_node(int x, int y, const std::string &label="")
Add a node to the graph.
uint32_t edge_count() const
Get the number of edges in the graph.
Class to generates a random map for the TSP with n nodes.
TSPMap(uint32_t grid_size=100)
Constructor: Creates an empty map.
uint32_t min_dist(uint32_t x, uint32_t y) const
Compute the minimum distance between a new node and all other nodes.
uint32_t dist(const std::pair< int32_t, int32_t > &p1, const std::pair< int32_t, int32_t > &p2) const
Compute the Euclidean distance between two nodes.
uint32_t dist(uint32_t i, uint32_t j) const
Compute the Euclidean distance between two nodes.
void add_node(uint32_t x, uint32_t y)
Add a node to the map.
uint32_t node_count() const
Gets the number of nodes in the map.
void gen_random_map(uint32_t n)
Generate a random map with n nodes.
std::vector< std::pair< int32_t, int32_t > > nodes
List of nodes with coordinate (x, y)
std::pair< int32_t, int32_t > & operator[](uint32_t index)
Get the position of the node at index i.
const uint32_t grid_size
Size of the grid. grid_size x grid_size coordinates are used for the map.
uint32_t get_grid_size() const
Gets the size of the grid.
Class to store the QUBO expression for the Traveling Salesman Problem (TSP).
TSPQuadModel(const TSPMap &map, bool fix_first=false)
Generate a QUBO expression for the Traveling Salesman Problem (TSP) from a map.
bool get_fix_first() const
Returns true if the first node is fixed to node 0.
uint32_t node_count() const
Returns the number of nodes in the TSP.
const qbpp::Vector< qbpp::Vector< qbpp::Var > > x
Variables for the TSP.
qbpp::Var get_var(uint32_t i, uint32_t j) const
Get the variable at (i, j).
TSPQuadModel(std::tuple< qbpp::QuadModel, bool, qbpp::Vector< qbpp::Vector< qbpp::Var >>> tuple)
const bool fix_first
true if the first node is fixed to node 0.
std::tuple< qbpp::Model, bool, qbpp::Vector< qbpp::Vector< qbpp::Var > > > helper_func(const TSPMap &map, bool fix_first)
Generate a QUBO expression for the Traveling Salesman Problem (TSP).
Class to store a Tour of the TSP.
TSPSol(const TSPQuadModel &tsp_quad_model, const qbpp::Sol &sol)
Generates a solution of the Traveling Salesman Problem (TSP) from a QUBO model and its solution.
uint32_t node_count() const
Get the number of nodes in the tour.
const Sol sol
Solution of the QUBO model.
const TSPQuadModel tsp_quad_model
QUBO expression for the Traveling Salesman Problem (TSP).
static std::vector< uint32_t > gen_tour(const TSPQuadModel &tsp_quad_model, const Sol &sol)
helper function to generate a tour from the solution.
uint32_t operator[](uint32_t index) const
Get the node at index in the tour.
void print() const
Print the tour.
void print_matrix() const
const std::vector< uint32_t > tour
A vector of nodes representing the tour.
constexpr uint32_t uint32_limit
Generates a QUBO Expression for the Graph Coloring Problem using QUBO++ library.
Expr vector_sum(const T &items[[maybe_unused]])
Var var(const std::string &var_str)
Vector< Vector< Expr > > transpose(const Vector< Vector< T >> &vec)
Expr sum(const Vector< T > &items)
std::list< std::pair< std::variant< Var, VarInt >, Expr > > MapList
QUBO++, a C++ library for generating expressions for binary and spin variables.
#define THROW_MESSAGE(...)
A miscellaneous library used for sample programs of the QUBO++ library.