/* Copyright (C) 2001 Dale Schuurmans, Finnegan Southey */ /* This work is licensed under the Gnu General Public License (see gpl.txt). */ /* Converts CATS to COP format. */ #include #include #include #include "cop.h" int main(int argc, char **argv) { FILE *in = NULL, *out = NULL; int fnum; int n, m, N, M, Nmax, Mmax; int *A, *nCs, **Cs, **Ccoefs; int *B, *nXs, **Xs, **Xcoefs; int x, c, var, cons, j; char name[50]; char outname[10000]; char *outputDir = NULL; int usestd = 0; char* lastslash; if (argc < 3 && argc != 1) { printf("Incorrect number of arguments.\n"); printf("Usage: cats2cop [ [cats file] ...]\n"); exit(1); } if (argc == 1) { in = stdin; out = stdout; argc = 3; usestd = 1; } else { outputDir = argv[1]; } for (fnum = 2; fnum < argc; fnum++) { if (!usestd) { in = fopen(argv[fnum], "r"); if (in == NULL) { printf("Error opening input file: %s\n", argv[fnum]); exit(1); } strcpy(outname, outputDir); if (outputDir[strlen(outputDir)-1] != '/') strcat(outname, "/"); lastslash = strrchr(argv[fnum], '/'); if (lastslash == NULL) strcat(outname, argv[fnum]); else strcat(outname, lastslash + 1); strcpy(strrchr(outname, '.'), ".cop"); out = fopen(outname, "w"); if (out == NULL) { printf("Error opening output file: %s\n", outname); exit(1); } } fscanf(in, "goods %d \n", &m); fscanf(in, "bids %d", &n); /* if (m > CATS_MAX_GOODS) { printf("Number of goods exceeds maximum for CATS problem (compile-time constant).\n"); exit(1); } if (n > CATS_MAX_BIDS) { printf("Number of bids exceeds maximum for CATS problem (compile-time constant).\n"); exit(1); } M = m; N = n; */ M = MAX_CONS_PER_VAR; N = MAX_VARS_PER_CON; /***** allocate cats *****/ A = (int *) calloc(n, sizeof(int)); nCs = (int *) calloc(n, sizeof(int)); Cs = (int **) calloc(n, sizeof(int *)); for (x = 0; x < n; x++) Cs[x] = (int *) calloc(M, sizeof(int)); Ccoefs = (int **) calloc(n, sizeof(int *)); for (x = 0; x < n; x++) Ccoefs[x] = (int *) calloc(M, sizeof(int)); B = (int *) calloc(m, sizeof(int)); nXs = (int *) calloc(m, sizeof(int)); Xs = (int **) calloc(m, sizeof(int *)); for (c = 0; c < m; c++) Xs[c] = (int *) calloc(N, sizeof(int)); Xcoefs = (int **) calloc(m, sizeof(int *)); for (c = 0; c < m; c++) Xcoefs[c] = (int *) calloc(N, sizeof(int)); /***** read cats *****/ Mmax = 0; for (x = 0; x < n; x++) { fscanf(in, "%d %d", &var, A+x); while (1) { fscanf(in, "%s", name); if (name[0] == '#') break; cons = atoi(name); if (nCs[x] == M) { printf("Exceeded maximum number of constraints per variable (var %d max %d)\n", x, M); exit(1); } Cs[x][nCs[x]] = cons; Ccoefs[x][nCs[x]] = 1; nCs[x]++; if (nXs[cons] == N) { printf("Exceeded maximum number of variables per constraint (cons %d max %d)\n", cons, N); exit(1); } Xs[cons][nXs[cons]] = x; Xcoefs[cons][nXs[cons]] = 1; nXs[cons]++; } if (nCs[x] > Mmax) Mmax = nCs[x]; } Nmax = 0; for (c = 0; c < m; c++) { B[c] = 2 - nXs[c]; if (nXs[c] > Nmax) Nmax = nXs[c]; } /***** print cats *****/ fprintf(out, "(%d %d)\n", n, m); fprintf(out, "(%d %d)\n", Nmax, Mmax); fprintf(out, "("); for (x = 0; x < n-1; x++) fprintf(out, "%d ", -A[x]); fprintf(out, "%d)\n", -A[n-1]); for (c = 0; c < m; c++) { fprintf(out, "(%d %d ", B[c], nXs[c]); for (j = 0; j < nXs[c]; j++) fprintf(out, "(%d %d)", Xs[c][j], Xcoefs[c][j]); fprintf(out, ")\n"); } if (!usestd) fclose(in); } return 0; }