C++ reference¶
The user-facing C++ library, in namespace cptp.
Build a
Problemwithio::loadorModel’s setters, then callModel::solve.Its options,
SolverOptions, are astd::vector<std::pair<std::string, std::string>>of the(key, value)pairs in Options.
Using it from CMake¶
Source-only: no install rules, no find_package. Build it inside your project
and link the target cptp, which brings the include root (src/) and the
patched HiGHS:
cmake_minimum_required(VERSION 3.25)
project(app LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 23) # cptp's headers need it
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CPTP_BUILD_TESTS OFF CACHE BOOL "" FORCE) # else it fetches Catch2
include(FetchContent)
FetchContent_Declare(cptp
GIT_REPOSITORY https://github.com/spoorendonk/cptp.git
GIT_TAG v0.3.0)
FetchContent_MakeAvailable(cptp)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE cptp)
Clone or submodule in your source tree:
add_subdirectory(cptp)replaces theFetchContentlines.First configure clones and patches HiGHS: it needs network access.
No build type set: the build becomes Release, with
-march=native, tied to this CPU;RelWithDebInfoleaves the flag out (quick reference).No
highsof your own (aFetchContentdeclaration or a target) before cptp: FetchContent keeps the first declaration, without cptp’s patch, and the build fails. To call HiGHS directly, use the one cptp brings.Cache variables for the whole build:
BUILD_TESTING,BUILD_SHARED_LIBS,BUILD_EXAMPLESandPDLPare forcedOFF;HIGHS_NO_DEFAULT_THREADSisON, so a HiGHS you call withthreads0 starts one thread. SetBUILD_TESTINGagain before testing it afterFetchContent_MakeAvailable(cptp).
A complete main.cpp, run from the clone (it reads tests/data/tiny4.txt):
#include <iostream>
#include "core/io.h"
#include "model/model.h"
int main() {
cptp::Model model;
model.set_problem(cptp::io::load("tests/data/tiny4.txt"));
auto result = model.solve({{"time_limit", "60"}, {"output_flag", "false"}});
if (!result.has_solution()) {
return 1;
}
std::cout << "objective " << result.objective << ", tour";
for (auto node : result.tour) {
std::cout << ' ' << node;
}
std::cout << '\n'; // objective -11, tour 0 1 2 0
}
Model¶
-
class Model¶
User-facing model: build an CPTP instance and solve it.
Public Functions
-
Model()¶
-
void set_source(int32_t source)¶
-
void set_target(int32_t target)¶
-
void set_depot(int32_t depot)¶
-
void set_profits(std::span<const double> profits)¶
-
void add_capacity_resource(std::span<const double> demands, double limit)¶
-
SolveResult solve(const SolverOptions &options = {})¶
-
inline Logger &logger()¶
-
Model()¶
-
struct Resource¶
A resource constraint on the problem (e.g., capacity).
Problem¶
-
class Problem¶
CPTP instance: undirected graph with profits, costs, and m >= 1 capacity rows sum_i d^k_i y_i <= Q_k (k = 0..m-1), all of which a solution must satisfy. The single-row accessors (demand(v), demands(), capacity()) are row 0.
Public Functions
-
Problem() = default¶
-
void build(int32_t num_nodes, std::span<const Edge> edges, std::span<const double> edge_costs, std::span<const double> profits, std::span<const double> demands, double capacity, int32_t source = 0, int32_t target = 0)¶
Build from raw data (copies from spans), one capacity row. edges[i] = {tail, head} with tail < head.
-
void build(int32_t num_nodes, std::span<const Edge> edges, std::vector<double> edge_costs, std::vector<double> profits, std::vector<double> demands, double capacity, int32_t source = 0, int32_t target = 0)¶
Build by moving owned vectors (avoids copies when caller no longer needs them), one capacity row.
-
void build(int32_t num_nodes, std::span<const Edge> edges, std::vector<double> edge_costs, std::vector<double> profits, std::vector<std::vector<double>> demand_rows, std::vector<double> capacities, int32_t source = 0, int32_t target = 0)¶
Build with m = demand_rows.size() >= 1 capacity rows: demand_rows[k] has one entry per node and capacities[k] is its limit.
-
inline const Graph &graph() const¶
-
inline int32_t num_nodes() const¶
-
inline int32_t num_edges() const¶
-
inline int32_t source() const¶
-
inline int32_t target() const¶
-
inline int32_t depot() const¶
-
inline bool is_tour() const¶
-
inline int32_t num_capacities() const¶
Number of capacity rows m (>= 1 once built).
-
inline double capacity() const¶
-
inline double capacity(int32_t k) const¶
-
inline const std::vector<double> &capacities() const¶
-
inline double edge_cost(int32_t e) const¶
-
inline double profit(int32_t v) const¶
-
inline double demand(int32_t v) const¶
-
inline double demand(int32_t k, int32_t v) const¶
-
inline const std::vector<double> &edge_costs() const¶
-
inline const std::vector<double> &profits() const¶
-
inline const std::vector<double> &demands() const¶
-
inline const std::vector<double> &demands(int32_t k) const¶
-
inline const std::vector<std::vector<double>> &demand_rows() const¶
-
bool fits_alone(int32_t v) const¶
True when node v fits alone under every row: d^k_v <= Q_k for all k.
Public Members
-
std::string name¶
-
Problem() = default¶
-
struct Edge¶
Results¶
-
struct SolveResult¶
Public Types
Public Functions
-
inline bool is_optimal() const¶
-
inline bool has_solution() const¶
True when the solve ended with an incumbent. A stop at a limit or an interrupt before any solution keeps its status (TimeLimit, Feasible) but has an empty tour, and this is false; an incumbent’s tour always holds at least the source.
Public Members
-
double objective = 0.0¶
-
double bound = 0.0¶
-
double gap = 1.0¶
-
double time_seconds = 0.0¶
-
int64_t nodes = 0¶
-
int64_t simplex_iterations = -1¶
-
std::vector<int32_t> tour¶
-
std::vector<int32_t> tour_arcs¶
-
std::map<std::string, SeparatorStats> separator_stats¶
Cut statistics per separator (keyed by separator name).
-
int64_t total_cuts = 0¶
-
int64_t separation_rounds = 0¶
-
double separation_time_seconds = 0.0¶
Per-phase wall-clock times (seconds), one span per callback call: a call’s internal parallelism (parallel separators, parallel labeling) is credited, not summed. Under HiGHS’s parallel tree search (the default) the tree workers’ calls run at once and their spans are summed, so the phases can add up to more than the solve’s wall time.
-
double propagator_time_seconds = 0.0¶
-
double rc_time_seconds = 0.0¶
-
double heuristic_time_seconds = 0.0¶
-
int64_t sweep_fixings = 0¶
Domain-propagation fixings (edges from sweep/chain, plus node fixings).
-
int64_t chain_fixings = 0¶
-
int64_t sweep_node_fixings = 0¶
-
int64_t chain_node_fixings = 0¶
-
int64_t rc_fix0_count = 0¶
Reduced-cost fixings: variables fixed to 0 and to 1.
-
int64_t rc_fix1_count = 0¶
-
int32_t hyperplane_columns = 0¶
Auxiliary integer columns branch_hyper added, one per hyperplane (0 with branch_hyper off).
-
inline bool is_optimal() const¶
-
struct SeparatorStats¶
Per-separator statistics collected during branch-and-cut.