Blame view

src/constraints.c 11.5 KB
94b2b13d   Pedro Roque   PHACT source
1
2
3
4
/*
 * constraints.c
 *
 *  Created on: 25/08/2014
4d26a735   Pedro Roque   Increased recogni...
5
 *      Author: pedro
94b2b13d   Pedro Roque   PHACT source
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 */

#include "constraints.h"

#include <stdio.h>
#include <stdlib.h>

#include "config.h"
#include "variables.h"
#include "bitmaps.h"

/*
 * Creates a new generic constraint that constrains the received variables
 * vs_id - array with the IDs of the variables that are constrained by this constraint
 * n_vs - number of ID variables in vs_id
 * consts - constrained constants (if more than one)
 * n_consts - number of constant values constrained by this constraint in consts vector (if more than one)
 * reif_v_id - -1 if not reified, or the ID of the reification variable if reified
 */
4d26a735   Pedro Roque   Increased recogni...
25
unsigned int c_new(unsigned int* vs_id, unsigned int n_vs, int* consts, unsigned int n_consts, int reif_v_id) {
94b2b13d   Pedro Roque   PHACT source
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
	unsigned int i, j;

	// if more constraints are needed, allocate a new vector and updates the variables pointers
	if (C_ID_CNTR == N_CS) {
		CS_AUX = (constr*) malloc(N_CS * 2 * (sizeof(constr)));

		cs_copy(CS_AUX, CS, N_CS);

		for (i = 0; i < V_ID_CNTR; i++) {
			for (j = 0; j < VS[i].n_cs; j++) {
				VS[i].cs[j] = &CS_AUX[VS[i].cs[j]->c_id];
			}
		}

		free(CS);
		CS = CS_AUX;

		N_CS *= 2;
	}

	CS[C_ID_CNTR].c_id = C_ID_CNTR;
4d26a735   Pedro Roque   Increased recogni...
47
48
	CS[C_ID_CNTR].n_c_vs = (unsigned short)n_vs;
	CS[C_ID_CNTR].n_c_consts = (unsigned short)n_consts;
94b2b13d   Pedro Roque   PHACT source
49
50
51
52
53
54
	CS[C_ID_CNTR].ignore = false;

	if (reif_v_id == -1) {
		CS[C_ID_CNTR].reif_v_id = 0;
		CS[C_ID_CNTR].reified = false;
	} else {
4d26a735   Pedro Roque   Increased recogni...
55
		CS[C_ID_CNTR].reif_v_id = (unsigned int)reif_v_id;
94b2b13d   Pedro Roque   PHACT source
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
		CS[C_ID_CNTR].reified = true;

		v_add_constr(&VS[reif_v_id], &CS[C_ID_CNTR]);
		VS[reif_v_id].reif = true;
	}

	if (n_consts > 0) {
		CS[C_ID_CNTR].c_consts = (int*) malloc(n_consts * sizeof(int));

		for (i = 0; i < n_consts; i++) {
			CS[C_ID_CNTR].c_consts[i] = consts[i];
		}
	} else {
		CS[C_ID_CNTR].c_consts = NULL;
	}

	CS[C_ID_CNTR].c_vs = (var**) malloc(n_vs * sizeof(var*));

	for (i = 0; i < n_vs; i++) {
		CS[C_ID_CNTR].c_vs[i] = &VS[vs_id[i]];

94b2b13d   Pedro Roque   PHACT source
77
78
79
80
81
82
83
84
85
86
87
88
89
90

	}

	CS[C_ID_CNTR].boolean = true;
	for (i = 0; i < n_vs; i++) {
		v_add_constr(&VS[vs_id[i]], &CS[C_ID_CNTR]);

		if (!VS[vs_id[i]].boolean) {
			CS[C_ID_CNTR].boolean = false;
		}
	}

	return C_ID_CNTR++;
}
4d26a735   Pedro Roque   Increased recogni...
91
92
93
94
95
96
97

