/*****************************************************************************/
void print_tree(start,z)        
int start[];
short z; {
  short k;
  int temp,gtemp;
  float mean, sdev, squared, sdev2, mean2, probj[2], probi[3];

  probj[0]=probj[1]=0.0;
  probi[0]=probi[1]=probi[2]=0.0;
  temp=start[z];
  fprintf(fdata, "\nLEVEL %d\n", z);
  while(temp<stop[z]) {
    gtemp=get(temp);
    if(node[gtemp]->optflag==1) {
      probi[node[gtemp]->Opti]+=node[gtemp]->prob;
      sumprobi[node[gtemp]->Opti]+=node[gtemp]->prob;
      probj[node[gtemp]->Optj]+=node[gtemp]->prob;
      sumprobj[node[gtemp]->Optj]+=node[gtemp]->prob;
      mean=squared=sdev=0.0;
      for(k=0;k<8;k++) {
        mean+=(k*(node[gtemp]->value[k]));
        squared+=(k*k*(node[gtemp]->value[k]));
      }
      sdev=sqrt(squared-(mean*mean));
      mean2=72*mean;
      sdev2=sdev*144;
      fprintf(fsens, "%d %d moveto (%d%d) show\n", (int)sdev2,
      		   (int)mean2, node[gtemp]->Opti,node[gtemp]->Optj);
      fprintf(fdata, "Jmin=%9.6f, Opti=%d, Optj=%d, mean=%8.6f",
              node[gtemp]->Jmin, node[gtemp]->Opti, node[gtemp]->Optj, mean);
      if (jval)
        fprintf(fdata, ", prob=%f\n",node[gtemp]->prob);
      else
        fprintf(fdata, "\n");
    }
    temp++;
  }
  fprintf(fdata,"\nProbability:\n");
  fprintf(fdata,"     j=0: %1.4f,                                i=0: %1.4f\n",
    probj[0],probi[0]);
  fprintf(fdata,"     j=1: %1.4f,                                i=1: %1.4f\n",
    probj[1],probi[1]);
  fprintf(fdata,"                                                 i=2: %1.4f\n",
    probi[2]);
  fprintf(fsens, "showpage\n");
}
/*****************************************************************************/
void make_sens(start, z, pchoice, qchoice)
int start;
short z; {
  int gstart;

  gstart=get(start);
  fprintf(fsens, "%%!\n");
  fprintf(fsens, "/NewCenturySchlbk-BoldItalic findfont 12 scalefont setfont\n");
  if (z==0)
    fprintf(fsens, "288 288 moveto (Jmin=%f) show\n", node[gstart]->Jmin);
  fprintf(fsens, "72 72 translate\n"); 
  fprintf(fsens, "/NewCenturySchlbk-BoldItalic findfont 12 scalefont setfont\n");
  fprintf(fsens, "0 0 moveto 432 0 rlineto stroke\n");
    fprintf(fsens, "0 0 moveto 0 504 rlineto stroke\n");
    fprintf(fsens, "-20 72 moveto (1.0) show\n");
  fprintf(fsens, "0 72 lineto stroke\n");
    fprintf(fsens, "-20 144 moveto (2.0) show\n");
  fprintf(fsens, "0 144 lineto stroke\n");
    fprintf(fsens, "-20 216 moveto (3.0) show\n");
  fprintf(fsens, "0 216 lineto stroke\n");
    fprintf(fsens, "-20 288 moveto (4.0) show\n");
  fprintf(fsens, "0 288 lineto stroke\n");
    fprintf(fsens, "-20 360 moveto (5.0) show\n");
  fprintf(fsens, "0 360 lineto stroke\n");
    fprintf(fsens, "-20 432 moveto (6.0) show\n");
  fprintf(fsens, "0 432 lineto stroke\n");
    fprintf(fsens, "-20 504 moveto (7.0) show\n");
  fprintf(fsens, "0 504 lineto stroke\n");
  fprintf(fsens, "140 -20 moveto (1.0) show\n");
  fprintf(fsens, "140 -7 moveto 140 0 lineto stroke\n");
  fprintf(fsens, "288 -20 moveto (2.0) show\n");
  fprintf(fsens, "288 -7 moveto 288 0 lineto stroke\n");
  fprintf(fsens, "432 -20 moveto (3.0) show\n");
  fprintf(fsens, "432 -7 moveto 432 0 lineto stroke\n");
  fprintf(fsens, "-20 530 moveto (MEAN) show\n");
  fprintf(fsens, "432 -40 moveto (SDEV) show\n");
  fprintf(fsens, "190 684 moveto (Optimum Policies) show\n");
  fprintf(fsens, "175 666 moveto (for all Possible States) show\n");
  fprintf(fsens, "185 648 moveto (of the Information) show\n");
  fprintf(fsens, "0 612 moveto (Initial State of the Information:) show\n");
  fprintf(fsens,
         "220 612 moveto (Pi[0]=%1.2f Pi[1]=%1.2f Pi[2]=%1.2f Pi[3]=%1.2f) show\n",
          node[gstart]->value[0], node[gstart]->value[1], node[gstart]->value[2], 
          node[gstart]->value[3]);
  fprintf(fsens, 
         "220 594 moveto (Pi[4]=%1.2f Pi[5]=%1.2f Pi[6]=%1.2f Pi[7]=%1.2f) show\n",
          node[gstart]->value[4], node[gstart]->value[5], node[gstart]->value[6],
          node[gstart]->value[7]);
  fprintf(fsens, "0 558 moveto (No. of levels : %d) show\n", levels); 
  fprintf(fsens, "0 576 moveto (Level %d) show\n", z);
  fprintf(fsens, "350 576 moveto (Choice of P matrix: %d) show\n", pchoice);
  fprintf(fsens, "350 558 moveto (Choice of Q matrix: %d) show\n", qchoice);
  fprintf(fsens, "/NewCenturySchlbk-BoldItalic findfont 6 scalefont setfont\n");
}
/*****************************************************************************/
void data_head(alpha, rnd, pchoice, qchoice, time, start)
float alpha, rnd;
int pchoice, qchoice;
int start[];
short time[3]; {
  int gstart;
  FILE *finm, *finh;
  short ftime[3],i;
  char b,c;

  clock(ftime);
  if((ftime[2]-=time[2])<0) {
    ftime[2]+=60;
    ftime[1]--;
  }
  if((ftime[1]-=time[1])<0) {
    ftime[1]+=60;
    ftime[0]--;
  }
  ftime[0]-=time[0];

  fprintf(fdata,"Run Time: %d:%02d:%02d\n",ftime[0],ftime[1],ftime[2]);
  fprintf(ffact,"Run Time: %d:%02d:%02d\n",ftime[0],ftime[1],ftime[2]);

  system("machtype -c > .mach");
  finm=fopen("/mit/smmadana/.mach","r");
  fprintf(fdata,"Machine: ");
  fprintf(ffact,"Machine: ");
  b=getc(finm);
  while((c=getc(finm))!=EOF) {
    fprintf(fdata,"%c",b);
    fprintf(ffact,"%c",b);
    b=c;
  }
  fprintf(fdata," ");
  fprintf(ffact," ");
  fclose(finm);
  unlink("/mit/smmadana/.mach");
  system("hostname > .host");
  finh=fopen("/mit/smmadana/.host","r");
  while((c=getc(finh))!=EOF){
    fprintf(fdata,"%c",c);
    fprintf(ffact,"%c",c);
  }
  fprintf(fdata,"\n\n");
  fclose(finh);
  unlink("/mit/smmadana/.host");

  fprintf(ffact,"Nodes/Level: 0001 ");
  for(i=0;i<levels;i++) {
    if(i==11) fprintf(ffact,"            ");
    fprintf(ffact,"%04d ",nnod[i]);
  }
  fprintf(ffact,"\n");
  fprintf(fdata, "pchoice=%d, qchoice=%d, alpha=%1.4f, interval=%1.3f\n",
          pchoice, qchoice, alpha, rnd);
  fprintf(ffact, 
    "pchoice=%d, qchoice=%d, alpha=%1.4f, interval=%1.3f, levels=%d\n",
          pchoice, qchoice, alpha, rnd, levels);
  gstart=get(start[0]);
  write_pi(node[gstart]->value);
  fprintf(ffact," ------ ------ ------ ------ ------ ------ ------ ------ \n");
  for (i=0;i<8;i++)
    fprintf(ffact,"| %.2f ",node[gstart]->value[i]);
  fprintf(ffact,"|\n");
  fprintf(ffact," ------ ------ ------ ------ ------ ------ ------ ------ \n");

  fprintf(ffact,"Jmin=%.3f\n",node[gstart]->Jmin);

  time[0]=ftime[0];
  time[1]=ftime[1];
  time[2]=ftime[2];
}

