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
|
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <sys/stat.h>
#include "lrsdriver.h"
#include "lrslib.h"
/* 10.05.2023 */
/* Variable string lengths for game payoffs */
/* 08.05.2023 */
/* This version represents rational payoffs as strings. */
/* It copies them from input file "game" to the */
/* polytopes in "game1.ine" and "game2.ine" after */
/* removing whitespace and reversing signs. */
/* Usage: setupnash game game1.ine game2.ine */
/* Reads input file game containing */
/* m n */
/* A matrix (m by n rationals ) */
/* B matrix (m by n rationals ) */
/* Outputs: two files game1.ine game2.ine */
/* that are used by nash */
long tread (char *str) /* read a payoff number as a string */
{
if(fscanf (lrs_ifp, "%s", str)==EOF)
{
fprintf (lrs_ofp, "\nInvalid input: check you have entered enough data!\n");
exit(1);
}
return (TRUE);
}
void tprint (char *str) /*print the negative of str */
{
if(!strcmp(str, "0")) { // Zero: print as is
fprintf (lrs_ofp, " %s", str);
}
else if(str[0] == '-') { // Negative: Remove minus sign
str += 1;
fprintf (lrs_ofp, " %s", str);
}
else // Positive: Insert minus sign
fprintf (lrs_ofp, " -%s", str);
}
int main (int argc, char *argv[])
{
long m, n, i, j;
char ***A, ***B; /* Payoff matrices */
char *payoffs, *p; /* Storage and pointer for payoffs as strings */
struct stat st; /* To get size of input file */
if ( argc < 3 )
{
printf ("\nUsage: setupnash infile outfile1 outfile2\n");
return(FALSE);
}
if ((lrs_ifp = fopen (argv[1], "r")) == NULL)
{
printf ("\nBad input file name\n");
return (FALSE);
}
else
printf ("\n*Input taken from file %s", argv[1]);
if(fscanf(lrs_ifp,"%ld %ld",&m,&n)==EOF)
{ printf("\nInvalid m,n");
return(FALSE);
}
if( m > 1000 || n > 1000)
{
printf ("\nm=%ld n=%ld",m,n);
printf ("\nBoth m and n must at most 1000\n");
return(FALSE);
}
/* Get size of input file and use it to allocate storage for payoff strings */
stat(argv[1], &st);
payoffs = (char *) calloc(st.st_size, sizeof(char));
/* Initialize payoff matrices */
A = (char ***) calloc(m, sizeof(char **));
for (i=0;i<m;i++) {
A[i] = (char **) calloc(n, sizeof(char *));
}
B = (char ***) calloc(m, sizeof(char **));
for (i=0;i<m;i++) {
B[i] = (char **) calloc(n, sizeof(char *));
}
/* process input file */
/* read A matrix */
p = payoffs;
for (i=0;i<m;i++)
for (j=0;j<n;j++) {
tread(p);
A[i][j] = p;
p += strlen(p)+1;
}
/* read B matrix */
for (i=0;i<m;i++)
for (j=0;j<n;j++) {
tread(p);
B[i][j] = p;
p += strlen(p)+1;
}
/* write first output file */
if ((lrs_ofp = fopen (argv[2], "w")) == NULL)
{
printf ("\nBad output file name\n");
return (FALSE);
}
else
printf ("\n*Output one sent to file %s\n", argv[2]);
fprintf(lrs_ofp,"*%s: player 1",argv[1]);
fprintf(lrs_ofp,"\nH-representation");
fprintf(lrs_ofp,"\nlinearity 1 %ld",m+n+1);
fprintf(lrs_ofp,"\nbegin");
fprintf(lrs_ofp,"\n%ld %ld rational",m+n+1,m+2);
for (i=0;i<m;i++) {
fprintf(lrs_ofp,"\n 0 ");
for (j=0;j<m;j++)
{
if ( i == j )
fprintf(lrs_ofp,"1 ");
else
fprintf(lrs_ofp,"0 ");
}
fprintf(lrs_ofp,"0 ");
}
for (i=0;i<n;i++) {
fprintf(lrs_ofp,"\n 0 ");
for (j=0;j<m;j++)
tprint(B[j][i]);
fprintf(lrs_ofp," 1 ");
}
fprintf(lrs_ofp,"\n-1 ");
for (j=0;j<m;j++)
fprintf(lrs_ofp,"1 ");
fprintf(lrs_ofp,"0 ");
fprintf(lrs_ofp,"\nend");
fprintf(lrs_ofp,"\n");
fclose(lrs_ofp);
/* output file 2 */
if ((lrs_ofp = fopen (argv[3], "w")) == NULL)
{
printf ("\nBad output file name\n");
return (FALSE);
}
else
printf ("\n*Output two sent to file %s\n", argv[3]);
fprintf(lrs_ofp,"*%s: player 2",argv[1]);
fprintf(lrs_ofp,"\nH-representation");
fprintf(lrs_ofp,"\nlinearity 1 %ld",m+n+1);
fprintf(lrs_ofp,"\nbegin");
fprintf(lrs_ofp,"\n%ld %ld rational",m+n+1,n+2);
for (i=0;i<m;i++) {
fprintf(lrs_ofp,"\n 0 ");
for (j=0;j<n;j++)
tprint(A[i][j]);
fprintf(lrs_ofp," 1 ");
}
for (i=0;i<n;i++)
{
fprintf(lrs_ofp,"\n 0 ");
for (j=0;j<n;j++)
{
if ( i == j )
fprintf(lrs_ofp,"1 ");
else
fprintf(lrs_ofp,"0 ");
}
fprintf(lrs_ofp,"0 ");
}
fprintf(lrs_ofp,"\n-1 ");
for (j=0;j<n;j++)
fprintf(lrs_ofp,"1 ");
fprintf(lrs_ofp,"0 ");
fprintf(lrs_ofp,"\nend");
fprintf(lrs_ofp,"\n");
fclose(lrs_ofp);
/* Free storage space */
for (i=0;i<m;i++) {
free(A[i]);
free(B[i]);
}
free(A);
free(B);
free(payoffs);
return(TRUE);
}
|