void cs_copy(constr* cs_dest, constr* cs_src, unsigned int n_cs) {
	unsigned int i;

	for (i = 0; i < n_cs; i++) {
		cs_dest[i].constant_val = cs_src[i].constant_val;
		cs_dest[i].c_consts = cs_src[i].c_consts;
94b2b13d   Pedro Roque   PHACT source
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
		cs_dest[i].c_id = cs_src[i].c_id;
		cs_dest[i].c_vs = cs_src[i].c_vs;
		cs_dest[i].check_sol_f = cs_src[i].check_sol_f;
		cs_dest[i].kind = cs_src[i].kind;
		cs_dest[i].n_c_consts = cs_src[i].n_c_consts;
		cs_dest[i].n_c_vs = cs_src[i].n_c_vs;
		cs_dest[i].reif_v_id = cs_src[i].reif_v_id;
		cs_dest[i].reified = cs_src[i].reified;
		cs_dest[i].ignore = cs_src[i].ignore;
		cs_dest[i].boolean = cs_src[i].boolean;
	}
}

/*
 * Return the count of values inside all variables of the constraints vector
 * cs - constraints vector to count variables values in all constraints
 * n_cs - number of constraints in the vector
 * */
int cs_cnt_vals(constr* cs, unsigned int n_cs) {
	unsigned int i, j;
	int cnt = 0;

	for (i = 0; i < n_cs; i++) {
4d26a735   Pedro Roque   Increased recogni...
121
		for (j = 0; j < cs[i].n_c_vs; j++) {
94b2b13d   Pedro Roque   PHACT source
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
			cnt += cs[i].c_vs[j]->n_vals;
		}
	}
	return cnt;
}

/*
 * Return the count of variables inside a constraints vector
 * cs - constraints vector to count variables in
 * n_cs - number of constraints in the vector
 * */
int cs_cnt_vs(constr* cs, unsigned int n_cs) {
	unsigned int i;
	unsigned int cnt = 0;

	for (i = 0; i < n_cs; i++) {
4d26a735   Pedro Roque   Increased recogni...
138
		cnt += cs[i].n_c_vs;
94b2b13d   Pedro Roque   PHACT source
139
140
141
142
143
144
	}
	return (int)cnt;
}

/*
 * Return the count of different variables inside a constraints vector
4d26a735   Pedro Roque   Increased recogni...
145
 * cs - constraints vector to count different variables in
94b2b13d   Pedro Roque   PHACT source
146
147
148
149
150
151
152
 * n_cs - number of constraints in the vector
 * */
int cs_cnt_diff_vs(constr* cs, unsigned int n_cs) {
	unsigned int i, j;
	unsigned int v_max_id = 0;

	for (i = 0; i < n_cs; i++) {
4d26a735   Pedro Roque   Increased recogni...
153
		for (j = 0; j < cs[i].n_c_vs; j++) {
94b2b13d   Pedro Roque   PHACT source
154
155
156
157
158
159
160
161
162
163
			if (v_max_id < cs[i].c_vs[j]->v_id) {
				v_max_id = cs[i].c_vs[j]->v_id;
			}
		}
	}
	return (int)v_max_id + 1;
}

/*
 * Return the count of constant values (more than one per constraint) inside a constraints vector
4d26a735   Pedro Roque   Increased recogni...
164
 * cs - constraints vector to count constant values in
94b2b13d   Pedro Roque   PHACT source
165
166
167
168
169
170
171
 * n_cs - number of constraints in the vector
 * */
int cs_cnt_constants(constr* cs, unsigned int n_cs) {
	unsigned int i;
	unsigned int cnt = 0;

	for (i = 0; i < n_cs; i++) {
4d26a735   Pedro Roque   Increased recogni...
172
		cnt += cs[i].n_c_consts;
94b2b13d   Pedro Roque   PHACT source
173
174
175
176
177
178
	}
	return (int)cnt;
}

/*
 * Remove all constraints marked as ignored after filtering the CSP
4d26a735   Pedro Roque   Increased recogni...
179
 */
