qap.c 14.4 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
/*
 * qap.c
 *
 *  Created on: 12/12/2016
 *      Author: pedro
 *
 *		http://anjos.mgi.polymtl.ca/qaplib//
 *
 *		Quadratic assignment problem:
 *      There are a set of n facilities and a set of n locations. For each pair of locations,
 *      a distance is specified and for each pair of facilities a weight or flow is specified
 *      (e.g., the amount of supplies transported between the two facilities). The problem is
 *      to assign all facilities to different locations with the goal of minimizing the sum of
 *      the distances multiplied by the corresponding flows.
 */

#include "qap.h"

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

#include "../config.h"
#include "../constraints/all_different.h"
#include "../constraints/element_var.h"
#include "../constraints/linear_var.h"
#include "../constraints/minimize.h"
#include "../split.h"
#include "../variables.h"

unsigned int p2_flow[] = { 0, 1, 3, 0 };
unsigned int p2_dist[] = { 0, 4, 2, 0 };

unsigned int p2c_flow[] = { 0, 1, 0, 0 };
unsigned int p2c_dist[] = { 3, 5, 7, 9 };

unsigned int p3_flow[] = { 0, 1, 4, 1, 0, 2, 4, 2, 0 };
unsigned int p3_dist[] = { 0, 2, 4, 2, 0, 1, 4, 1, 0 };

unsigned int p3b_flow[] = { 0, 1, 1, 1, 0, 1, 1, 1, 0 };
unsigned int p3b_dist[] = { 0, 1, 1, 1, 0, 1, 1, 1, 0 };

