Skip to content

Commit 2d74d79

Browse files
committed
refactor(builder): remove additional_swap_tries and rename swap_tries to improve_tries
- Remove obsolete additional_swap_tries parameter across C++ and Python - Rename swap_tries to improve_tries for consistency with improve_k/improve_eps - Clean up EvenRegularGraphBuilder constructor overloads - Update C++ benchmarks, unit/regression tests, Python bindings, and documentation
1 parent cee39ff commit 2d74d79

14 files changed

Lines changed: 51 additions & 116 deletions

File tree

cpp/API.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ DynamicExplorationGraph build_from_data(
128128
uint8_t improve_k = 0,
129129
float improve_eps = 0.001f,
130130
uint8_t max_path_length = 5,
131-
uint32_t swap_tries = 0,
132-
uint32_t additional_swap_tries = 0,
131+
uint32_t improve_tries = 0,
133132
size_t thread_count = 0,
134133
uint32_t seed = 42,
135134
function<void(BuilderStatus&)> callback = nullptr
@@ -176,14 +175,13 @@ public:
176175
EvenRegularGraphBuilder(
177176
DynamicExplorationGraph& graph,
178177
mt19937& rnd,
179-
OptimizationTarget target = OptimizationTarget::StreamingData,
178+
OptimizationTarget target = OptimizationTarget::LowLID,
180179
uint8_t extend_k = 0,
181180
float extend_eps = 0.1f,
182181
uint8_t improve_k = 0,
183182
float improve_eps = 0.001f,
184183
uint8_t max_path_length = 5,
185-
uint32_t swap_tries = 0,
186-
uint32_t additional_swap_tries = 0
184+
uint32_t improve_tries = 0
187185
);
188186

189187
// --- Entry Queue Management ---

cpp/bench/include/build.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ inline void create_graph(
6363
const deglib::cpu::InstructionSet instruction = deglib::cpu::InstructionSet::Auto
6464
) {
6565
auto rnd = std::mt19937(7);
66-
const uint32_t swap_tries = 0;
67-
const uint32_t additional_swap_tries = 0;
66+
const uint32_t improve_tries = 0;
6867

6968
const auto dims = repository.dims();
7069
const uint32_t max_vertex_count = uint32_t(repository.size());
@@ -76,7 +75,7 @@ inline void create_graph(
7675

7776
auto graph = deglib::graph::SizeBoundedGraph(max_vertex_count, k, feature_space);
7877

79-
auto builder = deglib::builder::EvenRegularGraphBuilder(graph, rnd, lid, k_ext, eps_ext, k_opt, eps_opt, i_opt, swap_tries, additional_swap_tries);
78+
auto builder = deglib::builder::EvenRegularGraphBuilder(graph, rnd, lid, k_ext, eps_ext, k_opt, eps_opt, i_opt, improve_tries);
8079
builder.setThreadCount(thread_count);
8180
builder.setBatchSize(10, 10);
8281

@@ -166,7 +165,7 @@ inline void optimize_graph(
166165
const uint32_t scale = 1
167166
) {
168167
auto rnd = std::mt19937(7);
169-
auto builder = deglib::builder::EvenRegularGraphBuilder(graph, rnd, deglib::builder::OptimizationTarget::LowLID, 0, 0, k_opt, eps_opt, i_opt, 1, 0);
168+
auto builder = deglib::builder::EvenRegularGraphBuilder(graph, rnd, deglib::builder::OptimizationTarget::LowLID, 0, 0, k_opt, eps_opt, i_opt, 1);
170169

171170
auto initial_avg_edge_weight = deglib::analysis::calc_avg_edge_weight(graph, scale);
172171
log("Optimizing graph with initial AEW {:.2f}\n", initial_avg_edge_weight);

cpp/bench/src/bench_edge_optimization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ void run_edge_optimization_benchmark(
123123
log("\n=== Optimizing Graph Edges (in memory) ===\n");
124124
auto rnd = std::mt19937(7);
125125
auto builder = deglib::builder::EvenRegularGraphBuilder(
126-
graph, rnd, deglib::builder::OptimizationTarget::LowLID, 0, 0, config.k_opt, config.eps_opt, config.max_path_length, 1, 0
126+
graph, rnd, deglib::builder::OptimizationTarget::LowLID, 0, 0, config.k_opt, config.eps_opt, config.max_path_length, 1
127127
);
128128

129129
auto start = std::chrono::steady_clock::now();

cpp/deglib/include/deglib/builder.h

Lines changed: 20 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@ class EvenRegularGraphBuilder {
217217
const uint8_t improve_k_; // k value for improving the graph
218218
const float improve_eps_; // eps value for improving the graph
219219
const uint8_t max_path_length_; // max amount of changes before canceling an improvement try
220-
const uint32_t swap_tries_; // number of improvement attempts per build step
221-
const uint32_t additional_swap_tries_; // additional improvement attempts after a successful improvement
220+
const uint32_t improve_tries_; // number of improvement attempts per build step
222221

223222
std::mt19937& rnd_; // Reference to a random number generator used for randomized operations
224223
deglib::graph::MutableGraph& graph_; // Reference to the mutable graph being built and optimized
@@ -252,32 +251,29 @@ class EvenRegularGraphBuilder {
252251
* @param improve_k Number of neighbors to consider when improving the graph.
253252
* @param improve_eps Epsilon value for neighbor search during graph improvement.
254253
* @param max_path_length Maximum number of edge swaps in a single improvement attempt (default: 5).
255-
* @param swap_tries Number of improvement attempts per build step (default: 0).
256-
* @param additional_swap_tries Additional improvement attempts after a successful improvement (default: 0).
254+
* @param improve_tries Number of improvement attempts per build step (default: 0).
257255
*
258256
* This constructor initializes the builder with the provided parameters and sets up internal
259257
* batching and threading parameters for efficient graph construction and optimization.
260258
*/
261259
EvenRegularGraphBuilder(
262260
deglib::graph::MutableGraph& graph,
263261
std::mt19937& rnd,
264-
const OptimizationTarget optimization_target,
265-
const uint8_t extend_k,
266-
const float extend_eps,
267-
const uint8_t improve_k,
268-
const float improve_eps,
262+
const OptimizationTarget optimization_target = OptimizationTarget::LowLID,
263+
const uint8_t extend_k = 0,
264+
const float extend_eps = 0.1f,
265+
const uint8_t improve_k = 0,
266+
const float improve_eps = 0.001f,
269267
const uint8_t max_path_length = 5,
270-
const uint32_t swap_tries = 0,
271-
const uint32_t additional_swap_tries = 0
268+
const uint32_t improve_tries = 0
272269
)
273270
: optimizationTarget_(optimization_target),
274271
extend_k_(extend_k),
275272
extend_eps_(extend_eps),
276273
improve_k_(improve_k),
277274
improve_eps_(improve_eps),
278275
max_path_length_(max_path_length),
279-
swap_tries_(swap_tries),
280-
additional_swap_tries_(additional_swap_tries),
276+
improve_tries_(improve_tries),
281277
rnd_(rnd),
282278
graph_(graph),
283279
build_status_() {
@@ -286,29 +282,6 @@ class EvenRegularGraphBuilder {
286282
extend_batch_size = extend_thread_count * extend_thread_task_count * extend_thread_task_size;
287283
}
288284

289-
/**
290-
* @brief Constructs an EvenRegularGraphBuilder with default parameters for streaming data.
291-
*
292-
* @param graph Reference to a MutableGraph object to be built and optimized.
293-
* @param rnd Reference to a random number generator (std::mt19937) used for randomized operations.
294-
* @param swaps Number of improvement attempts per build step (used for both swap_tries and additional_swap_tries).
295-
*
296-
* This constructor is a convenience overload for quickly creating a builder for streaming data
297-
* with default extension and improvement parameters.
298-
*/
299-
EvenRegularGraphBuilder(deglib::graph::MutableGraph& graph, std::mt19937& rnd, const uint32_t swaps)
300-
: EvenRegularGraphBuilder(graph, rnd, OptimizationTarget::StreamingData, graph.getEdgesPerVertex(), 0.1f, 0, 0.0f, 5, swaps, swaps) {}
301-
302-
/**
303-
* @brief Constructs an EvenRegularGraphBuilder with default parameters and a single swap attempt.
304-
*
305-
* @param graph Reference to a MutableGraph object to be built and optimized.
306-
* @param rnd Reference to a random number generator (std::mt19937) used for randomized operations.
307-
*
308-
* This constructor is a convenience overload for quickly creating a builder with minimal configuration.
309-
*/
310-
EvenRegularGraphBuilder(deglib::graph::MutableGraph& graph, std::mt19937& rnd) : EvenRegularGraphBuilder(graph, rnd, 1) {}
311-
312285
/**
313286
* @brief Constructs an EvenRegularGraphBuilder wrapping a DynamicExplorationGraph facade.
314287
*
@@ -317,14 +290,13 @@ class EvenRegularGraphBuilder {
317290
EvenRegularGraphBuilder(
318291
deglib::DynamicExplorationGraph& graph,
319292
std::mt19937& rnd,
320-
const OptimizationTarget optimization_target,
321-
const uint8_t extend_k,
322-
const float extend_eps,
323-
const uint8_t improve_k,
324-
const float improve_eps,
293+
const OptimizationTarget optimization_target = OptimizationTarget::LowLID,
294+
const uint8_t extend_k = 0,
295+
const float extend_eps = 0.1f,
296+
const uint8_t improve_k = 0,
297+
const float improve_eps = 0.001f,
325298
const uint8_t max_path_length = 5,
326-
const uint32_t swap_tries = 0,
327-
const uint32_t additional_swap_tries = 0
299+
const uint32_t improve_tries = 0
328300
)
329301
: EvenRegularGraphBuilder(
330302
check_and_get_mutable_graph(graph),
@@ -335,21 +307,9 @@ class EvenRegularGraphBuilder {
335307
improve_k,
336308
improve_eps,
337309
max_path_length,
338-
swap_tries,
339-
additional_swap_tries
310+
improve_tries
340311
) {}
341312

342-
/**
343-
* @brief Constructs an EvenRegularGraphBuilder wrapping a DynamicExplorationGraph with default parameters for streaming data.
344-
*/
345-
EvenRegularGraphBuilder(deglib::DynamicExplorationGraph& graph, std::mt19937& rnd, const uint32_t swaps)
346-
: EvenRegularGraphBuilder(check_and_get_mutable_graph(graph), rnd, swaps) {}
347-
348-
/**
349-
* @brief Constructs an EvenRegularGraphBuilder wrapping a DynamicExplorationGraph with default parameters and a single swap attempt.
350-
*/
351-
EvenRegularGraphBuilder(deglib::DynamicExplorationGraph& graph, std::mt19937& rnd) : EvenRegularGraphBuilder(check_and_get_mutable_graph(graph), rnd, 1) {}
352-
353313
private:
354314
static deglib::graph::MutableGraph& check_and_get_mutable_graph(deglib::DynamicExplorationGraph& graph) {
355315
if (!graph.isMutable()) {
@@ -1494,12 +1454,11 @@ class EvenRegularGraphBuilder {
14941454

14951455
// try to improve the graph
14961456
if (graph_.size() > edge_per_vertex && improve_k_ > 0) {
1497-
for (int64_t swap_try = 0; swap_try < int64_t(this->swap_tries_); swap_try++) {
1457+
for (uint32_t try_idx = 0; try_idx < this->improve_tries_; try_idx++) {
14981458
this->build_status_.tries++;
14991459

15001460
if (this->improveEdges()) {
15011461
this->build_status_.improved++;
1502-
swap_try -= this->additional_swap_tries_;
15031462
}
15041463
}
15051464
}
@@ -1539,8 +1498,7 @@ class EvenRegularGraphBuilder {
15391498
* @param improve_k Number of neighbors to consider during improvement (default: 0).
15401499
* @param improve_eps Epsilon value for neighbor search during improvement (default: 0.001f).
15411500
* @param max_path_length Maximum number of edge swaps in a single improvement (default: 5).
1542-
* @param swap_tries Number of improvement attempts per build step (default: 0).
1543-
* @param additional_swap_tries Additional improvement attempts after successful improvement (default: 0).
1501+
* @param improve_tries Number of improvement attempts per build step (default: 0).
15441502
* @param thread_count Number of threads for construction (0 = single-threaded / default).
15451503
* @param seed Random seed for deterministic construction (default: 42).
15461504
* @param callback Optional progress callback invoked during build.
@@ -1559,8 +1517,7 @@ DynamicExplorationGraph build_from_data(
15591517
const uint8_t improve_k = 0,
15601518
const float improve_eps = 0.001f,
15611519
const uint8_t max_path_length = 5,
1562-
const uint32_t swap_tries = 0,
1563-
const uint32_t additional_swap_tries = 0,
1520+
const uint32_t improve_tries = 0,
15641521
const size_t thread_count = 0,
15651522
const uint32_t seed = 42,
15661523
std::function<void(deglib::builder::BuilderStatus&)> callback = nullptr
@@ -1582,7 +1539,7 @@ DynamicExplorationGraph build_from_data(
15821539

15831540
std::mt19937 rng(seed);
15841541
auto builder = EvenRegularGraphBuilder(
1585-
graph, rng, optimization_target, extend_k, extend_eps, improve_k, improve_eps, max_path_length, swap_tries, additional_swap_tries
1542+
graph, rng, optimization_target, extend_k, extend_eps, improve_k, improve_eps, max_path_length, improve_tries
15861543
);
15871544

15881545
if (thread_count > 0) {

cpp/deglib/include/deglib/optimization.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ inline uint32_t prune_non_rng_edges_iterative(deglib::graph::MutableGraph& graph
170170
inline void optimize_edges(deglib::graph::MutableGraph& graph, const uint8_t k_opt, const float eps_opt, const uint8_t i_opt, const uint32_t iterations) {
171171
auto rnd = std::mt19937(7);
172172

173-
auto builder = deglib::builder::EvenRegularGraphBuilder(graph, rnd, deglib::builder::StreamingData, 0, 0.0f, k_opt, eps_opt, i_opt, 1, 0);
173+
auto builder = deglib::builder::EvenRegularGraphBuilder(graph, rnd, deglib::builder::StreamingData, 0, 0.0f, k_opt, eps_opt, i_opt, 1);
174174

175175
auto start = std::chrono::steady_clock::now();
176176
uint64_t duration_ms = 0;

cpp/test/src/common/test_helpers.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,7 @@ inline static void run_integration_test(
448448
const uint8_t improve_k = 0;
449449
const float improve_eps = 0.0f;
450450
const uint8_t max_path_length = 5;
451-
const uint32_t swap_tries = 0;
452-
const uint32_t additional_swap_tries = 0;
451+
const uint32_t improve_tries = 0;
453452
const uint32_t thread_count = 1;
454453

455454
// Build DEG Graph using the specified metric feature space
@@ -462,7 +461,7 @@ inline static void run_integration_test(
462461

463462
std::mt19937 rng(1337);
464463
deglib::builder::EvenRegularGraphBuilder builder(
465-
graph, rng, optimization_target, extend_k, extend_eps, improve_k, improve_eps, max_path_length, swap_tries, additional_swap_tries
464+
graph, rng, optimization_target, extend_k, extend_eps, improve_k, improve_eps, max_path_length, improve_tries
466465
);
467466
builder.setThreadCount(thread_count);
468467

@@ -530,11 +529,10 @@ inline static std::vector<uint32_t> build_graph_for_determinism(
530529
const uint8_t improve_k = 0;
531530
const float improve_eps = 0.0f;
532531
const uint8_t max_path_length = 5;
533-
const uint32_t swap_tries = 0;
534-
const uint32_t additional_swap_tries = 0;
532+
const uint32_t improve_tries = 0;
535533

536534
deglib::builder::EvenRegularGraphBuilder builder(
537-
graph, rng, optimization_target, extend_k, extend_eps, improve_k, improve_eps, max_path_length, swap_tries, additional_swap_tries
535+
graph, rng, optimization_target, extend_k, extend_eps, improve_k, improve_eps, max_path_length, improve_tries
538536
);
539537
builder.setThreadCount(1);
540538

@@ -662,8 +660,7 @@ inline static void run_regression_test(
662660
const uint8_t improve_k = 0;
663661
const float improve_eps = 0.0f;
664662
const uint8_t max_path_length = 5;
665-
const uint32_t swap_tries = 0;
666-
const uint32_t additional_swap_tries = 0;
663+
const uint32_t improve_tries = 0;
667664

668665
// Build DEG Graph using the specified metric feature space
669666
const deglib::distances::FloatSpace feature_space =
@@ -675,7 +672,7 @@ inline static void run_regression_test(
675672

676673
std::mt19937 rng(1337);
677674
deglib::builder::EvenRegularGraphBuilder builder(
678-
graph, rng, optimization_target, extend_k, extend_eps, improve_k, improve_eps, max_path_length, swap_tries, additional_swap_tries
675+
graph, rng, optimization_target, extend_k, extend_eps, improve_k, improve_eps, max_path_length, improve_tries
679676
);
680677
builder.setThreadCount(thread_count);
681678
auto t_build_start = std::chrono::high_resolution_clock::now();

cpp/test/src/regression/optimization/flas/test_fast_linear_assignment_sorter_regression.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,10 @@ static double build_deg_graph(
8686
const uint8_t improve_k = 0;
8787
const float improve_eps = 0.0f;
8888
const uint8_t max_path_length = 5;
89-
const uint32_t swap_tries = 0;
90-
const uint32_t additional_swap_tries = 0;
89+
const uint32_t improve_tries = 0;
9190

9291
deglib::builder::EvenRegularGraphBuilder builder(
93-
graph, rng, optimization_target, extend_k, extend_eps, improve_k, improve_eps, max_path_length, swap_tries, additional_swap_tries
92+
graph, rng, optimization_target, extend_k, extend_eps, improve_k, improve_eps, max_path_length, improve_tries
9493
);
9594
builder.setThreadCount(thread_count);
9695

cpp/test/src/regression/test_builder_regression.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ TEST(DeglibBuilderRegression, DynamicGraph_vs_SizeBoundedGraph_100k_Benchmark) {
4848
std::mt19937 rng(1337);
4949
deglib::builder::EvenRegularGraphBuilder builder(
5050
graph, rng, deglib::builder::OptimizationTarget::LowLID, extend_k, extend_eps, /*improve_k=*/0, /*improve_eps=*/0.0f,
51-
/*max_path_length=*/5, /*swap_tries=*/0, /*additional_swap_tries=*/0
51+
/*max_path_length=*/5, /*improve_tries=*/0
5252
);
5353
builder.setThreadCount(thread_count);
5454

cpp/test/src/unit/test_builder.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,8 +526,7 @@ TEST(EvenRegularGraphBuilder, BuildGraphWithCustomParameters) {
526526
4, // improve_k
527527
0.15f, // improve_eps
528528
5, // max_path_length
529-
2, // swap_tries
530-
1 // additional_swap_tries
529+
2 // improve_tries
531530
);
532531

533532
// Create feature vectors
@@ -783,7 +782,7 @@ TEST(BuilderBuildFromData, CustomLabelsAndThreads) {
783782

784783
auto graph = deglib::builder::build_from_data(
785784
std::span<const float>(dataset), dims, std::span<const uint32_t>(custom_labels), edges_per_vertex, deglib::distances::Metric::FP32_L2,
786-
deglib::builder::OptimizationTarget::StreamingData, 0, 0.2f, 0, 0.001f, 5, 0, 0, 2, 123, callback
785+
deglib::builder::OptimizationTarget::StreamingData, 0, 0.2f, 0, 0.001f, 5, 0, 2, 123, callback
787786
);
788787

789788
EXPECT_EQ(graph.size(), num_vectors);

docs/tutorials/building_graphs.rst

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,9 @@ Improvement & Deletion Parameters (Used after Deletions or during Continuous Ref
124124
* - ``max_path_length``
125125
- ``5``
126126
- Maximum number of consecutive edge swaps in a single improvement sequence.
127-
* - ``swap_tries``
127+
* - ``improve_tries``
128128
- ``0``
129129
- Number of improvement attempts executed per build step.
130-
* - ``additional_swap_tries``
131-
- ``0``
132-
- Extra improvement attempts executed immediately after a successful edge swap.
133130

134131
.. note::
135132

0 commit comments

Comments
 (0)