%
%  This generates N-beam treatments with a simple scoring technique
%

%
% Prob --------- Origninal Problem Statement
% N ------------ vector of angles to generate planes for -i.e. N = [10, 5]
%                means find a 10 and 5 beam treatment
% ScoreType ---- List of different scores (pdf's) that are used.
% prescription - Original problem prescription.
%

function [Soln] = Scoring(Prob, N, ScoreType, prescription, SaveType, Soln)

NumScores = length(fieldnames(ScoreType));

for t = 1:NumScores
   % Grab the scores
   Scores = eval(['Prob.', eval(sprintf('ScoreType.t%d',t)) ] );
   % Make sure Scores are meaningful
   if max(Scores) > .01
      t0 = clock;
      for n = 1:length(N)
         % Grab the system clock
         t0 = clock;
         % Make sure NOT to count zero scores
         NumPracticalScores = min([length(find(Scores >= 0.01)), N(n)]);
         % Find where the Highscores are
         [HighScores, ScoreIndex] = sort(-Scores);
         % Grab the best angles
         Angles = ScoreIndex(1:NumPracticalScores);
         % Store the Selection Time
         eval(sprintf('Soln.BeamSelector.S.%s.B%d.SelectionTime = %6.4f;', ...
            eval(sprintf('ScoreType.t%d',t)), N(n), etime(clock,t0) ));
         % Solve with Resticted Angles
         t0 = clock;
         TmpProb = RestrictedSolver(Prob, prescription, Angles);
         eval(sprintf('Soln.BeamSelector.S.%s.B%d.SolutionTime = %6.4f;', ...
            eval(sprintf('ScoreType.t%d',t)), N(n), etime(clock,t0) ));
         % Grab the optimal value
         eval(sprintf('Soln.BeamSelector.S.%s.B%d.JudgementValue = %6.4f;', ...
            eval(sprintf('ScoreType.t%d',t)), N(n), TmpProb.fval ));
         % Balance the Solution
         t0 = clock;
         TmpProb.ThePlan = BalancedPDF(TmpProb);
         eval(sprintf('Soln.BeamSelector.S.%s.B%d.BalancingTime = %6.4f;', ...
            eval(sprintf('ScoreType.t%d',t)), N(n), etime(clock,t0) ));
         % Try to make a pic or two
         Figure(TmpProb, prescription, sprintf('%s_NumBeams%d', ...
            eval(sprintf('ScoreType.t%d',t)), N(n)),'Scoring', SaveType)
         % Write to the LaTex File
         t1 = etime(clock, t0);
         LaTex('AddRow', 'Scoring', eval(sprintf('ScoreType.t%d',t)), ...
            Angles, t1);
         % Store the angles
         eval(sprintf('Soln.BeamSelector.S.%s.B%d.Angles = Angles;', ...
            eval(sprintf('ScoreType.t%d',t)), N(n)) );
         eval(sprintf('Soln.BeamSelector.S.%s.B%d.Iterations = 1;', ...
            eval(sprintf('ScoreType.t%d',t)), N(n)) );
      end
   else
      LogMesg('    -> PDF is zero, skipping Scoring')
   end
end