94b2b13d   Pedro Roque   PHACT source
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
void cs_remove_ignored() {
	int ignored_ctr = 0;
	int cs_count = 0;
	unsigned int i, j, k, l;

	for (i = 0; i < C_ID_CNTR; i++) {
		if (CS[i].ignore) {

			ignored_ctr++;

			// remove constraint pointer from variables
			for (j = 0; j < CS[i].n_c_vs; j++) {
				v_rem_constr(CS[i].c_vs[j], &CS[i]);
			}
			if (CS[i].reified) {
				if (VS[CS[i].reif_v_id].v_id == CS[i].reif_v_id) {
					v_rem_constr(&VS[CS[i].reif_v_id], &CS[i]);

				} else {
					for (k = 0; k < N_VS; k++) {
						if (VS[k].v_id == CS[i].reif_v_id) {
							v_rem_constr(&VS[k], &CS[i]);
							break;
						}
					}
				}
			}
		}
	}

	CS_AUX = malloc((C_ID_CNTR - (unsigned long)ignored_ctr) * sizeof(constr));

	for (i = 0; i < C_ID_CNTR; i++) {
		if (!CS[i].ignore) {

4d26a735   Pedro Roque   Increased recogni...
215
			cs_copy(&CS_AUX[cs_count], &CS[i], 1);
94b2b13d   Pedro Roque   PHACT source
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

			// update constraint pointer from variables
			for (j = 0; j < CS_AUX[cs_count].n_c_vs; j++) {

				for (k = 0; k < CS_AUX[cs_count].c_vs[j]->n_cs; k++) {

					if (CS_AUX[cs_count].c_vs[j]->cs[k]->c_id == CS_AUX[cs_count].c_id) {
						CS_AUX[cs_count].c_vs[j]->cs[k] = &CS_AUX[cs_count];
					}
				}

				if (CS_AUX[cs_count].reified) {

					if (VS[CS_AUX[cs_count].reif_v_id].v_id == CS_AUX[cs_count].reif_v_id) {

						for (l = 0; l < VS[CS_AUX[cs_count].reif_v_id].n_cs; l++) {

							if (VS[CS_AUX[cs_count].reif_v_id].cs[l]->c_id == CS_AUX[cs_count].c_id) {
								VS[CS_AUX[cs_count].reif_v_id].cs[l] = &CS_AUX[cs_count];
								break;
							}
						}
					} else {
						for (k = 0; k < N_VS; k++) {

							if (VS[k].v_id == CS_AUX[cs_count].reif_v_id) {

								for (l = 0; l < VS[k].n_cs; l++) {

									if (VS[k].cs[l]->c_id == CS_AUX[cs_count].c_id) {
										VS[k].cs[l] = &CS_AUX[cs_count];
										break;
									}
								}
								break;
							}
						}
					}
				}
			}

			CS_AUX[cs_count].c_id = (unsigned int)cs_count;
			cs_count++;

		} else {
			free(CS[i].c_consts);
4d26a735   Pedro Roque   Increased recogni...
262
			free(CS[i].c_vs);
94b2b13d   Pedro Roque   PHACT source
263
264
265
266
267
268
269
270
		}
	}

	N_CS =  (unsigned int)cs_count;
	C_ID_CNTR = (unsigned int)cs_count;

	free(CS);
	CS = CS_AUX;
4d26a735   Pedro Roque   Increased recogni...
271
272
}

94b2b13d   Pedro Roque   PHACT source
273
274
275
276
277
278
279
/*
 * Return true if all constraints are met (solution found) and false otherwise
 */
bool cs_check(bool explored) {
	unsigned int i;
	bool result = true;

4d26a735   Pedro Roque   Increased recogni...
280
	for (i = 0; i < N_CS && result; i++) {
94b2b13d   Pedro Roque   PHACT source
281
282
283
284
285
286
		if (result && (!CS[i].reified || (CS[i].reified && VS[CS[i].reif_v_id].n_vals == 1 && VS[CS[i].reif_v_id].min == 1))) {
			result = CS[i].check_sol_f(&CS[i], explored);

		} else if (!explored) {
			result = false;
		}
4d26a735   Pedro Roque   Increased recogni...
287
	}
94b2b13d   Pedro Roque   PHACT source
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

	return result;
}

