variables.c 16.6 KB
1 2 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <assert.h>

#include "fdc_int.h"
#include "values.h"
#include "store.h"
#include "constraints.h"

#include "util.h"

int fd_variables_count = 0;
__thread fd_int _fd_variables[MAX_VARIABLES];

// variables with singleton domains (ie, constants)
static fd_int fd__constants[MAX_VALUE + 1];

fd_int *fd__label_vars = 0;	// variables subject to labelling
int fd__label_vars_count = 0;
bool *fd__var_labelled;		// identifies the variables subject to labelling


/*
  Create and return a new variable with domain { MIN, ..., MAX }.
  Also store it in the global variable table.
*/
static fd_int create_variable(int min, int max)
{
  fd_int v;

  if (fd_variables_count == MAX_VARIABLES)
    fd__fatal("too many variables, increase MAX_VARIABLES");

  v = malloc(sizeof(struct fd_int));

  if (v)
    {
      v->index = fd_variables_count++;
      _fd_init_domain(DOMAIN(v), min, max);
      v->constraints = NULL;
      v->nconstraints = 0;
      v->nconnections = 0;
      v->epoch = 0;
      v->assigned = false;
      v->flags = 0;

      _fd_variables[v->index] = v;
    }

  return v;
}

/* Return a variable with singleton domain { VALUE }. */
fd_int fd_const(int value)
{
  if (value < MIN_VALUE)
    fd__fatal("value less than MIN_VALUE");

  if (value > MAX_VALUE)
    fd__fatal("value greater than MAX_VALUE");

  // only create the variable if it has not yet been created
  if (fd__constants[value] == NULL)
    fd__constants[value] = create_variable(value, value);

  return fd__constants[value];
}

/* Return a variable with domain { MIN, ..., MAX }. */
fd_int fd_new(int min, int max)
{
  if (min == max)
    return fd_const(min);
  else
    return create_variable(min, max);
}

#ifdef SPLITGO
/* create a skeletal copy of VARIABLE */ // XXX: description
fd_int _fd_var_copy(fd_int variable)
{
  fd_int v = malloc(sizeof(struct fd_int));

  if (v)
    {
      v->index = variable->index;
#ifdef USE_STORE
//      v->domain = variable->domain;
#else
      v->domain = 0;
#endif
      v->constraints = variable->constraints;
      v->nconstraints = variable->nconstraints;
      v->nconnections = variable->nconnections;
      v->epoch = 0;
      v->assigned = false;
      v->flags = 0;
    }

  return v;
}
#endif /* SPLITGO */

#ifndef USE_STORE
void _fd_var_copy_domain(fd_int to, fd_int from)
{
  if (DOMAIN(to))
    _fd_free_value(DOMAIN(to));

  DOMAIN(to) = _fd_val_clone(DOMAIN(from));
}

void _fd_var_copy_domains(fd_int to[], fd_int from[])
{
  int i;

  for (i = 0; i < fd_variables_count; ++i)
    _fd_val_copy(DOMAIN_REF(to[i]), DOMAIN(from[i]));
}
#endif /* USE_STORE */

/* Add the CONSTRAINT to the VARIABLE's constraints. */
void _fd_var_add_constraint(fd_int variable, fd_constraint constraint)
{
  // first make sure there is room for one more constraint
  /* initially make room for 4 constraints; everytime the number of
     constraints reaches a power of 2 (not less than 4), double the
     size of the array */
  if (variable->nconstraints == 0)
    variable->constraints = malloc(4 * sizeof(*variable->constraints)); // XXX
  else
    {
      // a variable may appear more than once in some constraints
      // check if this is the case
      if (variable->constraints[variable->nconstraints - 1] ==
	    _fd_constraint_count - 1)
	{
	  variable->nconnections--;

	  return;
	}

      if (variable->nconstraints >= 4 &&
	  variable->nconstraints ==
	    (variable->nconstraints & -variable->nconstraints))
	variable->constraints =
	  realloc(variable->constraints,
		  2 * variable->nconstraints * sizeof(*variable->constraints));
    }

  // then add the constraint
  variable->constraints[variable->nconstraints] = _fd_constraint_count - 1; // XXX
  variable->nconstraints++;

  // update the number of connections of the variable
  variable->nconnections += constraint->nvariables - 1;
}

