Blame view

bench/queens.c 2.52 KB
965dadaa   Salvador Abreu   initial commit fr...
1
#include <stdio.h>
eef94371   Vasco Pedro   Update to PaCCS v...
2
#include <string.h>
965dadaa   Salvador Abreu   initial commit fr...
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
#include <fcntl.h>
#include <errno.h>

#include "fdc_int.h"

#ifndef NMAX
#  define NMAX MAX_VARIABLES	// maximum number of queens
#endif

static int N = 11;		// actual number of queens

int main(int argc, char *argv[])
{
  fd_int queens[NMAX];
  int solutions = 0, one_solution = 1;
  int i, j;
  int seed;
  int dev_random;

  fd_init(&argc, &argv);

  for (i = 1; i < argc; ++i)
    if (!strcmp(argv[i], "--all"))
      one_solution = 0;
    else
      {
	N = atoi(argv[i]);

	break;
      }

  if (N > NMAX)
    _fd_fatal("queens: more than NMAX queens requested");
eef94371   Vasco Pedro   Update to PaCCS v...
36

965dadaa   Salvador Abreu   initial commit fr...
37
38
39
40
41
42
43
44
45
#ifdef LOCAL_SEARCH
#if 01
  seed = time(0);
  seed = 1206987119;
  //seed = 1207917731; // N = 6, best improvement termina
  //seed = 0;
#else
    if ((dev_random = open("/dev/urandom", O_RDONLY)) == -1)
    _fd_fatal("/dev/urandom: %s\n", strerror(errno));
eef94371   Vasco Pedro   Update to PaCCS v...
46

965dadaa   Salvador Abreu   initial commit fr...
47
48
49
50
51
  read(dev_random, &seed, sizeof(seed));
#endif

  srandom(seed);
  _fd_debug("seed = %u\n", seed);
eef94371   Vasco Pedro   Update to PaCCS v...
52
#endif
965dadaa   Salvador Abreu   initial commit fr...
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

/*
  FORWARD_CHECKING REBENTA(VA) (N == 6)

  queens[0] = fd_new(2, 5);
  _fd_val_del_val(5, queens[0]->domain);
  queens[N - 1] = fd_new(6, 6);
*/

  for (j = ++i, i = 0; j < argc; ++j, ++i)
    {
      int lb, ub;
      char *p;

      lb = ub = atoi(argv[j]) - 1;
      if (p = strchr(argv[j], '-'))
	ub = atoi(p + 1) - 1;

      queens[i] = fd_new(lb, ub);
    }

  for (; i < N; ++i)
    queens[i] = fd_new(0, N - 1);

#if 0
  fd_fake_all_different(queens, N);
#elif 01
  fd_all_different(queens, N);
#else
  for (i = 0; i < N; ++i)
    fd_exactly_one(queens, N, i + 1);
#endif

  for (i = 0; i < N; ++i)
    for (j = i + 1; j < N; ++j)
      fd_minus_ne(queens[i], queens[j], i - j);

  for (i = 0; i < N; ++i)
    for (j = i + 1; j < N; ++j)
      fd_minus_ne(queens[i], queens[j], j - i);

  while (fd_solve() == FD_OK)
    {
      int value;

      printf("solution %d:\n", ++solutions);

      for (i = 0; i < N; ++i)
	if (fd_var_single(queens[i], &value))
	  printf("%d%c", value + 1, (i < N - 1) ? ' ' : '\n');
	else
	  {
	    int j;

	    _fd_error("\nsolution contains non-singleton variable\n");
eef94371   Vasco Pedro   Update to PaCCS v...
108

965dadaa   Salvador Abreu   initial commit fr...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

	    for (j = 0; j < N; ++j)
	      {
		fd_print(queens[j]);
#if defined(FORWARD_CHECKING) && 0
		printf("\t(%d)", queens[i]->epoch);
		putchar('\n');
	      }
#else
		putchar(' ');
	      }
	    putchar('\n');
#endif

	    break;
	  }

#if !(defined(LOCAL_SEARCH) || defined(DISTRIBUTED_SOLVER))
      if (one_solution)
#endif
	break;
    }

  if (solutions)
a2ec137b   Vasco Pedro   updated to PaCCS ...
133
134
135
136
137
138
139
140
    printf("%d solutions found\n", solutions);
  else
    printf("inconsistent CSP\n");

  fd_end();

  return !solutions;
}
965dadaa   Salvador Abreu   initial commit fr...

965dadaa   Salvador Abreu   initial commit fr...