all_diff.c 1.35 KB
/*
 * all_diff.c
 *
 *  Created on: 15/10/2015
 *      Author: pedro
 *
 *      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
 */
void run_all_diff(int* csp_dims) {
	int n = csp_dims[0];
	unsigned long result;
	int i;

	// vector with the IDs of the variables constrained by the all_diff constraint
	unsigned int* c_vs = malloc((unsigned long)n * sizeof(unsigned int));

	// Create the CSP variables
	for (i = 0; i < n; i++) {
		c_vs[i] = v_new_range(0, (unsigned int)n - 1, true);
	}

	// Create the constraint
	c_all_different(c_vs, (unsigned int)n);

	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");
		vs_print_single_val(c_vs, (unsigned int)n, 1);
		printf("\n");
	} else {
		printf("%lu solution(s) found\n", result);
	}

	free(c_vs);
}