%[probabilities, min_index]=fitnessEstGoogle(propensities,indices_list,v,F,varF,nv,p,pp,pop_size,iterations)

%This function goes through each member of a population of propensities and measures their fitness using an approximation of the google steady state distribution
%Fitness is evaluated by comparing desired values to found values in indicated states, taking the sum of the absolute value of differences and using an exponential function to assign fitness

%Parameters
%Propensities- list of propensities for entire population
%indices_list,v- states we care about and the corresponding values we hope to find in those states
%varF, nv, p- corresponding to boolean network we are looking atan
%pp- google noise
%pop_size-size of each population
%iterations- the number of random walks estimatedGoogle uses to approximate distribution

%Returns:
%Probabilites: a list of floating point values between 0 and 1, the more fit members have a bigger gap between their value in probabilites and the value preceding it
%min_index: the index of the most fit set of propensities

%Uses: estimatedGoogle.m 

%Author: Jacob Miller, May 28th 2016
function [probabilities,min_index]=fitnessEstGoogle(propensities,indices_list,v,F,varF,nv,p,pp,pop_size,iterations)
  
  n=length(nv);
  fitnesses=zeros(1,pop_size);
  probabilities=zeros(1,pop_size);
  sum_of_fitness=0;
  
  s=0.1; %Used in fitness function

  
  %For every member of the generation
  for i=1: pop_size
  
    %Find steady state vector corresponding to propensities of that member
    propensity=reshape(cell2mat(propensities(:,i)),2,n);
    distribution=estimatedGoogle(iterations,indices_list,F,varF,nv,pp,propensity,p);
    
    %Find sum of absolute value of difference between desired values and found values
    diff=0;
    for j=1: length(indices_list) 
      diff=diff+abs(v(j)-distribution(j));
    end
    
    
    sum_of_fitness= sum_of_fitness+exp(-diff^2/s);%Calculate fitness using exponential function and add to accumulator
    probabilities(i)=sum_of_fitness;
    fitnesses(i)=diff;
    
  end
  [~,min_index]=min(fitnesses);%Find most fit propensities
  probabilities=probabilities/sum_of_fitness;
end
    