Camargue
Classes | Typedefs | Functions
CMR::util Namespace Reference

Utility functions/structures used miscellaneous places in the project. More...

Classes

struct  C_resource_deleter
 Class template for deleting resources allocated by C functions. More...
 
class  EdgeHash
 Hash map for node pairs representing edges. More...
 
class  Fixed64
 A 64-bit fixed precision number type "implemented" as a wrapper to CCbigguy. More...
 
struct  retcode_error
 Structure for converting retcodes to exceptions. More...
 
class  ScopeGuard
 Code from Andrei Alexandrescu's ScopeGuard11 slides. More...
 
class  SquareUT
 Class template for a square upper triangular matrix. More...
 

Typedefs

template<typename numtype >
using c_array_ptr = std::unique_ptr< numtype, C_resource_deleter< numtype >>
 Alias declaration for unique_ptr to C array. More...
 

Functions

template<typename act_type >
ScopeGuard< act_type > make_guard (act_type f)
 Type deduction function for ScopeGuard, also from Andrei Alexandrescu. More...
 
Fixed64 operator+ (Fixed64 a, Fixed64 b)
 
Fixed64 operator- (Fixed64 a, Fixed64 b)
 
void add_mult (Fixed64 &f, const Fixed64 &g, int m)
 
void add_mult (double &d, const double &g, int m)
 
std::ostream & operator<< (std::ostream &os, const Fixed64 &f)
 
void write_tour_nodes (const std::vector< int > &tour_nodes, const std::string &tour_nodes_fname)
 Writes a tour to file. More...
 
void write_tour_edges (const std::vector< int > &tour_edges, const std::vector< Graph::Edge > &edges, const int node_count, const std::string &tour_edges_fname)
 Writes tour edges to file. More...
 
void write_lp_edges (const std::vector< int > &lp_elist, const std::vector< double > &lp_ecap, const int node_count, const std::string &lp_edges_fname)
 Writes LP edges to file. More...
 
void write_xy_coords (const double *x, const double *y, const int ncount, const std::string &xy_coords_fname)
 Dumps the xy-coordinates for nodes in a graph. More...
 
void get_tour_nodes (const int node_count, std::vector< int > &tour_nodes, const std::string &tour_nodes_fname)
 Load a tour from file. More...
 
void get_lp_sol (const int node_count, std::vector< int > &support_elist, std::vector< double > &support_ecap, const std::string &lp_sol_fname)
 Loads an lp solution from file. More...
 
bool var_integral (double d)
 Is a zero-one variable considered integral.
 
double zeit (void)
 CPU time function.
 
double real_zeit (void)
 Wall clock time function.
 
template<typename T , typename... Args>
std::unique_ptr< T > make_unique (Args &&...args)
 As per Herb Sutter, port of C++14's make_unique faculty.
 
template<typename T , typename... Args>
void ptr_reset (std::unique_ptr< T > &target, Args &&...args)
 Adaptation of make_unique to reset a unique_ptr to an object. More...
 
template<typename ElemType , typename PredType >
void erase_remove (std::vector< ElemType > &vec, PredType pred)
 Standardization of erase-remove idiom. More...
 

Detailed Description

Utility functions/structures used miscellaneous places in the project.

Typedef Documentation

template<typename numtype >
using CMR::util::c_array_ptr = typedef std::unique_ptr<numtype, C_resource_deleter<numtype>>

Alias declaration for unique_ptr to C array.

This specialization of unique_ptr takes ownership of a C-style array which was dynamically allocated by a C routine, freeing its memory appropriately when necessary.

Function Documentation

template<typename ElemType , typename PredType >
void CMR::util::erase_remove ( std::vector< ElemType > &  vec,
PredType  pred 
)

Standardization of erase-remove idiom.

Parameters
[in,out]vecthe vector to modify.
[in]predthe predicate to apply.
Postcondition
deletes all elements of vec for which pred returns true.
void CMR::util::get_lp_sol ( const int  node_count,
std::vector< int > &  support_elist,
std::vector< double > &  support_ecap,
const std::string &  lp_sol_fname 
)

Loads an lp solution from file.

Reads a specified solution from file, storing edge endpoints and capacities in vectors.

Parameters
[in]node_countthe dimension of the instance.
[out]support_elista node-node list of the edges in the solution.
[out]support_ecapcapacities corresponding to the edges in support_elist.
[in]lp_sol_fnamethe filename to read from.
Precondition
lp_sol_fname names an existant file whose first line is n m where n is node_count and m is the number of nonzero edges in the solution.
Postcondition
support_ecap.size() == m and support_elist.size() == 2 * m.
For all positive i, if the ith line of lp_sol_fname is u v w then
support_elist[2 * i] == u
suppor_elist[(2 * i) + 1] == v
support_ecap[i] == w
void CMR::util::get_tour_nodes ( const int  node_count,
std::vector< int > &  tour_nodes,
const std::string &  tour_nodes_fname 
)