#ifndef fd_domain_empty // XXX: ???
/* Check if VARIABLE's domain is empty. */
int fd_domain_empty(fd_int variable)
{
  return _fd_val_empty(DOMAIN(variable));
}
#endif

/* Check if VARIABLE's domain is a singleton. If it is, store the
   value in the address pointed to by VALUE (if any). */
int fd_var_single(fd_int variable, int *value)
{
  return _fd_val_single(DOMAIN(variable), value);
}

/* Return the value assigned to VARIABLE. */
int fd_var_value(fd_int variable)
{
#ifndef USE_VALUE
  int value;

#ifndef NDEBUG
  assert( _fd_val_single(DOMAIN(variable), &value) );
#else
  _fd_val_single(DOMAIN(variable), &value);
#endif

  return value;
#else /* USE_VALUE */
#ifndef COMPACT_DOMAINS
  assert(DOMAIN(variable)->kind == FD_SINGLETON && DOMAIN(variable)->next == 0
	 && DOMAIN(variable)->value.value == variable->value);
#elif defined(INLINE_DOMAINS)
  assert(DOMAIN(variable) == ((unsigned) 0x80000000 >> variable->value));
#else
  assert(*DOMAIN(variable) == ((unsigned) 0x80000000 >> variable->value));
#endif

  return variable->value;
#endif /* USE_VALUE */
}

void _fd_revise_connected(fd_constraint constraint, fd_int variable)
{
#ifdef REVISION_IS_VAR
  _fd_add_new_revision(variable);
#else
  _fd_add_new_revision(variable, constraint);
#endif
}

/* print variable domain */
void fd_print(fd_int variable)
{
  _fd_val_print(DOMAIN(variable));
}

/* print variable domain + \n */
void fd_println(fd_int variable)
{
  fd_print(variable);
  putchar('\n');
}

// print all variables domains (and their epoch)
void _fd_print()
{
  int i;

  for (i = 0; i < fd_variables_count; ++i)
    {
      fd_print(_fd_variables[i]);
      printf("\t(%d)\n", _fd_variables[i]->epoch);
    }
}

// print all variables domains (and their epoch) on a single line
void _fd_cprint()
{
  int i;

  for (i = 0; i < fd_variables_count; ++i)
    {
      fd_print(_fd_variables[i]);
      putchar(' '); //printf("/%d ", _fd_variables[i]->epoch);
    }

  putchar('\n');
}

void _fd_gprint()
{
  int i, value;

  for (i = 0; i < fd_variables_count; ++i)
    {
      i % 9 == 0 ? putchar('\n') : 0;
      _fd_val_single(DOMAIN(_fd_variables[i]), &value);
      printf("%d ", value);
    }

  putchar('\n');
}

/* wrappers for functions dealing directly with the domains */

int _fd_var_max(fd_int variable)
{
  return _fd_val_max(DOMAIN(variable));
}

int _fd_var_min(fd_int variable)
{
  return _fd_val_min(DOMAIN(variable));
}

int _fd_var_del_ge(int value, fd_int variable)
{
//  _fd_var_save(variable);

  return _fd_val_del_ge(value, DOMAIN_REF(variable));
}

int _fd_var_del_gt(int value, fd_int variable)
{
//  _fd_var_save(variable);

  return _fd_val_del_gt(value, DOMAIN_REF(variable));
}

int _fd_var_del_le(int value, fd_int variable)
{
//  _fd_var_save(variable);

  return _fd_val_del_le(value, DOMAIN_REF(variable));
}

int _fd_var_del_lt(int value, fd_int variable)
{
//  _fd_var_save(variable);

  return _fd_val_del_lt(value, DOMAIN_REF(variable));
}

