      program mimain
      implicit double precision (a-h,o-z)
      parameter (nwcore = 500000)
      double precision z(nwcore)
 
      integer size, dim
      parameter (size = 20, dim = 3)
      double precision lambda
      double precision Ker(size,size*dim)
      integer Y(size)
      common /c1/ lambda 
      common /c2/ Ker 
      common /c3/ Y
     
      double precision sigma
      double precision myX(size, dim)
      double precision trProb(size)
C     double estProb(size)     
C     double precision fval(size)
C     real noise(size), newY(size)
C     real newB(size,size*dim)
    
C     double precision noiseMig
C     double precision tol
C     double precision tmp
C     double precision newz(nwcore)
C     double precision gacv
C     double precision gckl
      
C     tol = 1d-10

C     generate mps file
      call genRes(size, dim)

C     generate original myX, Y array with true prob
      do 500, j = 1,1
         sigma  = 0.5 + (j-1)/1000.0
      call simuXY(sigma, myX, trProb)
C     call simuXY(myX, trProb)

C     open file to store results
C            do 400, m = 10,10
C               lambda = 2**(-6 + 0.5*m)
               lambda = 0
C               open(5, file = 'n1d2.rs0926', status = 'old',
C     $              access = 'sequential', form = 'formatted')
               call minos1(z,nwcore)
            
C     compute objective solution fval


C      open(1,file = 'sol.dat', status = 'old',
C     $     access = 'sequential', form = 'formatted')   
C      do 70, k = 1, 2*size*dim
C         write(1,*) z(k)
C 70   continue
C      close(1)


C      do 100, k = 1, size
C         fval(k) = 0
C         do 90, nn = 1, size*dim
C            fval(k) = fval(k) + Ker(k, nn)*z(nn)
C 90      continue
C 100  continue



C     open(1,file = 'estProb.dat', status = 'old',
C    $     access = 'sequential', form = 'formatted')   
C      do 110, k =1, size
C         est_prob(k) = exp(fval(k))/(1+exp(fval(k)))
C         write(1,*) estProb(k)
C 110   continue
C      close(1)


C     compute gckl
C            gckl = 0
C            do 150, k = 1, size
C               gckl = gckl + (-prob(k)*fval(k)
C     $                +log(1+exp(fval(k))))/size
C150        continue
          

C     generate noise vector
C             noiseMig = 0.01
C             do 270, k = 1, size
C                temp = rand(0)
C                if (temp .le. 0.5) then
C                   noise(k) = 0.01
C                else
C                   noise(k) = -0.01
C                endif
C 270         continue

C     perturb Y with noise
C             do 300, k = 1, size
C                newY(k) = Y(k) + noise(k)
C 300         continue

C     re-generate mps file for  newY
C             call genRes(size, dim, lambda, sigma, myX, 
C     $            newY, newB, newp)

C             do 310, k = 1, size
C                write(*,*) newp(k), newY(k)
C 310         continue

C     write results to file
C            write(5, *) lambda, sigma, gckl, 
C     $           "(", absum(1), absum(2),absum(3), absum(4),")", 
C     $           "(", nonz(1),  nonz(2), nonz(3), nonz(4), ")"




C     write results to file
C            write(5, 900) lambda, sigma, gckl, 
C     $           "(", absum(1), absum(2), absum(3),")", 
C     $           "(", nonz(1),  nonz(2), nonz(3),")"


C     write results to file
C             write(5, *) lambda, sigma, gckl, absum(1), nonz(1)
         
C 400        continue
C     write(*, *) " ok"
 500        continue
            write(*, *) " ok "
C     close(5)
         end

C 900  format (G10.3, 3X, F4.2, 3X, F8.4, 3X, A1, F7.2, 1X, 
C     $     F7.2, 1X, F7.2, A13X, A1, I3, I3, I3, A1)