/*
 * Clear constraints memory
 */
void cs_clear() {
	unsigned int i;

	for (i = 0; i < N_CS; i++) {
		free(CS[i].c_vs);

		if (CS[i].n_c_consts > 0) {
			free(CS[i].c_consts);
		}
	}
}

/*
 * print all the variables ID that are constrained by all the CSP constraints, per contraint order
 */
void cs_print_all_vs_id() {
	unsigned int i;
	int j;

	printf("\n");
	for (i = 0; i < C_ID_CNTR; i++) {
		printf("Constraint (%d) %u -> ", CS[i].kind, i);
		for (j = 0; j < CS[i].n_c_vs - 1; j++) {
			printf("%u,",  CS[i].c_vs[j]->v_id);
		}
		printf("%u\n",  CS[i].c_vs[j]->v_id);
	}
}

4d26a735   Pedro Roque   Increased recogni...
324
/*
94b2b13d   Pedro Roque   PHACT source
325
 * Print the name of the constraint
4d26a735   Pedro Roque   Increased recogni...
326
 * cs -constraint to print the name
94b2b13d   Pedro Roque   PHACT source
327
328
329
330
331
332
333
 */
void cs_print_type(constr* cs) {
	switch (cs->kind) {
		case ALL_DIFFERENT:
			printf("ALL_DIFFERENT");
			break;
		case AT_LEAST:
4d26a735   Pedro Roque   Increased recogni...
334
			printf("AT_LEAST");
94b2b13d   Pedro Roque   PHACT source
335
			break;
4d26a735   Pedro Roque   Increased recogni...
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
		case AT_MOST:
			printf("AT_MOST");
			break;
		case AT_MOST_ONE:
			printf("AT_MOST_ONE");
			break;
		case ELEMENT:
			printf("ELEMENT");
			break;
		case ELEMENT_INT_VAR:
			printf("ELEMENT_INT_VAR");
			break;
		case ELEMENT_VAR:
			printf("ELEMENT_VAR");
			break;
		case EQ:
			printf("EQ");
			break;
		case EQ_VAR:
			printf("EQ_VAR");
			break;
		case EXACTLY:
			printf("EXACTLY");
			break;
		case EXACTLY_VAR:
			printf("EXACTLY_VAR");
			break;
		case LE:
			printf("LE");
			break;
		case LINEAR:
			printf("LINEAR");
			break;
		case LINEAR_VAR:
			printf("LINEAR_VAR");
			break;
		case LT:
			printf("LT");
			break;
		case MAX:
			printf("MAX");
			break;
		case MAXIMIZE:
			printf("MAXIMIZE");
			break;
		case MIN:
			printf("MIN");
			break;
		case MINIMIZE:
			printf("MINIMIZE");
			break;
		case MINUS_EQ:
			printf("MINUS_EQ");
			break;
		case MINUS_NE:
			printf("MINUS_NE");
			break;
		case NE:
			printf("NE");
			break;
		case SUM:
			printf("SUM");
			break;
		case SUM_PROD:
			printf("SUM_PROD");
			break;
		case SUM_VAR:
			printf("SUM_VAR");
			break;
		case VAR_EQ_MINUS:
			printf("VAR_EQ_MINUS");
			break;
		case VAR_EQ_PLUS:
			printf("VAR_EQ_PLUS");
			break;
		case VAR_EQ_TIMES:
			printf("VAR_EQ_TIMES");
			break;
		case VAR_EQ_MINUS_ABS:
			printf("VAR_EQ_MINUS_ABS");
			break;
		case LINEAR_NE:
			printf("LINEAR_NE");
			break;
		case LINEAR_LT:
			printf("LINEAR_LT");
			break;
		case BOOL_OR:
			printf("BOOL_OR");
			break;
		case BOOL_AND:
			printf("BOOL_AND");
			break;
		case BOOL_CLAUSE:
			printf("BOOL_CLAUSE");
			break;
		case BOOL2INT:
			printf("BOOL2INT");
			break;
		default:
			printf("NOT_RECOGNIZED");
			break;
	}

	if (cs->reified) {
		printf("_REIF");
	}
}

