You are here:

C++/convert C++ codes to Fortran codes

Advertisement


Question
QUESTION: Hello Dear Zlatko;
Is it possible to convert some C + + codes to FORTRAN codes? Is there any way?

sincerely
Amir

ANSWER: I do not know of any commercial program that will translate FORTRAN to C, but I've never searched for one either. From past experience, I know that FORTRAN is fairly easy to parse using PERL. If you know PERL, it is probably possible to write a PERL program to do the translation but it would be a big job. Instead of translating, you should consider trying to make the FORTRAN code into a DLL (on windows) or shared library (if using UNIX). Then you can call the FORTRAN functions directly from C. No translation would be required.

There are 2 ways to call a FORTRAN function from C. One is to link the functions into the executable. The other is to open the DLL or shared library using C functions, and search for the desired function by name in the C code. It has been a while since I've done either, so you may need to do some experimenting to learn the details. Parameter passing is one detail that I'm not sure about right now.

Sorry that I cannot give you more details. I don't have access to a FORTRAN compiler.

Best regards
Zlatko

---------- FOLLOW-UP ----------

QUESTION: Thank you Zlatko but I think you are in a misunderstanding! I'm searching for a way to convert C + + codes to FORTRAN codes not vice versa! Of course it may be similar to that you say.
And another request:  My C + + codes is crashes during running. I searched and understood that some times the program can not assign any value to some integer variables (for example where the program must search and assign a specific index i to variable i0). Nonetheless I can't found any reasonable explanation for this treatment of the program. Furthermore I can't assign any initial value (like 0) to the variables because if the program won't assign the appropriate value to them, an initial value may be an incorrect value for them. Moreover using of "cout" after some commands which caused to assign wrong values, some times causes the changing in program treatment! For example it may forces the program to assign true value to the variable but definitely the program will be crash at another command similar the first one. Unfortunately the program manner during running is completely unpredictable. What do you think about this problem?
This is my program(sorry if I directly attached a too long codes to this post):

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>

//..............................................................................
const int m=4,n=7,kl=6,hl=5,   horizon=60,   sum_ope=84;
const int hj[n]={4,5,5,3,4,5,3};
const int kj[n]={6,3,3,3,2,2,1};
const int mac_ijh[m][n][hl]={{{1,0,0,1},{1,0,0,1,0},{1,0,0,1,0},{1,0,0},{1,0,0,1},{1,0,0,1,0},{1,0,0}},
         {{1,0,1,0},{1,0,1,0,1},{1,0,1,0,1},{1,0,1},{1,0,1,0},{1,0,1,0,1},{1,0,1}},
         {{0,1,0,0},{0,1,0,0,1},{0,1,0,0,1},{0,1,0},{0,1,0,0},{0,1,0,0,1},{0,1,0}},
         {{0,1,0,1},{0,1,0,1,0},{0,1,0,1,0},{0,1,0},{0,1,0,1},{0,1,0,1,0},{0,1,0}}};       //machines can used for job j
const float pt_ijh[m][n][hl]={{{2  ,0  ,0  ,1.5},{2  ,0  ,0  ,1.5,0 },{2  ,0  ,0  ,1.5,0},{2  ,0  ,0  },{2  ,0  ,0  ,1.5},{2  ,0  ,0  ,1.5,0 },{2  ,0  ,0  }},
         {{1.5,0  ,1.5,0  },{1.5,0  ,1.5,0  ,2 },{1.5,0  ,1.5,0  ,2},{1.5,0  ,1.5},{1.5,0  ,1.5,0  },{1.5,0  ,1.5,0  ,2 },{1.5,0  ,1.5}},
         {{0  ,2.5,0  ,0  },{0  ,2.5,0  ,0  ,1 },{0  ,2.5,0  ,0  ,1},{0  ,2.5,0  },{0  ,2.5,0  ,0  },{0  ,2.5,0  ,0  ,1 },{0  ,2.5,0  }},
         {{0  ,2.5,0  ,2  },{0  ,2.5,0  ,2  ,0 },{0  ,2.5,0  ,2  ,0},{0  ,2.5,0  },{0  ,2.5,0  ,2  },{0  ,2.5,0  ,2  ,0 },{0  ,2.5,0  }}};
const float dead_jk[n][kl]={{10 ,20,30,40,50,60},
         {20,40,60, 0, 0, 0},
         {40,40,60, 0, 0, 0},
         {16,40,60, 0, 0, 0},
         {30,60, 0, 0, 0, 0},
         {30,60, 0, 0, 0, 0},
         {60, 0, 0, 0, 0, 0}};          //Dead Line
