function lpa_write ( filename, label, A1, b1, A2, b2 )

%*****************************************************************************80
%
%% lpa_write() writes an LP file for the approximate eternity problem.
%
%  Discussion:
%
%    This revision of lp_write() replaces the zero objective
%    function by a maximization of the sum of the z variables.
%
%    Each z variable represents the right hand side of a constraint,
%    which now becomes an inequality (less than) constraint.
%
%    The z variables are required to be binary.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    08 June 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string FILENAME, the output filename.
%
%    string LABEL, the problem title.
%
%    integer A1(M1,N), B1(M1), the first linear system.
%
%    integer A2(M2,N), B2(M2), the first linear system.
%
  [ m1, n ] = size ( A1 );
  [ m2, n ] = size ( A2 );
%
%  Open the file.
%
  output = fopen ( filename, 'wt' );

  if ( output < 0 ) 
    fprintf ( 1, '\n' );
    fprintf ( 1, 'lpa_write(): Error!\n' );
    fprintf ( 1, '  Could not open the output file.\n' );
    error ( 'lpa_write(): Error!' );
  end

  fprintf ( output, '%s\n', label );
  fprintf ( output, '\n' );
%
%  Generate the objective, to maximize z1 + z2 + ... + zm1
%
  fprintf ( output, 'Maximize\n' );
% fprintf ( output, '  Obj: ' );
% line_length = 7;
  fprintf ( output, ' ' );
  line_length = 1;
  for i = 1 : m1

    if ( 1 < i )
      byte_num = fprintf ( output, ' +' );
      line_length = line_length + byte_num;
    end

    byte_num = fprintf ( output, ' z%d', i );
    line_length = line_length + byte_num;

    if ( i == m1 )
      fprintf ( output, '\n' );
      break;
    end

    if ( 70 <= line_length )
      fprintf ( output, '\n' );
      fprintf ( output, ' ' );
      line_length = 1;
    end

  end
%
%  Constraints:
%
  fprintf ( output, 'Subject to\n' );
%
%  Equality constraints from A1ij xj = zi
%
  line_length = 0;
  for i = 1 : m1

    first = true;
    byte_num = fprintf ( output, ' ' );
    line_length = line_length + byte_num;

    for j = 1 : n

      if ( A1(i,j) ~= 0 )

        if ( A1(i,j) < 0 )
          byte_num = fprintf ( output, ' -' );
          line_length = line_length + byte_num;
        elseif ( ~ first )
          byte_num = fprintf ( output, ' +' );
          line_length = line_length + byte_num;
        end

        if ( abs ( A1(i,j) ) == 1 )
          byte_num = fprintf ( output, ' x%d', j );
          line_length = line_length + byte_num;
        else
          byte_num = fprintf ( output, ' %d x%d', abs ( A1(i,j) ), j );
          line_length = line_length + byte_num;
        end

        if ( 70 <= line_length )
          fprintf ( output, '\n' );
          fprintf ( output, ' ' );
          line_length = 1;
        end

        first = false;

      end
    end
    numbytes = fprintf ( output, ' - z%d = 0\n', i );
    line_length = 0;
  end
%
%  Counting constraints from A2ij xj <= b2i
%
  line_length = 0;
  for i = 1 : m2

    first = true;
    byte_num = fprintf ( output, ' ' );
    line_length = line_length + byte_num;

    for j = 1 : n

      if ( A2(i,j) ~= 0 )

        if ( A2(i,j) < 0 )
          byte_num = fprintf ( output, ' -' );
          line_length = line_length + byte_num;
        elseif ( ~ first )
          byte_num = fprintf ( output, ' +' );
          line_length = line_length + byte_num;
        end

        if ( abs ( A2(i,j) ) == 1 )
          byte_num = fprintf ( output, ' x%d', j );
          line_length = line_length + byte_num;
        else
          byte_num = fprintf ( output, ' %d x%d', abs ( A2(i,j) ), j );
          line_length = line_length + byte_num;
        end

        if ( 70 <= line_length )
          fprintf ( output, '\n' );
          fprintf ( output, ' ' );
          line_length = 1;
        end

        first = false;

      end
    end
    numbytes = fprintf ( output, ' <= %d\n', b2(i) );
    line_length = 0;
  end
%
%  'Binary' followed by a list of x and z variables which can only take on
%  the values 0 or 1.
%
  fprintf ( output, 'Binary\n' );
  line_length = 0;

  byte_num = fprintf ( output, ' ' );
  line_length = line_length + byte_num;

  for j = 1 : n
    byte_num = fprintf ( output, ' x%d', j );
    line_length = line_length + byte_num;
    if ( 73 <= line_length && j < n )
      fprintf ( output, '\n' );
      fprintf ( output, ' ' );
      line_length = 1;
    end
  end
  for j = 1 : m1
    byte_num = fprintf ( output, ' z%d', j );
    line_length = line_length + byte_num;
    if ( 73 <= line_length && j < n )
      fprintf ( output, '\n' );
      fprintf ( output, ' ' );
      line_length = 1;
    end
  end
  fprintf ( output, '\n' );
%
%  End of LP file.
%
  fprintf ( output, 'End\n' );
%
%  Close the file.
%
  fclose ( output );

  return
end
