2014-06-24 80 views
1

我想知道溫度和結合能對DNA分裂的影響。 我用一個簡單的一維步行來描述DNA聚合物的位置。我已經設定了它們重疊的初始條件。初始位置是一種自我避免和單向隨機遊走。該聚合物具有128個單體,每個單體的位置由陣列a [0-127]和b [0-127]給出。有兩種單體在相同位置上的能量E(我已經採用-1)。所有其他分隔距離都沒有能量。 現在我已經使用Metropolis算法來使聚合物達到平衡。 我已經隨機選擇了一個單體(256個)並翻轉了它。翻轉已被定義爲DNA分裂算法

A [1] = A [1 + 1] + A [I-1] + A [1]

這將是 'B' 而不是「一個'在第二種聚合物的情況下。 當然,如果所選擇的最終聚合物被倒裝將由

來定義[127] = 2 *一個[126] + A [127]

應當注意的是,由於翻轉位置將改變爲2,0或-2。

現在,大都會算法規定,如果由於翻轉而沒有能量變化(例如,如果已經分離的單體進一步變得更遠,或者如果分離的單體變得更近但不完全在一起),將總是允許翻蓋。 當能量變化爲負值時,也就是在翻轉兩個單體後,總是可以翻轉。 當有一個積極的能量變化即,當最初兩種單體在一起但翻轉後,他們然後分離該倒裝被接受與

函數powf(M_E,(E/T))的概率

此時T也取1。

該算法迭代很多次,直到達到平衡已經達到了結束間隔距離,即b [127] -a [127]。 爲了生成隨機數,我使用了我在代碼中定義的drand函數。由於有人告訴我這可能不是一個非常好的隨機數生成器,我還嘗試使用函數ran2,我剛剛將代碼複製到代碼中,但沒有如何工作。

無論如何,現在我的問題是平衡距離出來遠遠高於應該是。理想情況下,我被告知它應該最多爲0或者2和4。比這更不可能。但是我的代碼非常頻繁地給出像22,30等值。 有人能告訴我什麼是錯的?隨意要求進一步澄清。

#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 
#define IM1 2147483563 
#define IM2 2147483399 
#define AM (1.0/IM1) 
#define IMM1 (IM1-1) 
#define IA1 40014 
#define IA2 40692 
#define IQ1 53668 
#define IQ2 52774 
#define IR1 12211 
#define IR2 3791 
#define NTAB 32 
#define NDIV (1+IMM1/NTAB) 
#define EPS 1.2e-7 
#define RNMX (1.0-EPS) 


float drand() 
{ 
    float f, r, randmax; 
    r = rand(); 
    randmax = RAND_MAX; 
    f = r/(randmax+1); 
    return(f); 
} 

double ran2(long *idum) 
{ 
    int j; 
    long k; 
    static long idum2=123456789; 
    static long iy=0; 
    static long iv[NTAB]; 
    double temp; 

    if (*idum <= 0)   /* Initialize. */ 
    { 
     if (-(*idum) < 1) *idum=1; /*Be sure to prevent idum = 0. */ 
     else *idum = -(*idum); 
     idum2=(*idum); 
     for (j=NTAB+7; j>=0; j--) /* Load the shuffle table (after 8 warm-ups).*/ 
     { 
      k=(*idum)/IQ1; 
      *idum=IA1*(*idum-k*IQ1)-k*IR1; 
      if (*idum < 0) *idum += IM1; 
      if (j < NTAB) iv[j] = *idum; 
     } 
     iy=iv[0]; 
    } 
    k=(*idum)/IQ1; /* Start here when not initializing.*/ 
    *idum=IA1*(*idum-k*IQ1)-k*IR1; /* Compute idum=(IA1*idum) % IM1 without 
            overflows by Schrage's method. */ 
    if (*idum < 0) *idum += IM1; 
    k=idum2/IQ2; 
    idum2=IA2*(idum2-k*IQ2)-k*IR2; /* Compute idum2=(IA2*idum) % IM2 likewise. */ 
    if (idum2 < 0) idum2 += IM2; 
    j=iy/NDIV;     /* Will be in the range 0..NTAB-1. */ 
    iy=iv[j]-idum2;    /* Here idum is shuffled, idum and idum2 are 
            combined to generate output. */ 
    iv[j] = *idum; 
    if (iy < 1) iy += IMM1; 
    if ((temp=AM*iy) > RNMX) 
     return RNMX;    /* Because users don't expect endpoint values. */ 
    else return temp; 
} 


