Blame view

bench/k-queens.c 2.63 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
#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;
965dadaa   Salvador Abreu   initial commit fr...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  int dev_random;
  fd_int copy;
  int ncopies = 4;

  fd_init(&argc, &argv);

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

	  break;
	}

      i++;
    }

eef94371   Vasco Pedro   Update to PaCCS v...
43
  if (N > NMAX)
965dadaa   Salvador Abreu   initial commit fr...
44
45
46
47
48
49
50
    _fd_fatal("queens: more than NMAX queens requested");

  if (ncopies < 1)
    ncopies = 1;

  copy = fd_new(0, ncopies - 1);	// XXX: only allows MAX_VALUE+1 copies

eef94371   Vasco Pedro   Update to PaCCS v...
51
#ifdef LOCAL_SEARCH
965dadaa   Salvador Abreu   initial commit fr...
52
53
54
55
56
57
#if 01
  seed = time(0);
  seed = 1206987119;
  //seed = 1207917731; // N = 6, best improvement termina
  //seed = 0;
#else
eef94371   Vasco Pedro   Update to PaCCS v...
58
59
60
61
    if ((dev_random = open("/dev/urandom", O_RDONLY)) == -1)
    _fd_fatal("/dev/urandom: %s\n", strerror(errno));

  read(dev_random, &seed, sizeof(seed));
965dadaa   Salvador Abreu   initial commit fr...
62
63
64
65
66
#endif

  srandom(seed);
  _fd_debug("seed = %u\n", seed);
#endif
eef94371   Vasco Pedro   Update to PaCCS v...
67

965dadaa   Salvador Abreu   initial commit fr...
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
  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...
123
	    for (j = 0; j < N; ++j)
965dadaa   Salvador Abreu   initial commit fr...
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
	      {
		fd_print(queens[j]);
		putchar(' ');
	      }
	    putchar('\n');

	    break;
	  }

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

  if (solutions)
    printf("%d solutions found\n", solutions);
  else
    printf("inconsistent CSP\n");

  fd_end();

  return !solutions;
}