int _fd_var_del_val(int value, fd_int variable)
{
//  _fd_var_save(variable);

  return _fd_val_del_val(value, DOMAIN_REF(variable));
}

int _fd_var_del_other(fd_int variable, int value)
{
//  _fd_var_save(variable);

  return _fd_val_del_other(DOMAIN_REF(variable), value);
}

int _fd_var_intersect(fd_int variable1, fd_int variable2)
{
//  _fd_var_save(variable1);

  return _fd_val_intersect(DOMAIN_REF(variable1), DOMAIN(variable2));
}

int _fd_var_contains_val(fd_int variable, int value)
{
  return _fd_val_contains_val(DOMAIN(variable), value);
}

// XXX: only used by exactly-*?
void _fd_var_set_value(fd_int variable, int value)
{
  _fd_val_set_value(DOMAIN_REF(variable), value);
}

// SEARCH

/* select the variables which are candidate to be assigned */
void fd_label(fd_int variables[], int n)
{
  if (fd__label_vars)
    fd__fatal("fd_label() can only be called once");

  fd__label_vars = malloc(n * sizeof(*fd__label_vars));	// XXX: NULL
  fd__label_vars_count = n;

  memcpy(fd__label_vars, variables, n * sizeof(*fd__label_vars));
}

// XXX: variables addresses change when packing; reflect that on the
//	labelled variables
static void relocate_label_vars()
{
  int i;

  for (i = 0; i < fd__label_vars_count; ++i)
    fd__label_vars[i] = _fd_variables[fd__label_vars[i]->index];
}

#ifndef ASSIGNED_AFTER
// assigned variables should come before non-assigned ones, and
// relative order between assigned variables doesn't matter
#define cmp_var_assigned(a, b)    			\
  do    						\
    {    						\
      if (fd_var_single(a, NULL))    			\
	return -1;    					\
    							\
      if (fd_var_single(b, NULL))    			\
	return 1;    					\
    }    						\
  while (0);
#else
// assigned variables should come after non-assigned ones, and
// relative order between assigned variables doesn't matter
#define cmp_var_assigned(a, b)    			\
  do    						\
    {    						\
      if (fd_var_single(a, NULL))    			\
	return fd_var_single(b, NULL) ? -1 : 1;		\
    							\
      if (fd_var_single(b, NULL))    			\
	return -1;    					\
    }    						\
  while (0);
#endif

// variables with smaller domains come before variables with greater
int fd__cmp_var_size(fd_int a, fd_int b)
{
  cmp_var_assigned(a, b);

  return _fd_val_size(DOMAIN(a)) - _fd_val_size(DOMAIN(b));
}

// variables involved in more constraints come before variables
// involved in less
int fd__cmp_var_constraints(fd_int a, fd_int b)
{
  cmp_var_assigned(a, b);

  return b->nconstraints - a->nconstraints;
}

// variables involved in more constraints come before variables
// involved in less; in case of a tie, variables with smaller domains
// come before variables with greater
int fd__cmp_var_size_degree(fd_int a, fd_int b)
{
  cmp_var_assigned(a, b);

  if (a->nconstraints != b ->nconstraints)
    return b->nconstraints - a->nconstraints;

  return _fd_val_size(DOMAIN(a)) - _fd_val_size(DOMAIN(b));
}

// variables connected to more variables come before variables
// connected to less
int fd__cmp_var_connections(fd_int a, fd_int b)
{
  int ac, bc;

  cmp_var_assigned(a, b);

  return b->nconnections - a->nconnections;
}

// variables with smaller minimum value come before variables with
// greater
int fd__cmp_var_min(fd_int a, fd_int b)
{
  cmp_var_assigned(a, b);

  return _fd_var_min(a) - _fd_var_min(b);
}

// variables with greater maximum value come before variables with
// smaller
int fd__cmp_var_max(fd_int a, fd_int b)
{
  cmp_var_assigned(a, b);

  return _fd_var_max(b) - _fd_var_max(a);
}

int (*fd__cmp_variables)(fd_int, fd_int) = NULL;

