# include # include # include # include # include "polyomino_lp_write.h" /******************************************************************************/ void polyomino_lp_write ( char *filename, char *label, int m, int n, int a[], int b[] ) /******************************************************************************/ /* Purpose: POLYOMINO_LP_WRITE writes an LP file for the polyomino problem. Licensing: This code is distributed under the GNU LGPL license. Modified: 17 May 2018 Author: John Burkardt Parameters: Input, char *FILENAME, the output filename. Input, char *LABEL, the problem title. Input, int M, the number of equations Input, int N, the number of variables. Input, int A[M*N], the coefficients. Input, int B[M], the right hand sides. */ { bool first; int i; int j; FILE *output; /* Open the file. */ output = fopen ( filename, "w" ); if ( ! output ) { fprintf ( stderr, "\n" ); fprintf ( stderr, "POLYOMINO_LP_WRITE - Error!\n" ); fprintf ( stderr, " Could not open the output file.\n" ); exit ( 1 ); } fprintf ( output, "%s\n", label ); fprintf ( output, "\n" ); fprintf ( output, "Maximize\n" ); fprintf ( output, " Obj: 0\n" ); fprintf ( output, "Subject to\n" ); for ( i = 0; i < m; i++ ) { first = true; for ( j = 0; j< n; j++ ) { if ( a[i+j*m] != 0 ) { if ( a[i+j*m] < 0 ) { fprintf ( output, " -" ); } else if ( ! first ) { fprintf ( output, " +" ); } if ( abs ( a[i+j*m] ) == 1 ) { fprintf ( output, " x%d", j + 1 ); } else { fprintf ( output, " %d x%d", abs ( a[i+j*m] ), j + 1 ); } first = false; } } fprintf ( output, " = %d\n", b[i] ); } fprintf ( output, "Binary\n" ); fprintf ( output, " " ); for ( j = 0; j < n; j++ ) { fprintf ( output, " x%d", j + 1 ); } fprintf ( output, "\n" ); fprintf ( output, "End\n" ); /* Close the file. */ fclose ( output ); return; } /******************************************************************************/ void timestamp ( ) /******************************************************************************/ /* Purpose: TIMESTAMP prints the current YMDHMS date as a time stamp. Example: 17 June 2014 09:45:54 AM Licensing: This code is distributed under the GNU LGPL license. Modified: 17 June 2014 Author: John Burkardt Parameters: None */ { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; time_t now; now = time ( NULL ); tm = localtime ( &now ); strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); printf ( "%s\n", time_buffer ); return; # undef TIME_SIZE }