/*****************************************************************************/
void open_files(argc,argv)
int argc;
char *argv[]; {
  char sens[50], data[50], fact[50], base[50];

  if(argc!=2) 
    strcpy(base,"/mit/smmadana/graph");
  else {
    strcpy(base,"/mit/smmadana/");
    strcat(base,argv[1]);
  }
  printf("Writing to %s",base);

  strcpy(sens,base);
  strcpy(data,base);
  strcpy(fact,base);
  strcat(sens,"sense.PS");
  strcat(data,".data");
  strcat(fact,".fact");
  
  fsens=fopen(sens,"w");
  fdata=fopen(data,"w");
  ffact=fopen(fact,"w");
}
/*****************************************************************************/
void close_files() {
  printf("Closing files...\n");
  fclose(fsens);
  fprintf(fdata,"\nExpectation for:\n");
  fprintf(fdata,"     j=0: %1.4f,                                i=0: %1.4f\n",
    sumprobj[0],sumprobi[0]);
  fprintf(fdata,"     j=1: %1.4f,                                i=1: %1.4f\n",
    sumprobj[1],sumprobi[1]);
  fprintf(fdata,"                                                 i=2: %1.4f\n",
    sumprobi[2]);  fprintf(fdata,"Expectation for:\n");
  fprintf(ffact,"     j=0: %1.4f,                                i=0: %1.4f\n",
    sumprobj[0],sumprobi[0]);
  fprintf(ffact,"     j=1: %1.4f,                                i=1: %1.4f\n",
    sumprobj[1],sumprobi[1]);
  fprintf(ffact,"                                                 i=2: %1.4f\n\n\n",
    sumprobi[2]);
  fclose(fdata);
  fclose(ffact);
}

 /*****************************************************************************/
 void write_pi(pi)
 float pi[]; {
 
 int i;
 
   for (i=0;i<8;i++) {
     fprintf(fdata,"pi[%d]=%.2f   ",i,pi[i]);
     if (i==3)
       fprintf(fdata,"\n");
   }
   fprintf(fdata,"\n\n");
 }
 /*****************************************************************************/
 
