%
% 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] = RestrictedParse(A, prescription, NumAngles, NumPencils);

%
% 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', [], 'ImageResolution', []);

%
% Set the anlge info
%

Prob.NumAngles  = NumAngles;
Prob.Angles = [1:NumAngles]';
Prob.NumPencils = NumPencils;

%
% These index vectors show where the three types of tissue are.
%    1 - Regular
%    2 - Target
%    3+ - Critical
%

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

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

[s1, s2] = size(A);
Prob.DoseMatrix         = A;
Prob.TumorDoseMatrix    = sparse(A(Prob.TumorRowIndex,:));
if length(Prob.CriticalRowIndex) >= 1
  Prob.CriticalDoseMatrix = sparse(A(Prob.CriticalRowIndex,:));
elseif length(Prob.CriticalRowIndex) == 0
  Prob.CriticalDoseMatrix = sparse(zeros(1,s2));
else
  LogMesg('RAD -> Confused about Prob.CricitalRowIndex')
end
%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;
if length(Prob.CriticalRowIndex) >= 1
   Prob.CUB   = prescription(Prob.CriticalRowIndex);
elseif length(Prob.CriticalRowIndex) == 0 
   Prob.CUB = [0];
else
  LogMesg('RAD -> Confused about Prob.CricitalRowIndex')
end
Prob.RUB   = prescription(Prob.RegularRowIndex);

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

Prob.AnalysisType = 1;

% AnalysisType = get(findobj('Tag','absoluteButton'),'Value');

%
% 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
%

Prob.SolverType = 0;