unsigned int p3c_flow[] = { 0, 0, 1, 0, 0, 0, 0, 0, 0 };
unsigned int p3c_dist[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

unsigned int p5_flow[] = { 0, 1, 2, 1, 0, 1, 0, 2, 1, 0, 2, 2, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0 };
unsigned int p5_dist[] = { 0, 5, 1, 3, 2, 5, 0, 2, 1, 4, 1, 2, 0, 2, 1, 3, 1, 2, 0, 2, 2, 4, 1, 2, 0 };

unsigned int p10_flow[] = { 0, 1, 2, 1, 0, 0, 1, 2, 1, 0, 1, 0, 2, 1, 0, 0, 1, 2, 0, 1, 2, 2, 0, 1, 0, 0, 1, 0, 2, 2, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
		0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 2, 2, 0, 1, 0, 0, 1, 0, 2, 2, 1, 0, 2, 1, 0, 0, 1, 2, 0, 1, 0, 1, 2, 1, 0, 0, 1, 2, 1, 0 };
unsigned int p10_dist[] = { 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0,
		1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0 };

unsigned int choi15_flow[] = {
  16,  4,  6,  8,  7,  4,  6,  8,  7,  4,  6,  8,  7,  4,  6,
   4,  6,  1,  3,  4,  6,  1,  3,  4,  6,  1,  3,  4,  6,  1,
   6,  1,  4,  2,  6,  1,  9,  2,  6,  1,  9,  2,  6,  1,  9,
   8,  3,  2, 10,  8,  3,  2,  5,  8,  3,  2,  5,  8,  3,  2,
   7,  4,  6,  8, 16,  4,  6,  8,  7,  4,  6,  8,  7,  4,  6,
   4,  6,  1,  3,  4,  6,  1,  3,  4,  6,  1,  3,  4,  6,  1,
   6,  1,  9,  2,  6,  1,  4,  2,  6,  1,  9,  2,  6,  1,  9,
   8,  3,  2,  5,  8,  3,  2, 10,  8,  3,  2,  5,  8,  3,  2,
   7,  4,  6,  8,  7,  4,  6,  8, 16,  4,  6,  8,  7,  4,  6,
   4,  6,  1,  3,  4,  6,  1,  3,  4,  6,  1,  3,  4,  6,  1,
   6,  1,  9,  2,  6,  1,  9,  2,  6,  1,  4,  2,  6,  1,  9,
   8,  3,  2,  5,  8,  3,  2,  5,  8,  3,  2, 10,  8,  3,  2,
   7,  4,  6,  8,  7,  4,  6,  8,  7,  4,  6,  8, 16,  4,  6,
   4,  6,  1,  3,  4,  6,  1,  3,  4,  6,  1,  3,  4,  6,  1,
   6,  1,  9,  2,  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 0, 1, 0, 0, 0, 3, 2, 2, 1, 2, 1, 1, 0,
		0, 1, 1, 2, 1, 2, 2, 3, 0, 0, 0, 1, 0, 1, 1, 2,
		1, 0, 2, 1, 2, 1, 3, 2, 0, 0, 1, 0, 1, 0, 2, 1,
		1, 2, 0, 1, 2, 3, 1, 2, 0, 1, 0, 0, 1, 2, 0, 1,
		2, 1, 1, 0, 3, 2, 2, 1, 1, 0, 0, 0, 2, 1, 1, 0,
		1, 2, 2, 3, 0, 1, 1, 2, 0, 1, 1, 2, 0, 0, 0, 1,
		2, 1, 3, 2, 1, 0, 2, 1, 1, 0, 2, 1, 0, 0, 1, 0,
		2, 3, 1, 2, 1, 2, 0, 1, 1, 2, 0, 1, 0, 1, 0, 0,
		3, 2, 2, 1, 2, 1, 1, 0, 2, 1, 1, 0, 1, 0, 0, 0
};

/*
 * Solve one of the above instances of the Quadratic assignment problem
 */
void run_qap(char* csp_inst_name) {
	int n;
	int* p_flow;
	unsigned int* p_dist;
	unsigned long result;
	unsigned int d_max;
	unsigned int cost;
	unsigned int* n_pos;
	unsigned int* new_dist;
	unsigned int vs[3];
	bool symmetrical = true;
	bool symmetrical_aux;
	int i, j;

	if (!strcmp(csp_inst_name, "p2")) {
		n = 2;
		p_flow = (int*) p2_flow;
		p_dist = p2_dist;
	} else if (!strcmp(csp_inst_name, "p2c")) {
		n = 2;
		p_flow = (int*) p2c_flow;
		p_dist = p2c_dist;
	} else if (!strcmp(csp_inst_name, "p3")) {
		n = 3;
		p_flow = (int*) p3_flow;
		p_dist = p3_dist;
	} else if (!strcmp(csp_inst_name, "p3b")) {
		n = 3;
		p_flow = (int*) p3b_flow;
		p_dist = p3b_dist;
	} else if (!strcmp(csp_inst_name, "p3c")) {
		n = 3;
		p_flow = (int*) p3c_flow;
		p_dist = p3c_dist;
	} else if (!strcmp(csp_inst_name, "p5")) {
		n = 5;
		p_flow = (int*) p5_flow;
		p_dist = p5_dist;
	} else if (!strcmp(csp_inst_name, "p10")) {
		n = 10;
		p_flow = (int*) p10_flow;
		p_dist = p10_dist;
	} else if (!strcmp(csp_inst_name, "choi15")) {
		n = 15;
		p_flow = (int*) choi15_flow;
		p_dist = choi15_dist;
	} else if (!strcmp(csp_inst_name, "esc16i")) {
		n = 16;
		p_flow = (int*) esc16i_flow;
		p_dist = esc16i_dist;
	} else if (!strcmp(csp_inst_name, "esc16e")) {
		n = 16;
		p_flow = (int*) esc16e_flow;
		p_dist = esc16e_dist;
	} else if (!strcmp(csp_inst_name, "esc16g")) {
		n = 16;
		p_flow = (int*) esc16g_flow;
		p_dist = esc16g_dist;
	} else {
		printf("\n%s QAP problem is not available. Please select an instance from \"csps/qap.c\" file.\n", csp_inst_name);
		return;
	}

	int cs[] = { n, 1, 1 };

	unsigned int* pos = malloc((unsigned int)n * sizeof(unsigned int));
	unsigned int* dist = malloc((unsigned int)n * (unsigned int)n * sizeof(unsigned int));

	// see if the problem is symmetric
	for (i = 0; i < n; ++i) {
		for (j = 0; j < i; ++j) {
			if (p_flow[i * n + j] != p_flow[j * n + i] || p_dist[i * n + j] != p_dist[j * n + i]) {
				symmetrical = false;
				break;
			}
		}
	}

	// see if the cost associated with the main diagonal is 0
	symmetrical_aux = symmetrical;

	if (symmetrical) {
		for (i = 0; i < n; ++i) {
			if (p_flow[i * n + i] != 0) {
				break;
			}
		}
		if (i < n) {
			for (i = 0; i < n; ++i) {
				if (p_dist[i * n + i] != 0) {
					break;
				}
			}
		}
		if (i < n) {
			symmetrical_aux = false;
		}
	}

	d_max = 0;
	for (i = 0; i < n * n; ++i) {
		d_max += (unsigned int)p_flow[i] * p_dist[i];
	}
	d_max++;
	if (d_max > 255) {
		d_max = 255;
	}

	for (i = 0; i < n; ++i) {
		pos[i] = v_new_range(0, (unsigned int)n - 1, true);
	}
	c_all_different(pos, (unsigned int)n);

	cost = v_new_range(0, d_max, false);

	for (i = 0; i < n * n; ++i) {
		dist[i] = v_new_val(p_dist[i]);
	}

	if (!symmetrical_aux) {
		new_dist = malloc((unsigned long)n * (unsigned long)n * sizeof(unsigned int));
		n_pos = malloc((unsigned long)n * (unsigned long)n * sizeof(unsigned int));

		for (i = 0; i < n * n; ++i) {
			new_dist[i] = v_new_range(0, d_max, false);
		}

		for (i = 0; i < n * n; ++i) {
			n_pos[i] = v_new_range(1, (unsigned int)n * (unsigned int)n, false);
		}

		vs[2] = v_new_val(1);	// element_var works with Y indexs from 1 to ... Because of Flatzinc specification
		for (i = 0; i < n; ++i) {
			vs[0] = pos[i];

			for (j = 0; j < n; ++j) {
				vs[1] = pos[j];

				c_linear_var(cs, vs, 3, n_pos[i * n + j]);
				c_element_var(dist, (unsigned int)n * (unsigned int)n, n_pos[(unsigned int)i * (unsigned int)n + (unsigned int)j], new_dist[(unsigned int)i * (unsigned int)n + (unsigned int)j]);
			}
		}
		c_linear_var(p_flow, new_dist, (unsigned int)n * (unsigned int)n, cost);

	} else {
		new_dist = malloc((unsigned long)n * (unsigned long)n * sizeof(unsigned int));
		n_pos = malloc((unsigned long)n * (unsigned long)n * sizeof(unsigned int));

		for (i = 1; i < n; ++i) {
			for (j = 0; j < i; ++j) {
				new_dist[i * n + j] = new_dist[j * n + i] = v_new_range(0, d_max, false);
				n_pos[(unsigned int)i * (unsigned int)n + (unsigned int)j] = n_pos[(unsigned int)j * (unsigned int)n + (unsigned int)i] = v_new_range(1, (unsigned int)n * (unsigned int)n, false);
			}
		}

		unsigned int val = 0;
		unsigned int v_id = v_new_val(val);
		for (i = 0; i < n; ++i) {
			new_dist[i * n + i] = v_id;
			n_pos[i * n + i] = v_id;
		}

		vs[2] = v_new_val(1);	// element_var works with Y indexs from 1 to ... Because of Flatzinc specification
		for (i = 1; i < n; ++i) {
			vs[0] = pos[i];

			for (j = 0; j < i; ++j) {
				vs[1] = pos[j];

				c_linear_var(cs, vs, 3, n_pos[i * n + j]);
				c_element_var(dist, (unsigned int)n * (unsigned int)n, n_pos[(unsigned int)i * (unsigned int)n + (unsigned int)j], new_dist[(unsigned int)i * (unsigned int)n + (unsigned int)j]);
			}
		}
		c_linear_var(p_flow, new_dist, (unsigned int)n * (unsigned int)n, cost);
	}

	// for optimization
	c_minimize(cost);

	if (OPTIMIZING) {
			printf("\nOptimizing %s Quadratic Assignment Problem with %d values.\n", csp_inst_name, n);
	} else if (FINDING_ONE_SOLUTION) {
		printf("\nFinding one solution for %s Quadratic Assignment Problem with %d values.\n", csp_inst_name, n);
	} else {
		printf("\nCounting all the solutions for %s Quadratic Assignment Problem with %d values.\n", csp_inst_name, n);
	}

	// Solve the CSP
	result = solve_CSP();

	if (OPTIMIZING && result == 1) {
		printf("Best solution:\n");
		vs_print_single_val(pos, (unsigned int)n, 1);
		printf("\n");
		v_print_cost(0);

	} else if (FINDING_ONE_SOLUTION && result == 1) {
		printf("Solution:\n");
		vs_print_single_val(pos, (unsigned int)n, 1);
		printf("\n");

	} else {
		printf("%lu solution(s) found\n", result);
	}

	free(pos);
	free(dist);
	free(n_pos);
	free(new_dist);
}