Blame view

src/problem.c 4.52 KB
965dadaa   Salvador Abreu   initial commit fr...
1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <stdlib.h>

#ifdef SPLITGO_MPI
#include <mpi.h>
#endif

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

eef94371   Vasco Pedro   Update to PaCCS v...
13
14
extern int fd__workers;

965dadaa   Salvador Abreu   initial commit fr...
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
static void _fd_cleanup(void);

static void _fd_parse_general_options(int *argc, char *argv[])
{
  int args = *argc;
  int i, j;

  bool sort = true;	// the default is to sort labelled variables

  // variable selection function
  _fd_var_select2 = _fd_select_first_var;

  // value selection function
  _fd_val_select = _fd_val_min;
  _fd_val_del_select = _fd_val_del_min;

  // variable comparison function
  fd__cmp_variables = NULL;

#ifdef SPLITGO
  // running mode (the default is to return the first solution)
  _fd_counting_solutions = 0;
#endif

  for (i = j = 1; i < args; i++)
    if (!strcmp(argv[i], "--first-var"))    /* variable selection heuristics */
      _fd_var_select2 = _fd_select_first_var;
    else if (!strcmp(argv[i], "--first-fail"))
      {
	_fd_var_select2 = _fd_select_first_fail;
	if (sort)
	  fd__cmp_variables = fd__cmp_var_size;
      }
    else if (!strcmp(argv[i], "--most-constrained"))
      {
	// once sorted, select the first unassigned variable
	if (sort)
	  fd__cmp_variables = fd__cmp_var_constraints;
	else
	  _fd_var_select2 = _fd_select_most_constrained;
      }
    else if (!strcmp(argv[i], "--size-degree"))
      {
	_fd_var_select2 = _fd_select_size_degree;
	if (sort)
	  fd__cmp_variables = fd__cmp_var_size_degree;
      }
    else if (!strcmp(argv[i], "--most-connected"))
      {
	// once sorted, select the first unassigned variable
	if (sort)
	  fd__cmp_variables = fd__cmp_var_connections;
	else
	  _fd_var_select2 = _fd_select_most_connected;
      }
    else if (!strcmp(argv[i], "--random-var"))
      _fd_var_select2 = _fd_select_random_var;
    else if (!strcmp(argv[i], "--min-value"))
      {
	_fd_var_select2 = _fd_select_min_value;
	if (sort)
	  fd__cmp_variables = fd__cmp_var_min;
      }
    else if (!strcmp(argv[i], "--max-value"))
      {
	_fd_var_select2 = _fd_select_max_value;
	if (sort)
	  fd__cmp_variables = fd__cmp_var_max;
      }
    else if (!strcmp(argv[i], "--val-min"))    /* value selection heuristics */
      {
	_fd_val_select = _fd_val_min;
	_fd_val_del_select = _fd_val_del_min;
      }
    else if (!strcmp(argv[i], "--val-max"))
      {
	_fd_val_select = _fd_val_max;
	_fd_val_del_select = _fd_val_del_max;
      }
    else if (!strcmp(argv[i], "--count-solutions"))          /* running mode */
#ifdef SPLITGO
      _fd_counting_solutions = 1;
#else
      ;
#endif
    else if (!strcmp(argv[i], "--no-sort"))		    /* miscellaneous */
      // Note: the meaning of this option depends on whether it
      // appears in the command line before or after the choice of the
a2ec137b   Vasco Pedro   updated to PaCCS ...
103
104
105
      // variable selection heuristic (see the code above)
      sort = false;
    else if (!strncmp(argv[i], "--workers=", sizeof("--workers=") - 1))
965dadaa   Salvador Abreu   initial commit fr...
106
107
108
109
110
111
112
113
114
115
116
117
118
      fd__workers = atoi(argv[i] + sizeof("--workers=") - 1);
    else
      argv[j++] = argv[i];

  *argc = j;
}

void fd_init(int *argc, char **argv[])
{
#ifdef SPLITGO_MPI
  if (MPI_Init(argc, argv))
    _fd_fatal("MPI_Init failed");
#endif
eef94371   Vasco Pedro   Update to PaCCS v...
119

965dadaa   Salvador Abreu   initial commit fr...
120
121
122
123
124
125
126
127
  _fd_parse_general_options(argc, *argv);

#ifdef SPLITGO
  fd__init_splitgo(argc, *argv);
#endif

#ifdef CONSTRAINT_CLASS
  _fd_init_constraints();
eef94371   Vasco Pedro   Update to PaCCS v...
128
#endif
965dadaa   Salvador Abreu   initial commit fr...
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

#ifdef USE_STORE
  _fd_init_main_store();
#endif
}

void fd_end()
{
#ifdef SPLITGO_MPI
  {
    int rank;

    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    _fd_cleanup_mpi_state(rank);
  }
#endif

  _fd_cleanup();
}

// XXX: called from the constraints' initialisation functions
void _fd_add_constraint(fd_constraint constraint)
{
}

int fd_solve()
{
#ifdef DISTRIBUTED_SOLVER
#ifdef PACK_PROBLEM
  _fd_pack_problem();
#endif

  return _fd_dsolve();
#else // PROPAGATE
#ifdef PACK_PROBLEM
  _fd_pack_problem();
#endif

  return _fd_filter_domains();
#endif
}

/* it helps finding if all allocated memory is accounted for */
static void _fd_cleanup()
{
  extern void _fd_free_value();
  int i, j;

#ifndef PACK_PROBLEM
  for (i = 0; i < _fd_constraint_count; ++i)
    {
      fd_constraint c = _fd_constraints[i];

      free(c->variables);
      if (c->constants)
	free(c->constants);

      free(c);
    }

  for (i = 0; i < fd_variables_count && _fd_variables[i]; ++i)
    {
      _fd_free_value(DOMAIN(_fd_variables[i]));
      free(_fd_variables[i]->constraints);
      free(_fd_variables[i]);
    }
#else /* PACK_PROBLEM */
  // XXX: some things are missing
  _fd_free_packed_memory();
#endif /* PACK_PROBLEM */

  _fd_cleanup_revisions();

#ifdef USE_STORE
  free(store);
#endif

  // XXX
  fd_variables_count = 0;
}