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

%This function goes through each member of a population of propensities and measures their fitness using 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

%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: googleRank.m 

%Author: Jacob Miller, May 28th 2016
function [probabilities,min_index]=fitnessGoogle(propensities,indices_list,v,F,varF,nv,p,pp)
  
  n=length(nv);%number of genes
  
  fitnesses=zeros(1,length(propensities));
  probabilities=zeros(1,length(propensities));
  sum_of_fitness=0;
  
  s=0.1; %Used in fitness function
  
  %For every member of the generation
  for i=1: length(propensities)
  
    %Find steady state vector corresponding to propensity of that member
    propensity=reshape(cell2mat(propensities(:,i)),2,n);
    v2=googleRank(F,varF,nv,propensity,p,pp);
    
    %diff calculates the difference between chosen points of steady state vector and desired vector
    diff=0;
    for j=1: length(indices_list) 
      diff=diff+abs(v(j)-v2(indices_list(j)));
    end
    
    %Calculate fitness and add to accumulator
    sum_of_fitness= sum_of_fitness+exp(-diff^2/s);  
    
    probabilities(i)=sum_of_fitness;
    fitnesses(i)=diff;
  end
  [~,min_index]=min(fitnesses);%Find index of most fit set of propensities
  probabilities=probabilities/sum_of_fitness;
end
    