const float delay_c_j[n]={2,1,2,3,4,3,4};
const float hold_c_jh[n][hl]={{0.1,0.2,0.3,0.3,0},{0.1,0.2,0.3,0.3,0.4},{0.1,0.2,0.3,0.3,0.4},{0.1,0.2,0.3,0,0},{0.1,0.2,0.3,0.3,0},{0.1,0.2,0.3,0.3,0.4},{0.1,0.2,0.3,0,0}};
const float setup_c_ijh[m][n][hl]={{{1,0,0,1},{1,0,0,1,0},{1,0,0,1,0},{1,0,0},{1,0,0,1},{1,0,0,1,0},{1,0,0}},
         {{2,0,2,0},{2,0,2,0,2},{2,0,2,0,2},{2,0,2},{2,0,2,0},{2,0,2,0,2},{2,0,2}},
         {{0,2,0,0},{0,2,0,0,2},{0,2,0,0,2},{0,2,0},{0,2,0,0},{0,2,0,0,2},{0,2,0}},
         {{0,3,0,3},{0,3,0,3,0},{0,3,0,3,0},{0,3,0},{0,3,0,3},{0,3,0,3,0},{0,3,0}}};

const float setup_t_ijh[m][n][hl]={{{0.1,0,0,0.1},{0.1,0,0,0.1,0  },{0.1,0,0,0.1,0  },{0.1,0,0  },{0.1,0,0,0.1},{0.1,0,0,0.1,0  },{0.1,0,0  }},
         {{0.2,0,2,0  },{0.2,0,0.2,0,0.2},{0.2,0,0.2,0,0.2},{0.2,0,0.2},{0.2,0,0.2,0},{0.2,0,0.2,0,0.2},{0.2,0,0.2}},
         {{0,0.2,0,0  },{0,0.2,0,0,0.2  },{0,0.2,0,0,0.2  },{0,0.2,0  },{0,0.2,0,0  },{0,0.2,0,0,0.2  },{0,0.2,0  }},
         {{0,0.3,0,0.3},{0,0.3,0,0.3,0  },{0,0.3,0,0.3,0  },{0,0.3,0  },{0,0.3,0,0.3},{0,0.3,0,0.3,0  },{0,0.3,0  }}};

const int l_max=20;
const float cool_r=0.005;
const int p_mut_asgn_r=100, p_mut_asgn_d=100, p_mut_seq_r=100, p_mut_seq_d=100, p_mut_seq_s=100 ;


// .............................................................................
int impossible_counter=0;
int *mut_asgn_r(int indiv[5][sum_ope]);
int *mut_asgn_d(int indiv[5][sum_ope]);
int *mut_seq_r(int indiv[5][sum_ope]);
int *mut_seq_d(int indiv[5][sum_ope]);
int *mut_seq_s(int indiv[5][sum_ope]);
float *ffcalc(int indiv[5][sum_ope]);


typedef unsigned __int64 PC_t;
PC_t PerfCounter(void);
double ElapsedMs(PC_t earlier, PC_t later);

//------------------------------------------------------------------------------

