golomb.c
2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* golomb.c
*
* Created on: 26/03/2016
* Author: pedro
*
* http://www.csplib.org/Problems/prob006/
* https://github.com/MiniZinc/minizinc-benchmarks/tree/master/golomb
* https://www.cril.univ-artois.fr/~lecoutre/benchmarks.html#
*
* Golomb Ruler problem:
* The Golomb Ruler problem is the task of putting n marks on a ruler of length m such that the
* distance between any two pairs of marks is distinct.
*/
#include "golomb.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "../config.h"
#include "../constraints/fake_all_different.h"
#include "../constraints/ge.h"
#include "../constraints/lt.h"
#include "../constraints/minimize.h"
#include "../constraints/var_eq_minus.h"
#include "../split.h"
#include "../variables.h"
/*
* Solve the Golomb ruler problem with N marks
*/
void run_golomb(int* csp_dims) {
int n = csp_dims[0];
unsigned int* mark;
unsigned int* differences;
unsigned long result;
int i, j, d;
// ruler variables
mark = malloc((unsigned int)n * sizeof(unsigned int));
// vector with the ID of the variables constrained with all_diff
differences = malloc((unsigned int)n * ((unsigned int)n - 1) / 2 * sizeof(unsigned int));
// Create the Golomb ruler main variables and lt constraint
mark[1] = v_new_range(1, (unsigned int)n * (unsigned int)n, true);
for (i = 2; i < n; i++) {
mark[i] = v_new_range(0, (unsigned int)n * (unsigned int)n, true);
}
// for optimization
c_minimize(mark[n - 1]);
for (i = 1; i < n - 1; i++) {
c_lt(mark[i], mark[i + 1]);
}
// Create the triangle of differences variables and its c_var_eq_minus constraints
j = 0;
for (d = 1; d < n; d++) {
differences[j++] = mark[d];
for (i = d + 1; i < n; i++) {
differences[j] = v_new_range(0, (unsigned int)n * (unsigned int)n, false);
c_var_eq_minus(differences[j++], mark[i], mark[i - d]);
}
}
mark[0] = v_new_val(0);
c_fake_all_different(differences, (unsigned int)n * ((unsigned int)n - 1) / 2);
// Symmetry breaking
c_lt(differences[0], differences[n * (n - 1) / 2 - 1]);
if (OPTIMIZING) {
printf("Optimizing one solution for Golomb Ruler with %u marks.\n", n);
} else if (FINDING_ONE_SOLUTION) {
printf("\nFinding one solution for Golomb Ruler with %u marks.\n", n);
} else {
printf("\nCounting all the solutions for Golomb Ruler with %u marks.\n", n);
}
// Solve the CSP
result = solve_CSP();
if (OPTIMIZING && result == 1) {
printf("Best solution:\n");
vs_print_single_val(mark, (unsigned int)n, 0);
printf("\n");
v_print_cost(0);
} else if (FINDING_ONE_SOLUTION && result == 1) {
printf("Solution:\n");
vs_print_single_val(mark, (unsigned int)n, 0);
printf("\n");
} else {
printf("%lu solution(s) found\n", result);
}
free(differences);
free(mark);
}