Skip to content

Commit 1b7912a

Browse files
committed
Update to the equation solver.
1 parent 677299a commit 1b7912a

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/misc/util/utilLinear.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ static int verify_solution(
276276
}
277277

278278
// Checks whether a particular free-variable assignment yields an integer solution.
279-
// Optionally enforces that the first two variables differ once rounded to integers.
279+
// Optionally enforces that the first four variables differ once rounded to integers.
280280
static int assign_and_check_integer(
281281
const double *rref,
282282
int nVars,
@@ -292,7 +292,7 @@ static int assign_and_check_integer(
292292
double *rounded,
293293
double *out_solution,
294294
int require_nonzero,
295-
int require_distinct_first_two) {
295+
int require_distinct_first_four) {
296296
compute_solution_from_free(
297297
rref,
298298
nVars,
@@ -334,9 +334,13 @@ static int assign_and_check_integer(
334334
}
335335
}
336336

337-
if (require_distinct_first_two && nVars >= 2) {
338-
if (fabs(rounded[0] - rounded[1]) < INTEGER_TOLERANCE) {
339-
return 0;
337+
if (require_distinct_first_four && nVars >= 4) {
338+
for (int i = 0; i < 4; ++i) {
339+
for (int j = i + 1; j < 4; ++j) {
340+
if (fabs(rounded[i] - rounded[j]) < INTEGER_TOLERANCE) {
341+
return 0;
342+
}
343+
}
340344
}
341345
}
342346

@@ -367,7 +371,7 @@ static int search_integer_solutions(
367371
double *rounded,
368372
double *out_solution,
369373
int require_nonzero,
370-
int require_distinct_first_two,
374+
int require_distinct_first_four,
371375
int *explored,
372376
int exploration_limit) {
373377
if (*explored >= exploration_limit) {
@@ -391,7 +395,7 @@ static int search_integer_solutions(
391395
rounded,
392396
out_solution,
393397
require_nonzero,
394-
require_distinct_first_two);
398+
require_distinct_first_four);
395399
}
396400

397401
for (int i = 0; i < candidate_count; ++i) {
@@ -414,7 +418,7 @@ static int search_integer_solutions(
414418
rounded,
415419
out_solution,
416420
require_nonzero,
417-
require_distinct_first_two,
421+
require_distinct_first_four,
418422
explored,
419423
exploration_limit)) {
420424
return 1;
@@ -440,7 +444,7 @@ static int try_integer_solution(
440444
double *rounded,
441445
double *out_solution,
442446
int require_nonzero,
443-
int require_distinct_first_two) {
447+
int require_distinct_first_four) {
444448
if (free_count == 0) {
445449
return 0;
446450
}
@@ -496,7 +500,7 @@ static int try_integer_solution(
496500
rounded,
497501
out_solution,
498502
require_nonzero,
499-
require_distinct_first_two)) {
503+
require_distinct_first_four)) {
500504
free(free_values);
501505
return 1;
502506
}
@@ -519,7 +523,7 @@ static int try_integer_solution(
519523
rounded,
520524
out_solution,
521525
require_nonzero,
522-
require_distinct_first_two,
526+
require_distinct_first_four,
523527
&explored,
524528
exploration_limit);
525529

@@ -548,7 +552,7 @@ double *linear_equation_solver(double *pMatrix, int nEqus, int nVars) {
548552
int free_count = 0;
549553
int homogeneous_rhs = 0;
550554
int require_nonzero_integer = 0;
551-
int require_distinct_first_two = 0;
555+
int require_distinct_first_four = 0;
552556
int pivot_row_index = 0;
553557
int rank = 0;
554558
double max_residual = 0.0;
@@ -673,7 +677,7 @@ double *linear_equation_solver(double *pMatrix, int nEqus, int nVars) {
673677
// Only demand integer solutions when the system is homogeneous with free variables.
674678
require_nonzero_integer = homogeneous_rhs && free_count > 0;
675679
// Enforce distinct first two variables whenever multiple variables remain free.
676-
require_distinct_first_two = (free_count > 0 && nVars >= 2);
680+
require_distinct_first_four = (free_count > 0 && nVars >= 4);
677681

678682
solution = (double *)malloc((size_t)nVars * sizeof(double));
679683
workspace = (double *)malloc((size_t)nVars * sizeof(double));
@@ -711,7 +715,7 @@ double *linear_equation_solver(double *pMatrix, int nEqus, int nVars) {
711715
rounded,
712716
solution,
713717
require_nonzero_integer,
714-
require_distinct_first_two)) {
718+
require_distinct_first_four)) {
715719
for (int i = 0; i < free_count; ++i) {
716720
workspace[i] = 0.0;
717721
}
@@ -724,12 +728,12 @@ double *linear_equation_solver(double *pMatrix, int nEqus, int nVars) {
724728
free_count,
725729
workspace,
726730
solution);
727-
if (require_nonzero_integer && require_distinct_first_two) {
728-
printf("Note: integer solution meeting non-negative and distinct-first-two constraints not found; returning floating-point solution.\n");
731+
if (require_nonzero_integer && require_distinct_first_four) {
732+
printf("Note: integer solution meeting non-negative and distinct-first-four constraints not found; returning floating-point solution.\n");
729733
} else if (require_nonzero_integer) {
730734
printf("Note: non-negative integer solution with mixed zero/positive entries not found; returning floating-point solution.\n");
731-
} else if (require_distinct_first_two) {
732-
printf("Note: integer solution with distinct first two variables not found; returning floating-point solution.\n");
735+
} else if (require_distinct_first_four) {
736+
printf("Note: integer solution with distinct first four variables not found; returning floating-point solution.\n");
733737
} else {
734738
printf("Note: integer solution not found; returning floating-point solution.\n");
735739
}

0 commit comments

Comments
 (0)