// ascending stable merge
static void merge_vars(fd_int vs[], fd_int vt[], int f, int l,
		       int (*cmp)(fd_int, fd_int))
{
  int f0 = f, l0 = (f + l) / 2;
  int f1 = l0 + 1, l1 = l;

  while (f <= l && f0 <= l0 && f1 <= l1)
    vs[f++] = cmp(vt[f0], vt[f1]) < 1 ? vt[f0++] : vt[f1++];

  while (f0 <= l0)
    vs[f++] = vt[f0++];

  while (f1 <= l1)
    vs[f++] = vt[f1++];
}

static void merge_sort_vars(fd_int vs[], fd_int vt[], int f, int l,
			    int (*cmp)(fd_int, fd_int))
{
  if (f == l)
    return;

  merge_sort_vars(vt, vs, f, (f + l) / 2, cmp);
  merge_sort_vars(vt, vs, (f + l) / 2 + 1, l, cmp);
  merge_vars(vs, vt, f, l, cmp);
}

void fd__sort_variables(fd_int vars[], int nvars, int (*cmp)(fd_int, fd_int))
{
  fd_int *aux;

  aux = alloca(nvars * sizeof(*aux));
  memcpy(aux, vars, nvars * sizeof(*aux));

  merge_sort_vars(vars, aux, 0, nvars - 1, cmp);
}

void fd__sort_label_vars()
{
  fd_int *aux;

  if (!fd__cmp_variables)
    return;

  fd__sort_variables(fd__label_vars, fd__label_vars_count, fd__cmp_variables);
}

void fd__setup_label_vars()
{
  int i;

  assert(PACK_PROBLEM);		// XXX: variables addresses are shared

  if (!fd__label_vars)
    {
      fd__label_vars = malloc(fd_variables_count * sizeof(*fd__label_vars));
      memcpy(fd__label_vars, _fd_variables,
	     fd_variables_count * sizeof(*fd__label_vars));
      fd__label_vars_count = fd_variables_count;
    }
  else
    relocate_label_vars();

  fd__sort_label_vars();

  fd__var_labelled = calloc(fd_variables_count, sizeof(*fd__var_labelled)); // XXX: NULL

  for (i = 0; i < fd__label_vars_count; ++i)
    fd__var_labelled[fd__label_vars[i]->index] = true;
}

/* select the next variable to be instantiated (search) */
fd_int (*_fd_var_select2)(fd_int[]);

fd_int _fd_select_first_var(fd_int _fd_variables[])
{
  int i;

  for (i = 0; i < fd__label_vars_count; ++i)
    if (!fd_var_single(_fd_variables[i], NULL))
      {
	return _fd_variables[i];
      }

  return NULL;
}

fd_int _fd_select_first_fail(fd_int _fd_variables[])
{
  // select the first variable with the smallest domain
  fd_int variable = NULL;
  int i, domain_size, s;

  i = 0;
  while (i < fd__label_vars_count && fd_var_single(_fd_variables[i], NULL))
    ++i;

  if (i == fd__label_vars_count)
    return NULL;		// all variables' domains are singletons

  variable = _fd_variables[i++];
  domain_size = _fd_val_size(DOMAIN(variable));

  for (; i < fd__label_vars_count; ++i)
    if ((s = _fd_val_size(DOMAIN(_fd_variables[i]))) > 1 && s < domain_size)
      {
	variable = _fd_variables[i];
	domain_size = s;
      }

  return variable;
}

fd_int _fd_select_most_constrained(fd_int _fd_variables[])
{
  // select the first variable with the most constraints
  // XXX: should global constraints count as more than one?
  fd_int variable = NULL;
  int i, nconstraints;

  i = 0;
  while (i < fd__label_vars_count && fd_var_single(_fd_variables[i], NULL))
    ++i;

  if (i == fd__label_vars_count)
    return NULL;		// all variables' domains are singletons

  variable = _fd_variables[i++];
  nconstraints = variable->nconstraints;

  for (; i < fd__label_vars_count; ++i)
    if (_fd_variables[i]->nconstraints > nconstraints &&
	!fd_var_single(_fd_variables[i], NULL))
      {
	variable = _fd_variables[i];
	nconstraints = variable->nconstraints;
      }

  return variable;
}