Load a tour from file.

Reads a tour from file, storing it in a vector of nodes.

Some basic checks are performed to ensure that the tour is on the right number of nodes, and visits no city more than once.

Parameters
[in]node_countthe number of nodes in the instance.
[out]tour_nodesthe vector used to store the nodes.
[in]tour_nodes_fnamethe filename to read from.
Precondition
tour_nodes_fname names an existant file whose first line is node_count and whose following entries are a cyclic permutation of the numbers 0, ..., node_count
Postcondition
tour_nodes has length node_count and its entries are the nodes from tour_nodes_fname in the same order
template<typename act_type >
ScopeGuard<act_type> CMR::util::make_guard ( act_type  f)

Type deduction function for ScopeGuard, also from Andrei Alexandrescu.

Parameters
[in]fthe function to be called when the guard goes out of scope. The suggested usage is auto cleanup = make_guard([] {cleanup_lambda});
template<typename T , typename... Args>
void CMR::util::ptr_reset ( std::unique_ptr< T > &  target,
Args &&...  args 
)

Adaptation of make_unique to reset a unique_ptr to an object.

For classes with reference members, we cannot easily define move constructors. This function provides a surrogate move constructor by resetting a unique_ptr to such an object.

void CMR::util::write_lp_edges ( const std::vector< int > &  lp_elist,
const std::vector< double > &  lp_ecap,
const int  node_count,
const std::string &  lp_edges_fname 
)

Writes LP edges to file.

Writes the specified lp solution with edges and capacities to file.

Parameters
[in]lp_elistthe edges in an LP solution in node node format
[in]lp_ecapthe value assigned to each edge in the LP solution
[in]node_countthe number of nodes in the instance.
[in]lp_edges_fnamethe file to write to.
Precondition
lp_elist and lp_ecap nonempty
lp_elist.size() is twice lp_ecap.size()
lp_ecap[i] is the weight on the edge lp_elist[2i] , lp_elist[2i + 1]
lp_edges_fname is a nonempty string
Postcondition
lp_edges_fname will be the name of a file with node_count lp_ecap.size() on the first line, followed by support_elist[2i] support_elist[2i + 1] support_ecap[2i] for all i from 0 to lp_ecap.size()
void CMR::util::write_tour_edges ( const std::vector< int > &  tour_edges,
const std::vector< Graph::Edge > &  edges,
const int  node_count,
const std::string &  tour_edges_fname 
)

Writes tour edges to file.

Parameters
[in]tour_edgesthe edges of the tour to write to file.
[in]edgesa vector of Edge structs indicating edge endpoints
[in]node_countthe number of nodes in the problem, hence indicating maximum index of an endpoint in edges or tour_edges.
[in]tour_edges_fnamethe file name to write to.
Precondition
tour_edges and edges are nonempty, of the same size
tour_edges is binary, and the collection of edges[i] for which tour_edges[i] == 1 gives a connected cyclical graph
edges is a list of edges in a graph with node_count nodes
tour_edges_fname is a nonempty string
Postcondition
tour_edges_fname will be the name of a file with node_count and edges.size() on the top line, followed by edges[i].end[0] edges[i].end[1] 1.0 for each i such that tour_edges[i] == 1
void CMR::util::write_tour_nodes ( const std::vector< int > &  tour_nodes,
const std::string &  tour_nodes_fname 
)

Writes a tour to file.

Writes the tour specified by tour_nodes to the file named tour_nodes_fname.

Precondition
tour_nodes is nonempty, containing a cyclic permutation of the numbers from 0 to tour_nodes.size()
tour_nodes_fname is a nonempty filename
Postcondition
tour_nodes_fname will be the name of a file with tour_nodes.size() at the top, followed by the entries of tour_nodes
Returns
0 if successful, 1 if error
void CMR::util::write_xy_coords ( const double *  x,
const double *  y,
const int  node_count,
const std::string &  xy_coords_fname 
)

Dumps the xy-coordinates for nodes in a graph.

Parameters
[in]xa c-style array of x coordinates
[in]ya c-style array of y coordinates
[in]node_countthe number of nodes in the instance, hence the lengths of x and y.
[in]xy_coords_fnamethe filename to write to.
Postcondition
xy_coords_fname has ncount on its first line, followed by x[i] y[i] on all the following lines.