% Please reference to the article as follow:
% Mohammad Reza Bonyadi and Mohsen Ebrahimi Moghaddam, "A Bipartite Genetic
% Algorithm for Multi-processor Task Scheduling", International Journal Of
% Parallel Programming (IJPP), Springer, 2009, DOI: 10.1007/s10766-009-0107-8
% ******************************************************
% This function read the data for job scheduling problem from STG library. 
% *****************************************************
% Input variables: 
% adrinp:the address which the files are in that 'D:\books\my conferance\job scheduling\data\rnc100\100\'. 
% The folder rnc100\100\ contains some files each of them contains a problem with 100 
% jobs.
% fname: The file name that should be red. for example rand00043.stg
% np: number of processors
% u: always 0
% nj: number of jobs
% adrout: an address which the program use it to save its results. for
% example 'D:\books\my conferance\job scheduling\program2\data\in'
% ********************************************************
% Output vriables:
% ComCost: A two dimensional matrix which contains comunication costs between nodes. 
% See help in the website. 
% Times: the times that each process needs.
% Heights: the height of each node.
% Parents: a matrix of 0/1. If Parents(a,b) is 1 it means b is a parent for
% a.
% NP: number of parents for each job.
% nt, np: number of tasks and processors.
% ************************************************************
% Programmed by Mohammad Reza Bonyadi, 15 Agu 2008
% Email: vardiar@gmail.com, m_bonyadi@std.sbu.ac.ir
function [ComCost Times Heights Parents NP nt np]=Readd(adrinp,fname,np,u,nj,adrout)
prob=nj;
if(u==0)
    s=[adrinp fname];
    
    p=np;
    adr=adrinp;
    fid=fopen(s,'r');
    nj=fscanf(fid,'%d',[1 1]);%number of jobs
    % s1='D:\books\my conferance\job scheduling\program2\data\in.txt';
    s1=[adrout '.txt'];
    fid1=fopen(s1,'wt');
    fprintf(fid1,'%d %d\n',[nj p]);
    fscanf(fid,'%d',[1 3]);
    He=zeros(1,nj);
    for i=1:nj
        jn=fscanf(fid,'%d',[1 1]);
        fprintf(fid1,'%d ',jn);
        pt=fscanf(fid,'%d',[1 1]);
    
        np=fscanf(fid,'%d',[1 1]);
        pa=[];
        for j=1:np
            pa=[pa fscanf(fid,'%d',[1 1])];
        end;
        if(pa(1)~=0)
            He(i)=max(He(pa))+1;
            fprintf(fid1,'%d ',He(i));
            fprintf(fid1,'%d ',pt);
            fprintf(fid1,'%d ',[np pa]);
        else
            fprintf(fid1,'%d ',0);
            fprintf(fid1,'%d ',pt);
            fprintf(fid1,'%d ',0);
        end;
        fprintf(fid1,'\n',0);
    end;
    fclose(fid);
    fclose(fid1);
    fid=fopen([adrout '.txt'],'r');
else 
        fid=fopen([adrout num2str(prob) '.txt'],'r');
end;
nt=fscanf(fid,'%d',[1 1]);%number of jobs
np=fscanf(fid,'%d',[1 1]);%number of machines
nj=nt;
Heights=zeros(1,nj);
Times=zeros(1,nj);
NP=zeros(1,nj);%Number of Parents
Parents=zeros(nj,nj);
for i=1:nj
    fscanf(fid,'%d',[1 1]);
    Heights(i)=fscanf(fid,'%d',[1 1]);
    Times(i)=fscanf(fid,'%d',[1 1]);
    NP(i)=fscanf(fid,'%d',[1 1]);
    for j=1:NP(i)
        Parents(i,fscanf(fid,'%d',[1 1]))=1;
    end;
end;
fidc=fopen([adrinp 'CC' num2str(prob) '.txt'],'rt');
ComCost=fscanf(fidc,'%d',[nt nt])';
fclose(fidc);
