94b2b13d
Pedro Roque
PHACT source
|
1
2
3
4
|
/*
* golomb.c
*
* Created on: 26/03/2016
|
4d26a735
Pedro Roque
Increased recogni...
|
5
|
* Author: pedro
|
94b2b13d
Pedro Roque
PHACT source
|
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
*
* 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"
|
4d26a735
Pedro Roque
Increased recogni...
|
25
|
#include "../constraints/lt.h"
|
94b2b13d
Pedro Roque
PHACT source
|
26
27
28
29
30
31
32
33
|
#include "../constraints/minimize.h"
#include "../constraints/var_eq_minus.h"
#include "../split.h"
#include "../variables.h"
/*
* Solve the Golomb ruler problem with N marks
*/
|
4d26a735
Pedro Roque
Increased recogni...
|
34
|
void run_golomb(int* csp_dims) {
|
94b2b13d
Pedro Roque
PHACT source
|
35
|
int n = csp_dims[0];
|
4d26a735
Pedro Roque
Increased recogni...
|
36
37
|
unsigned int* mark;
unsigned int* differences;
|
94b2b13d
Pedro Roque
PHACT source
|
38
39
40
41
|
unsigned long result;
int i, j, d;
// ruler variables
|
4d26a735
Pedro Roque
Increased recogni...
|
42
|
mark = malloc((unsigned int)n * sizeof(unsigned int));
|
94b2b13d
Pedro Roque
PHACT source
|
43
|
// vector with the ID of the variables constrained with all_diff
|
4d26a735
Pedro Roque
Increased recogni...
|
44
|
differences = malloc((unsigned int)n * ((unsigned int)n - 1) / 2 * sizeof(unsigned int));
|
94b2b13d
Pedro Roque
PHACT source
|
45
46
|
// Create the Golomb ruler main variables and lt constraint
|
4d26a735
Pedro Roque
Increased recogni...
|
47
|
mark[1] = v_new_range(1, (unsigned int)n * (unsigned int)n, true);
|
94b2b13d
Pedro Roque
PHACT source
|
48
|
for (i = 2; i < n; i++) {
|
4d26a735
Pedro Roque
Increased recogni...
|
49
|
mark[i] = v_new_range(0, (unsigned int)n * (unsigned int)n, true);
|
94b2b13d
Pedro Roque
PHACT source
|
50
51
52
53
54
55
|
}
// for optimization
c_minimize(mark[n - 1]);
for (i = 1; i < n - 1; i++) {
|
4d26a735
Pedro Roque
Increased recogni...
|
56
|
c_lt(mark[i], mark[i + 1]);
|
94b2b13d
Pedro Roque
PHACT source
|
57
58
59
60
61
62
63
64
|
}
// 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++) {
|
4d26a735
Pedro Roque
Increased recogni...
|
65
|
differences[j] = v_new_range(0, (unsigned int)n * (unsigned int)n, false);
|
94b2b13d
Pedro Roque
PHACT source
|
66
67
68
69
70
71
72
|
c_var_eq_minus(differences[j++], mark[i], mark[i - d]);
}
}
mark[0] = v_new_val(0);
|
4d26a735
Pedro Roque
Increased recogni...
|
73
|
c_fake_all_different(differences, (unsigned int)n * ((unsigned int)n - 1) / 2);
|
94b2b13d
Pedro Roque
PHACT source
|
74
75
|
// Symmetry breaking
|
4d26a735
Pedro Roque
Increased recogni...
|
76
|
c_lt(differences[0], differences[n * (n - 1) / 2 - 1]);
|
94b2b13d
Pedro Roque
PHACT source
|
77
78
|
if (OPTIMIZING) {
|
4d26a735
Pedro Roque
Increased recogni...
|
79
|
printf("Optimizing one solution for Golomb Ruler with %u marks.\n", n);
|
94b2b13d
Pedro Roque
PHACT source
|
80
81
82
83
84
85
86
87
88
89
90
|
} 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");
|
4d26a735
Pedro Roque
Increased recogni...
|
91
|
vs_print_single_val(mark, (unsigned int)n, 0);
|
94b2b13d
Pedro Roque
PHACT source
|
92
93
94
95
96
|
printf("\n");
v_print_cost(0);
} else if (FINDING_ONE_SOLUTION && result == 1) {
printf("Solution:\n");
|
4d26a735
Pedro Roque
Increased recogni...
|
97
|
vs_print_single_val(mark, (unsigned int)n, 0);
|
94b2b13d
Pedro Roque
PHACT source
|
98
99
100
101
102
103
104
105
106
|
printf("\n");
} else {
printf("%lu solution(s) found\n", result);
}
free(differences);
free(mark);
}
|