int main() 
{ 
    int a[128],b[128]; /*array defining position of polymer*/ 
    long int i, j;   /* integers defined for iteration purposes*/ 
    int r;    /* The rth random monomer of the polymer while conducting the MC algorithm*/ 
    int x;    /* The new position of the monomer if it overcomes the probability*/ 
    float E = -1;  /* Energy associated with overlapping monomers*/ 
    float T = 1;  /* Temperature*/ 
    int t;    /*separation between final monomers*/ 
    long idum = time(NULL); 
    srand (time(NULL)); /*set seed for the random number*/ 
    a[0]=0; 
    b[0]=0; 
    for (i=1; i<128; i++) /*Defining a random but overlapping initial position for the strands*/ 
    { 
     if (ran2(&idum)<0.5) 
     { 
      a[i]=a[i-1]+1; 
      b[i]=a[i]; 
     } 
     else 
     { 
      a[i]=b[i]=a[i-1]-1; 
      b[i]=a[i]; 
     } 
    } 

    /* Following is the metropolis algorithm*/ 
    for (j=1; j<1000000; j=j+1) 
    { 
     r = floor(ran2(&idum)*128); 
     if (ran2(&idum)<0.5) 
     { 
      if (r<=126) 
      { 
       x=a[r+1]+a[r-1]-a[r]; 
       if (x==b[r]) 
       { 
        a[r]=x; 
       } 
       else if (x==b[r]-2) 
       { 
        if (ran2(&idum)<powf(M_E,(E/T))) 
        { 
         a[r]=x; 
        } 
       } 
       else if (x<b[r]-2) 
       { 
        a[r]=x; 
       } 
      } 
      else if (r==127) 
      { 
       x=2*a[126]-a[127]; 
       if (x==b[127]) 
       { 
        a[127]=x; 
       } 
       else if (x==b[127]-2) 
       { 
        if (ran2(&idum)<powf(M_E,(E/T))) 
        { 
         a[127]=x; 
        } 
       } 
       else if (x<b[127]-2) 
       { 
        a[127]=x; 
       } 
      } 
     } 
     else 
     { 
      if (r<=126) 
      { 
       x=b[r+1]+b[r-1]-b[r]; 
       if (x==a[r]) 
       { 
        b[r]=x; 
       } 
       else if (x==a[r]+2) 
       { 
        if (ran2(&idum)<powf(M_E,(E/T))) 
        { 
         b[r]=x; 
        } 
       } 
       else if (x>a[r]+2) 
       { 
        b[r]=x; 
       } 
      } 
      else if (r==127) 
      { 
       x=2*b[126]-b[127]; 
       if (x==a[127]) 
       { 
        b[127]=x; 
       } 
       else if (x==a[127]+2) 
       { 
        if (ran2(&idum)<powf(M_E,(E/T))) 
        { 
         b[127]=x; 
        } 
       } 
       else if (x>a[127]+2) 
       { 
        b[127]=x; 
       } 
      } 
     } 
     t = b[127]-a[127]; 
     if (j%(25600)==0) 
     { 
      printf("%d\n", t); 
     } 
    } 
    printf("%f\n", powf(M_E,(E/T))); 
    return 0; 
} 
+0

所以你問我們分析你的算法,然後找到它的問題?我認爲這個問題太多了,因爲你的算法非常廣泛。你的代碼看起來非常好/乾淨;做得好!你不能放大,或削減部分處理和檢查中間結果? –

+0

是的。我確實意識到代碼很長,但在我發佈之前儘量找到問題。 – aniruud

+0

我確實嘗試隔離某些部件,以便找到問題。隨機數發生器似乎沒有任何問題。初始設置聚合物也是如此。問題必須在大都會算法中。當迭代次數較低時,結果更加明智。但是當我們增加迭代次數時,儘管事實上它們應該在某個點達到平衡,並且在後續迭代之後才停留在那裏,但值會變得更加怪異和怪異。 – aniruud

回答

1

我已經意識到我一直在做什麼錯誤。 首先有一個冗餘b [i] = a [i]。 可能不是一個錯誤,但我相信一個不好的編碼習慣。其次。我的朋友指出,我的算法稱爲r = 0的值,而我的代碼使用[r-1],然後發出隨機數。由於兩種聚合物的第一單體保持固定在0,所以我實際上必須確保r不佔0的值。

最後也是最重要的問題,是文中陳述

else if (x==a[127]+2) 
      { 
       if (ran2(&idum)<powf(M_E,(E/T))) 
       { 
        b[127]=x; 
       } 
      } 

else if (x==b[r]-2) 
      { 
       if (ran2(&idum)<powf(M_E,(E/T))) 
       { 
        a[r]=x; 
       } 

而且其中r = 127,但你明白了吧。實際上我認爲單體只能從與其他聚合物相鄰的位置翻轉,即a [r] = b [r]到新的位置x。但經過多次迭代之後,還會出現a [r] + 4 = b [r]的情況,並且它會進入x = b [r] -2的新位置x。 而這個翻蓋總是會被接受,而不是以一定的概率。 因此,代碼的新的生產線應該是

else if (x==b[r]-2) 
      { 
       if (a[r]==b[r]) 
       { 
        if (drand()<powf(M_E,(E/T))) 
       { 
        a[r]=x; 
       } 
       } 
       else 
       { 
        a[r]=x; 
       } 
      } 

,類似的還有另外3例。這考慮到了兩種可能性。代碼現在工作正常。 感謝所有那些經歷了這麼長的問題而痛苦的人。