C      program main
C      integer size, dim
C      parameter (size = 40, dim = 3)
C      double precision lambda
C      double precision Ker(size,size*dim)
C      common /c1/ lambda 
C      common /c2/ Ker 
      
C      double precision sigma
C      double precision myX(size, dim)
C      double precision trProb(size)
C      integer Y(size)
C      double precision ry(size)

C      sigma = 0.25
C      call simuXY(sigma,myX,trProb,Y)
C      lambda = 1.0
C      do 5, i=1,size
C         ry(i)=dble(Y(i))
C 5    continue
C      call genRes(ry)
C      end

      subroutine genRes(yy)
      integer size,dim
      include 'param.dat'
C     integer size, dim
C     parameter (size = 10, dim = 2)
      double precision yy(size)
      double precision Ker(size, size*dim)
      double precision lambda
      common /c1/ lambda
      common /c2/ Ker
      double precision p(size*dim)

      integer nRow
      nRow = 2*size*dim


      do 50, j = 1, size*dim
         p(j) = 0 
         do 40, i = 1, size
            p(j) = p(j) - (1+yy(i))*Ker(i,j)/(2*size)
 40      continue
 50   continue

  
      open(2,file = 'lincoef.dat', status = 'old',
     $     access = 'sequential', form = 'formatted')   
      do 55, i=1, size*dim
         write(2,*) p(i)
 55   continue
      close(2)


C     in order to solve the most current problem, need to update file fort.10 every 
C     time just before solving. We could not generate the newest problem in lbs.mps, 
C     since minos only read the problem out of fort.10, rather than lbs.mps. 
C     open(1,file = 'lbs.mps', status = 'old',
      open(1,file = 'fort.10', status = 'old',
     $     access = 'sequential', form = 'formatted')
      write(1,210) "NAME","lbs"
      write(1,210) "ROWS"
      
      do 60, i = 1, nRow
         write(1,220) "G","CON",i
 60   continue

      write(1,225) "N","COST"
      write(1,230) "COLUMNS"

      do 70, i = 1, size*dim
         write(1,240) "X",i,"CON",i,1,"CON",size*dim+i,-1
         write(1,250) "X",i,"COST",p(i)
 70   continue

      do 80, i = 1, size*dim
         write(1,240) "X",size*dim+i,"CON",i,1,"CON",size*dim+i,1
         write(1,250) "X",size*dim+i,"COST",lambda
C     write(*,*) "lambda",lambda
 80   continue
      
      
      write(1,330) "RHS"
      write(1,340) "*"
      write(1,350) "*","The RHS is zero"
      write(1,340) "*"
      write(1,430) "BOUNDS"

      do 100, i = 1, size*dim
         write(1,440) "FR","BOUND01","X",i
 100  continue

      do 110, i = 1, size*dim
         write(1,450) "FX","INITIAL","X",i,"0.5"
 110  continue
      
C     do 120, i = 1, size*dim
C     write(1,450) "FX","INITIAL","X",i+size*dim,"0"
C     120  continue
      
      write(1,430) "ENDATA"
      close(1)

 210  format(A4, 10X,A)
 220  format(A2, 2X, A, I4.4)
 225  format(A2, 2X, A)
 230  format(A)
 240  format(A5, I4.4, 5X, A, I4.4, I4, 14X, A, I4.4, I5)
 250  format(A5, I4.4, A9, 2X, F13.6)
      
 330  format(A)
 340  format(A1)
 350  format(A1, A20)

 430  format(A6)
 440  format(A3,A8,A4,I4.4)
 450  format(A3,A8,A4,I4.4,5X,A)
      End























