Blame view

Debug/src/csps/all_diff.c 1.36 KB
0c8ce2b0   Pedro Roque   missing files
1
2
3
4
/*
 * all_diff.c
 *
 *  Created on: 15/10/2015
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
 *
 *      All_diff constraint:
 *      Solve a CSP with 1 all_different constraint with N variables with N values each.
 */

#include "all_diff.h"

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include "../config.h"
#include "../constraints/all_different.h"
#include "../split.h"
#include "../variables.h"

/*
 * Solve one single all_diff constraint with N variables and N values each
 */
4d26a735   Pedro Roque   Increased recogni...
25
void run_all_diff(int *csp_dims) {
0c8ce2b0   Pedro Roque   missing files
26
27
28
29
30
	int n = csp_dims[0];
	unsigned long result;
	int i;

	// vector with the IDs of the variables constrained by the all_diff constraint
4d26a735   Pedro Roque   Increased recogni...
31
	unsigned int *c_vs = malloc((unsigned long) n * sizeof(unsigned int));
0c8ce2b0   Pedro Roque   missing files
32
33
34

	// Create the CSP variables
	for (i = 0; i < n; i++) {
4d26a735   Pedro Roque   Increased recogni...
35
		c_vs[i] = v_new_range(0, (unsigned int) n - 1, true);
0c8ce2b0   Pedro Roque   missing files
36
37
38
	}

	// Create the constraint
4d26a735   Pedro Roque   Increased recogni...
39
	c_all_different(c_vs, (unsigned int) n);
0c8ce2b0   Pedro Roque   missing files
40
41
42
43
44
45
46
47
48
49
50
51

	if (FINDING_ONE_SOLUTION) {
		printf("\nFinding one solution for the all-different constraint with %u variables.\n", n);
	} else {
		printf("\nCounting all the solutions for the all-different constraint with %u variables.\n", n);
	}

	// Solve the CSP
	result = solve_CSP();

	if (FINDING_ONE_SOLUTION && result == 1) {
		printf("Solution:\n");
4d26a735   Pedro Roque   Increased recogni...
52
		vs_print_single_val(c_vs, (unsigned int) n, 1);
0c8ce2b0   Pedro Roque   missing files
53
54
55
56
57
58
59
		printf("\n");
	} else {
		printf("%lu solution(s) found\n", result);
	}

	free(c_vs);
}