/*
 * Return the name of the constraint
 * kind -kind of the constraint to return the name
 */
char* cs_get_type(c_kind kind) {
	switch (kind) {
		case ALL_DIFFERENT:
			return "ALL_DIFFERENT";
			break;
		case AT_LEAST:
			return"AT_LEAST";
			break;
		case AT_MOST:
			return "AT_MOST";
			break;
		case AT_MOST_ONE:
			return "AT_MOST_ONE";
			break;
		case ELEMENT:
			return "ELEMENT";
			break;
		case ELEMENT_INT_VAR:
			return "ELEMENT_INT_VAR";
			break;
		case ELEMENT_VAR:
			return "ELEMENT_VAR";
			break;
		case EQ:
			return "EQ";
			break;
		case EQ_VAR:
			return "EQ_VAR";
			break;
		case EXACTLY:
			return "EXACTLY";
			break;
		case EXACTLY_VAR:
			return "EXACTLY_VAR";
94b2b13d   Pedro Roque   PHACT source
483
484
485
486
487
488
489
490
491
492
493
494
495
			break;
		case LE:
			return "LE";
			break;
		case LINEAR:
			return "LINEAR";
			break;
		case LINEAR_VAR:
			return "LINEAR_VAR";
			break;
		case LT:
			return "LT";
			break;
4d26a735   Pedro Roque   Increased recogni...
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
		case MAX:
			return "MAX";
			break;
		case MAXIMIZE:
			return "MAXIMIZE";
			break;
		case MIN:
			return "MIN";
			break;
		case MINIMIZE:
			return "MINIMIZE";
			break;
		case MINUS_EQ:
			return "MINUS_EQ";
			break;
		case MINUS_NE:
			return "MINUS_NE";
			break;
		case NE:
			return "NE";
			break;
		case SUM:
			return "SUM";
			break;
		case SUM_PROD:
			return "SUM_PROD";
			break;
		case SUM_VAR:
			return "SUM_VAR";
			break;
		case VAR_EQ_MINUS:
			return "VAR_EQ_MINUS";
			break;
		case VAR_EQ_PLUS:
			return "VAR_EQ_PLUS";
			break;
		case VAR_EQ_TIMES:
			return "VAR_EQ_TIMES";
			break;
		case VAR_EQ_MINUS_ABS:
			return "VAR_EQ_MINUS_ABS";
			break;
		case LINEAR_NE:
			return "LINEAR_NE";
			break;
		case LINEAR_LT:
			return "LINEAR_LT";
			break;
		case BOOL_OR:
			return "BOOL_OR";
			break;
		case BOOL_AND:
			return "BOOL_AND";
			break;
		case BOOL_CLAUSE:
			return "BOOL_CLAUSE";
			break;
		case BOOL2INT:
			return "BOOL2INT";
			break;
		default:
			return "NOT_RECOGNIZED";
			break;
	}
}

void cs_print_used() {
	int i;

	for (i = 0; i < N_C_TYPES; i++) {

		if (USE_CS[i]) {
			printf("   - %s", cs_get_type((c_kind)i));

			if (USE_CS_REIFI[i]) {
				printf("_REIF\n");

				if (USE_NON_CS_REIFI[i]) {
					printf("   - %s\n", cs_get_type((c_kind)i));
				}

			} else {
				printf("\n");
			}
		}
	}
}
94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source

4d26a735   Pedro Roque   Increased recogni...

94b2b13d   Pedro Roque   PHACT source