0c8ce2b0
Pedro Roque
missing files
|
1
2
3
4
|
/*
* all_interval.c
*
* Created on: 15/04/2017
|
4d26a735
Pedro Roque
Increased recogni...
|
5
|
* Author: Pedro
|
0c8ce2b0
Pedro Roque
missing files
|
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
*
* https://www.cril.univ-artois.fr/~lecoutre/benchmarks.html#
* http://www.csplib.org/Problems/prob007/
*
* All Interval Series problem:
* Given the twelve standard pitch-classes (c, c#, d, …), represented by numbers 0,1,…,11,
* find a series in which each pitch-class occurs exactly once and in which the musical intervals
* between neighbouring notes cover the full set of intervals from the minor second (1 semitone)
* to the major seventh (11 semitones). That is, for each of the intervals, there is a pair of
* neigbhouring pitch-classes in the series, between which this interval appears.
*/
#include "all_interval.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "../config.h"
|
4d26a735
Pedro Roque
Increased recogni...
|
25
26
|
#include "../constraints/all_different.h"
#include "../constraints/int_lt.h"
|
0c8ce2b0
Pedro Roque
missing files
|
27
28
29
30
31
32
33
|
#include "../constraints/var_eq_minus_abs.h"
#include "../split.h"
#include "../variables.h"
/*
* Solve an instance of All Interval problem with N values
*/
|
4d26a735
Pedro Roque
Increased recogni...
|
34
|
void run_all_interval(int *csp_dims) {
|
0c8ce2b0
Pedro Roque
missing files
|
35
36
37
38
39
|
int n = csp_dims[0];
unsigned long result;
int i;
// vectors with the variables ID
|
4d26a735
Pedro Roque
Increased recogni...
|
40
41
|
unsigned int *x_vs = malloc((unsigned int) n * sizeof(unsigned int));
unsigned int *diff_vs = malloc(((unsigned int) n - 1) * sizeof(unsigned int));
|
0c8ce2b0
Pedro Roque
missing files
|
42
43
44
|
// Create the CSP variables
for (i = 0; i < n; i++) {
|
4d26a735
Pedro Roque
Increased recogni...
|
45
|
x_vs[i] = v_new_range(1, (unsigned int) n, true);
|
0c8ce2b0
Pedro Roque
missing files
|
46
47
48
|
}
for (i = 0; i < n - 1; i++) {
|
4d26a735
Pedro Roque
Increased recogni...
|
49
|
diff_vs[i] = v_new_range(1, (unsigned int) n - 1, false);
|
0c8ce2b0
Pedro Roque
missing files
|
50
51
52
|
}
// Create the all_different constraints
|
4d26a735
Pedro Roque
Increased recogni...
|
53
54
|
c_all_different(x_vs, (unsigned int) n);
c_all_different(diff_vs, (unsigned int) n - 1);
|
0c8ce2b0
Pedro Roque
missing files
|
55
|
|
0c8ce2b0
Pedro Roque
missing files
|
56
57
|
for (i = 0; i < n - 1; i++) {
c_var_eq_minus_abs(diff_vs[i], x_vs[i + 1], x_vs[i]);
|
0c8ce2b0
Pedro Roque
missing files
|
58
59
60
|
}
// symmetry breaking
|
4d26a735
Pedro Roque
Increased recogni...
|
61
62
|
c_int_lt(x_vs[0], x_vs[n - 2]);
c_int_lt(diff_vs[0], diff_vs[1]);
|
0c8ce2b0
Pedro Roque
missing files
|
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
|
if (FINDING_ONE_SOLUTION) {
printf("\nFinding one solution for the all interval series with %u values.\n", n);
} else {
printf("\nCounting all the solutions for the all interval with %u values.\n", n);
}
// Solve the CSP
result = solve_CSP();
if (FINDING_ONE_SOLUTION && result == 1) {
printf("Solution:\n");
printf("{");
for (i = 0; i < n - 1; i++) {
printf("%u(%u),", v_get_value(x_vs[i]), v_get_value(diff_vs[i]));
}
printf("%u}\n", v_get_value(x_vs[n - 1]));
} else {
printf("%lu solution(s) found\n", result);
}
free(x_vs);
free(diff_vs);
}
|