%
% Parse - A. Holder and J. Reese, 6/4/03
%
% ver. 1.0
%
% This function takes the dose matrix A, the prescription, and the 
% uniformity level and parses the information to form a problem instance.
%

function [Prob] = Parse(A, prescription, UniLevel);

%
% This defines our problem structure.  A problem instance is defined 
% by the dosematrix (and its submatrices, as well as the bounds of 
% the problem, and the solution. We also indicate the solver used 
% and some other useful numbers.
%

Prob = struct('DoseMatrix', [], 'TumorDoseMatrix', [], ...
   'CriticalDoseMatrix', [], 'RegularDoseMatrix', [], ...
   'TumorRowIndex', [], 'CriticalRowIndex', [], ...
   'RegularRowIndex', [], 'TumorColIndex', [], ...
   'TGoal', [], 'TUB', [], 'TLB', [], 'CUB', [], 'RUB', [], ...
   'AnalysisType', [], 'SolverType', [], 'omega', [], ...
   'tol', [], 'x', [], 'fval', [], 'alpha', [], 'beta', [], ...
   'gamma', [], 'ThePlan', [], 'RadLevel', [], 'Angles', [], ...
   'NumAngles', [], 'NumPencils', [], 'BalancedSoln', [], ...
   'BalancedAvgPDF', [], 'BalancedMaxPDF', [], 'PrimalAvgPDF', [], ...
   'PrimalMaxPDF', [], 'DualAvgPDF', [], 'DualMaxPDF', [], ...
   'InteriorAvgPDF', [], 'InteriorMaxPDF', [], 'SetCover1PDF', [], ...
   'SetCover2PDF', [], 'SetCover3PDF', [], 'ScoringPDF', [], ...
   'EntropyAvgInteriorPDF', [], 'EntropyMaxInteriorPDF', [], ...
   'EntropyAvgBalancedPDF', [], 'EntropyMaxBalancedPDF', [] );

%
% We get the angle and sub-beam geometry from the GUI.
%

%if get(findobj('Tag','button_15_10'),'Value') == 1
%        Prob.NumAngles  = 24;
%	Prob.Angles = [1:24]';
%        Prob.NumPencils = 10;
%elseif get(findobj('Tag','button_5_32'),'Value') == 1
%        Prob.NumAngles  = 72;
%	Prob.Angles = [1:72]';
%        Prob.NumPencils = 32;
%elseif get(findobj('Tag','button_1_32'),'Value') == 1
%        Prob.NumAngles  = 360;
%	Prob.Angles = [1:360]';
%        Prob.NumPencils = 32;
%else
%        disp('No angle geometry to load.')
%        return;
%end

% For now, hard code the geometry
%fprintf('    -> NumAngles = 360, NumPencils = 32 \n')
%Prob.NumAngles  = 360;
%Prob.Angles = [1:360]';
%Prob.NumPencils = 32;

fprintf('    -> NumAngles = 72, NumPencils = 32 \n')
Prob.NumAngles  = 72;
Prob.Angles = [1:72]';
Prob.NumPencils = 32;

%fprintf('    -> NumAngles = 24, NumPencils = 10 \n')
%Prob.NumAngles  = 24;
%Prob.Angles = [1:24]';
%Prob.NumPencils = 10;

%
% These index vectors show where the three types of tissue are.
%    1 - Tumor
%    2 - Critical Structure
%    3 - Regular Tissue
%

Prob.TumorRowIndex = find(prescription(:, : , 2) == 1);
Prob.CriticalRowIndex = find(prescription(:, : , 2) == 2);
Prob.RegularRowIndex = find(prescription(:, : , 2) == 3);

%
% Partition the dose deposition matrix A into those rows corresponding
% to tumor pixels, critical structure pixels, and regular tissue pixels.
%

Prob.DoseMatrix         = A;
Prob.TumorDoseMatrix    = sparse(A(Prob.TumorRowIndex,:));
Prob.CriticalDoseMatrix = sparse(A(Prob.CriticalRowIndex,:));
Prob.RegularDoseMatrix  = sparse(A(Prob.RegularRowIndex,:));

%
% Grab some sizes.
%

[tr,tc] = size(Prob.TumorDoseMatrix);
[gr,gc] = size(Prob.CriticalDoseMatrix);
[rr,rc] = size(Prob.RegularDoseMatrix);

%
% Remove the columns corresponding to the sub-beams that do not
% strike the tumor.
%

CheckColSum             = ones(1,tr)*Prob.TumorDoseMatrix;
Prob.TumorColIndex      = find(CheckColSum >= 0.1);
Prob.TumorDoseMatrix    = sparse(Prob.TumorDoseMatrix(:,Prob.TumorColIndex));
Prob.CriticalDoseMatrix = sparse(Prob.CriticalDoseMatrix(:,Prob.TumorColIndex));
Prob.RegularDoseMatrix  = sparse(Prob.RegularDoseMatrix(:,Prob.TumorColIndex));

%
% Extract the upper and lower bounds from the prescription matrix
%
% Prob.tol - the amount that we are going to increase the tumor lower
%            bound

Prob.tol   = 10^(-4);
%p          = UniLevel / 100;
% this is a fix until we get it correct
p          = 3 / 100;
Prob.TGoal = prescription(Prob.TumorRowIndex);
Prob.TUB   = Prob.TGoal * (1 + p);
Prob.TLB   = (Prob.TGoal * (1 - p)) + Prob.tol;
Prob.CUB   = prescription(Prob.CriticalRowIndex);
Prob.RUB   = prescription(Prob.RegularRowIndex);

%
% Set the Analysis Type.
%       0 - Average Analysis
%       1 - Absolute Analysis
%

%if get(findobj('Tag','absoluteButton'),'Value') == 1
%  Prob.AnalysisType = 1;
%%elseif get(findobj('Tag','averageButton'),'Value') == 1
%  Prob.AnalysisType = 0;
%else
%  disp('No Analysis Type Chosen')
%  return
%end
%
% Hard code the anlysis type
fprintf('    -> Analysis Type is set to Absolute \n')
Prob.AnalysisType = 1;

%
% Calculate the weight omega that guarantees the tumor uniformity.
%
% The bound presented in the paper leads to numerical instability
% with the interior point method always thinking that it is infeasible.
% This value seems to work well.
%

kappa      = (norm(Prob.TLB, inf));
Prob.omega = kappa / Prob.tol;

%
% Set the Solver Type
%       0 - Interior Point Method
%       1 - Simplex Method
%

%if get(findobj('Tag','simplexButton'),'Value') == 1
%  Prob.SolverType = 1;
%elseif get(findobj('Tag','interiorButton'),'Value') == 1
%  Prob.SolverType = 0;
%else
%  disp('No Solver Chosen')
%  return
%end
fprintf('    -> SolverType set to Interior, may not matter \n')
Prob.SolverType = 0;
