7 #include <boost/program_options.hpp>
14 namespace po = boost::program_options;
37 if (where == GRB_CB_MIPSOL) {
40 std::cout <<
"TTS = " << std::fixed << std::setprecision(3)
56 int main(
int argc,
char **argv) {
58 po::options_description desc(
59 "Solving the randomly generated TSP using QUBO++ Easy Solver");
60 desc.add_options()(
"help,h",
"produce help message")
61 (
"nodes,n", po::value<uint32_t>()->default_value(10),
"set the number of nodes in the TSP map")
62 (
"time,t", po::value<uint32_t>()->default_value(10),
"set time limit in seconds")
63 (
"tsp_seed,s", po::value<uint32_t>(),
"set the random seed for the TSP map")
64 (
"output,o", po::value<std::string>(),
"set the output file (png, svg, etc) to save the TSP solution")
65 (
"fix,f",
"fix node 0 as the starting node");
70 po::store(po::parse_command_line(argc, argv, desc), vm);
71 }
catch (
const std::exception &e) {
72 std::cout <<
"Wrong arguments. Please use -h/--help option to see the "
78 if (vm.count(
"help")) {
79 std::cout << desc << std::endl;
84 uint32_t nodes = vm[
"nodes"].as<uint32_t>();
86 uint32_t time_limit = vm[
"time"].as<uint32_t>();
88 bool fix_first = vm.count(
"fix");
91 if (vm.count(
"tsp_seed")) {
97 std::cout <<
"Generating random TSP Map with " << nodes <<
" nodes"
102 std::cout <<
"Generating a TSP QUBO model" << std::endl;
105 std::cout <<
"Variables = " << tsp_quad_model.
var_count()
106 <<
" Linear Terms = " << tsp_quad_model.
term_count(1)
107 <<
" Quadratic Terms = " << tsp_quad_model.
term_count(2)
113 grb_model.
set(grb_callback);
115 std::cout <<
"Solving the TSP" << std::endl;
121 if (vm.count(
"output")) {
123 for (uint32_t i = 0; i < nodes; ++i) graph.
add_node(tsp_map[i]);
125 for (uint32_t i = 0; i < nodes; ++i)
126 graph.
add_edge(tsp_sol[i], tsp_sol[(i + 1) % nodes]);
128 graph.
draw(vm[
"output"].as<std::string>());
Class to define Gurobi callback function for factorization.
std::optional< qbpp::energy_t > target_energy
const qbpp::tsp::TSPQuadModel & tsp_quad_model
GRB_Callback(const qbpp::tsp::TSPQuadModel &tsp_quad_model, qbpp::energy_t target_energy)
Construct a new grb callback object.
void callback() override
callback function for Gurobi optimizer.
vindex_t var_count() const
size_t term_count(vindex_t deg) const
static void set_seed(uint32_t seed=1)
Class to draw a simple undirected graph.
void add_edge(unsigned int node1, unsigned int node2)
Add an edge to the graph.
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.
Class to generates a random map for the TSP with n nodes.
void gen_random_map(uint32_t n)
Generate a random map with n nodes.
Class to store the QUBO expression for the Traveling Salesman Problem (TSP).
Class to store a Tour of the TSP.
void print() const
Print the tour.
Class to manage a callback function called by Gurobi Optimizer.
Sol get_sol()
Get the solution obtained by Gurobi Optimizer.
Callback(const QuadModel &quad_model)
Constructor: a new Callback object.
Class to store a QUBO model using Gurobi Optimizer through QUBO++ library.
Sol optimize()
Optimize the QUBO model.
void set_time_limit(uint32_t time_limit)
Sets time limit to the Gurobi model.
void set(const std::string &key, const std::string &val)
Sets a parameter to the Gurobi environment.
Namespace to use Gurobi optimizer from QUBO++ library.
QUBO++, a C++ library for generating expressions for binary and spin variables.
QUBO++ interface to call Gurobi Optimizer.
A miscellaneous library used for sample programs of the QUBO++ library.
Generates a QUBO Expression for the Traveling Salesman Problem (TSP) using QUBO++ library.
int main(int argc, char **argv)
Main function to generate a random map and solve the Traveling Salesman Problem (TSP) using the Gurob...