int main()
{
PC_t start;
PC_t end;
double ms;

float f0,f1,f00,df,ddf,  r,rr,   min,sum_rtime,   tc,tf;
int i,j,k,h,q,u,w,v,i0,j0,k0,h0,v0,u0,l,counter,sequ,g,impos=1;
double pow=0,acpt=0;
int asgn0[4][sum_ope]={0},seq0[4][sum_ope]={0};
float t4m[sum_ope][m+3]={0},ps[n][kl][hl]={0},srpt[n][kl][hl]={0};          //for initial solution
int *mut_1d;
float *ff_1d;
int indiv0[5][sum_ope]={0},indiv1[5][sum_ope]={0},indiv00[5][sum_ope]={0};    // indiv 5 satr darad: ff jodagane sabt mishavad


         //*************  Generate   asgn0 & seq0   *************
start = PerfCounter();

v=0;
for(j=0;j<n;j++)
  for(k=0;k<kj[j];k++)
     for(h=0;h<hj[j];h++){
        t4m[v][m]=j;
        t4m[v][m+1]=k;
        t4m[v][m+2]=h;
        asgn0[0][v]=j;
        asgn0[1][v]=k;
        asgn0[2][v]=h;
        v++;
        }
         //  fill the pt_ijh
for(v=0;v<sum_ope;v++)
  for(i=0;i<m;i++){
     j=t4m[v][m];
     h=t4m[v][m+2];
     t4m[v][i]=pt_ijh[i][j][h];
     if(t4m[v][i]!=0)    t4m[v][i]+=setup_t_ijh[i][j][h];
     }
for(v=0;v<sum_ope;v++)
  for(i=0;i<m;i++)
     if(t4m[v][i]==0)    t4m[v][i]=-1;

         // find the minimums & asgn the operations to the machines
for(u=0;u<sum_ope;u++){
  for(i=0;i<m;i++)          // find a number as min
     for(v=0;v<sum_ope;v++)
        if(t4m[v][i]!=-1){
         min=t4m[v][i];
         break;
         }
  for(v=0;v<sum_ope;v++)
     for(i=0;i<m;i++)
        if(t4m[v][i]<=min && t4m[v][i]!=-1){
         v0=v;
         i0=i;
         min=t4m[v][i];
         }          //  cout<<"\nv0= "<<v0;
  asgn0[3][v0]=i0;          //  cout<<"\nasgn0="<<asgn0[3][u0];

  for(v=0;v<sum_ope;v++)
     if(v!=v0 && t4m[v][i0]!=-1)    t4m[v][i0]+=t4m[v0][i0];
  for(i=0;i<m;i++)
     t4m[v0][i]=-1;
         /*     cout<<"\n\n";
         cout<<v0<<i0<<"\n";
         for(v=0;v<sum_ope;v++){
         for(i=0;i<m+3;i++)     {
         cout<<t4m[v][i]<<"   ";
         }cout<<"\n";}          */
  }
         cout<<"\nasgn0:\n";
         for(u=0;u<sum_ope;u++)
         cout<<asgn0[3][u]<<"  ";
         //  seq 0
for(j=0;j<n;j++){
  v=0;
  if(j>0)
     for(g=0;g<j;g++)
        v+=kj[g]*hj[g];
  for(k=0;k<kj[j];k++)
     for(h=0;h<hj[j];h++){
        q=v+k*hj[j]+h;
        i0=asgn0[3][q];
        ps[j][k][h]=pt_ijh[i0][j][h]+setup_t_ijh[i0][j][h];
        }
  }

for(j=0;j<n;j++)
  for(k=0;k<kj[j];k++)
     for(h=0;h<hj[j];h++){
        sum_rtime=0;
        for(g=h;g<hj[j];g++)
         sum_rtime+=ps[j][k][g];
        srpt[j][k][h]=(dead_jk[j][k]-sum_rtime)/sum_rtime;
     }
         /*          cout<<'\n'<<'\n'<<"SRPT:"<<'\n';
         for(j=0;j<n;j++){
         for(k=0;k<kj[j];k++){
         for(h=0;h<hj[j];h++){
         cout<<srpt[j][k][h]<<"   ";
         }cout<<'\n';}cout<<'\n';}          */
sequ=1;
choose_min_srpt:
min=1000;
for(j=0;j<n;j++)
  for(k=0;k<kj[j];k++)
     for(h=0;h<hj[j];h++)
        if(srpt[j][k][h]<min){
         min=srpt[j][k][h];
         j0=j;
         k0=k;
         h0=h;
        }
seq0[0][sequ-1]=j0;
seq0[1][sequ-1]=k0;
seq0[2][sequ-1]=h0;
seq0[3][sequ-1]=sequ;

if(sequ<sum_ope){
  srpt[j0][k0][h0]=1000;
  sequ++;
  goto choose_min_srpt;
  }
         cout<<'\n'<<'\n'<<"seq0:"<<'\n';
         for(w=0;w<4;w++){
         for(v=0;v<sum_ope;v++){
         cout<<seq0[w][v]<<"  ";
         }cout<<'\n';}

         //*************  End of initial  **************

for(w=0;w<5;w++)
  for(u=0;u<sum_ope;u++){
     indiv0[0][u]=seq0[0][u];
     indiv0[1][u]=seq0[1][u];
     indiv0[2][u]=seq0[2][u];
     indiv0[3][u]=u+1;
     for(v=0;v<sum_ope;v++)
        if(asgn0[0][v]==indiv0[0][u] && asgn0[1][v]==indiv0[1][u] && asgn0[2][v]==indiv0[2][u]){
         indiv0[4][u]=asgn0[3][v];
         break;
         }
     }

ff_1d=ffcalc(indiv0);
f0=ff_1d[0];
delete[] ff_1d;
         cout<<"\nf0="<<f0;
         tc=m*f0;
         tf=tc/1000;
f00=f0;
for(w=0;w<5;w++)
  for(u=0;u<sum_ope;u++)
     indiv00[w][u]=indiv0[w][u];

counter=0;
srand(time(NULL));

generate_x1:
         //  Generate X1
counter+=1;

r=rand()%1000;
if(r<p_mut_asgn_r)          mut_1d=mut_asgn_r(indiv0);
else if(r<p_mut_asgn_r+p_mut_asgn_d )          mut_1d=mut_asgn_d(indiv0);
else if(r<p_mut_asgn_r+p_mut_asgn_d+p_mut_seq_r)          mut_1d=mut_seq_r(indiv0);
else if(r<p_mut_asgn_r+p_mut_asgn_d+p_mut_seq_r+p_mut_seq_d)      mut_1d=mut_seq_d(indiv0);
else          mut_1d=mut_seq_s(indiv0);

q=0;
for(w=0;w<5;w++)
  for(u=0;u<sum_ope;u++){
     indiv1[w][u]=mut_1d[q];
     q++;
     }
delete[] mut_1d;

         /*   cout<<'\n'<<"    indiv 1:"<<'\n';
         for(w=0;w<5;w++){
         for(u=0;u<sum_ope;u++){
         cout<<indiv1[w][u]<<" ";
         }cout<<'\n';}          */
ff_1d=ffcalc(indiv1);
f1=ff_1d[0];
impos=ff_1d[7*sum_ope+7]<impos ? ff_1d[7*sum_ope+7]:impos;
delete[] ff_1d;

df=f1-f0;
if(df<=0){
  f0=f1;
     for(w=0;w<5;w++)
        for(u=0;u<sum_ope;u++)
         indiv0[w][u]=indiv1[w][u];

  ddf=f1-f00;
  if(ddf<=0){
     f00=f1;
     for(w=0;w<5;w++)
        for(u=0;u<sum_ope;u++)
         indiv00[w][u]=indiv1[w][u];
     }
  l+=1;
  }
else{
  r=rand()%1000;
  rr=r/1000;
  pow=-(df/tc);
  acpt=exp(pow);
  if(rr<acpt){
     f0=f1;
     for(w=0;w<5;w++)
        for(u=0;u<sum_ope;u++)
         indiv0[w][u]=indiv1[w][u];
     l+=1;
  }}
if(l<=l_max)
  goto generate_x1;
else{
  tc*=(1-cool_r);
  l=0;
  if(tc>=tf)
     goto generate_x1;
}

         cout<<'\n'<<'\n'<<"+++ Final solution +++"<<'\n';

         cout<<"f* is : "<<f00<<'\n';
         cout<<"\nindiv* :"<<'\n';
         for(w=0;w<5;w++){
         for(u=0;u<sum_ope;u++){
         cout<<indiv00[w][u]<<" ";
         }cout<<'\n';}



         cout<<'\n'<<"Counter= "<<counter;

end = PerfCounter();
ms = ElapsedMs(start, end);
cout <<'\n'<< "CPU Time " << ms << " sec.\n\n";
cout<<"f* is : "<<f00<<'\n';

         cout<<"\n**************************************";
ff_1d=ffcalc(indiv00);
cout<<"\n ts:\n";
for(q=7+5*sum_ope;q<7+6*sum_ope;q++){
  cout<<ff_1d[q]<<" ";
  }
cout<<"\n tf:\n";
for(q=7+6*sum_ope;q<7+7*sum_ope;q++){
  cout<<ff_1d[q]<<" ";
  }
cout<<"\n     d_cost=     "<<ff_1d[1];
cout<<"\n     s_cost=     "<<ff_1d[2];
cout<<"\n     hfp_cost=   "<<ff_1d[3];
cout<<"\n     hwip_cost=  "<<ff_1d[4];
cout<<"\n     total_cost= "<<ff_1d[0];

if(impos==1)   cout<<"\n\n     *** Can not schedule this problem in determined horizon! ***";

getch();
return 0;
}
//******************************************************************************
//******************************************************************************
//*************************     fitness  function    ***************************
float *ffcalc(int indiv[5][sum_ope])
{
float *result=new float[8+sum_ope*7];

int i,j,k,h,q,v,u,w,  u1,   impos=0,  max_dcost_j,max_dcost_k;
int z[sum_ope]={0};
float ps_jkh[n][kl][hl]={0},t_mac[m]={0},ts[sum_ope]={0},tf[sum_ope]={0};
float dt_jk[n][kl]={0},rt_jk[n][kl]={0};
float cost_d_jk[n][kl]={0},cost_hfp_jk[n][kl]={0},cost_hwip_jkh[n][kl][hl]={0},cost_s_jkh[n][kl][hl]={0};
float max=0,temp1=0,   cost_d=0,cost_hfp=0,cost_hwip=0,cost_s=0,total_cost=0;

         /*    cout<<"\n\nindiv:\n";
         for(w=0;w<5;w++){
         for(u=0;u<sum_ope;u++){
         cout<<indiv[w][u]<<" ";
         }cout<<'\n';}         getch();     */

for(u=0;u<sum_ope;u++)    z[u]=1;
for(u=1;u<sum_ope;u++)
  for(v=u-1;v>=0;v--){
      if(indiv[0][v]==indiv[0][u] && indiv[2][v]==indiv[2][u] && indiv[4][v]==indiv[4][u])    z[u]=0;
     else if(indiv[4][v]==indiv[4][u])   break;
     }

for(u=0;u<sum_ope;u++){          //ps_jkh
  j=indiv[0][u];
  k=indiv[1][u];
  h=indiv[2][u];
  i=indiv[4][u];
  ps_jkh[j][k][h]=pt_ijh[i][j][h]+z[u]*setup_t_ijh[i][j][h];
  }
         /*          cout<<"\nps"<<'\n';
         for (j=0;j<n;j++){
         for (k=0;k<kj[j];k++){
         for (h=0;h<hj[j];h++){
         cout<<ps_jkh[j][k][h]<<"  ";
         }cout<<'\n';}cout<<'\n';}         */

         // Forward
for(u=0;u<sum_ope;u++){
  j=indiv[0][u];
  k=indiv[1][u];
  h=indiv[2][u];
  i=indiv[4][u];
  if(h==0)
     ts[u]=t_mac[i];
  else{
     for(v=0;v<u;v++)
        if(indiv[0][v]==j && indiv[1][v]==k && indiv[2][v]==h-1){
         u1=v;
         break;
         }
     ts[u]=t_mac[i]>tf[u1] ? t_mac[i]:tf[u1];
     }
  tf[u]=ts[u]+ps_jkh[j][k][h];
  t_mac[i]=tf[u];
  }
         /*       cout<<"ts: \n";
         for(u=0;u<sum_ope;u++)
         cout<<ts[u]<<"  ";
         cout<<"\ntf: \n";
         for(u=0;u<sum_ope;u++)
         cout<<tf[u]<<"  ";     */
for(i=0;i<m;i++)
  if(t_mac[i]>horizon){          impos=1;   goto after_backward;}

impossible_counter=0;
         // Backward
for(i=0;i<m;i++)
  t_mac[i]=horizon;
for(u=sum_ope-1;u>=0;u--){
  j=indiv[0][u];
  k=indiv[1][u];
  h=indiv[2][u];
  i=indiv[4][u];
  if(h==hj[j]-1){
     temp1=min(dead_jk[j][k],t_mac[i]);
     if(temp1>tf[u])      tf[u]=temp1;
     }
  else{
     for(v=u+1;v<sum_ope;v++)
        if(indiv[0][v]==j && indiv[1][v]==k && indiv[2][v]==h+1)      u1=v;
     temp1=min(ts[u1],t_mac[i]);
     if(temp1>tf[u])      tf[u]=temp1;
     }
  ts[u]=tf[u]-ps_jkh[j][k][h];
  t_mac[i]=ts[u];
  }
         /*        cout<<"\n\n\nts: \n";
         for(u=0;u<sum_ope;u++)
         cout<<ts[u]<<"  ";
         cout<<"\ntf: \n";
         for(u=0;u<sum_ope;u++)
         cout<<tf[u]<<"  ";          getch();    */
after_backward:

for(u=0;u<sum_ope;u++){
  j=indiv[0][u];
  k=indiv[1][u];
  h=indiv[2][u];
  if(h==hj[j]-1){
     if(dead_jk[j][k]-tf[u]<0)      dt_jk[j][k]=tf[u]-dead_jk[j][k];
     else          rt_jk[j][k]=dead_jk[j][k]-tf[u];
     }
  }
         /*     cout<<'\n';
         for(j=0;j<n;j++)      {
         for(k=0;k<kj[j];k++){
         cout<<dt_jk[j][k]<<" ";
         }cout<<'\n';}
         cout<<'\n';
         for(j=0;j<n;j++)      {
         for(k=0;k<kj[j];k++){
         cout<<rt_jk[j][k]<<" ";
         }cout<<'\n';}          */

         //obj-func
for(j=0;j<n;j++)
  for(k=0;k<kj[j];k++){
     if(k==kj[j]-1)      cost_d_jk[j][k]=dt_jk[j][k]*delay_c_j[j]*100;
     else          cost_d_jk[j][k]=dt_jk[j][k]*delay_c_j[j];
     }
         for(j=0;j<n;j++)
         for(k=0;k<kj[j];k++)
         cost_d+=cost_d_jk[j][k];

         max=0;
         for(j=0;j<n;j++)
         for(k=0;k<kj[j];k++)
         if(cost_d_jk[j][k]>=max){
         max=cost_d_jk[j][k];
         max_dcost_j=j;
         max_dcost_k=k;
         }
         
for(j=0;j<n;j++)
  for(k=0;k<kj[j];k++)
     cost_hfp_jk[j][k]=rt_jk[j][k]*hold_c_jh[j][hj[j]-1];

         for(j=0;j<n;j++)
         for(k=0;k<kj[j];k++)
         cost_hfp+=cost_hfp_jk[j][k];

for(u=0;u<sum_ope;u++){
  j=indiv[0][u];
  k=indiv[1][u];
  h=indiv[2][u];
  if(h==hj[j]-1)         continue;
  for(v=u+1;v<sum_ope;v++)
     if(indiv[0][v]==j && indiv[1][v]==k && indiv[2][v]==h+1){
     u1=v;
     break;
     }
  cost_hwip_jkh[j][k][h]=(ts[u1]-tf[u])*hold_c_jh[j][h];
  }
         for(j=0;j<n;j++)
         for(k=0;k<kj[j];k++)
         for(h=0;h<hj[j]-1;h++)
         cost_hwip+=cost_hwip_jkh[j][k][h];

for(u=0;u<sum_ope;u++){
  j=indiv[0][u];
  k=indiv[1][u];
  h=indiv[2][u];
  i=indiv[4][u];
  cost_s_jkh[j][k][h]=z[u]*setup_c_ijh[i][j][h];
  }
         for(j=0;j<n;j++)
         for(k=0;k<kj[j];k++)
         for(h=0;h<hj[j];h++)
         cost_s+=cost_s_jkh[j][k][h];



         total_cost = cost_d + cost_s + cost_hfp + cost_hwip ;

         /*      cout<<'\n'<<"cost_d:    "<<cost_d;
         cout<<'\n'<<"cost_s:    "<<cost_s;
         cout<<'\n'<<"cost_hfp:  "<<cost_hfp;
         cout<<'\n'<<"cost_hwip: "<<cost_hwip;
         cout<<'\n'<<"total cost is: "<<total_cost;    */

result[0]=total_cost;
result[1]=cost_d;
result[2]=cost_s;
result[3]=cost_hfp;
result[4]=cost_hwip;
result[5]=max_dcost_j;
result[6]=max_dcost_k;
result[7*sum_ope+7]=impos;

q=7;
for(w=0;w<5;w++)
  for(u=0;u<sum_ope;u++){
     result[q]=indiv[w][u];
     q++;
     }
for(u=0;u<sum_ope;u++){
  result[q]=ts[u];
  q++;
  }
for(u=0;u<sum_ope;u++){
  result[q]=tf[u];
  q++;
  }

return result;
}
//*************************  END of   Fitness  function ************************
//***************************   Mutation_asgn_r  *******************************
int *mut_asgn_r(int indiv[5][sum_ope])
{
int *result=new int[5*sum_ope];
int i,u,v,w,q,  i0,j0,k0,h0,u0,   i1,   temp1=0;
float r,nr,ls=0;
float *ff_1d;

select_a_gen:
  ls=0;
  nr=1000/sum_ope;
  r=rand()%1000;
  for(u=0;u<sum_ope;u++){
     if(ls>r)   break;
     u0=u;
     ls+=nr;
     }
  j0=indiv[0][u0];
  k0=indiv[1][u0];
  h0=indiv[2][u0];
  i0=indiv[4][u0];
  temp1=0;
  for(i=0;i<m;i++)
     if(mac_ijh[i][j0][h0]==1)    temp1++;
  if(temp1==1)    goto select_a_gen;
  temp1=0;          //   select another i for j0,k0,h0
  while(temp1==0){
     ls=0;
     nr=1000/m;
     r=rand()%1000;
     for(i=0;i<m;i++){
        if(ls>r)  break;
        i1=i;
        ls+=nr;
        }
     if(mac_ijh[i1][j0][h0]==1 && i1!=i0)    temp1=1;
     }
  indiv[4][u0]=i1;
  ff_1d=ffcalc(indiv);

         /*      cout<<'\n';
         for(w=0;w<5;w++){
         for(u=0;u<sum_ope;u++){
         cout<<indiv[w][u]<<"  ";
         }cout<<"\n";}          */
q=0;
for(w=0;w<5;w++)
  for(v=0;v<sum_ope;v++){
     result[q]=indiv[w][v];
     q++;
  }
return result;
}
//************************  End of   Mutation_asgn_r  **************************
//***************************   Mutation_asgn_d  *******************************
int *mut_asgn_d(int indiv[5][sum_ope])
{
int *result=new int[5*sum_ope];
int i,h,u,v,w,q,  i0,j0,k0,h0,u0,   i1,   temp1=0,temp2=0;
float r,nr,ls=0;
float *ff_1d;

ff_1d=ffcalc(indiv);
j0=ff_1d[5];
k0=ff_1d[6];
delete[] ff_1d;

temp2=0;          // if any h of the JK has more than one assignable machine?
for(h=0;h<hj[j0];h++){
  temp1=0;
  for(i=0;i<m;i++)
     if(mac_ijh[i][j0][h]==1)      temp1++;
  if(temp1>1)        temp2=1;
  }
if(temp2==0)  goto end;
         // select an operation of JK
select_a_h0:
ls=0;
nr=1000/hj[j0];
r=rand()%1000;
for(h=0;h<hj[j0];h++){
  if(ls>r)      break;
  h0=h;
  ls+=nr;
  }
temp1=0;          // If the sected operation has more than 1 assignable machine?
for(i=0;i<m;i++)
  if(mac_ijh[i][j0][h0]==1)      temp1++;
if(temp1==1)     goto select_a_h0;

for(u=0;u<sum_ope;u++)
  if(indiv[0][u]==j0 && indiv[1][u]==k0 && indiv[2][u]==h0){
     u0=u;
     i0=indiv[4][u];
     break;
     }

select_i1:
ls=0;
nr=1000/m;
r=rand()%1000;
for(i=0;i<m;i++){
  if(ls>r)   break;
  i1=i;
  ls+=nr;
  }
if(mac_ijh[i1][j0][h0]==0 || i1==i0)    goto select_i1;
indiv[4][u0]=i1;
         //  cout<<"u0,i0,i1 : "<<u0<<i0<<i1;
end:
q=0;
for(w=0;w<5;w++)
  for(v=0;v<sum_ope;v++){
     result[q]=indiv[w][v];
     q++;
     }
return result;
}
//***********************  End of   Mutation_asgn_d  ***************************
//***************************   Mutation_seq_r  *******************************
int *mut_seq_r(int indiv[5][sum_ope])
{
int *result=new int[5*sum_ope];
int u,w,q,  u0=0,    temp1=0;
float r,nr,ls=0;


select_u0:
ls=0;
nr=1000/(sum_ope-1);
r=rand()%1000;
for(u=1;u<sum_ope;u++){
  if(ls>r)   break;
  u0=u;
  ls+=nr;
  }
if(indiv[0][u0]==indiv[0][u0-1] && indiv[1][u0]==indiv[1][u0-1])   goto select_u0;
if(indiv[0][u0]==indiv[0][u0-1] && indiv[2][u0]==indiv[2][u0-1])   goto select_u0;

for(w=0;w<5;w++){
  if(w==3)      continue;
  temp1=indiv[w][u0-1];
  indiv[w][u0-1]=indiv[w][u0];
  indiv[w][u0]=temp1;
  }
         //   cout<<"\nu0: "<<u0;
q=0;
for(w=0;w<5;w++)
  for(u=0;u<sum_ope;u++){
     result[q]=indiv[w][u];
     q++;
     }
return result;
}
//***********************  End of   Mutation_seq_r  ***************************
//***************************   Mutation_seq_d  *******************************
int *mut_seq_d(int indiv[5][sum_ope])
{
int *result=new int[5*sum_ope];
int h,u=0,w,q,  j0,k0,h0,a0,    counter,temp1=0;
float r,nr,ls=0;
float *ff_1d;

ff_1d=ffcalc(indiv);
j0=ff_1d[5];
k0=ff_1d[6];
delete[] ff_1d;

counter=0;
select_h0:
counter++;
if(counter==3*hj[j0])   goto end;
ls=0;
nr=1000/hj[j0];
r=rand()%1000;
for(h=0;h<hj[j0];h++){
  if(ls>r)   break;
  h0=h;
  ls+=nr;
  }          //  cout<<"\nj0,k0,h0: "<<j0<<k0<<h0;
for(u=0;u<sum_ope;u++)
  if(indiv[0][u]==j0 && indiv[1][u]==k0 && indiv[2][u]==h0){
     a0=u;
     break;
     }          //cout<<"\n a0"<<a0;
if(a0==0)        goto select_h0;
if(indiv[0][a0]==indiv[0][a0-1] && indiv[1][a0]==indiv[1][a0-1])      goto select_h0;
if(indiv[0][a0]==indiv[0][a0-1] && indiv[2][a0]==indiv[2][a0-1])      goto select_h0;

for(w=0;w<5;w++){
  if(w==3)      continue;
  temp1=indiv[w][a0-1];
  indiv[w][a0-1]=indiv[w][a0];
  indiv[w][a0]=temp1;
  }
         //  cout<<"u0: "<<u0;

end:
q=0;
for(w=0;w<5;w++)
  for(u=0;u<sum_ope;u++){
     result[q]=indiv[w][u];
     q++;
     }
return result;
}
//************************  End of   Mutation_seq_d  ***************************
//***************************   Mutation_seq_s  ********************************
int *mut_seq_s(int indiv[5][sum_ope])
{
int *result=new int[5*sum_ope];
int j,k,h,u,w,q,  j0,k0,h0,u0=0,u1,   temp1=0;
float r,nr,ls=0;
         /*    cout<<"\nindiv";
         for(w=0;w<5;w++){
         for(u=0;u<sum_ope;u++){
         cout<<indiv[w][u]<<" ";
         }cout<<'\n';}          */
select_j0:
ls=0;      nr=1000/n;      r=rand()%1000;
for(j=0;j<n;j++){
  if(ls>r)   break;
  j0=j;
  ls+=nr;
  }
if(kj[j0]==1)      goto select_j0;

ls=0;      nr=1000/hj[j0];      r=rand()%1000;
for(h=0;h<hj[j0];h++){
  if(ls>r)   break;
  h0=h;
  ls+=nr;
  }
ls=0;      nr=1000/(kj[j0]-1);      r=rand()%1000;
for(k=1;k<kj[j0];k++){
  if(ls>r)   break;
  k0=k;
  ls+=nr;
  }
         //  cout<<"\n j0,k0,h0 , kj: "<<j0<<k0<<h0<<" "<<kj[j0];
for(u=0;u<sum_ope;u++)
  if(indiv[0][u]==j0 && indiv[1][u]==k0 && indiv[2][u]==h0){
     u1=u;
     break;
     }
for(u=0;u<sum_ope;u++)
  if(indiv[0][u]==j0 && indiv[1][u]==k0-1 && indiv[2][u]==h0){
     u0=u;
     break;
     }          // cout<<"\tu0 "<<u0;
if(u1==u0+1)  goto end;
for(u=u0+1;u<u1;u++)          // infeasibility
  if(indiv[0][u]==j0 && indiv[1][u]==k0 && indiv[2][u]<h0)   goto end;


for(w=0;w<5;w++){
  if(w==3)   continue;
  temp1=indiv[w][u1];          
  for(u=u1;u>u0+1;u--)
     indiv[w][u]=indiv[w][u-1];
  indiv[w][u0+1]=temp1;
  }
indiv[4][u0+1]=indiv[4][u0];

         // cout<<"u0,u1: "<<u0<<u1;
end:
q=0;
for(w=0;w<5;w++)
  for(u=0;u<sum_ope;u++){
     result[q]=indiv[w][u];
     q++;
     }
return result;
}
//**************************  End of   Mutation_seq_s  *****************************


