function init(mop)
%Set up the initial setting for the MOEA/D.
%loading the params.
global subproblems params idealpoint parDim objDim ;  
  idealpoint=ones(mop.od,1)*inf;
  parDim = mop.pd;
  objDim = mop.od;
  %initial the subproblem's initital state.
  subproblems = init_weights(mop.od); 
  inds = randompoint(mop, params.popsize);
  [V, INDS] = arrayfun(@evaluate, repmat(mop, size(inds)), inds, 'UniformOutput', 0);
   v = cell2mat(V);
  idealpoint = min(idealpoint, min(v,[],2)); 
%  [subproblems.curpoint] = INDS{:}; 
  allpoint = cell2mat(INDS);
  subproblems=assignpop(subproblems,allpoint,idealpoint);
    clear inds INDS V indcells;
end

function subp=init_weights(objDim)
% init_weights function initialize a pupulation of subproblems structure
% with the generated decomposition weight and the neighborhood relationship.
%initialize direction vectors with unity simplex;
%compute their neighborhood relationship and get their corresponding weight vectors
global params neighborrelation dirvectors boundary;
    
    subp = [];

    H = floor(params.popsize^(1/(objDim-1)))-2;
    M = 0;
    while M<params.popsize
       H = H+1;
       M = simplex_weightno(H, 0, objDim); 
    end
    if  isempty(params.H)||params.H~=H
     params.H=H;
     warning('H is redefined because of the inconformity');       
    end
    %to load the parameter from the given file.
    filename = sprintf('weight/W%uD_%u.dat',objDim,params.popsize);
    path('../weight',path);
    if (exist(filename, 'file'))
    % copy from the exist file. 
     allws = importdata(filename);
     W = allws';
    else
     W   = zeros(objDim, M);
     C   = 0;
     V   = zeros(objDim,1);
     [W, ~] = simplex_weightset(W, C, V, H, 0, objDim, objDim);
     W   = W / (H+0.0);
    end
     W((W < 1.0E-5))  = 1.0E-5;
     v  = squareform(pdist(W'));
     %Set up the neighbourhood.
     [~, neighborrelation]= sort(v);
     dirvectors=W;
     % [~,pos]=max(W');
     % boundary=pos;%
    if  strcmp(params.dmethod,'te')
     W=(1./W)./repmat(sum(1./W),[objDim 1]);
    end
    if params.popsize~=M %correct the populaiton size
     params.popsize=M;
     warning('Population size is redefined because of the inconformity');
     if strcmp(methodname,'MOEAD-GR')||strcmp(methodname,'MOEAD-DE')
      params.Tm = ceil(params.perTm*params.popsize);     params.Tr = ceil(params.perTr*params.popsize);
     elseif strcmp(methodname,'MOEAD-AGR')||strcmp(methodname,'gMOEAD-AGR')
      params.Tm = ceil(params.perTm*params.popsize);
      params.Tr = agr(params.popsize,params.adaptmethod,params.adaptparams,params.iteration,1);
     end
    end   
    p=get_structure('subproblem');
    subp=repmat(p,1,params.popsize);
    Wcells = mat2cell(W, objDim, ones(1, size(W,2)));
    [subp.weight] = Wcells{:};
    Tmcells=repmat(num2cell(params.Tm),1,params.popsize);
    [subp.Tm] = Tmcells{:};
    Trcells=repmat(num2cell(params.Tr),1,params.popsize);
    [subp.Tr] = Trcells{:};
end
function M = simplex_weightno(unit, sum, dim)

M = 0;

if dim == 1
    M = 1; 
    return;
end

for i=0:1:(unit - sum)
    M = M + simplex_weightno(unit, sum+i, dim-1);
end

end
function [w, c] =simplex_weightset(w, c, v, unit, sum, objdim, dim)

if dim == objdim
    v = zeros(objdim, 1);
end

if dim == 1
    c       = c+1;
    v(1)    = unit-sum;
    w(:,c)  = v;
    return;
end

for i=0:1:(unit - sum)
    v(dim)  = i;
    [w, c]  = simplex_weightset(w, c, v, unit, sum+i, objdim, dim-1);
end

end