fd_int _fd_select_size_degree(fd_int _fd_variables[])
{
  // select the first variable with the smallest domain size / constraints
  // (seen in Gecode)
  fd_int variable = NULL;
  double ratio;
  int i, s;

  i = 0;
  while (i < fd__label_vars_count && fd_var_single(_fd_variables[i], NULL))
    ++i;

  if (i == fd__label_vars_count)
    return NULL;		// all variables' domains are singletons

  variable = _fd_variables[i++];
  ratio = (double) _fd_val_size(DOMAIN(variable)) /
          (double) variable->nconstraints;

  for (; i < fd__label_vars_count; ++i)
    if ((s = _fd_val_size(DOMAIN(_fd_variables[i]))) > 1)
      {
	double r = (double) s / (double) _fd_variables[i]->nconstraints;

	if (r < ratio)
	  {
	    variable = _fd_variables[i];
	    ratio = r;
	  }
      }

  return variable;
}

fd_int _fd_select_most_connected(fd_int _fd_variables[])
{
  // select the first variable with the most connections
  fd_int variable = NULL;
  int i, j, connections;

  i = 0;
  while (i < fd__label_vars_count && fd_var_single(_fd_variables[i], NULL))
    ++i;

  if (i == fd__label_vars_count)
    return NULL;		// all variables' domains are singletons

  variable = _fd_variables[i++];
  connections = variable->nconnections;

  for (; i < fd__label_vars_count; ++i)
    if (!fd_var_single(_fd_variables[i], NULL))
      if (_fd_variables[i]->nconnections > connections)
	{
	  variable = _fd_variables[i];
	  connections = _fd_variables[i]->nconnections;
	}

  return variable;
}

fd_int _fd_select_random_var(fd_int _fd_variables[])
{
  fd_int *vs;
  int i, n;

  vs = alloca(fd__label_vars_count * sizeof(*vs)); // XXX: may be too big?

  for (i = n = 0; i < fd__label_vars_count; ++i)
    if (!fd_var_single(_fd_variables[i], NULL))
      vs[n++] = _fd_variables[i];

  return n ? vs[random() % n] : 0;
}

/* select the first variable with the smallest value in its domain */
fd_int _fd_select_min_value(fd_int _fd_variables[])
{
  fd_int variable = NULL;
  int i, k, min;

  i = 0;
  while (i < fd__label_vars_count && fd_var_single(_fd_variables[i], NULL))
    ++i;

  if (i == fd__label_vars_count)
    return NULL;		// all variables' domains are singletons

  variable = _fd_variables[i++];
  min = _fd_val_min(DOMAIN(variable));

  for (; i < fd__label_vars_count; ++i)
    if (!fd_var_single(_fd_variables[i], NULL) &&
	(k = _fd_val_min(DOMAIN(_fd_variables[i]))) < min)
      {
	variable = _fd_variables[i];
	min = k;
      }

  return variable;
}

/* select the first variable with the greatest value in its domain */
fd_int _fd_select_max_value(fd_int _fd_variables[])
{
  fd_int variable = NULL;
  int i, k, max;

  i = 0;
  while (i < fd__label_vars_count && fd_var_single(_fd_variables[i], NULL))
    ++i;

  if (i == fd__label_vars_count)
    return NULL;		// all variables' domains are singletons

  variable = _fd_variables[i++];
  max = _fd_val_max(DOMAIN(variable));

  for (; i < fd__label_vars_count; ++i)
    if (!fd_var_single(_fd_variables[i], NULL) &&
	(k = _fd_val_max(DOMAIN(_fd_variables[i]))) > max)
      {
	variable = _fd_variables[i];
	max = k;
      }

  return variable;
}

/* select the next variable to be instantiated (search) */
fd_int _fd_var_select()
{
  return _fd_var_select2(_fd_variables);
}