PC_t PerfCounterFreq(void)
{
static PC_t result = 0;
if (result == 0)
{
LARGE_INTEGER li;
QueryPerformanceFrequency(&li);
result = (PC_t)li.QuadPart;
}
return result;
}

PC_t PerfCounter(void)
{
  LARGE_INTEGER result;
  QueryPerformanceCounter(&result);
  return (PC_t)result.QuadPart;
}

double ElapsedMs(PC_t earlier, PC_t later)
{
  PC_t diff;
  diff = (earlier < later) ? (later - earlier) : (earlier - later);
  return double(diff)/PerfCounterFreq();
}

Answer
Hello Amir.

You should use Microsoft Visual Studio (free express edition) in addition to, or instead of, your turbo compiler. The Microsoft compiler will show you errors caused by using uninitialized variables.

This is what visual studio told me when running the program.

Seeding the random number generator with 1, instead of time(NULL) allows for reproduction of results. I fixed each error by initializing the variable to 0 at the top of its function. This may or may not be the correct value. In many cases you have the variable initialized only under certain conditions, leading me to believe that the logic is incomplete.

In line 786
if(u1==u0+1)  goto end;
u1 is being used without being initialized.

Line 721
if(a0==0) goto select_h0;
a0 needs init

Line 642
if(mac_ijh[i1][j0][h0]==0 || i1==i0)    goto select_i1;
i0 needs init.

Line 643
indiv[4][u0]=i1;
u0 needs init.

Line 379
ts[u]=t_mac[i]>tf[u1] ? t_mac[i]:tf[u1];
u1 needs init.

Line 562
while(temp1==0){
seems to be an infinite loop.

My advice is to read over the program carefully and look for cases where variables are initialized under certain conditions. Also, it will not hurt to initialize all variables to 0. As it stands now, the variables have random, or undetermined values.


I don't know how to translate C++ to FORTRAN automatically. Perhaps you could make a C interface to the C++ code, then call the C from FORTRAN. It is difficult to call C++ from FORTRAN because C++ changes function names during the compilation process. That is called "name mangling" (http://en.wikipedia.org/wiki/Name_mangling). But calling C from FORTRAN should be possible.

I hope that helps you some.

Best regards
Zlatko

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.